-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhook_singularity.py
225 lines (158 loc) · 6.82 KB
/
hook_singularity.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
import pbs
import os
import copy
import re
import subprocess
class Singularity(object):
e = None
j = None
jid = None
image = None
is_running = False
job_file = None
container_type = None #interactive, service, script, executable
hook_events = {}
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_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, 'Singularity 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("singularity=[Tt]{1}rue", i):
newselect.append(i)
continue
newselect.append(i + ":singularity=true")
else:
newselect.append("singularity=true")
pbs.logmsg(pbs.LOG_DEBUG, f'Singularity old select: {str(self.j.Resource_List)}')
self.j.Resource_List["select"] = pbs.select("+".join(newselect))
pbs.logmsg(pbs.LOG_DEBUG, f'Singularity new select: {str(self.j.Resource_List)}')
def __execjob_launch_handler(self):
pbs.logmsg(pbs.LOG_DEBUG, 'Singularity execjob_launch handler start')
if not self.check_container_running():
self.create_container()
if self.j.interactive and "PBS_TASKNUM" in self.e.env and self.e.env["PBS_TASKNUM"] == "1":
self.container_type = "interactive"
pbs.logmsg(pbs.LOG_DEBUG, 'Singularity interactive approach.')
elif self.e.progname.find("singularity") != -1:
self.container_type = "service"
pbs.logmsg(pbs.LOG_DEBUG, 'Singularity service approach.')
elif self.e.progname.find("bash") != -1:
self.container_type = "script"
pbs.logmsg(pbs.LOG_DEBUG, 'Singularity script approach.')
else:
self.container_type = "executable"
pbs.logmsg(pbs.LOG_DEBUG, 'Singularity executable approach.')
self.check_job_file()
self.launch_job()
def __execjob_end_handler(self):
pbs.logmsg(pbs.LOG_DEBUG, 'Singularity execjob_end handler start')
if self.check_container_running():
self.destroy_container()
def main(self):
if self.hook_events[self.e.type]:
self.hook_events[self.e.type]()
def accept(self):
self.e.accept()
def call_as_user(self, call):
user_name = self.j.Job_Owner.split("@")[0]
krb_ticket=f'export KRB5CCNAME=/tmp/krb5cc_pbsjob_{self.jid}; cd;'
return ["su", user_name, "-c", f'{krb_ticket} {" ".join(call)}']
def get_singularity_vars(self):
self.image = None
vars = str(self.j.Variable_List).split(",")
for var in vars:
if var.startswith("PBS_SINGULARITY_IMAGE="):
name = var.split("=", 1)
self.image = name[1]
break
pbs.logmsg(pbs.LOG_DEBUG, f'Singularity image is: {self.image}')
def check_job_file(self):
job_file = pbs.pbs_conf["PBS_HOME"] + f'/mom_priv/jobs/{self.jid}.SC'
if os.path.isfile(job_file):
self.job_file = job_file
def check_container_running(self):
self.is_running = False
call = ['singularity', 'instance', 'list', '2>/dev/null']
call = self.call_as_user(call)
grep = ['grep', self.jid]
l = subprocess.Popen(call, stdout=subprocess.PIPE)
g = subprocess.Popen(grep, stdout=subprocess.PIPE, stdin=l.stdout)
stdout, stderr = g.communicate()
if len(stdout) > 0:
self.is_running = True
if not self.container_type:
self.container_type = "service"
pbs.logmsg(pbs.LOG_DEBUG, f'Singularity container {self.jid} has been found.')
return self.is_running
def create_container(self):
if self.is_running:
pbs.logmsg(pbs.LOG_DEBUG, f'Singularity container {self.jid} already running!')
return
pbs.logmsg(pbs.LOG_DEBUG, f'Singularity image is: {self.image}, creating...')
call = ["singularity", "instance", "start"]
call += ['--bind', '/var/spool/pbs']
call += ['--bind', '/etc/pbs.conf']
call.append(self.image)
call.append(self.jid)
pbs.logmsg(pbs.LOG_DEBUG, f'Singularity create call is: {str(call)}')
result = subprocess.run(self.call_as_user(call), capture_output = True, text = True)
pbs.logmsg(pbs.LOG_DEBUG, f'Singularity call stderr is: {result.stderr}')
def destroy_container(self):
call = ["singularity", "instance", "stop"]
call.append(self.jid)
pbs.logmsg(pbs.LOG_DEBUG, f'Singularity stop call is: {str(call)}')
result = subprocess.run(self.call_as_user(call), capture_output = True, text = True)
pbs.logmsg(pbs.LOG_DEBUG, f'Singularity call stderr is: {result.stderr}')
def launch_job(self):
args = copy.deepcopy(self.e.argv)
self.e.progname = "/usr/bin/singularity"
self.e.argv = []
self.e.argv.append("singularity")
if self.container_type == "interactive":
self.e.argv.append("shell")
self.e.argv.append(f'instance://{self.jid}')
return
if self.container_type == "script":
self.e.argv.append("shell")
self.e.argv.append(f'instance://{self.jid}')
self.e.argv.append("/bin/bash")
if not self.job_file:
pbs.logmsg(pbs.LOG_DEBUG, f'Job file {self.job_file} is missing.')
return
self.e.argv.append(self.job_file)
return
if self.container_type == "service":
self.e.argv.append("shell")
self.e.argv.append(f'instance://{self.jid}')
return
if self.container_type == "executable":
self.e.argv.append("exec")
self.e.argv.append(f'instance://{self.jid}')
self.e.argv.append("/bin/bash")
self.e.argv.append("-c")
executable = ""
for arg in args:
executable += arg + " "
self.e.argv.append(executable)
return
try:
e = pbs.event()
singularity = Singularity(e)
singularity.get_singularity_vars()
if not singularity.image:
singularity.accept()
singularity.main()
singularity.accept()
except SystemExit:
pass
except Exception as err:
e.reject(f'Singularity hook failed: {str(err)}')