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
|
|
|
|
import os
|
|
|
|
import optparse
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
2012-04-18 05:58:10 +00:00
|
|
|
import shutil
|
2012-04-20 16:25:26 +00:00
|
|
|
import random
|
|
|
|
import hashlib
|
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')
|
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-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
|
|
|
|
self.confs = set()
|
|
|
|
|
|
|
|
def mkconf(self):
|
|
|
|
'''
|
|
|
|
Create a config file for a single minion
|
|
|
|
'''
|
2012-04-25 07:20:49 +00:00
|
|
|
fd_, path = tempfile.mkstemp()
|
2012-04-20 16:25:26 +00:00
|
|
|
path = '{0}{1}'.format(
|
|
|
|
path,
|
|
|
|
hashlib.md5(str(random.randint(0, 999999))).hexdigest())
|
2012-04-25 07:20:49 +00:00
|
|
|
os.close(fd_)
|
2012-04-18 02:38:15 +00:00
|
|
|
dpath = '{0}.d'.format(path)
|
|
|
|
os.makedirs(dpath)
|
|
|
|
data = {'id': os.path.basename(path),
|
|
|
|
'pki_dir': os.path.join(dpath, 'pki'),
|
|
|
|
'cache_dir': os.path.join(dpath, 'cache'),
|
2012-04-18 05:58:10 +00:00
|
|
|
'master': self.opts['master'],
|
2012-04-18 02:38:15 +00:00
|
|
|
}
|
2012-06-05 17:44:01 +00:00
|
|
|
if self.opts['keep']:
|
|
|
|
ignore = set()
|
|
|
|
keep = self.opts['keep'].split(',')
|
|
|
|
modpath = os.path.join(os.path.dirname(salt.__file__), 'modules')
|
|
|
|
for fn_ in os.listdir(modpath):
|
|
|
|
if fn_.split('.')[0] in keep:
|
|
|
|
continue
|
|
|
|
ignore.add(fn_.split('.')[0])
|
|
|
|
data['disable_modules'] = list(ignore)
|
2012-04-18 02:38:15 +00:00
|
|
|
with open(path, 'w+') as fp_:
|
|
|
|
yaml.dump(data, fp_)
|
2012-04-18 01:12:35 +00:00
|
|
|
self.confs.add(path)
|
|
|
|
|
|
|
|
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-06-01 05:31:27 +00:00
|
|
|
for ind in range(self.opts['minions']):
|
2012-04-18 01:12:35 +00:00
|
|
|
self.mkconf()
|
|
|
|
|
|
|
|
def clean_configs(self):
|
|
|
|
'''
|
|
|
|
Clean up the config files
|
|
|
|
'''
|
|
|
|
for path in self.confs:
|
|
|
|
try:
|
|
|
|
os.remove(path)
|
2012-04-18 05:58:10 +00:00
|
|
|
os.remove('{0}.pid'.format(path))
|
|
|
|
shutil.rmtree('{0}.d'.format(path))
|
2012-04-18 01:12:35 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
'''
|
|
|
|
Start the minions!!
|
|
|
|
'''
|
|
|
|
self.prep_configs()
|
|
|
|
self.start_minions()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
swarm = Swarm(parse())
|
|
|
|
swarm.start()
|