-
Notifications
You must be signed in to change notification settings - Fork 0
/
hsadmin.py
executable file
·366 lines (289 loc) · 12.6 KB
/
hsadmin.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python
from __future__ import print_function
import argparse
import getpass
import os
import select
import socket
import subprocess
import sys
import tempfile
from multiprocessing.pool import ThreadPool
from fabric2 import ThreadingGroup
from fabric2.exceptions import GroupException
# valid administration action keywords
ACTIONS = ("deploy", "install_crontab", "restart", "status", "stop",
"remove_crontab")
class BaseSystem(object):
HS_INSTALL_PATH = "/mnt/data/pdaqlocal/HsInterface"
HS_SOURCE_PATH = "/home/pdaq/HsInterface/current"
DEFAULT_BWLIMIT = 1000
DEFAULT_LOG_FORMAT = "%i%n%L"
RSYNC_EXCLUDE_LIST = (".svn", ".hg", ".hgignore", "*.log", "*.pyc", "*.swp")
CRONTAB_TEXT = """
###HitSpool Jobs -- Please contact daq-dev@icecube.wisc.edu for questions
HitSpoolPath=/mnt/data/pdaqlocal/HsInterface/current
15 * * * * source \$HOME/env/bin/activate && python \$HitSpoolPath/HsWatcher.py
@reboot source \$HOME/env/bin/activate && python \$HitSpoolPath/HsWatcher.py
"""
def __init__(self, targets=None):
self.__group = ThreadingGroup(*targets)
def __create_exclude_file(self):
"""
Create a file containing patterns of files/directories
which `rsync` should ignore
"""
tmp_fd, tmpname = tempfile.mkstemp(text=True)
text = "\n".join(self.RSYNC_EXCLUDE_LIST)
os.write(tmp_fd, text.encode())
os.close(tmp_fd)
return tmpname
def __get_hs_name(self, hostname):
if hostname == "2ndbuild":
return "HsSender"
if hostname == "expcont":
return "HsPublisher"
if "hub" in hostname or hostname == "scube" or hostname == "dmice":
return "HsWorker"
raise Exception("Unknown HitSpool host \"%s\"" % hostname)
def __report_failures(self, results, action):
if not results.failed:
return 0
fail_list = None
for conn, rslt in results.failed.items():
if hasattr(rslt, "stdout"):
print("%s ERROR from %s: %s" %
(action, conn.host, rslt.stdout.rstrip()),
file=sys.stderr)
else:
# handle partial results wrapped in a GroupException
if fail_list is None:
fail_list = []
fail_list.append(conn.host)
if fail_list is not None:
print("%s FAILED on %s" % (action, ", ".join(fail_list)),
file=sys.stderr)
return len(results.failed)
def __rsync_to_remote_host(self, cmdfmt, hostname=None):
"""
Copy the current HitSpool release to a single remote host
"""
# hack around the missing 'ThreadPool.startmap()'
if hostname is None and \
(isinstance(cmdfmt, tuple) or isinstance(cmdfmt, list)):
hostname = cmdfmt[1]
cmdfmt = cmdfmt[0]
return self.__run_command(hostname, cmdfmt % hostname)
def __run_command(self, hostname, cmd):
"""
Run a local command, print any output,
and return the process's return code (non-zero indicates an error)
"""
proc = subprocess.Popen(cmd, bufsize=256, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
num_err = 0
while True:
reads = [proc.stdout.fileno(), proc.stderr.fileno()]
try:
ret = select.select(reads, [], [])
except select.error:
# ignore a single interrupt
if num_err > 0:
break
num_err += 1
continue
for fin in ret[0]:
if fin == proc.stderr.fileno():
line = proc.stderr.readline().decode("utf-8")
if line != "":
print("[%s] %s" % (hostname, line), file=sys.stderr)
continue
if fin != proc.stdout.fileno():
# ignore unknown file descriptors
continue
line = proc.stderr.readline().decode("utf-8")
if line != "":
print("[%s] %s" % (hostname, line))
if proc.poll() is not None:
break
finally:
proc.stdout.close()
proc.stderr.close()
return proc.wait()
def modify_crontab(self, delete=False, debug=False, quiet=False):
delete_cmd = "egrep -v 'HitSpool|HSiface|HsWatcher|^\$'"
if delete:
verb = "delete"
past_verb = "deleted"
cmd = "crontab -l | %s | crontab -" % (delete_cmd, )
else:
verb = "install"
past_verb = "installed"
cmd = "(crontab -l | %s; echo \"%s\") | crontab -" % \
(delete_cmd, self.CRONTAB_TEXT.strip(), )
try:
results = self.__group.run(cmd, hide=quiet)
except GroupException as gex:
for key, val in gex.result.items():
if isinstance(val, Exception):
print("Failed to %s crontab on %s\n%s" %
(verb, key.host, val.result.tail("stderr")))
results = gex.result
num_failed = self.__report_failures(results,
"%sCrontab" % verb.capitalize())
if not quiet:
print("%s crontab on %s hosts" %
(past_verb.capitalize(), len(self.__group) - num_failed))
def make_directories(self, debug=False, quiet=False):
"""
Create HitSpool directories on all remote systems
"""
for subdir in "current", "logs":
path = os.path.join(self.HS_INSTALL_PATH, subdir)
results = self.__group.run("mkdir -p %s" % path, hide=True)
num_failed = self.__report_failures(results, "MakeDirs")
if not quiet:
print("Created '%s' on %s hosts" %
(path, len(self.__group) - num_failed))
def report_status(self):
results = self.__group.run("ps axwww", hide=True)
num_failed = self.__report_failures(results, "ProcessList")
active = []
inactive = []
for conn, rslt in sorted(results.items(), key=lambda x: x[0].host):
hostname = conn.host
outlines = rslt.stdout
target = self.__get_hs_name(hostname)
if target in outlines:
active.append(hostname)
else:
inactive.append(hostname)
if len(self.__group) == len(active):
print("All hosts are active")
elif len(self.__group) == len(inactive):
print("All hosts are INACTIVE")
else:
if len(active) > 0:
print("Active: %s" % ", ".join(active))
if len(inactive) > 0:
print("Inactive: %s" % ", ".join(inactive))
def rsync_install(self, bwlimit=None, no_relative=False, debug=False,
quiet=False):
if bwlimit is None:
bwlimit = self.DEFAULT_BWLIMIT
exclude_file = self.__create_exclude_file()
try:
rmtpath = os.path.join(self.HS_INSTALL_PATH, "current")
cmdfmt = "nice rsync -a --bwlimit=%s%s --exclude-from=%s" \
" %s/." \
" \"%%s:%s\"" % \
(bwlimit, " --no-relative" if no_relative else "",
exclude_file, self.HS_SOURCE_PATH, rmtpath)
# build argument lists for all hosts
params = [(cmdfmt, entry.host) for entry in self.__group]
# run rsync on all remote hosts
pool = ThreadPool()
results = pool.map(self.__rsync_to_remote_host, params)
synced = None
for idx, rtncode in enumerate(results):
hostname = params[idx][1]
if rtncode != 0:
print("WARNING: %s exited with non-zero return code %s" %
(params[idx][1], rtncode), file=sys.stderr)
else:
if synced is None:
synced = []
synced.append(hostname)
if not quiet and synced is not None:
print("Deployed to %d hosts" % len(synced))
finally:
os.unlink(exclude_file)
def run_watcher(self, stop_process=False, debug=False, quiet=False):
watcher = os.path.join(self.HS_INSTALL_PATH, "current", "HsWatcher.py")
stop_arg = " -k" if stop_process else ""
try:
results = self.__group.run("python %s%s" % (watcher, stop_arg),
hide=True)
except GroupException as gex:
for key, val in gex.result.items():
if isinstance(val, Exception):
print("Failed to %s %s\n%s" %
("stop" if stop_process else "start", key.host,
val.result.tail("stderr")))
results = gex.result
num_failed = self.__report_failures(results, "Stop" if stop_process
else "Start")
if not quiet and num_failed < len(self.__group):
verb = "Stopped" if stop_process else "Started"
print("%s HitSpool daemon on %s hosts" %
(verb, len(self.__group) - num_failed))
return num_failed == 0
class SouthPoleSystem(BaseSystem):
def __init__(self, targets=None):
if targets is None or len(targets) == 0:
targets = ["expcont", "2ndbuild"] + \
["ichub%02d" % i for i in range(1, 87)] + \
["ithub%02d" % i for i in range(1, 12)] + \
["dmice"]
super(SouthPoleSystem, self).__init__(targets=targets)
class TestSystem(BaseSystem):
def __init__(self, targets=None):
if targets is None or len(targets) == 0:
targets = ["expcont", "2ndbuild", "ichub21", "ichub29", "scube", "dmice"]
super(TestSystem, self).__init__(targets=targets)
def add_arguments(parser, valid_actions):
"Add command-line arguments"
parser.add_argument("-q", "--quiet", dest="quiet",
action="store_true", default=False,
help="Do not print details")
parser.add_argument("-x", "--debug", dest="debug",
action="store_true", default=False,
help="Print debugging messages")
parser.add_argument("-t", "--target", dest="target", action='append',
help="Perform action only on specified target machine(s)")
parser.add_argument("--no-stop", dest="no_stop",
action="store_true", default=False,
help="Do not stop first (e.g. for fresh deploy)")
parser.add_argument("action", choices=valid_actions,
help="Action to perform on host(s)")
def main():
parser = argparse.ArgumentParser()
add_arguments(parser, ACTIONS)
args = parser.parse_args()
hostname = socket.gethostname()
if ".icecube." not in hostname:
raise SystemExit("Please run this on either SPS or SPTS")
elif "access" not in hostname:
raise SystemExit("Please run this from 'access'")
username = getpass.getuser()
if username != "pdaq":
raise SystemExit("Please run this from the 'pdaq' account")
if "wisc.edu" in hostname:
system = TestSystem(targets=args.target)
elif "usap.gov" in hostname:
system = SouthPoleSystem(targets=args.target)
else:
raise SystemExit("Please run this on either SPS or SPTS")
if args.action == "status":
system.report_status()
return
if args.action == "install_crontab":
system.modify_crontab(debug=args.debug, quiet=args.quiet)
return
if args.action == "remove_crontab":
system.modify_crontab(delete=True, debug=args.debug, quiet=args.quiet)
return
if not args.no_stop:
if args.action == "deploy" or args.action == "restart" or \
args.action == "stop":
if not system.run_watcher(stop_process=True, debug=args.debug,
quiet=args.quiet):
raise SystemExit("ABORTING without %s" % args.action)
if args.action == "deploy":
system.make_directories(debug=args.debug, quiet=args.quiet)
system.rsync_install(debug=args.debug, quiet=args.quiet)
if args.action == "deploy" or args.action == "restart":
system.run_watcher(debug=args.debug, quiet=args.quiet)
if __name__ == "__main__":
main()