-
Notifications
You must be signed in to change notification settings - Fork 22
/
addons.py
99 lines (86 loc) · 3.42 KB
/
addons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import subprocess
import tempfile
import shutil
import shlex
import yaml
import sys
import os
import glob
import logging
LOCAL = 'local'
REMOTE = 'remote'
ADDONS_FILE_PATH = os.path.join(os.path.dirname(__file__), 'addons.yaml')
FNULL = open(os.devnull, 'w')
def do_addons(addons_file_path, enabled_addons, units, dump_to, uniq):
push_location = '/{dump_to}/{uniq}/addons'.format(dump_to=dump_to, uniq=uniq)
pull_location = '/{dump_to}/{uniq}/addon_output'.format(dump_to=dump_to, uniq=uniq)
addons = {}
for addon_file in addons_file_path:
addons.update(load_addons(addon_file))
if enabled_addons:
async_commands('juju ssh {} "mkdir -p %s"' % push_location, units)
async_commands('juju ssh {} "mkdir -p %s"' % pull_location, units)
for addon in enabled_addons:
if addon not in addons:
logging.warning('The addons file: "%s" does not define %s' %
(addons_file_path, addon))
sys.exit(1)
addons[addon].push(units, push_location)
addons[addon].run(units,
{'location': push_location, 'output': pull_location})
def tempdir(func):
def temp_function(*args, **kwargs):
olddir = os.getcwd()
tempdir = tempfile.mkdtemp()
os.chdir(tempdir)
func(*args, **kwargs)
os.chdir(olddir)
shutil.rmtree(tempdir)
return temp_function
def load_addons(addons_file_path):
with open(addons_file_path) as addons_file:
addon_specs = yaml.safe_load(addons_file)
addons = {}
for name, info in addon_specs.items():
try:
assert(LOCAL in info)
assert(REMOTE in info)
except AssertionError:
logging.warning(
'The addons file: "%s" is malformed, "%s" does not have one'
' of the necessary keys "%s" or "%s"' %
(addons_file_path, name, LOCAL, REMOTE))
sys.exit(1)
addons[name] = CrashdumpAddon(name, info[LOCAL], info[REMOTE])
return addons
def async_commands(command, contexts, timeout=45):
"""Run the command concurrently for each given context."""
procs = []
for context in contexts:
args = shlex.split(('timeout %ds ' % timeout) +
command.format(context))
logging.debug("Running {} in context {}".format(command, context))
procs.append(subprocess.Popen(args, stdin=FNULL, stdout=FNULL,
stderr=FNULL))
for proc in procs:
proc.communicate()
if proc.returncode != 0:
logging.warning('command %s failed' % command)
class CrashdumpAddon(object):
"""An addon to run on the nodes"""
def __init__(self, name, local, remote):
self.name = name
self.local = local
self.remote = remote
@tempdir
def push(self, units, location):
"""This will fetch the command, and push it to the units"""
subprocess.check_call(self.local, shell=True, stdout=FNULL,
stderr=FNULL)
files = ' '.join(glob.glob('*'))
async_commands('juju scp -- -r %s {}:%s' % (files, location), units)
def run(self, units, context):
"""This will runt the remote command on the units"""
remote_cmd = '"cd {location}; %s"' % self.remote.format(**context)
remote_cmd = remote_cmd.format(**context)
async_commands('juju ssh {} -- %s' % remote_cmd, units)