def setup_master(and_minion=1, upgrade=0): """Bootstraps and sets up a master. Sets up a minion pointing to itself unless otherwise said. Options: and_minion: Set to 'no' to not install salt-minion. upgrade: Set to 'yes' to upgrade all system packages before installing salt. """ lowlevel.bootstrap(upgrade) lowlevel.master() lowlevel.upload(sync=False) if boolean(and_minion): name = hostname() roles = ['salt-master'] + list(env.salt_roles) lowlevel.create_minion_key(name) lowlevel.minion(master='127.0.0.1', hostname=name, roles=roles) if env.salt_bleeding: lowlevel.convert_to_bleeding() time.sleep(1) if boolean(and_minion): service('salt-minion', 'start') 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 bootstrap(upgrade=False): "Performs a bootstrap depending on the operating system." runner.action('Bootstrapping salt') with runner.with_prefix(' ~> '): pkgmgr = find_pkgmgr() bootstrappers = { 'apt': bootstrap_with_aptget, } runner.silent('rm -rf /etc/salt/') runner.silent('rm -rf /opt/salt/') bootstrappers[pkgmgr.name](boolean(upgrade))
def deploy(filter='*', upload=1, sync=1, debug=0): """Tells master to send pillars and execute salt state files on clients. If debug is set to 'yes', runs locally with more debugging information output. If upload is set to 'yes', then uploads current salt configurations to the master before deploying. """ runner.action('Deploying salt files') output = '' with runner.with_prefix(' '): # if has('/opt/saltstack/', 'test -e %(app)s'): # lowlevel.upgrade_bleeding() if boolean(upload): lowlevel.upload() if boolean(sync): if boolean(debug): cmd = "salt-call state.highstate -l debug" else: cmd = "salt '{0}' state.highstate".format(filter) runner.action('Ensuring minion state') with show('stdout', 'stderr'): out = runner.sudo(cmd, combine_stderr=True) with open('sync.log', 'w+') as handle: handle.write(out) runner.action('Wrote output to sync.log')
def download_packages(everything=False): "Downloads all the package files for seafood states" with open('packages.json', 'r') as handle: data = json.loads(handle.read()) for conf_name, config in data.items(): for name, packages in config.items(): if not boolean(everything): packages = [packages[0]] for package in packages: download_package( url=package['url'], path=package['path'], md5hash=package['md5'], configuration=conf_name, ) print 'Done'