Refactor into base swarm and master/minion swarm

This commit is contained in:
Mike Place 2014-07-15 11:31:07 -06:00
parent 83ce730ec2
commit 200ef6b642

View File

@ -30,49 +30,58 @@ def parse():
Parse the cli options
'''
parser = optparse.OptionParser()
parser.add_option('-m',
parser.add_option(
'-m',
'--minions',
dest='minions',
default=5,
type='int',
help='The number of minions to make')
parser.add_option('-M',
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',
parser.add_option(
'--master',
dest='master',
default='salt',
help='The location of the salt master that this swarm will serve')
parser.add_option('--name',
parser.add_option(
'--name',
'-n',
dest='name',
default='ms',
help=('Give the minions an alternative id prefix, this is used '
'when minions from many systems are being aggregated onto '
'a single master'))
parser.add_option('-k',
parser.add_option(
'-k',
'--keep-modules',
dest='keep',
default='',
help='A comma delimited list of modules to enable')
parser.add_option('-f',
parser.add_option(
'-f',
'--foreground',
dest='foreground',
default=False,
action='store_true',
help=('Run the minions with debug output of the swarm going to '
'the terminal'))
parser.add_option('--no-clean',
parser.add_option(
'--no-clean',
action='store_true',
default=False,
help='Don\'t cleanup temporary files/directories')
parser.add_option('--root-dir',
parser.add_option(
'--root-dir',
dest='root_dir',
default=None,
help='Override the minion root_dir config')
parser.add_option('--transport',
parser.add_option(
'--transport',
dest='transport',
default='zeromq',
help='Declare which transport to use, default is zeromq')
@ -83,7 +92,7 @@ def parse():
)
parser.add_option('-u', '--user', default=pwd.getpwuid(os.getuid()).pw_name)
options, args = parser.parse_args()
options, _args = parser.parse_args()
opts = {}
@ -107,12 +116,13 @@ class Swarm(object):
else:
tmpdir = opts['root_dir']
self.swarm_root = tempfile.mkdtemp(prefix='mswarm-root', suffix='.d',
self.swarm_root = tempfile.mkdtemp(
prefix='mswarm-root', suffix='.d',
dir=tmpdir)
if self.opts['transport'] == 'zeromq':
self.pki = self._pki_dir()
self.__zfill = len(str(self.opts['minions']))
self.zfill = len(str(self.opts['minions']))
self.confs = set()
@ -133,13 +143,97 @@ class Swarm(object):
print('Keys generated')
return path
def start(self):
'''
Start the magic!!
'''
if self.opts['master_too']:
master_swarm = MasterSwarm(self.opts)
master_swarm.start()
minions = MinionSwarm(self.opts)
minions.start_minions()
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...')
while True:
try:
time.sleep(5)
except KeyboardInterrupt:
print('\nShutting down minions')
self.clean_configs()
break
def shutdown(self):
'''
Tear it all down
'''
print('Killing any remaining running minions')
subprocess.call(
'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')
def clean_configs(self):
'''
Clean up the config files
'''
for path in self.confs:
pidfile = '{0}.pid'.format(path)
try:
try:
pid = int(open(pidfile).read().strip())
os.kill(pid, signal.SIGTERM)
except ValueError:
pass
if os.path.exists(pidfile):
os.remove(pidfile)
if not self.opts['no_clean']:
shutil.rmtree(path)
except (OSError, IOError):
pass
class MinionSwarm(Swarm):
'''
Create minions
'''
def __init__(self, opts):
super(MinionSwarm, self).__init__(opts)
def start_minions(self):
'''
Iterate over the config files and start up the minions
'''
self.prep_configs()
for path in self.confs:
cmd = 'salt-minion -c {0} --pid-file {1}'.format(
path,
'{0}.pid'.format(path)
)
if self.opts['foreground']:
cmd += ' -l debug &'
else:
cmd += ' -d &'
subprocess.call(cmd, shell=True)
def mkconf(self, idx):
'''
Create a config file for a single minion
'''
minion_id = '{0}-{1}'.format(
self.opts['name'],
str(idx).zfill(self.__zfill)
str(idx).zfill(self.zfill)
)
dpath = os.path.join(self.swarm_root, minion_id)
@ -184,21 +278,6 @@ class Swarm(object):
yaml.dump(data, fp_)
self.confs.add(dpath)
def start_minions(self):
'''
Iterate over the config files and start up the minions
'''
for path in self.confs:
cmd = 'salt-minion -c {0} --pid-file {1}'.format(
path,
'{0}.pid'.format(path)
)
if self.opts['foreground']:
cmd += ' -l debug &'
else:
cmd += ' -d &'
subprocess.call(cmd, shell=True)
def prep_configs(self):
'''
Prepare the confs set
@ -206,67 +285,15 @@ class Swarm(object):
for idx in range(self.opts['minions']):
self.mkconf(idx)
def clean_configs(self):
'''
Clean up the config files
'''
for path in self.confs:
pidfile = '{0}.pid'.format(path)
try:
try:
pid = int(open(pidfile).read().strip())
os.kill(pid, signal.SIGTERM)
except ValueError:
pass
if os.path.exists(pidfile):
os.remove(pidfile)
if not self.opts['no_clean']:
shutil.rmtree(path)
except (OSError, IOError):
pass
def start(self):
'''
Start the magic!!
'''
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...')
while True:
try:
time.sleep(5)
except KeyboardInterrupt:
print('\nShutting down minions')
self.clean_configs()
break
def shutdown(self):
print('Killing any remaining running minions')
subprocess.call(
'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 __init__(self, opts):
super(MasterSwarm, self).__init__(opts)
self.conf = os.path.join(self.swarm_root, 'master')
def start(self):
'''
Prep the master start and fire it off
@ -285,8 +312,8 @@ class MasterSwarm(Swarm):
Do the master start
'''
cmd = 'salt-master -c {0} --pid-file {1}'.format(
self.config,
'{0}.pid'.format(self.config)
self.conf,
'{0}.pid'.format(self.conf)
)
if self.opts['foreground']:
cmd += ' -l debug &'
@ -298,18 +325,16 @@ class MasterSwarm(Swarm):
'''
Make a master config and write it'
'''
dpath = os.path.join(self.swarm_root, 'master')
data = {
'log_file': os.path.join(dpath, 'master.log'),
'log_file': os.path.join(self.conf, 'master.log'),
'open_mode': True # TODO Pre-seed keys
}
os.makedirs(dpath)
path = os.path.join(dpath, 'master')
os.makedirs(self.conf)
path = os.path.join(self.conf, 'master')
with open(path, 'w+') as fp_:
yaml.dump(data, fp_)
self.config = dpath
def shutdown(self):
print('Killing master')
@ -319,7 +344,7 @@ class MasterSwarm(Swarm):
)
print('Master killed')
# pylint: disable=C0103
if __name__ == '__main__':
swarm = Swarm(parse())
try: