Skip to content

Instantly share code, notes, and snippets.

@abele
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abele/4ffbffe1871d47afa4c7 to your computer and use it in GitHub Desktop.
Save abele/4ffbffe1871d47afa4c7 to your computer and use it in GitHub Desktop.
Fabfile used in combination with Salt-Stack
from fabric.api import sudo, task, env, local, settings
from fabric.contrib.files import upload_template
from fabric.contrib.project import upload_project
import os
salt_task = hosts('@'.join([os.environ['AS_MASTER_USER'], os.environ['AS_MASTER_HOST']]))
"""Decorator to specify Salt-Stack master user and host."""
@task
def bootstrap_master():
"""Use insecure one-liner to install Salt-Stack master."""
# XXX: Fails on Docker, see
# https://github.com/saltstack/salt-bootstrap/issues/394
with settings(warn_only=True):
sudo('curl -L http://bootstrap.saltstack.org | sudo sh -s -- -M -N')
upload_template(
filename='master.template',
destination='/etc/salt/master',
template_dir='config',
use_sudo=True,
use_jinja=True,
)
sudo('service salt-master restart')
@task
def bootstrap(master_hostname, hostname):
"""Setup salt minion.
$ fab -H <some_host> bootstrap:<master_hostname>,<some_host_readable_name>
"""
with settings(warn_only=True):
sudo('curl -L http://bootstrap.saltstack.org | sudo sh')
context = {
'master_hostname': master_hostname,
'id': hostname,
}
upload_template(
filename='minion.template',
destination='/etc/salt/minion',
template_dir='config',
context=context,
use_sudo=True,
use_jinja=True,
)
sudo('service salt-minion restart')
@task
def sync_states():
"""Sync Salt-Stack state files."""
upload_project(local_dir='config/salt', remote_dir='/srv', use_sudo=True)
upload_project(local_dir='config/pillar', remote_dir='/srv', use_sudo=True)
@task
def master():
"""Run in production."""
env.user = os.environ['AS_MASTER_USER']
env.hosts = os.environ['AS_MASTER_HOST']
@task
def vagrant():
"""Run tasks on vagrant instance."""
env.user = 'vagrant'
env.hosts = ['127.0.0.1:2222']
result = local('vagrant ssh-config | grep IdentityFile', capture=True)
env.key_filename = result.split()[1]
@salt_task
@task
def nginx_reload(*servername_seq):
"""Restart Nginx service."""
salt('service.relaod nginx', *servername_seq)
def salt(cmd, *servername_seq):
"""Run salt command on given hosts."""
args = '-L' if '*' not in servername_seq else ''
cmd = cmd if cmd else 'cmd.run "uptime"'
env.output_prefix = False
sudo('salt {args} "{servername_csv}" {cmd}'.format(
args=args,
servername_csv=','.join(servername_seq),
cmd=cmd,
))
@abele
Copy link
Author

abele commented Dec 15, 2014

To add new minion:

    fab -H <minion_host> -u <minion_user> bootstrap:<master_ip>,<human_readable_minion_hostname>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment