From 83ce730ec23fdf793255c531048a54dce14fbb9a Mon Sep 17 00:00:00 2001 From: Mike Place Date: Mon, 14 Jul 2014 16:59:44 -0600 Subject: [PATCH] Basic master support for minionswarm --- tests/minionswarm.py | 77 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/tests/minionswarm.py b/tests/minionswarm.py index 264aaed649..9744485ce9 100644 --- a/tests/minionswarm.py +++ b/tests/minionswarm.py @@ -16,6 +16,7 @@ import optparse import subprocess import tempfile import shutil +import sys # Import salt libs import salt @@ -35,6 +36,11 @@ def parse(): default=5, type='int', help='The number of minions to make') + parser.add_option('-M', + action='store_true', + dest='master_too', + default=False, + help='Run a local master and tell the minions to connect to it') parser.add_option('--master', dest='master', default='salt', @@ -221,10 +227,13 @@ class Swarm(object): def start(self): ''' - Start the minions!! + Start the magic!! ''' - print('Starting minions...') self.prep_configs() + if self.opts['master_too']: + master_swarm = MasterSwarm(self.opts) + master_swarm.start() + print('Starting minions...') self.start_minions() print('All {0} minions have started.'.format(self.opts['minions'])) print('Waiting for CTRL-C to properly shutdown minions...') @@ -242,11 +251,75 @@ class Swarm(object): 'pkill -KILL -f "python.*salt-minion"', shell=True ) + if self.opts['master_too']: + print('Killing any remaining masters') + subprocess.call( + 'pkill -KILL -f "python.*salt-master"', + shell=True + ) if not self.opts['no_clean']: print('Remove ALL related temp files/directories') shutil.rmtree(self.swarm_root) print('Done') + +class MasterSwarm(Swarm): + ''' + Create one or more masters + ''' + def start(self): + ''' + Prep the master start and fire it off + ''' + # sys.stdout for no newline + sys.stdout.write('Generating master config...') + self.mkconf() + print('done') + + sys.stdout.write('Starting master...') + self.start_master() + print('done') + + def start_master(self): + ''' + Do the master start + ''' + cmd = 'salt-master -c {0} --pid-file {1}'.format( + self.config, + '{0}.pid'.format(self.config) + ) + if self.opts['foreground']: + cmd += ' -l debug &' + else: + cmd += ' -d &' + subprocess.call(cmd, shell=True) + + def mkconf(self): + ''' + Make a master config and write it' + ''' + dpath = os.path.join(self.swarm_root, 'master') + data = { + 'log_file': os.path.join(dpath, 'master.log'), + 'open_mode': True # TODO Pre-seed keys + } + + os.makedirs(dpath) + path = os.path.join(dpath, 'master') + + with open(path, 'w+') as fp_: + yaml.dump(data, fp_) + self.config = dpath + + def shutdown(self): + print('Killing master') + subprocess.call( + 'pkill -KILL -f "python.*salt-master"', + shell=True + ) + print('Master killed') + + if __name__ == '__main__': swarm = Swarm(parse()) try: