forked from cirosantilli/linux-kernel-module-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-docker
executable file
·69 lines (66 loc) · 2.05 KB
/
run-docker
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
#!/usr/bin/env python3
import argparse
import os
import sys
import common
import shell_helpers
from shell_helpers import LF
container_name = common.consts['repo_short_id']
container_hostname = common.consts['repo_short_id']
image_name = common.consts['repo_short_id']
target_dir = '/root/{}'.format(common.consts['repo_short_id'])
docker = ['sudo', 'docker']
def create(args):
sh.run_cmd(docker + ['build', '-t', image_name, '.', LF])
# --privileged for KVM:
# https://stackoverflow.com/questions/48422001/launching-qemu-kvm-from-inside-docker-container
sh.run_cmd(
docker +
[
'create', LF,
'--hostname', container_hostname, LF,
'-i', LF,
'--name', container_name, LF,
'--net', 'host', LF,
'--privileged', LF,
'-t', LF,
'-w', target_dir, LF,
'-v', '{}:{}'.format(os.getcwd(), target_dir), LF,
image_name,
]
)
def destroy(args):
stop(args)
sh.run_cmd(docker + ['rm', container_name, LF])
sh.run_cmd(docker + ['rmi', image_name, LF])
def sh_func(args):
start(args)
if args:
sh_args = args
else:
sh_args = ['bash']
exit_status = sh.run_cmd(
docker + ['exec', '-i', '-t', container_name] +
sh_args +
[LF],
raise_on_failure=False
)
sys.exit(exit_status)
def start(args):
sh.run_cmd(docker + ['start', container_name, LF])
def stop(args):
sh.run_cmd(docker + ['stop', container_name, LF])
cmd_action_map = {
'create': lambda args: create(args),
'DESTROY': lambda args: destroy(args),
'sh': lambda args: sh_func(args),
'start': lambda args: start(args),
'stop': lambda args: stop(args),
}
parser = argparse.ArgumentParser()
parser.add_argument('--dry-run', default=False, action='store_true')
parser.add_argument('cmd', choices=cmd_action_map, default='sh', nargs='?')
parser.add_argument('args', nargs='*')
args = parser.parse_args()
sh = shell_helpers.ShellHelpers(dry_run=args.dry_run)
cmd_action_map[args.cmd](args.args)