This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
forked from hpcugent/Lmod-UGent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSitePackage.lua
295 lines (225 loc) · 9.03 KB
/
SitePackage.lua
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
--------------------------------------------------------------------------
-- The SitePackage customization for UGent-HPC & VUB-HPC
-- Ward Poelmans <[email protected]>
-- Kenneth Hoste <[email protected]>
-- Ward Poelmans <[email protected]>
--------------------------------------------------------------------------
require("strict")
require("cmdfuncs")
require("utils")
require("lmod_system_execute")
require("parseVersion")
local Dbg = require("Dbg")
local dbg = Dbg:dbg()
local hook = require("Hook")
local FrameStk = require("FrameStk")
local function logmsg(logTbl)
-- Print to syslog with generic header
-- All the elements in the table logTbl are
-- added in order. Expect format:
-- logTbl[#logTbl+1] = {'log_key', 'log_value'}
local cluster = os.getenv("VSC_INSTITUTE_CLUSTER") or ""
local jobid = os.getenv("PBS_JOBID") or os.getenv("SLURM_JOB_ID") or ""
local user = os.getenv("USER")
local arch = os.getenv("VSC_ARCH_LOCAL") or ""
local msg = string.format("username=%s, cluster=%s, arch=%s, jobid=%s",
user, cluster, arch, jobid)
for _, val in ipairs(logTbl) do
msg = msg .. string.format(", %s=%q", val[1], val[2] or "")
end
-- Don't log any modules load by the monitoring
if user ~= "zabbix" then
lmod_system_execute("/bin/logger -t lmod -p user.notice -- " .. msg)
end
end
local function module_age(modT)
-- Calculate the age of a module, relative to the current toolchain generation
-- modT should have a entry 'fn' with the module path
-- returns age (in months) between module toolchain generation and current toolchain generation
local tcyear, tcsuffix = modT.fn:match("^/apps/brussel/.*/modules/(20[0-9][0-9])([ab])/all/")
if tcyear == nil or tcsuffix == nil then return 0 end
local suffixmonth = {a=1, b=7}
local tcmonth = suffixmonth[tcsuffix]
local tcstamp = os.time({year=tcyear, month=tcmonth, day=1})
-- 1 month is 2629743 seconds
return math.floor((os.time() - tcstamp) / (6 * 2629743))
end
local function load_hook(t)
-- Called every time a module is loaded
-- the arg t is a table:
-- t.modFullName: the module full name: (i.e: gcc/4.7.2)
-- t.fn: the file name: (i.e /apps/modulefiles/Core/gcc/4.7.2.lua)
-- unclear whether this is needed (and rtmclay agrees), but no harm in keeping it
if (mode() ~= "load") then return end
-- if userload is yes, the user request to load this module. Else
-- it is getting loaded as a dependency.
local frameStk = FrameStk:singleton()
-- yes means that it is a module directly request by the user
local userload = (frameStk:atTop()) and "yes" or "no"
local logTbl = {}
logTbl[#logTbl+1] = {"userload", userload}
logTbl[#logTbl+1] = {"module", t.modFullName}
logTbl[#logTbl+1] = {"fn", t.fn}
logmsg(logTbl)
-- inform/warn users about old modules (only directly loaded ones)
local age = module_age(t)
if frameStk:atTop() then
if age > 7 then
LmodWarning{msg="vub_very_old_module", fullName=t.modFullName}
elseif age > 5 then
LmodMessage{msg="vub_old_module", fullName=t.modFullName}
end
end
end
--[[
local function restore_hook(t)
-- This hook is called after a restore operation
-- the arg t is a table:
-- t.collection: the input name of the collection
-- t.name: the output name of the collection
-- t.fn: The file name: (i.e /apps/modulefiles/Core/gcc/4.7.2.lua)
dbg.start{"restore_hook"}
local cluster = os.getenv("VSC_INSTITUTE_CLUSTER")
local def_cluster = os.getenv("VSC_DEFAULT_CLUSTER_MODULE")
if (not cluster or not def_cluster) then return end
if (cluster == def_cluster) then return end
dbg.print{"Have: ", cluster, " Switch to ", def_cluster}
Swap("cluster/" .. cluster, "cluster/" .. def_cluster)
dbg.fini()
end
]]--
local function startup_hook(usrCmd)
-- This hook is called right after starting Lmod
-- usrCmd holds the currect active command
-- if you want access to all give arguments, use
-- masterTbl
dbg.start{"startup_hook"}
-- masterTbl has all info about the arguments passed to Lmod
local masterTbl = masterTbl()
dbg.print{"Received usrCmd: ", usrCmd, "\n"}
dbg.print{"masterTbl:", masterTbl, "\n"}
-- Log how Lmod was called
local fullargs = table.concat(masterTbl.pargs, " ") or ""
local logTbl = {}
logTbl[#logTbl+1] = {"cmd", usrCmd}
logTbl[#logTbl+1] = {"args", fullargs}
logmsg(logTbl)
dbg.fini()
end
local function msg_hook(mode, output)
-- mode is avail, list and spider
-- output is a table with the current output
dbg.start{"msg_hook"}
dbg.print{"Mode is ", mode, "\n"}
if mode == "avail" then
output[#output+1] = "\nIf you need software that is not listed, request it at [email protected]\n"
end
dbg.fini()
return output
end
-- This gets called on every message, warning and error
local function errwarnmsg_hook(kind, key, msg, t)
-- kind is either lmoderror, lmodwarning or lmodmessage
-- key is a unique key for the message (see messageT.lua)
-- msg is the actual message to display (as string)
-- t is a table with the keys used in msg
dbg.start{"errwarnmsg_hook"}
-- dbg.print{"kind: ", kind," key: ",key,"\n"}
-- dbg.print{"keys: ", t}
if key == "e_No_AutoSwap" then
-- Customize this error for EasyBuild modules
-- When the users gets this error, it mostly likely means
-- that they are trying to load modules belonging to different version of the same toolchain
--
-- find the module name causing the issue (almost always toolchain module)
local sname = t.sn
local frameStk = FrameStk:singleton()
local errmsg = {"A different version of the '" .. sname .. "' module is already loaded (see output of 'ml')."}
if not frameStk:empty() then
local framesn = frameStk:sn()
errmsg[#errmsg+1] = "You should load another '" .. framesn .. "' module that is compatible with " ..
"the currently loaded version of '" .. sname .. "'."
errmsg[#errmsg+1] = "Use 'ml spider " .. framesn .. "' to get an overview of the available versions."
end
errmsg[#errmsg+1] = "\n"
msg = table.concat(errmsg, "\n")
end
if kind == "lmoderror" or kind == "lmodwarning" then
msg = msg .. "\nIf you don't understand the warning or error, contact the helpdesk at [email protected]"
end
-- log any errors users get
if kind == "lmoderror" then
local logTbl = {}
logTbl[#logTbl+1] = {"error", key}
for tkey, tval in pairs(t) do
logTbl[#logTbl+1] = {tkey, tval}
end
logmsg(logTbl)
end
dbg.fini()
return msg
end
local function site_name_hook()
-- set the SiteName, it must be a valid
-- shell variable name.
return "VUB_HPC"
end
-- To combine EasyBuild with XALT
local function packagebasename(t)
-- Use the EBROOT variables in the module
-- as base dir for the reverse map
t.patDir = "^EBROOT.*"
end
local function visible_hook(modT)
-- modT is a table with: fullName, sn, fn and isVisible
-- The latter is a boolean to determine if a module is visible or not
if modT.fullName:find("cluster/%.") then
modT.isVisible = false
elseif modT.fullName:find("EESSI/") then
modT.isVisible = false
elseif modT.fullName:find("JupyterHub/") then
modT.isVisible = false
elseif module_age(modT) > 5 then
modT.isVisible = false
end
end
local function get_avail_memory()
-- If a limit is set, return the maximum allowed memory, else nil
-- look for the memory cgroup (if any)
local cgroup = nil
for line in io.lines("/proc/self/cgroup") do
cgroup = line:match("^[0-9]+:memory:(.*)$")
if cgroup then
break
end
end
if not cgroup then
return nil
end
-- Slurm tasks are only limited by the job step that launched it
cgroup = cgroup:gsub("/task_[%d]+$", "")
-- read the current maximum allowed memory usage (memory + swap)
local memory_file = io.open("/sys/fs/cgroup/memory/" .. cgroup .. "/memory.memsw.limit_in_bytes")
if not memory_file then
return nil
end
local memory_value = tonumber(memory_file:read())
memory_file:close()
-- if the value is 2^63-1 (rounded down to multiples of 4096), it's unlimited
if memory_value == 9223372036854771712 then
return nil
end
return memory_value
end
hook.register("load", load_hook)
-- Needs more testing before enabling:
-- hook.register("restore", restore_hook)
hook.register("startup", startup_hook)
hook.register("msgHook", msg_hook)
hook.register("SiteName", site_name_hook)
hook.register("packagebasename", packagebasename)
hook.register("errWarnMsgHook", errwarnmsg_hook)
hook.register("isVisibleHook", visible_hook)
sandbox_registration{
get_avail_memory = get_avail_memory,
}