-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhica
executable file
·260 lines (216 loc) · 8.25 KB
/
hica
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python
# vim: set fileencoding=utf-8
# Pavel Odvody <[email protected]>
#
# HICA - Host integrated container applications
#
# MIT License (C) 2015
import json, os, sys, docker, argparse, imp, subprocess, shlex
from base.hica_base import *
# create docker client for inspecting images
dock = docker.Client()
_injectors_path = '/var/lib/docker-hica/injectors'
_selinux = '/usr/sbin/getenforce'
_injector_type = 'HicaInjector'
_injector_registry = {}
# adjust for container arguments passed after the -- delimeter
container_args = []
sliceat = 0
for i, arg in enumerate(sys.argv[1:]):
if sliceat:
container_args.append(arg)
if arg == '--':
sliceat = i + 1
if sliceat:
sys.argv = sys.argv[:sliceat]
def _HicaBailIf(rc, *msg):
if rc > 0:
for m in msg:
print(m)
sys.exit(rc)
def HicaTestSingleGuest(injector, spec, image, config):
run = ['docker', 'run', '--entrypoint', '/bin/sh'] + config + [image, '-c', spec]
rc = subprocess.call(run)
_HicaBailIf(rc, '*** Guest test failed for injector {0}!'.format(injector),
' ' + spec)
def HicaTestSingleHost(injector, spec):
rc = subprocess.call(['/bin/sh', '-c', spec])
_HicaBailIf(rc, '*** Host test failed for injector {0}!'.format(injector),
' ' + spec)
def HicaModuleFindInjectors(module):
""" Find injectors in the given module
:param module: Loaded module
:type module: imp.Module
"""
injectors = []
for n in dir(module):
attr = getattr(module, n)
if hasattr(attr, '__base__') and attr.__base__.__name__ == _injector_type:
injectors.append(attr)
return injectors
def HicaLoadInjectors(path):
""" Load all Python modules from `path`
:param path: Relative path to module directory
"type path: str
"""
r = {}
for inj in os.listdir(path):
modname, suffix = inj.rsplit('.', 1)
if suffix == 'py':
fullpath = os.path.abspath(path)
f, filename, description = imp.find_module(modname, [fullpath])
m = imp.load_module(modname, open(filename, 'U'), filename, description)
try:
injs = HicaModuleFindInjectors(m)
for i in injs:
obj = i()
r[obj.get_config_key()] = obj
except AttributeError:
print('Module {0} does not implement `register(context)`'.format(modname))
return r
def HicaGetBaseArgparse():
""" Provides with common `argparse` settings """
args = argparse.ArgumentParser()
args.add_argument('hica_app_name')
args.add_argument('named_action', default=None, nargs=argparse.REMAINDER)
args.add_argument('--show-args', help='show possible arguments for the specified "hica_app_name"', default=False, action='store_true')
args.add_argument('--test-injectors', help='executes injector tests for specified "hica_app_name"', default=False, action='store_true')
args.add_argument('--verbose', help='print additional information', default=False, action='store_true')
args.add_argument('--yes', help='bypass the capability review check', default=False, action='store_true')
args.add_argument('--selinux-label', help='provide a confinement context', default='label:disable')
args.add_argument('--user', help='user:group to run as ({0}:{0})'.format(str(os.getuid())), default='{0}:{0}'.format(str(os.getuid())))
return args
def HicaGetImageName():
""" Get image name so that we can fetch labels from it """
args = HicaGetBaseArgparse()
p, _ = args.parse_known_args()
return p.hica_app_name
def HicaGetLabelsFromImage(image):
""" Get labels from image as list
:param image: Docker image
:type image: dict
"""
return [(k, v) for (k, v) in image['Config']['Labels'].iteritems()]
def HicaParseArguments(labels):
""" Parse argument and optionally show usage and terminate with `0`
return code
:param labels: `Labels` read from the `Image`
:type labels: list(str)
"""
args = HicaGetBaseArgparse()
# parse now to catch --show-args
p, _ = args.parse_known_args()
# top-level label cache
seen = []
# add needed argparsers
for label in labels.items:
if label[0] in _injector_registry:
seen.append(label[0].rsplit('.', 1)[1])
inj = _injector_registry[label[0]]
inj.labels = labels
for (arg, typ, defval) in inj.get_injected_args():
if arg:
args.add_argument(arg, default=defval)
# now that we added the argparsers we can print usage and bail
if p.show_args:
args.print_usage()
sys.exit(0)
# test injectors
if p.test_injectors:
guest, host = [], []
# collect tests
for label in seen:
gl = [(label, a, b) for (a, b) in labels.query(label, 'test.guest')]
guest.extend(gl)
host.extend(labels.query(label, 'test.host'))
# run guest
for (inj, lbl, spec) in guest:
conf, iargs = [], []
i = _injector_registry[HicaLabelStore.PREFIX + '.' + inj]
for (_, t, v) in i.get_injected_args():
iargs.append((t, v))
i.inject_config(conf, iargs)
HicaTestSingleGuest(lbl, spec, p.hica_app_name, conf)
# run host
for (lbl, spec) in host:
HicaTestSingleHost(lbl, spec)
sys.exit(0)
return args.parse_args()
def HicaInjectConfiguration(labels, args, config):
""" Inject configuration parameters for `labels` with values
obtained from `args` into the `config` array
:param labels: `Labels` read from the `Image`
:type labels: list(str)
:param args: Result of `parse_args` operation
:type args: argparse.ArgumentParser
:param config: Array where to inject configuration parameters
:type config: list(str)
"""
formatted = []
actions = {}
caps = []
# map params from argparse back to injectors
for label in labels.items:
# command aliases are not handled via explicit injector
if label[0] == 'io.hica.command_aliases':
actions = json.loads(label[1])
if label[0] in _injector_registry:
inj = _injector_registry[label[0]]
cfgs = []
sub = labels.query_full(label[0], 'description')
if sub:
caps.append(sub[0][1])
caps.append(inj.get_description())
for (arg, typ, defval) in inj.get_injected_args():
if arg:
x = getattr(args, arg[2:].replace('-', '_'))
cfgs.append((typ, x))
else:
cfgs.append((typ, defval))
inj.inject_config(config, cfgs)
# process named actions aka command aliases
if args.named_action:
action, aargs = args.named_action[0], args.named_action[1:]
if action in actions:
if len(aargs) > 0 and aargs[0] in ['--help', 'help']:
print("{0} synopsis:\n {1}".format(action, actions[action]["synopsis"]))
sys.exit(0)
try:
formatted = shlex.split(actions[action]["cmd"].format(*aargs))
except IndexError:
_HicaBailIf(1, '*** Action "{0}" expects parameters:\n {1}'.format(action, actions[action]))
instr = 'The container requests the following capabilities: \n - ' + '\n - '.join(caps) + '\nProceed? [y/Y/n]: '
if args.yes or not caps or raw_input(instr) in ['y', 'Y']:
return formatted, config
else:
_HicaBailIf(1, '*** Operation aborted!')
def HicaGetImages(images):
""" Gets a list of all image names
:param images: all docker images
:type images: dict
"""
tags = filter(lambda y: y != [u'<none>:<none>'], [x['RepoTags'] for x in images])
return set([a for b in tags for a in b])
_injector_registry = HicaLoadInjectors(_injectors_path)
name = HicaGetImageName()
images = HicaGetImages(dock.images(all=True))
# search for the image, also try the default 'latest' tag
if name not in images and (name + ':latest') not in images:
_HicaBailIf(1, '*** Image \'{0}\' not found locally, bailing!'.format(name))
image = dock.inspect_image(name)
labels = HicaGetLabelsFromImage(image)
ls = HicaLabelStore(labels)
parsed = HicaParseArguments(ls)
# base configuration
# run the container as current user so that file system rights are fine
# for bind mounted files
cfg = ['-i', '-u', parsed.user ]
# check if we have SELinux enabled
if os.path.exists(_selinux) and 'Enforcing' in subprocess.check_output([_selinux]):
cfg += ['--security-opt', parsed.selinux_label]
aliased, config = HicaInjectConfiguration(ls, parsed, cfg)
runcmd = ['docker', 'run'] + config + [name] + container_args + aliased
if parsed.verbose:
print('Executing: ' + ' '.join(runcmd))
p = subprocess.Popen(runcmd, stdin=sys.stdin)
out, err = p.communicate()