Improve the tests minion swarm.

The `pki` and all minion's configuration directories are now all created under the same temporary directory.
The minion's ID's are now something more meaningful and less random to ease the interpretation of what's going on while running.
Minor pep-8 corrections.
This commit is contained in:
Pedro Algarvio 2012-09-08 20:31:25 +01:00
parent ce0c8832a8
commit 348cd9e51e

View File

@ -13,8 +13,6 @@ import optparse
import subprocess import subprocess
import tempfile import tempfile
import shutil import shutil
import random
import hashlib
# Import salt libs # Import salt libs
import salt import salt
@ -53,8 +51,7 @@ def parse():
parser.add_option('--no-clean', parser.add_option('--no-clean',
action='store_true', action='store_true',
default=False, default=False,
help='Don\'t cleanup temporary files/directories' help='Don\'t cleanup temporary files/directories')
)
options, args = parser.parse_args() options, args = parser.parse_args()
@ -72,36 +69,49 @@ class Swarm(object):
''' '''
def __init__(self, opts): def __init__(self, opts):
self.opts = opts self.opts = opts
self.swarm_root = tempfile.mkdtemp(prefix='mswarm-root', suffix='.d')
self.pki = self._pki_dir() self.pki = self._pki_dir()
self.__zfill = len(str(self.opts['minions']))
self.confs = set() self.confs = set()
def _pki_dir(self): def _pki_dir(self):
''' '''
Create the shared pki directory Create the shared pki directory
''' '''
path = tempfile.mkdtemp(prefix='mswarm-pki-') path = os.path.join(self.swarm_root, 'pki')
cmd = 'salt-key -c {0} --gen-keys minion --gen-keys-dir {0} --key-logfile {1}' os.makedirs(path)
print('Creating shared pki keys for the swarm on: {0}'.format(path)) print('Creating shared pki keys for the swarm on: {0}'.format(path))
subprocess.call(cmd.format(path, os.path.join(path, 'keys.log')), shell=True) subprocess.call(
'salt-key -c {0} --gen-keys minion --gen-keys-dir {0} '
'--key-logfile {1}'.format(
path, os.path.join(path, 'keys.log')
), shell=True
)
print('Keys generated') print('Keys generated')
return path return path
def mkconf(self): def mkconf(self, idx):
''' '''
Create a config file for a single minion Create a config file for a single minion
''' '''
minion_id = hashlib.md5(str(random.randint(0, 999999))).hexdigest() minion_id = 'ms-{0}'.format(str(idx).zfill(self.__zfill))
dpath = tempfile.mkdtemp(
prefix='mswarm-{0}'.format(minion_id), suffix='.d' dpath = os.path.join(self.swarm_root, minion_id)
) os.makedirs(dpath)
data = {'id': minion_id,
data = {
'id': minion_id,
'user': pwd.getpwuid(os.getuid()).pw_name, 'user': pwd.getpwuid(os.getuid()).pw_name,
'pki_dir': self.pki, 'pki_dir': self.pki,
'cachedir': os.path.join(dpath, 'cache'), 'cachedir': os.path.join(dpath, 'cache'),
'master': self.opts['master'], 'master': self.opts['master'],
'log_file': os.path.join(dpath, 'minion.log') 'log_file': os.path.join(dpath, 'minion.log')
} }
path = os.path.join(dpath, 'minion') path = os.path.join(dpath, 'minion')
if self.opts['keep']: if self.opts['keep']:
ignore = set() ignore = set()
keep = self.opts['keep'].split(',') keep = self.opts['keep'].split(',')
@ -111,6 +121,7 @@ class Swarm(object):
continue continue
ignore.add(fn_.split('.')[0]) ignore.add(fn_.split('.')[0])
data['disable_modules'] = list(ignore) data['disable_modules'] = list(ignore)
with open(path, 'w+') as fp_: with open(path, 'w+') as fp_:
yaml.dump(data, fp_) yaml.dump(data, fp_)
self.confs.add(dpath) self.confs.add(dpath)
@ -134,8 +145,8 @@ class Swarm(object):
''' '''
Prepare the confs set Prepare the confs set
''' '''
for ind in range(self.opts['minions']): for idx in range(self.opts['minions']):
self.mkconf() self.mkconf(idx)
def clean_configs(self): def clean_configs(self):
''' '''
@ -149,7 +160,6 @@ class Swarm(object):
os.kill(pid, signal.SIGTERM) os.kill(pid, signal.SIGTERM)
except ValueError: except ValueError:
pass pass
#os.remove(path)
if os.path.exists(pidfile): if os.path.exists(pidfile):
os.remove(pidfile) os.remove(pidfile)
if not self.opts['no_clean']: if not self.opts['no_clean']:
@ -177,12 +187,13 @@ class Swarm(object):
def shutdown(self): def shutdown(self):
print('Killing any remaining running minions') print('Killing any remaining running minions')
subprocess.call( subprocess.call(
"kill -KILL $(ps aux | grep python | grep \"salt-minion\" | awk '{print $2}')", 'kill -KILL $(ps aux | grep python | grep "salt-minion" '
'| awk \'{print $2}\')',
shell=True shell=True
) )
if not self.opts['no_clean']: if not self.opts['no_clean']:
print('Remove ALL related temp files/directories') print('Remove ALL related temp files/directories')
subprocess.call('rm -rf /tmp/mswarm*', shell=True) shutil.rmtree(self.swarm_root)
print('Done') print('Done')
if __name__ == '__main__': if __name__ == '__main__':