2013-11-27 11:19:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-04-18 01:12:35 +00:00
|
|
|
#/usr/bin/env python
|
|
|
|
'''
|
|
|
|
The minionswarm script will start a group of salt minions with different ids
|
|
|
|
on a single system to test scale capabilities
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python Libs
|
2014-02-03 11:07:11 +00:00
|
|
|
from __future__ import print_function
|
2012-04-18 01:12:35 +00:00
|
|
|
import os
|
2012-09-05 10:54:17 +00:00
|
|
|
import pwd
|
|
|
|
import time
|
|
|
|
import signal
|
2012-04-18 01:12:35 +00:00
|
|
|
import optparse
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
2012-04-18 05:58:10 +00:00
|
|
|
import shutil
|
2012-04-18 01:12:35 +00:00
|
|
|
|
2012-06-05 17:44:01 +00:00
|
|
|
# Import salt libs
|
|
|
|
import salt
|
|
|
|
|
2012-04-18 01:12:35 +00:00
|
|
|
# Import third party libs
|
|
|
|
import yaml
|
|
|
|
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2012-04-18 01:12:35 +00:00
|
|
|
def parse():
|
|
|
|
'''
|
|
|
|
Parse the cli options
|
|
|
|
'''
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('-m',
|
|
|
|
'--minions',
|
|
|
|
dest='minions',
|
|
|
|
default=5,
|
|
|
|
type='int',
|
|
|
|
help='The number of minions to make')
|
2012-04-18 05:58:10 +00:00
|
|
|
parser.add_option('--master',
|
|
|
|
dest='master',
|
|
|
|
default='salt',
|
|
|
|
help='The location of the salt master that this swarm will serve')
|
2013-04-11 21:36:28 +00:00
|
|
|
parser.add_option('--name',
|
|
|
|
'-n',
|
|
|
|
dest='name',
|
2013-04-14 23:05:43 +00:00
|
|
|
default='ms',
|
2013-04-11 21:36:28 +00:00
|
|
|
help=('Give the minions an alternative id prefix, this is used '
|
2013-05-01 23:06:17 +00:00
|
|
|
'when minions from many systems are being aggregated onto '
|
2013-04-11 21:36:28 +00:00
|
|
|
'a single master'))
|
2012-06-05 17:44:01 +00:00
|
|
|
parser.add_option('-k',
|
|
|
|
'--keep-modules',
|
|
|
|
dest='keep',
|
|
|
|
default='',
|
|
|
|
help='A comma delimited list of modules to enable')
|
2012-04-18 05:58:10 +00:00
|
|
|
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'))
|
2012-09-05 11:01:48 +00:00
|
|
|
parser.add_option('--no-clean',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
2012-09-08 19:31:25 +00:00
|
|
|
help='Don\'t cleanup temporary files/directories')
|
2012-10-08 20:13:05 +00:00
|
|
|
parser.add_option('--root-dir',
|
|
|
|
dest='root_dir',
|
|
|
|
default=None,
|
|
|
|
help='Override the minion root_dir config')
|
2014-01-23 11:22:26 +00:00
|
|
|
parser.add_option(
|
|
|
|
'-c', '--config-dir', default='/etc/salt',
|
|
|
|
help=('Pass in an alternative configuration directory. Default: '
|
|
|
|
'%default')
|
|
|
|
)
|
2014-02-06 17:57:02 +00:00
|
|
|
parser.add_option('-u', '--user', default=pwd.getpwuid(os.getuid()).pw_name)
|
2012-04-18 01:12:35 +00:00
|
|
|
|
|
|
|
options, args = parser.parse_args()
|
2012-05-13 14:18:05 +00:00
|
|
|
|
2012-04-18 01:12:35 +00:00
|
|
|
opts = {}
|
|
|
|
|
|
|
|
for key, val in options.__dict__.items():
|
|
|
|
opts[key] = val
|
|
|
|
|
|
|
|
return opts
|
|
|
|
|
|
|
|
|
|
|
|
class Swarm(object):
|
|
|
|
'''
|
|
|
|
Create a swarm of minions
|
|
|
|
'''
|
|
|
|
def __init__(self, opts):
|
|
|
|
self.opts = opts
|
2012-10-08 20:13:05 +00:00
|
|
|
|
|
|
|
# If given a root_dir, keep the tmp files there as well
|
2012-10-09 18:19:50 +00:00
|
|
|
if opts['root_dir']:
|
|
|
|
tmpdir = os.path.join(opts['root_dir'], 'tmp')
|
|
|
|
else:
|
|
|
|
tmpdir = opts['root_dir']
|
|
|
|
|
2012-10-08 20:13:05 +00:00
|
|
|
self.swarm_root = tempfile.mkdtemp(prefix='mswarm-root', suffix='.d',
|
|
|
|
dir=tmpdir)
|
|
|
|
|
2012-06-13 19:37:28 +00:00
|
|
|
self.pki = self._pki_dir()
|
2012-09-08 19:31:25 +00:00
|
|
|
self.__zfill = len(str(self.opts['minions']))
|
|
|
|
|
2012-04-18 01:12:35 +00:00
|
|
|
self.confs = set()
|
|
|
|
|
2012-06-13 19:37:28 +00:00
|
|
|
def _pki_dir(self):
|
|
|
|
'''
|
|
|
|
Create the shared pki directory
|
|
|
|
'''
|
2012-09-08 19:31:25 +00:00
|
|
|
path = os.path.join(self.swarm_root, 'pki')
|
|
|
|
os.makedirs(path)
|
|
|
|
|
2012-09-05 10:54:17 +00:00
|
|
|
print('Creating shared pki keys for the swarm on: {0}'.format(path))
|
2012-09-08 19:31:25 +00:00
|
|
|
subprocess.call(
|
|
|
|
'salt-key -c {0} --gen-keys minion --gen-keys-dir {0} '
|
2014-02-06 17:59:30 +00:00
|
|
|
'--log-file {1} --user {2}'.format(
|
|
|
|
path, os.path.join(path, 'keys.log'), self.opts['user'],
|
2012-09-08 19:31:25 +00:00
|
|
|
), shell=True
|
|
|
|
)
|
2012-09-05 10:54:17 +00:00
|
|
|
print('Keys generated')
|
2012-06-13 19:37:28 +00:00
|
|
|
return path
|
|
|
|
|
2012-09-08 19:31:25 +00:00
|
|
|
def mkconf(self, idx):
|
2012-04-18 01:12:35 +00:00
|
|
|
'''
|
|
|
|
Create a config file for a single minion
|
|
|
|
'''
|
2013-04-11 21:36:28 +00:00
|
|
|
minion_id = '{0}-{1}'.format(
|
|
|
|
self.opts['name'],
|
|
|
|
str(idx).zfill(self.__zfill)
|
|
|
|
)
|
2012-09-08 19:31:25 +00:00
|
|
|
|
|
|
|
dpath = os.path.join(self.swarm_root, minion_id)
|
|
|
|
os.makedirs(dpath)
|
|
|
|
|
2012-12-11 21:41:40 +00:00
|
|
|
minion_pkidir = os.path.join(dpath, 'pki')
|
|
|
|
os.makedirs(minion_pkidir)
|
|
|
|
minion_pem = os.path.join(self.pki, 'minion.pem')
|
|
|
|
minion_pub = os.path.join(self.pki, 'minion.pub')
|
|
|
|
shutil.copy(minion_pem, minion_pkidir)
|
|
|
|
shutil.copy(minion_pub, minion_pkidir)
|
|
|
|
|
2012-09-08 19:31:25 +00:00
|
|
|
data = {
|
|
|
|
'id': minion_id,
|
2014-02-06 17:57:02 +00:00
|
|
|
'user': self.opts['user'],
|
2012-12-11 21:41:40 +00:00
|
|
|
'pki_dir': minion_pkidir,
|
2012-09-08 19:31:25 +00:00
|
|
|
'cachedir': os.path.join(dpath, 'cache'),
|
|
|
|
'master': self.opts['master'],
|
|
|
|
'log_file': os.path.join(dpath, 'minion.log')
|
|
|
|
}
|
|
|
|
|
2012-10-09 18:19:50 +00:00
|
|
|
if self.opts['root_dir']:
|
|
|
|
data['root_dir'] = self.opts['root_dir']
|
|
|
|
|
2012-09-03 04:42:49 +00:00
|
|
|
path = os.path.join(dpath, 'minion')
|
2012-09-08 19:31:25 +00:00
|
|
|
|
2012-06-05 17:44:01 +00:00
|
|
|
if self.opts['keep']:
|
|
|
|
keep = self.opts['keep'].split(',')
|
|
|
|
modpath = os.path.join(os.path.dirname(salt.__file__), 'modules')
|
2013-05-02 21:49:43 +00:00
|
|
|
fn_prefixes = (fn_.partition('.')[0] for fn_ in os.listdir(modpath))
|
|
|
|
ignore = [fn_prefix for fn_prefix in fn_prefixes if fn_prefix not in keep]
|
|
|
|
data['disable_modules'] = ignore
|
2012-09-08 19:31:25 +00:00
|
|
|
|
2012-04-18 02:38:15 +00:00
|
|
|
with open(path, 'w+') as fp_:
|
|
|
|
yaml.dump(data, fp_)
|
2012-09-03 04:42:49 +00:00
|
|
|
self.confs.add(dpath)
|
2012-04-18 01:12:35 +00:00
|
|
|
|
|
|
|
def start_minions(self):
|
|
|
|
'''
|
|
|
|
Iterate over the config files and start up the minions
|
|
|
|
'''
|
|
|
|
for path in self.confs:
|
2012-04-18 05:58:10 +00:00
|
|
|
cmd = 'salt-minion -c {0} --pid-file {1}'.format(
|
2012-04-18 01:12:35 +00:00
|
|
|
path,
|
|
|
|
'{0}.pid'.format(path)
|
|
|
|
)
|
2012-04-18 05:58:10 +00:00
|
|
|
if self.opts['foreground']:
|
|
|
|
cmd += ' -l debug &'
|
|
|
|
else:
|
|
|
|
cmd += ' -d &'
|
2012-04-18 02:38:15 +00:00
|
|
|
subprocess.call(cmd, shell=True)
|
2012-04-18 01:12:35 +00:00
|
|
|
|
|
|
|
def prep_configs(self):
|
|
|
|
'''
|
|
|
|
Prepare the confs set
|
|
|
|
'''
|
2012-09-08 19:31:25 +00:00
|
|
|
for idx in range(self.opts['minions']):
|
|
|
|
self.mkconf(idx)
|
2012-04-18 01:12:35 +00:00
|
|
|
|
|
|
|
def clean_configs(self):
|
|
|
|
'''
|
|
|
|
Clean up the config files
|
|
|
|
'''
|
|
|
|
for path in self.confs:
|
2012-09-05 10:54:17 +00:00
|
|
|
pidfile = '{0}.pid'.format(path)
|
2012-04-18 01:12:35 +00:00
|
|
|
try:
|
2012-09-05 10:54:17 +00:00
|
|
|
try:
|
|
|
|
pid = int(open(pidfile).read().strip())
|
|
|
|
os.kill(pid, signal.SIGTERM)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
if os.path.exists(pidfile):
|
|
|
|
os.remove(pidfile)
|
2012-09-05 11:01:48 +00:00
|
|
|
if not self.opts['no_clean']:
|
|
|
|
shutil.rmtree(path)
|
2012-06-11 20:43:42 +00:00
|
|
|
except (OSError, IOError):
|
2012-04-18 01:12:35 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
'''
|
|
|
|
Start the minions!!
|
|
|
|
'''
|
2012-09-05 10:54:17 +00:00
|
|
|
print('Starting minions...')
|
2012-04-18 01:12:35 +00:00
|
|
|
self.prep_configs()
|
|
|
|
self.start_minions()
|
2012-09-05 10:54:17 +00:00
|
|
|
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(
|
2013-10-11 08:02:23 +00:00
|
|
|
'pkill -KILL -f "python.*salt-minion"',
|
2012-09-05 10:54:17 +00:00
|
|
|
shell=True
|
|
|
|
)
|
2012-09-05 11:01:48 +00:00
|
|
|
if not self.opts['no_clean']:
|
|
|
|
print('Remove ALL related temp files/directories')
|
2012-09-08 19:31:25 +00:00
|
|
|
shutil.rmtree(self.swarm_root)
|
2012-09-05 10:54:17 +00:00
|
|
|
print('Done')
|
2012-04-18 01:12:35 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
swarm = Swarm(parse())
|
2012-09-05 10:54:17 +00:00
|
|
|
try:
|
|
|
|
swarm.start()
|
|
|
|
finally:
|
|
|
|
swarm.shutdown()
|