-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfff_cluster.py
executable file
·313 lines (257 loc) · 8.02 KB
/
fff_cluster.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python3
# this should later become a reader for a configuration file in /etc/
import socket
import subprocess
import os
from threading import Timer
import json
clusters = {
"production_c2a06": [
"dqmrubu-c2a06-01-01.cms",
"dqmfu-c2b03-45-01.cms",
"dqmfu-c2b04-45-01.cms",
],
"playback_c2a06": [
"dqmrubu-c2a06-03-01.cms",
"dqmfu-c2b01-45-01.cms",
"dqmfu-c2b02-45-01.cms",
],
"lookarea_c2a06": ["dqmrubu-c2a06-05-01.cms"],
}
# Make sure the keys of this dictionary start with "production", "playback" or "lookarea"
assert all(
cluster_name.split("_")[0] in ["production", "playback", "lookarea"]
for cluster_name in clusters
)
def popen_timeout(cmd: str, seconds: int = 10):
kill = lambda process: process.kill()
p = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
timer = Timer(seconds, kill, [p])
answer, stderr = " ... ", " ... "
try:
timer.start()
answer, stderr = p.communicate()
except Exception as error_log:
answer = error_log
if isinstance(answer, bytes):
answer = answer.decode("utf-8", errors="ignore")
if isinstance(stderr, bytes):
stderr = stderr.decode("utf-8", errors="ignore")
if p.returncode:
answer = stderr
timer.cancel()
return answer
def get_rpm_version(host, soft_path):
if not host:
return "host argument not defined"
if not soft_path:
return "soft_path argument not defined"
return popen_timeout(["ssh " + host + ' "rpm -qf ' + soft_path + '"'], 5)
def get_rpm_version_all(soft_path: str):
answer = {}
for key, lst in clusters.items():
subanswer = {}
for host in lst:
version = get_rpm_version(host, soft_path)
subanswer[host] = version
answer[key] = subanswer
return answer
def get_cmssw_info(cmssw_path: str) -> str:
if not cmssw_path:
return "cmssw_path argument not defined"
if cmssw_path[-1] != "/":
cmssw_path += "/"
# 1. read CMSSW logs
versions_raw = popen_timeout(
[
'grep "Selected release:" --exclude-dir="*" --include=*.log '
+ cmssw_path
+ "*"
],
15,
)
if not "Selected release:" in versions_raw:
return versions_raw
answer = versions_raw.split("Selected release: ")[-1]
# 2. get PRs
prs_raw = popen_timeout(["find " + cmssw_path + ' -type f -name "merge*log"'], 15)
answer += "PRs :"
# 3. get PRs merge status
for fname in prs_raw.split("\n"):
try:
pr_id = os.path.basename(fname).split(".")[1]
status = popen_timeout(['grep "Merge successful" ' + fname], 15)
answer += "\n " + pr_id
answer += " ok" if status else " "
except:
continue
# 4. get GTs
gts_raw = popen_timeout(
[
'grep -r "GlobalTag.globaltag = " '
+ cmssw_path
+ "src/DQM/Integration/python/config/*"
],
15,
)
if not "GlobalTag.globaltag" in gts_raw:
return answer
answer += "\nGTs:\n"
for line in gts_raw.split("\n"):
if "autoCond" in line:
continue
answer += line + "\n"
return answer
def get_dqm_clients(host: str, cmssw_path: str, clients_path: str) -> list:
if not host:
return "host argument not defined"
if not cmssw_path:
return "cmssw_path argument not defined"
available = popen_timeout(
["ssh " + host + ' "find ' + cmssw_path + ' -type f -name *_cfg.py"'], 15
)
activated = popen_timeout(
["ssh " + host + ' "find ' + clients_path + ' -type l"'], 15
)
available = [os.path.basename(a) for a in available.split("\n") if a]
activated = [os.path.basename(a) for a in activated.split("\n") if a]
answer = [[a, a in activated] for a in available]
return answer
def change_dqm_client(
host: str, cmssw_path: str, clients_path: str, client: str, state: str
) -> str:
answer = None
if state == "0":
answer = popen_timeout(
[
"ssh "
+ host
+ ' "sudo find '
+ clients_path
+ " -type l -name "
+ client
+ ' -delete"'
],
15,
)
else:
inp = os.path.join(cmssw_path, client)
answer = popen_timeout(
["ssh " + host + ' "cd ' + clients_path + "/idle; sudo ln -s " + inp + '"'],
15,
)
if not answer:
return "Ok"
return answer
def get_simulator_config(opts: dict, this_host: str, simulator_host: str) -> str:
if not simulator_host:
return "host argument not defined"
path = opts["simulator.conf"]
cfg = None
if this_host == simulator_host:
cfg = popen_timeout(["cat " + path], 5)
else:
cfg = popen_timeout(["ssh " + simulator_host + ' "cat ' + path + '"'], 5)
return cfg
def update_config(cfg: dict, key: str, value) -> dict:
if not key:
return cfg
if not key in cfg:
return cfg
if not value:
return cfg
cfg[key] = value
return cfg
def write_config(opts: dict, cfg: dict): # only locally
path = "/tmp/" + os.path.basename(opts["simulator.conf"])
f = open(path, "w")
json.dump(cfg, f, sort_keys=True, indent=2)
f.close()
answer = popen_timeout(["sudo cp " + path + " " + opts["simulator.conf"]], 5)
return answer
def get_simulator_runs(opts: dict, this_host: str, simulator_host: str) -> list:
if not simulator_host:
return []
cfg_json = get_simulator_config(opts, this_host, simulator_host)
cfg = json.loads(cfg_json)
# Use normpath to trim trailing slashes which might confuse dirname
path = os.path.dirname(os.path.normpath(cfg["source"]))
runs_raw = None
if this_host == simulator_host:
runs_raw = popen_timeout(["ls -1d " + path + "/run*"], 5)
else:
runs_raw = popen_timeout(
["ssh " + simulator_host + ' "ls -1d ' + path + '/run*"'], 5
)
runs = []
for run in runs_raw.split("\n"):
runs += [os.path.basename(run)]
return runs
def restart_hltd(host: str) -> str:
if not host:
return "host argument not defined"
answer = popen_timeout(
[
"ssh "
+ host
+ ' "sudo -i /sbin/service hltd stop; sudo -i /sbin/service hltd start"'
],
15,
)
if not answer:
return "Ok"
return answer
def restart_fff(host: str) -> str:
if not host:
return "host argument not defined"
answer = popen_timeout(
["ssh " + host + ' "sudo systemctl restart fff_dqmtools.service"'], 15
)
if not answer:
return "Ok"
return answer
def get_txt_file(host: str, path: str, timeout=30):
if not host:
return "host argument not defined"
if not path:
return "path argument not defined"
return popen_timeout(["ssh " + host + ' "cat ' + path + '"'], timeout)
def get_host() -> str:
host = socket.gethostname()
host = host.lower()
return host
def get_node() -> dict:
host = get_host()
current = {
"_all": clusters,
}
for key, lst in clusters.items():
if host in lst:
current["node"] = host
current["nodes"] = lst
current["label"] = key
break
return current
def host_wrapper(allow: list):
"""This is function decorator.
Runs a function of the given hosts,
just returns on others.
"""
host = get_host()
def run_wrapper(f):
return f
def noop_wrapper(f):
def noop(*args, **kwargs):
name = kwargs["name"]
log = kwargs["logger"]
log.info("The %s applet is not allowed to run on %s, disabling", name, host)
return None
return noop
if host in allow:
return run_wrapper
else:
return noop_wrapper
if __name__ == "__main__":
print(get_node())