def convert_to_bleeding(): "Converts the master installation to bleeding edge." runner.action('Convert Salt to bleeding edge') if runner.silent('test -d /opt/saltstack', use_sudo=True): runner.state('Already installed.') return with runner.with_prefix(' ~> '): pkgmgr = find_pkgmgr() pkgmgr.install('python') pkgmgr.install('python-dev') pkgmgr.install('python-pip') sudo('yes | pip install pyzmq PyYAML pycrypto msgpack-python jinja2 psutil') pkgmgr.install('salt-common') runner.silent('pkill salt-master', use_sudo=True) runner.silent('pkill salt-minion', use_sudo=True) time.sleep(1) pkgmgr.remove('salt-master') pkgmgr.remove('salt-minion') pkgmgr.install('git-core') runner.silent('rm -rf /opt/saltstack', use_sudo=True) runner.silent('mkdir /opt', use_sudo=True) with cd('/opt'): sudo('git clone {0} {1}'.format(env.salt_bleeding, 'saltstack')) with cd('/opt/saltstack/'): sudo('python setup.py install')
def reboot_if_required(): """Reboots the machine only if the system indicates a restart is required for updates. """ out = runner.silent('[ -f /var/run/reboot-required ]') if not out.return_code: runner.state("System requires reboot => Rebooting NOW") reboot()
def upload(sync=True): "Uploads all pillars, modules, and states to the remote server." runner.action("Upload Salt Data") with runner.with_prefix(' ~> '): runner.state("Clear existing data") sudo('rm -rf {0}'.format(SALT_DIR)) sudo('mkdir -p {0}'.format(SALT_DIR)) for system in env.configs: runner.state("Upload configuration: " + system) for dirname in ('states', 'pillars'): src = os.path.join(CONFIG_DIR, system, dirname) path = os.path.join(SALT_DIR, system) dest = os.path.join(path, dirname) runner.state(" - {0}/{1}".format(system, dirname)) upload_project(src) # remove dot files sudo('find {0} -name ".*" | xargs rm -rf'.format(dirname)) # remove pyc files sudo('find {0} -name "*.pyc" | xargs rm -rf'.format(dirname)) with settings(warn_only=True): sudo('mkdir -p {0}'.format(path)) sudo('mv {0} {1}'.format(dirname, dest)) if sync: runner.state("Sync pillar data to minions") sudo("salt '*' saltutil.refresh_pillar") runner.state("Sync states and modules to minions") sudo("salt '*' saltutil.sync_all")
def upload_minion_config(master, roles=()): "Uploads the minion config to the remote server and restarts the salt-minion." runner.state('Upload minion configuration') config_template_upload('minion', '/etc/salt/minion', context=salt_config_context(roles, master=master)) runner.state("Reboot minion") service('salt-minion', 'stop; true') runner.silent('pkill salt-minion', use_sudo=True) time.sleep(1) service('salt-minion', 'start')
def upload_master_config(): "Uploads master config to the remote server and restarts the salt-master." runner.state('Upload master configuration') config_template_upload('master', '/etc/salt/master', context=salt_config_context()) runner.state("Reboot master") service('salt-master', 'stop') runner.silent('pkill salt-master', use_sudo=True) # to ensure it gets killed time.sleep(1) service('salt-master', 'start')
def hostname(name='', fqdn=True): "Gets or sets the machine's hostname" if name: previous_host = run('hostname').strip() runner.state("Set hostname {0} => {1}".format(repr(previous_host), repr(name))) sudo('hostname ' + name) sudo('echo {0} > /etc/hostname'.format(name)) files.sed('/etc/hosts', previous_host, name, use_sudo=True) else: return sudo('hostname {0}'.format('-f' if boolean(fqdn) else '')).strip()
def config_template_upload(filename, dest, context={}, use_sudo=True): for config in env.configs: filepath = os.path.relpath(os.path.join('configurations', config, filename)) context['__file__'] = filepath if os.path.exists(filepath): runner.state('Use template: {0}', filepath) files.upload_template(filepath, dest, context=context, use_jinja=True, use_sudo=use_sudo) return else: runner.state('Template {0} does not exist', repr(filepath)) raise TypeError('Could not find {0} to upload in any configuration!'.format(repr(filename)))
def bootstrap_with_aptget(upgrade): "Bootstraps installation of saltstalk on the remote machine" apt = find_pkgmgr() apt.update() requires_update = True runner.state('Add salt repository') if is_distro('ubuntu'): apt.install('python-software-properties') sudo('add-apt-repository ppa:saltstack/salt -y') elif is_distro('debian'): if not apt.has('salt-master'): runner.state("Add debian backports to repositories") sudo('''cat <<EOF | sudo tee /etc/apt/sources.list.d/backports.list deb http://backports.debian.org/debian-backports squeeze-backports main EOF''') else: runner.state("Already has salt packages") requires_update = False else: raise TypeError("Unknowned OS") if upgrade: upgrade_all_packages() if requires_update: apt.update()