-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·89 lines (76 loc) · 2.44 KB
/
main.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
#!/usr/bin/env python
import argparse
import traceback
from systemd.docker.Docker import Docker
from systemd.log.Logger import Logger
class App:
logger = Logger.getLogger(__name__)
def prepareArgumentParser(self):
argParser = argparse.ArgumentParser()
argParser.add_argument(
'--image',
required=True,
help='Docker image name'
)
argParser.add_argument(
'--network',
help='Docker network type'
)
argParser.add_argument(
'--volumes',
nargs = '+',
help='Docker container volumes'
)
argParser.add_argument(
'--cmd',
help='Docker container cmd. If not present default Dockerfile CMD will be used'
)
argParser.add_argument(
'--data-archive',
help='The path from *.tar archive will be read'
)
argParser.add_argument(
'--data-unarchive-path',
help='The path were *.tar archive will be placed and unarchived in the docker container'
)
argParser.add_argument(
'--privileged',
action="store_true",
default=False,
help='If present docker image will be run with root privileged'
)
argParser.add_argument(
'--exec-commands',
required=True,
nargs = '+',
help='List of commands that will be executed in the docker container'
)
return argParser
def run(self, params):
container = None
try:
docker = Docker()
container = docker.runContainer(params)
docker.copyDataToContainer(
container,
params.data_archive,
params.data_unarchive_path
)
docker.executeCommand(container, params.exec_commands)
finally:
try:
docker.stopContainer(container)
except:
# Ignore because we just try to stop the container
pass
if __name__ == '__main__':
logger = Logger.getLogger(__name__)
try:
app = App()
args = app.prepareArgumentParser().parse_args()
logger.debug("Run app with arguments: %s" % args)
app.run(args)
except Exception as exception:
logger.error("Fatal error occurred! Reason: %s!", exception)
traceback.print_exc()
exit(1)