-
Notifications
You must be signed in to change notification settings - Fork 1
/
script-podman.py
executable file
·374 lines (283 loc) · 12 KB
/
script-podman.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/python3
import argparse
import os
import rpm
import selinux
import subprocess
import sys
from podman import PodmanClient
from termcolor import cprint
# import podman variables from local file
sys.dont_write_bytecode = True
from podman_variables import *
def print_yes():
cprint(' [YES]', 'green')
def print_no():
cprint(' [NO]', 'red')
def print_soft_no():
cprint(' [NO]', 'yellow', attrs=['bold','dark'])
def print_success():
cprint(' [SUCCESS]', 'green')
def print_failure():
cprint(' [FAILURE]', 'red')
def print_debug(msg, cmd):
cprint(f'DEBUG: {msg}:', 'yellow')
cprint(f'{cmd}\n', 'yellow', attrs=['bold'])
def check_podman_installed():
cprint('{0:.<70}'.format('PODMAN: is podman installed'), 'yellow', end='')
podman_installed = None
ts = rpm.TransactionSet()
rpm_listing = ts.dbMatch()
for rpm_pkg in rpm_listing:
if rpm_pkg['name'] == 'podman':
podman_installed = True
if podman_installed:
print_yes()
else:
print_no()
cprint('\npodman is not installed', 'magenta')
cprint('Exiting...', 'red')
sys.exit(1)
def ensure_podman_socket_running():
if os.geteuid() == 0:
user = ''
else:
user = '--user '
cmd_str = f'systemctl {user} is-active --quiet podman.socket'
cmd = cmd_str.split()
cmd_output = subprocess.run(cmd)
if cmd_output.returncode == 0:
return
cprint('PODMAN: starting podman.socket...', 'yellow')
cmd_str = f'systemctl {user}start podman.socket'
cmd = cmd_str.split()
cmd_output = subprocess.run(cmd, capture_output=True, universal_newlines=True)
if args.debug:
print_debug('to manually start podman.socket', cmd_str)
if cmd_output.returncode != 0:
err_output = cmd_output.stderr.rstrip()
cprint(err_output, 'red', attrs=['bold'])
sys.exit(2)
def ensure_image_exists():
cprint('{0:.<70}'.format('PODMAN: checking if image exists'), 'yellow', end='')
podman_image = client.images.list(filters = {'reference' : image_name})
if podman_image:
print_yes()
else:
print_soft_no()
cprint('PODMAN: building image...', 'yellow')
# using the api function will hide the build process
# use subprocess so we can see it in real-time
# client.images.build(path=cur_dir, tag=image_name, squash=True, rm=True)
podman_cmd_str = f'podman build --squash -t {image_name} {cur_dir}'
podman_cmd = podman_cmd_str.split()
if args.debug:
print_debug('to manually build the image', podman_cmd_str)
cmd_output = subprocess.run(podman_cmd, universal_newlines=True)
cprint('{0:.<70}'.format('PODMAN: build image'), 'yellow', end='')
if cmd_output.returncode != 0:
print_failure()
cprint('Exiting...', 'red')
sys.exit(3)
else:
print_success()
def ensure_image_removed():
cprint('{0:.<70}'.format('PODMAN: checking if image exists'), 'yellow', end='')
podman_image_exists = client.images.list(filters = {'reference' : image_name})
if podman_image_exists:
print_yes()
cprint('PODMAN: removing image...', 'yellow')
client.images.remove(image=image_name)
else:
print_soft_no()
def ensure_container_exists_and_running(interactive):
cprint('{0:.<70}'.format('PODMAN: checking if container exists'), 'yellow', end='')
container_exists = client.containers.list(all=True, filters = {'name' : container_name})
if container_exists:
print_yes()
podman_container = client.containers.get(container_name)
container_status = podman_container.status
cprint('{0:.<70}'.format('PODMAN: checking if container is running'), 'yellow', end='')
if container_status == 'running':
print_yes()
return
else:
print_soft_no()
cprint('PODMAN: starting container...', 'yellow')
if args.debug:
print_debug('to manually start the container', f'podman start {container_name}')
podman_container.start()
ensure_container_exists_and_running(interactive)
else:
print_soft_no()
run_container(interactive)
if interactive:
ensure_container_exists_and_running(interactive)
def ensure_container_stopped_removed(remove_container=True):
cprint('{0:.<70}'.format('PODMAN: checking if container exists'), 'yellow', end='')
container_exists = client.containers.list(all=True, filters = {'name' : container_name})
if container_exists:
print_yes()
podman_container = client.containers.get(container_name)
container_status = podman_container.status
cprint('{0:.<70}'.format('PODMAN: checking if container is running'), 'yellow', end='')
if container_status == 'running':
print_yes()
cprint('PODMAN: stopping container...', 'yellow')
if args.debug:
print_debug('to manually stop the container', f'podman stop {container_name}')
podman_container.stop()
if remove_container:
# in the event that auto-remove is set the container may already be deleted
ensure_container_stopped_removed(remove_container)
return
else:
print_soft_no()
if remove_container:
cprint('PODMAN: removing container...', 'yellow')
if args.debug:
print_debug('to manually remove the container', f'podman rm {container_name}')
podman_container.remove()
else:
print_soft_no()
def set_selinux_context_t():
container_context_t = 'container_file_t'
dir_file_paths = []
for vol in bind_volumes:
host_dir = None
recursive = None
if vol['selinux_label']:
host_dir = vol['source']
recursive = vol['selinux_recursive']
if not recursive:
dir_file_paths.append(host_dir)
else:
for dir_path, dirs, files in os.walk(host_dir):
for filename in files:
file_path = os.path.join(dir_path,filename)
dir_file_paths.append(file_path)
dir_file_paths.append(dir_path)
if not dir_file_paths:
return
cprint('{0:.<70}'.format('PODMAN: selinux label check'), 'yellow', end='')
for dir_file_path in dir_file_paths:
ret, mount_dir_context = selinux.getfilecon(dir_file_path)
if ret < 0:
print_failure()
cprint(f'selinux.getfilecon({dir_file_path}) failed....exiting', red)
sys.exit(4)
mount_dir_context_t = mount_dir_context.split(':')[2]
if mount_dir_context_t != container_context_t:
mount_dir_context = mount_dir_context.replace(mount_dir_context_t, container_context_t)
selinux.setfilecon(dir_file_path, mount_dir_context)
print_yes()
def create_podman_vol_str():
mount_vol_list = []
if not bind_volumes:
return ''
for vol in bind_volumes:
option = ''
if vol['read_only']:
option = ':ro'
mount_vol_list.append(f"-v {vol['source']}:{vol['target']}{option} ")
mount_vol_str = ''.join(mount_vol_list).rstrip()
return mount_vol_str
def run_container(interactive):
if selinux.is_selinux_enabled():
set_selinux_context_t()
cprint('PODMAN: run container...', 'yellow')
podman_vol_str = create_podman_vol_str()
if privileged:
privileged_str = '--privileged '
else:
privileged_str = ''
if interactive:
podman_cmd_str = f'podman run -d -it {privileged_str}{podman_vol_str} -h {container_hostname} --name {container_name} {image_name}'
if args.debug:
print_debug('to manually run the container', podman_cmd_str)
client.containers.run(image=image_name, name=container_name, hostname=container_hostname, detach=True, tty=True, privileged=privileged, mounts=bind_volumes)
else:
podman_cmd_str = f'podman run -it --rm {privileged_str}{podman_vol_str} -h {container_hostname} --name {container_name} {image_name} {container_script}'
podman_cmd = podman_cmd_str.split()
if args.debug:
print_debug('to manually run the container', podman_cmd_str)
cprint(f'PODMAN: running command {container_script}', 'yellow')
# using the api function will hide the script output
# use subprocess so we can see it in real-time
# client.containers.run(image=image_name, name=container_name, hostname=container_hostname, detach=True, auto_remove=True, mounts=bind_volumes, command=container_script)
cmd_output = subprocess.run(podman_cmd, universal_newlines=True)
cprint('{0:.<70}'.format(f'PODMAN: running {container_script}'), 'yellow', end='')
if cmd_output.returncode != 0:
print_failure()
cprint('Exiting...', 'red')
sys.exit(5)
else:
print_success()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
parser.add_argument('--auto',
action='store_true',
help='ensure image is built, then run container_script in a non-interactive container',
default=False)
parser.add_argument('--debug',
action='store_true',
help='display debug messages',
default=False)
group.add_argument('--rebuild',
action='store_true',
help='remove podman image and container if they exist, '
'then build (new) podman image and run container',
default=False)
group.add_argument('--rerun',
action='store_true',
help='remove container if it exists, then (re-)run it',
default=False)
group.add_argument('--restart',
action='store_true',
help='stop the container if it exists, then (re-)run it',
default=False)
group.add_argument('--rm_image',
action='store_true',
help='remove podman image and container if they exist',
default=False)
group.add_argument('--rm_container',
action='store_true',
help='remove container if it exists',
default=False)
group.add_argument('--stop_container',
help='stop podman container it exists and is running',
action='store_true',
default=False)
args = parser.parse_args()
check_podman_installed()
ensure_podman_socket_running()
if os.geteuid() == 0:
client = PodmanClient(base_url='unix:/run/podman/podman.sock')
else:
client = PodmanClient()
if args.auto:
interactive = False
else:
interactive = True
if args.rm_image or args.rebuild:
ensure_container_stopped_removed()
ensure_image_removed()
if args.rm_image: sys.exit()
if args.rm_container or args.rerun:
ensure_container_stopped_removed()
if args.rm_container: sys.exit()
if args.stop_container or args.restart:
ensure_container_stopped_removed(remove_container=False)
if args.stop_container: sys.exit()
if not interactive and not args.rebuild:
ensure_container_stopped_removed()
cprint('{0:.<70}'.format('PODMAN: image name'), 'yellow', end='')
cprint(f' {image_name}', 'cyan')
ensure_image_exists()
cprint('{0:.<70}'.format('PODMAN: container name'), 'yellow', end='')
cprint(f' {container_name}', 'cyan')
ensure_container_exists_and_running(interactive)
if interactive:
cprint('PODMAN: to login to the container run:', 'yellow')
cprint(f'podman exec -it {container_name} /bin/bash\n', 'green')