mirror of
https://github.com/valitydev/salt.git
synced 2024-11-06 16:45:27 +00:00
Fix test/minionswarm.py
. Refs #1964.
* Allow the `minionswarm` to run under user space * Provide some additional configuration to the minions to use the temporary directories created for the swarm. * Try to properly shutdown the running minions. * As a last resort, `kill -KILL` all minions still running.
This commit is contained in:
parent
777820133f
commit
145197b48b
@ -6,6 +6,9 @@ on a single system to test scale capabilities
|
|||||||
|
|
||||||
# Import Python Libs
|
# Import Python Libs
|
||||||
import os
|
import os
|
||||||
|
import pwd
|
||||||
|
import time
|
||||||
|
import signal
|
||||||
import optparse
|
import optparse
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
@ -71,27 +74,27 @@ class Swarm(object):
|
|||||||
'''
|
'''
|
||||||
Create the shared pki directory
|
Create the shared pki directory
|
||||||
'''
|
'''
|
||||||
path = tempfile.mkdtemp()
|
path = tempfile.mkdtemp(prefix='mswarm-pki-')
|
||||||
cmd = 'salt-key --gen-keys minion --gen-keys-dir {0}'.format(path)
|
cmd = 'salt-key -c {0} --gen-keys minion --gen-keys-dir {0} --key-logfile {1}'
|
||||||
print('Creating shared pki keys for the swarm')
|
print('Creating shared pki keys for the swarm on: {0}'.format(path))
|
||||||
subprocess.call(cmd, shell=True)
|
subprocess.call(cmd.format(path, os.path.join(path, 'keys.log')), shell=True)
|
||||||
|
print('Keys generated')
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def mkconf(self):
|
def mkconf(self):
|
||||||
'''
|
'''
|
||||||
Create a config file for a single minion
|
Create a config file for a single minion
|
||||||
'''
|
'''
|
||||||
fd_, path = tempfile.mkstemp()
|
minion_id = hashlib.md5(str(random.randint(0, 999999))).hexdigest()
|
||||||
path = '{0}{1}'.format(
|
dpath = tempfile.mkdtemp(
|
||||||
path,
|
prefix='mswarm-{0}'.format(minion_id), suffix='.d'
|
||||||
hashlib.md5(str(random.randint(0, 999999))).hexdigest())
|
)
|
||||||
os.close(fd_)
|
data = {'id': minion_id,
|
||||||
dpath = '{0}.d'.format(path)
|
'user': pwd.getpwuid(os.getuid()).pw_name,
|
||||||
os.makedirs(dpath)
|
|
||||||
data = {'id': os.path.basename(path),
|
|
||||||
'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')
|
||||||
}
|
}
|
||||||
path = os.path.join(dpath, 'minion')
|
path = os.path.join(dpath, 'minion')
|
||||||
if self.opts['keep']:
|
if self.opts['keep']:
|
||||||
@ -134,10 +137,17 @@ class Swarm(object):
|
|||||||
Clean up the config files
|
Clean up the config files
|
||||||
'''
|
'''
|
||||||
for path in self.confs:
|
for path in self.confs:
|
||||||
|
pidfile = '{0}.pid'.format(path)
|
||||||
try:
|
try:
|
||||||
os.remove(path)
|
try:
|
||||||
os.remove('{0}.pid'.format(path))
|
pid = int(open(pidfile).read().strip())
|
||||||
shutil.rmtree('{0}.d'.format(path))
|
os.kill(pid, signal.SIGTERM)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
#os.remove(path)
|
||||||
|
if os.path.exists(pidfile):
|
||||||
|
os.remove(pidfile)
|
||||||
|
shutil.rmtree(path)
|
||||||
except (OSError, IOError):
|
except (OSError, IOError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -145,9 +155,32 @@ class Swarm(object):
|
|||||||
'''
|
'''
|
||||||
Start the minions!!
|
Start the minions!!
|
||||||
'''
|
'''
|
||||||
|
print('Starting minions...')
|
||||||
self.prep_configs()
|
self.prep_configs()
|
||||||
self.start_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(
|
||||||
|
"kill -KILL $(ps aux | grep python | grep \"salt-minion\" | awk '{print $2}')",
|
||||||
|
shell=True
|
||||||
|
)
|
||||||
|
print('Remove ALL related temp files/directories')
|
||||||
|
subprocess.call('rm -rf /tmp/mswarm*', shell=True)
|
||||||
|
print('Done')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
swarm = Swarm(parse())
|
swarm = Swarm(parse())
|
||||||
swarm.start()
|
try:
|
||||||
|
swarm.start()
|
||||||
|
finally:
|
||||||
|
swarm.shutdown()
|
||||||
|
Loading…
Reference in New Issue
Block a user