-
Notifications
You must be signed in to change notification settings - Fork 3
/
hook_docker.py
278 lines (196 loc) · 7.77 KB
/
hook_docker.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
import pbs
import os
import copy
import re
class Docker(object):
e = None
j = None
jid = None
image = None
entrypoint = None
is_running = False
job_file = None
container_type = None #interactive, service, script, executable
hook_events = {}
ignored_env = ["PATH", "TMPDIR"]
def __init__(self, pbs_event):
self.hook_events = { pbs.QUEUEJOB: self.__queuejob_handler,
pbs.MODIFYJOB: self.__queuejob_handler,
pbs.EXECJOB_LAUNCH: self.__execjob_launch_handler,
pbs.EXECJOB_PROLOGUE: self.__execjob_prologue_handler,
pbs.EXECJOB_END: self.__execjob_end_handler,
}
self.e = pbs_event
self.j = self.e.job
self.jid = self.j.id
def __queuejob_handler(self):
pbs.logmsg(pbs.LOG_DEBUG, "Docker queuejob handler start")
newselect = []
if "select" in self.j.Resource_List.keys():
for i in str(self.j.Resource_List["select"]).split("+"):
if re.search("docker=[Tt]{1}rue", i):
newselect.append(i)
continue
newselect.append(i + ":docker=true")
else:
newselect.append("docker=true")
pbs.logmsg(pbs.LOG_DEBUG, "Old select: %s" % str(self.j.Resource_List))
self.j.Resource_List["select"] = pbs.select("+".join(newselect))
pbs.logmsg(pbs.LOG_DEBUG, "New select: %s" % str(self.j.Resource_List))
def __execjob_launch_handler(self):
pbs.logmsg(pbs.LOG_DEBUG, "Docker execjob_launch handler start")
if self.j.interactive:
self.container_type = "interactive"
pbs.logmsg(pbs.LOG_DEBUG, "It's an interactive")
elif self.e.progname.find("docker") != -1:
self.container_type = "service"
pbs.logmsg(pbs.LOG_DEBUG, "It's a service")
elif self.e.progname.find("bash") != -1:
self.container_type = "script"
pbs.logmsg(pbs.LOG_DEBUG, "It's a script")
else:
#musi byt posledni
self.container_type = "executable"
pbs.logmsg(pbs.LOG_DEBUG, "It's an executable")
self.check_container_running()
self.check_job_file()
self.create_container()
self.launch_job()
def __execjob_prologue_handler(self):
# true pokud primarni exec. host
if self.j.in_ms_mom():
self.accept()
pbs.logmsg(pbs.LOG_DEBUG, "Docker execjob_prologue handler start")
self.container_type = "service"
self.check_container_running()
self.check_job_file()
self.create_container()
def __execjob_end_handler(self):
pbs.logmsg(pbs.LOG_DEBUG, "Docker execjob_end handler start")
call = "docker stop " + str(self.jid)
pbs.logmsg(pbs.LOG_DEBUG, "Call is: %s" % call)
os.system(call)
call = "docker rm " + str(self.jid)
pbs.logmsg(pbs.LOG_DEBUG, "Call is: %s" % call)
os.system(call)
def main(self):
self.hook_events[self.e.type]()
def accept(self):
self.e.accept()
def get_docker_vars(self):
self.image = None
self.entrypoint = None
vars = str(self.j.Variable_List).split(",")
for var in vars:
#pbs.logmsg(pbs.LOG_DEBUG, "Variable is: %s" % var )
if var.startswith("DOCKER_IMAGE="):
name = var.split("=", 1)
self.image = name[1]
if var.startswith("DOCKER_ENTRYPOINT="):
name = var.split("=", 1)
self.entrypoint = name[1]
pbs.logmsg(pbs.LOG_DEBUG, "Docker image is: %s" % self.image)
def check_container_running(self):
self.is_running = False
search_container = "docker ps 2>/dev/null | grep %s | grep -v grep" % str(self.jid)
try:
already_running = os.popen(search_container).read()
except:
self.is_running = False
if already_running:
self.is_running = True
if not self.container_type:
self.container_type = "service"
pbs.logmsg(pbs.LOG_DEBUG, "Docker container found!")
return self.is_running
def add_resource_restriction(self, call):
n = self.j.Resource_List["ncpus"]
m = self.j.Resource_List["mem"]
if n:
#call += " -c %s" % str(n)
call += " -c 0"
if m:
call += " -m %s" % str(m)
return call
def add_env(self, call):
# v env se me obcas vyskytovala chybna data, odstranit
for i in self.e.env.keys():
if len(self.e.env[i]) == 0:
del self.e.env[i]
for i in self.e.env.keys():
if str(i) in self.ignored_env:
continue
call += " -e %s=%s" % (str(i), self.e.env[i])
return call
def check_job_file(self):
job_file = pbs.pbs_conf["PBS_HOME"] + "/mom_priv/jobs/" + str(self.jid) + ".SC"
if os.path.isfile(job_file):
self.job_file = job_file
def create_container(self):
if self.is_running:
pbs.logmsg(pbs.LOG_DEBUG, "Docker container %s already running!")
return
pbs.logmsg(pbs.LOG_DEBUG, "Docker image is: %s, creating..." % self.image)
call = "docker run -d -it --name %s" % str(self.jid)
#if self.container_type == "interactive":
# call += " -it"
call = self.add_resource_restriction(call)
pbs.logmsg(pbs.LOG_DEBUG, "Resource is: %s" % call)
call = self.add_env(call)
pbs.logmsg(pbs.LOG_DEBUG, "Env is: %s" % call)
if self.job_file:
call += " -v " + self.job_file + ":" + self.job_file
pbs.logmsg(pbs.LOG_DEBUG, "Job file is: %s" % self.job_file)
if self.entrypoint:
call += " --entrypoint " + self.entrypoint
user_name = self.j.Job_Owner.split("@")[0]
call += " -v /home/" + user_name + ":/home/" + user_name + ":ro"
#call += " --net=host"
call += " " + self.image
pbs.logmsg(pbs.LOG_DEBUG, "Call is: %s" % call)
os.popen(call)
def launch_job(self):
args = copy.deepcopy(self.e.argv)
pbs.logmsg(pbs.LOG_DEBUG, "args are %s" % self.e.progname)
self.e.progname = "/usr/bin/docker"
self.e.argv = []
self.e.argv.append("docker")
self.e.argv.append("exec")
if self.container_type == "interactive":
self.e.argv.append("-it")
self.e.argv.append(self.jid)
if self.container_type == "interactive":
self.e.argv.append("/bin/bash")
return
if self.container_type == "script":
self.e.argv.append("/bin/bash")
if not self.job_file:
pbs.logmsg(pbs.LOG_DEBUG, "Job file is missing")
return
self.e.argv.append("-c")
self.e.argv.append(self.job_file)
return
if self.container_type == "service":
self.e.argv.append("/bin/sh")
return
if self.container_type == "executable":
self.e.argv.append("/bin/bash")
self.e.argv.append("-c")
executable = ""
for arg in args:
executable += arg + " "
pbs.logmsg(pbs.LOG_DEBUG,"arg: %s" % arg)
self.e.argv.append(executable)
return
try:
e = pbs.event()
docker = Docker(e)
docker.get_docker_vars()
if not docker.image:
docker.accept()
docker.main()
docker.accept()
except SystemExit:
pass
except Exception as err:
e.reject("docker hook failed: %s " % str(err))