forked from hansemro/hyakvnc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyakvnc.py
executable file
·1319 lines (1208 loc) · 51.4 KB
/
hyakvnc.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
# Copyright (c) 2023 Hansem Ro <hansem7@uw.edu> <hansemro@outlook.com>
# default node allocation settings
RES_TIME = 4 # hours
TIMEOUT = 120 # seconds
CPUS = 8
MEM = "16G"
PARTITION = "compute-hugemem"
ACCOUNT = "ece"
JOB_NAME = "ece_vnc"
VERSION = 1.2
# Quick start guide
#
# 1. To start VNC session for 5 hours on node with 16 cores and 32GB of memory,
# run the following
# $ ./hyakvnc.py -t 5 -c 16 --mem 32G
# ...
# =====================
# Run the following in a new terminal window:
# ssh -N -f -L 5901:127.0.0.1:5901 hansem7@klone.hyak.uw.edu
# then connect to VNC session at localhost:5901
# =====================
#
# This should print a command to setup port forwarding. Copy it for the
# following step.
#
# 2. Set up port forward between your computer and HYAK login node.
# On your machine, in a new terminal window, run the the copied command.
#
# In this example, run
# $ ssh -N -f -L 5901:127.0.0.1:5901 hansem7@klone.hyak.uw.edu
#
# 3. Connect to VNC session at instructed address (in example: localhost:5901)
#
# 4. To close VNC session, run the following
# $ ./hyakvnc.py --kill-all
#
# 5. Kill port forward process from step 2
# Usage: ./hyakvnc.py [-h/--help] [OPTIONS]
#
# Options:
# -h, --help : print help message and exit
#
# -v, --version : print program version and exit
#
# -d, --debug : [default: disabled] show debug messages and enable debug
# logging at ~/hyakvnc.log
#
# --skip-check : [default: single VNC job] allow multiple VNC jobs/sessions
#
# -p <part>, --partition <part> : [default: compute-hugemem] Slurm partition
#
# -A <account>, --account <account> : [default: ece] Slurm account
#
# -J <job_name> : [default: ece_vnc] Slurm job name
#
# --timeout : [default: 120] Slurm node allocation and VNC startup timeout length (in seconds)
#
# -p <port>, --port <port> : [default: automatically found] override
# User<->LoginNode port
#
# -t <time>, --time <time> : [default: 4] VNC node reservation length in hours
#
# -c <ncpus>, --cpus <ncpus> : [default: 8] VNC node CPU count
#
# --mem <size[units]> : [default: 16G] VNC node memory
# Valid units: K, M, G, T
#
# --status : print details of active VNC jobs and exit. Details include the
# following for each active job:
# - Job ID
# - Subnode name
# - VNC session status
# - VNC display number
# - Subnode/VNC port
# - User/LoginNode port
# - Time left
# - SSH port forward command
#
# --container <sif> : [default: /gscratch/ece/xfce_singularity/xfce.sif] Use
# specified XFCE+VNC Apptainer/Singularity container
#
# --repair : Repair all missing/broken LoginNode<->SubNode port forwards, and then exit
#
# --kill <job_id> : kill specific job
#
# --kill-all : kill all VNC jobs with targeted Slurm job name
#
# --set-passwd : prompt to set VNC password and exit
#
# Dependencies:
# - Python 3.6 or newer
# - Apptainer 1.0 or newer
# - Slurm
# - netstat utility
# - XFCE container:
# - xfce4
# - tigervnc with vncserver
import argparse # for argument handling
import logging # for debug logging
import time # for sleep
import signal # for signal handling
import glob
import pwd
import os # for path, file/dir checking, hostname
import subprocess # for running shell commands
import re # for regex
# tasks:
# - [x] user arguments to control hours
# - [x] user arguments to close active vnc sessions and vnc slurm jobs
# - [x] user arguments to override automatic port forward (with conflict checking)
# - [x] reserve node with slurm
# - [x] start vnc session (also check for active vnc sessions)
# - [x] identify used ports
# - [x] map node<->login port to unused user<->login port
# - [x] map port forward and job ID
# - [x] port forward between login<->subnode
# - [x] print instructions to user to setup user<->login port forwarding
# - [x] print time left for node with --status argument
# - [ ] Set vnc settings via file (~/.config/hyakvnc.conf)
# - [ ] Write unit tests for functions
# - [x] Remove psutil dependency
# - [x] Handle SIGINT and SIGTSTP signals
# - [ ] user argument to reset specific VNC job
# - [x] Specify singularity container to run
# - [x] Document dependencies of external tools: slurm, apptainer/singularity, xfce container, tigervnc
# - [ ] Use pyslurm to interface with Slurm: https://github.com/PySlurm/pyslurm
# - [x] Delete ~/.ssh/known_hosts before ssh'ing into subnode
# - [ ] Replace netstat with ss
# - [ ] Create and use apptainer instances. Then share instructions to enter instance.
# - [ ] Add user argument to restart container instance
# - [x] Delete /tmp/.X11-unix/X<DISPLAY_NUMBER> if display number is not used on subnode
# Info: This can cause issues for vncserver (tigervnc)
# - [x] Delete all owned socket files in /tmp/.ICE-unix/
# - [ ] Add singularity to $PATH if missing.
# - [x] Remove stale VNC processes
# - [ ] Check if container meets dependencies
# - [ ] Add argument to specify xstartup
# - [x] Migrate Singularity to Apptainer
# - [x] Repair LoginNode<->SubNode port forwards when login node goes down.
# Base VNC port cannot be changed due to vncserver not having a stable argument
# interface:
BASE_VNC_PORT = 5900
# List of Klone login node hostnames
LOGIN_NODE_LIST = ["klone-login01", "klone1.hyak.uw.edu", "klone2.hyak.uw.edu"]
# Full path to Apptainer binary (formerly Singularity)
APPTAINER_BIN = "/sw/apptainer/default/bin/apptainer"
# Singularity container with XFCE + vncserver (tigervnc)
XFCE_CONTAINER = "/gscratch/ece/common_containers/ece_containers/centos7_singularity/centos7.sif"
# Script used to start desktop environment (XFCE)
XSTARTUP_FILEPATH = "/gscratch/ece/common_containers/ece_containers/xfce4_config/xstartup"
# Checked to see if klone is authorized for intracluster access
AUTH_KEYS_FILEPATH = os.path.expanduser("~/.ssh/authorized_keys")
# Apptainer bindpaths can be overwritten if $APPTAINER_BINDPATH is defined.
# Bindpaths are used to mount storage paths to containerized environment.
APPTAINER_BINDPATH = os.getenv("APPTAINER_BINDPATH")
if APPTAINER_BINDPATH is None:
APPTAINER_BINDPATH = os.getenv("SINGULARITY_BINDPATH")
if APPTAINER_BINDPATH is None:
APPTAINER_BINDPATH = "/tmp:/tmp,$HOME,$PWD,/gscratch,/opt:/opt,/:/hyak_root"
class Node:
"""
The Node class has the following initial data: bool: debug, string: name,
string: sing_exec.
debug: Print and log debug messages if True.
name: Shortened hostname of node.
sing_exec: Added before command to execute inside a singularity container
"""
def __init__(self, name, debug=False, sing_container=XFCE_CONTAINER):
self.debug = debug
self.name = name
self.sing_exec = f"{APPTAINER_BIN} exec -B {APPTAINER_BINDPATH} {sing_container}"
self.sing_container = os.path.abspath(sing_container)
assert os.path.exists(self.sing_container)
class SubNode(Node):
"""
The SubNode class specifies a node requested via Slurm (also known as work
or interactive node). SubNode class is initialized with the following:
bool: debug, string: name, string: sing_exec, string: hostname, int: job_id.
SubNode class with active VNC session may contain vnc_display_number and
vnc_port.
debug: Print and log debug messages if True.
name: Shortened subnode hostname (e.g. n3000) described inside `/etc/hosts`.
hostname: Full subnode hostname (e.g. n3000.hyak.local).
job_id: Slurm Job ID that allocated the node.
vnc_display_number: X display number used for VNC session.
vnc_port: vnc_display_number + BASE_VNC_PORT.
sing_exec: Added before command to execute inside singularity container.
"""
def __init__(self, name, job_id, debug=False, sing_container=XFCE_CONTAINER):
assert os.path.exists(AUTH_KEYS_FILEPATH)
super().__init__(name, debug, sing_container)
self.hostname = f"{name}.hyak.local"
self.job_id = job_id
self.vnc_display_number = None
self.vnc_port = None
def print_props(self):
"""
Print properties of SubNode object.
"""
print("SubNode properties:")
props = vars(self)
for item in props:
msg = f"{item} : {props[item]}"
print(f"\t{msg}")
if self.debug:
logging.debug(msg)
def run_command(self, command:str, timeout=None):
"""
Run command (with arguments) on subnode
Args:
command:str : command and its arguments to run on subnode
timeout : [Default: None] timeout length in seconds
Returns ssh subprocess with stderr->stdout and stdout->PIPE
"""
assert self.name is not None
cmd = ["ssh", self.hostname, command]
if timeout is not None:
cmd.insert(0, "timeout")
cmd.insert(1, str(timeout))
if self.debug:
msg = f"Running on {self.name}: {cmd}"
print(msg)
logging.info(msg)
return subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
def list_pids(self):
"""
Returns list of PIDs of current job_id using
`scontrol listpids <job_id>`.
"""
ret = list()
cmd = f"scontrol listpids {self.job_id}"
proc = self.run_command(cmd)
while proc.poll() is None:
line = str(proc.stdout.readline(), "utf-8").strip()
if self.debug:
msg = f"list_pids: {line}"
logging.debug(msg)
if "PID" in line:
pass
elif re.match("[0-9]+", line):
pid = int(line.split(' ', 1)[0])
ret.append(pid)
return ret
def check_pid(self, pid:int):
"""
Returns True if given pid is active in job_id and False otherwise.
"""
return pid in self.list_pids()
def get_vnc_pid(self, hostname, display_number):
"""
Returns pid from file <hostname>:<display_number>.pid or None if file
does not exist.
"""
if hostname is None:
hostname = self.hostname
if display_number is None:
display_number = self.vnc_display_number
assert(hostname is not None)
if display_number is not None:
filepaths = glob.glob(os.path.expanduser(f"~/.vnc/{hostname}*:{display_number}.pid"))
for path in filepaths:
try:
f = open(path, "r")
except:
pass
return int(f.readline())
return None
def check_vnc(self):
"""
Returns True if VNC session is active and False otherwise.
"""
assert(self.name is not None)
assert(self.job_id is not None)
pid = self.get_vnc_pid(self.hostname, self.vnc_display_number)
if pid is None:
pid = self.get_vnc_pid(self.name, self.vnc_display_number)
if pid is None:
return False
if self.debug:
logging.debug(f"check_vnc: Checking VNC PID {pid}")
return self.check_pid(pid)
def start_vnc(self, display_number=None, timeout=20):
"""
Starts VNC session
Args:
display_number: Attempt to acquire specified display number if set.
If None, then let vncserver determine display number.
timeout: timeout length in seconds
Returns True if VNC session was started successfully and False otherwise
"""
target = ""
if display_number is not None:
target = f":{display_number}"
vnc_cmd = f"{self.sing_exec} vncserver {target} -xstartup {XSTARTUP_FILEPATH} &"
if not self.debug:
print("Starting VNC server...", end="", flush=True)
proc = self.run_command(vnc_cmd, timeout=timeout)
# get display number and port number
while proc.poll() is None:
line = str(proc.stdout.readline(), 'utf-8').strip()
if line is not None:
if self.debug:
logging.debug(f"start_vnc: {line}")
if "desktop" in line:
# match against the following pattern:
#New 'n3000.hyak.local:1 (hansem7)' desktop at :1 on machine n3000.hyak.local
#New 'n3000.hyak.local:6 (hansem7)' desktop is n3000.hyak.local:6
pattern = re.compile("""
(New\s)
(\'([^:]+:(?P<display_number>[0-9]+))\s([^\s]+)\s)
""", re.VERBOSE)
match = re.match(pattern, line)
assert match is not None
self.vnc_display_number = int(match.group("display_number"))
self.vnc_port = self.vnc_display_number + BASE_VNC_PORT
if self.debug:
logging.debug(f"Obtained display number: {self.vnc_display_number}")
logging.debug(f"Obtained VNC port: {self.vnc_port}")
else:
print('\x1b[1;32m' + "Success" + '\x1b[0m')
return True
if self.debug:
logging.error("Failed to start vnc session (Timeout/?)")
else:
print('\x1b[1;31m' + "Timed out" + '\x1b[0m')
return False
def list_vnc(self):
"""
Returns a list of active and stale vnc sessions on subnode.
"""
active = list()
stale = list()
cmd = f"{self.sing_exec} vncserver -list"
#TigerVNC server sessions:
#
#X DISPLAY # PROCESS ID
#:1 7280 (stale)
#:12 29 (stale)
#:2 83704 (stale)
#:20 30
#:3 84266 (stale)
#:4 90576 (stale)
pattern = re.compile(r":(?P<display_number>\d+)\s+\d+(?P<stale>\s\(stale\))?")
proc = self.run_command(cmd)
while proc.poll() is None:
line = str(proc.stdout.readline(), "utf-8").strip()
match = re.search(pattern, line)
if match is not None:
display_number = match.group("display_number")
if match.group("stale") is not None:
stale.append(display_number)
else:
active.append(display_number)
return (active,stale)
def __remove_files__(self, filepaths:list):
"""
Removes files on subnode and returns True on success and False otherwise.
Arg:
filepaths: list of file paths to remove. Each entry must be a file
and not a directory.
"""
cmd = f"rm -f"
for path in filepaths:
cmd = f"{cmd} {path}"
cmd = f"{cmd} &> /dev/null"
if self.debug:
logging.debug(f"Calling ssh {self.hostname} {cmd}")
return subprocess.call(['ssh', self.hostname, cmd]) == 0
def __listdir__(self, dirpath):
"""
Returns a list of contents inside directory.
"""
ret = list()
cmd = f"test -d {dirpath} && ls -al {dirpath} | tail -n+4"
pattern = re.compile("""
([^\s]+\s+){8}
(?P<name>.*)
""", re.VERBOSE)
proc = self.run_command(cmd)
while proc.poll() is None:
line = str(proc.stdout.readline(), "utf-8").strip()
match = re.match(pattern, line)
if match is not None:
name = match.group("name")
ret.append(name)
return ret
def kill_vnc(self, display_number=None):
"""
Kill specified VNC session with given display number or all VNC sessions.
"""
if display_number is None:
active,stale = self.list_vnc()
for entry in active:
if self.debug:
logging.debug(f"kill_vnc: active entry: {entry}")
self.kill_vnc(entry)
for entry in stale:
if self.debug:
logging.debug(f"kill_vnc: stale entry: {entry}")
self.kill_vnc(entry)
# Remove all remaining pid files
pid_list = glob.glob(os.path.expanduser("~/.vnc/*.pid"))
for pid_file in pid_list:
try:
os.remove(pid_file)
except:
pass
# Remove all owned socket files on subnode
# Note: subnode maintains its own /tmp/ directory
x11_unix = "/tmp/.X11-unix"
ice_unix = "/tmp/.ICE-unix"
file_targets = list()
for entry in self.__listdir__(x11_unix):
file_targets.append(f"{x11_unix}/{entry}")
for entry in self.__listdir__(ice_unix):
file_targets.append(f"{x11_unix}/{entry}")
self.__remove_files__(file_targets)
else:
assert display_number is not None
target = f":{display_number}"
if self.debug:
print(f"Attempting to kill VNC session {target}")
logging.debug(f"Attempting to kill VNC session {target}")
cmd = f"{self.sing_exec} vncserver -kill {target}"
proc = self.run_command(cmd)
killed = False
while proc.poll() is None:
line = str(proc.stdout.readline(), "utf-8").strip()
# Failed attempt:
#Can't kill '29': Operation not permitted
#Killing Xtigervnc process ID 29...
# On successful attempt:
#Killing Xtigervnc process ID 29... success!
if self.debug:
logging.debug(f"kill_vnc: {line}")
if "success" in line:
killed = True
if self.debug:
logging.debug(f"kill_vnc: killed? {killed}")
# Remove target's pid file if present
try:
os.remove(os.path.expanduser(f"~/.vnc/{self.hostname}{target}.pid"))
except:
pass
try:
os.remove(os.path.expanduser(f"~/.vnc/{self.name}{target}.pid"))
except:
pass
# Remove associated /tmp/.X11-unix/<display_number> socket
socket_file = f"/tmp/.X11-unix/{display_number}"
self.__remove_files__([socket_file])
class LoginNode(Node):
"""
The LoginNode class specifies Hyak login node for its Slurm and SSH
capabilities.
"""
def __init__(self, name, debug=False, sing_container=XFCE_CONTAINER):
assert os.path.exists(XSTARTUP_FILEPATH)
assert os.path.exists(APPTAINER_BIN)
super().__init__(name, debug, sing_container)
self.subnode = None
def find_nodes(self, job_name="vnc"):
"""
Returns a set of subnodes with given job name and returns None otherwise
"""
ret = set()
command = f"squeue | grep {os.getlogin()} | grep {job_name}"
proc = self.run_command(command)
while True:
line = str(proc.stdout.readline(), 'utf-8')
if self.debug:
logging.debug(f"find_nodes: {line}")
if not line:
if not ret:
return None
return ret
if os.getlogin() in line:
# match against pattern:
# 864877 compute-h vnc hansem7 R 4:05 1 n3000
# or the following if a node is in the process of being acquired
# 870400 compute-h vnc hansem7 PD 0:00 1 (Resources)
# or the following if a node failed to be acquired and needs to be killed
# 984669 compute-h vnc hansem7 PD 0:00 1 (QOSGrpCpuLimit)
pattern = re.compile("""
(\s+)
(?P<job_id>[0-9]+)
(\s+[^ ]+){6}
(\s+)
(?P<subnode_name>[^\s]+)
""", re.VERBOSE)
match = pattern.match(line)
assert match is not None
name = match.group("subnode_name")
job_id = match.group("job_id")
if "Resources" in name:
# Quit if another node is being allocated (from another process?)
proc.kill()
msg = f"Warning: Already allocating node with job {job_id}"
print(msg)
print("Please try again later or run again with '--skip-check' argument")
if self.debug:
logging.info(f"name: {name}")
logging.info(f"job_id: {job_id}")
logging.warning(msg)
elif "QOS" in name:
proc.kill()
msg = f"Warning: job {job_id} needs to be killed"
print(msg)
print(f"Please run this script again with '--kill {job_id}' argument")
if self.debug:
logging.info(f"name: {name}")
logging.info(f"job_id: {job_id}")
logging.warning(msg)
elif self.debug:
msg = f"Found active subnode {name} with job ID {job_id}"
logging.debug(msg)
tmp = SubNode(name, job_id, self.debug)
ret.add(tmp)
return None
def check_vnc_password(self):
"""
Returns True if vnc password is set and False otherwise
"""
return os.path.exists(os.path.expanduser("~/.vnc/passwd"))
def set_vnc_password(self):
"""
Set VNC password
"""
cmd = f"{self.sing_exec} vncpasswd"
self.call_command(cmd)
def call_command(self, command:str):
"""
Call command (with arguments) on login node (to allow user interaction).
Args:
command:str : command and its arguments to run on subnode
Returns command exit status
"""
if self.debug:
msg = f"Calling on {self.name}: {command}"
print(msg)
logging.debug(msg)
return subprocess.call(command, shell=True)
def run_command(self, command):
"""
Run command (with arguments) on login node.
Commands can be in either str or list format.
Example:
cmd_str = "echo hi"
cmd_list = ["echo", "hi"]
Args:
command : command and its arguments to run on login node
Returns subprocess with stderr->stdout and stdout->PIPE
"""
assert command is not None
if self.debug:
msg = f"Running on {self.name}: {command}"
print(msg)
logging.debug(msg)
if isinstance(command, list):
return subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
elif isinstance(command, str):
return subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
def reserve_node(self, res_time=3, timeout=10, cpus=8, mem="16G", partition="compute-hugemem", account="ece", job_name="vnc"):
"""
Reserves a node and waits until the node has been acquired.
Args:
res_time: Number of hours to reserve sub node
timeout: Number of seconds to wait for node allocation
cpus: Number of cpus to allocate
mem: Amount of memory to allocate (Examples: "8G" for 8GiB of memory)
partition: Partition name (see `man salloc` on --partition option for more information)
account: Account name (see `man salloc` on --account option for more information)
job_name: Slurm job name displayed in `squeue`
Returns SubNode object if it has been acquired successfully and None otherwise.
"""
cmd = ["timeout", str(timeout), "salloc",
"-J", job_name,
"--no-shell",
"-p", partition,
"-A", account,
"-t", f"{res_time}:00:00",
"--mem=" + mem,
"-c", str(cpus)]
proc = self.run_command(cmd)
alloc_stat = False
subnode_job_id = None
subnode_name = None
def __reserve_node_irq_handler__(signalNumber, frame):
"""
Pass SIGINT to subprocess and exit program.
"""
if self.debug:
msg = f"reserve_node: Caught signal: {signalNumber}"
print(msg)
logging.info(msg)
proc.send_signal(signal.SIGINT)
print("Cancelled node allocation. Exiting...")
exit(1)
# Stop allocation when SIGINT (CTRL+C) and SIGTSTP (CTRL+Z) signals
# are detected.
signal.signal(signal.SIGINT, __reserve_node_irq_handler__)
signal.signal(signal.SIGTSTP, __reserve_node_irq_handler__)
print(f"Allocating node with {cpus} CPUs and {mem} RAM for {res_time} hours...")
while proc.poll() is None and not alloc_stat:
print("...")
line = str(proc.stdout.readline(), 'utf-8').strip()
if self.debug:
msg = f"reserve_node: {line}"
logging.debug(msg)
if "Pending" in line or "Granted" in line:
# match against pattern:
#salloc: Pending job allocation 864875
#salloc: Granted job allocation 864875
pattern = re.compile("""
(salloc:\s)
((Granted)|(Pending))
(\sjob\sallocation\s)
(?P<job_id>[0-9]+)
""", re.VERBOSE)
match = pattern.match(line)
if match is not None:
subnode_job_id = match.group("job_id")
elif "are ready for job" in line:
# match against pattern:
#salloc: Nodes n3000 are ready for job
pattern = re.compile("""
(salloc:\sNodes\s)
(?P<node_name>[ngz][0-9]{4})
(\sare\sready\sfor\sjob)
""", re.VERBOSE)
match = pattern.match(line)
if match is not None:
subnode_name = match.group("node_name")
alloc_stat = True
break
elif self.debug:
msg = f"Skipping line: {line}"
print(msg)
logging.debug(msg)
if proc.stdout is not None:
proc.stdout.close()
if proc.stderr is not None:
proc.stderr.close()
if not alloc_stat:
# check if node actually got reserved
# Background: Sometimes salloc does not print allocated node names
# at the end, so we have to check with squeue
if subnode_job_id is not None:
tmp_nodes = self.find_nodes(job_name)
for tmp_node in tmp_nodes:
if self.debug:
logging.debug(f"reserve_node: fallback: Checking {tmp_node.name} with Job ID {tmp_node.job_id}")
if tmp_node.job_id == subnode_job_id:
if self.debug:
logging.debug(f"reserve_node: fallback: Match found")
# get subnode name
subnode_name = tmp_node.name
break
else:
return None
if subnode_name is None:
msg = "Error: node allocation timed out."
print(msg)
if self.debug:
logging.error(msg)
return None
assert subnode_job_id is not None
assert subnode_name is not None
self.subnode = SubNode(name=subnode_name, job_id=subnode_job_id, debug=self.debug, sing_container=self.sing_container)
return self.subnode
def cancel_job(self, job_id:int):
"""
Cancel specified job ID
Reference:
See `man scancel` for more information on usage
"""
msg = f"Canceling job ID {job_id}"
print(f"\t{msg}")
if self.debug:
logging.debug(msg)
proc = self.run_command(["scancel", str(job_id)])
print(str(proc.communicate()[0], 'utf-8'))
def check_port(self, port:int):
"""
Returns True if port is unused and False if used.
"""
if self.debug:
logging.debug(f"Checking if port {port} is used...")
cmd = f"netstat -ant | grep LISTEN | grep {port}"
proc = self.run_command(cmd)
while proc.poll() is None:
line = str(proc.stdout.readline(), 'utf-8').strip()
if self.debug:
logging.debug(f"netstat line: {line}")
if str(port) in line:
return False
return True
def get_port(self):
"""
Returns unused port number if found and None if not found.
"""
# 300 is arbitrary limit
for i in range(0,300):
port = BASE_VNC_PORT + i
if self.check_port(port):
return port
return None
def create_port_forward(self, login_port:int, subnode_port:int):
"""
Port forward between login node and subnode
Args:
login_port:int : Login node port number
subnode_port:int : Subnode port number
Returns True if port forward succeeds and False otherwise.
"""
assert self.subnode is not None
assert self.subnode.name is not None
msg = f"Creating port forward: Login node({login_port})<->Subnode({subnode_port})"
if self.debug:
logging.debug(msg)
else:
print(f"{msg}...", end="", flush=True)
cmd = f"ssh -N -f -L {login_port}:127.0.0.1:{subnode_port} {self.subnode.hostname} &> /dev/null"
status = self.call_command(cmd)
if status == 0:
# wait (at most ~20 seconds) until port forward succeeds
count = 0
port_used = not self.check_port(login_port)
while count < 20 and not port_used:
if self.debug:
msg = f"create_port_forward: attempt #{count + 1}: port used? {port_used}"
logging.debug(msg)
port_used = not self.check_port(login_port)
count += 1
time.sleep(1)
if port_used:
if self.debug:
msg = f"Successfully created port forward"
logging.info(msg)
else:
print('\x1b[1;32m' + "Success" + '\x1b[0m')
return True
if self.debug:
msg = f"Error: Failed to create port forward"
logging.error(msg)
else:
print('\x1b[1;31m' + "Failed" + '\x1b[0m')
return False
def get_port_forwards(self, nodes=None):
"""
For each node in the SubNodes set `nodes`, get a port map between login
node port and subnode port, and then fill `vnc_port` and
`vnc_display_number` subnode attributes if None.
Example:
Suppose we have the following VNC sessions (on a single user account):
n3000 with a login<->subnode port forward from 5900 to 5901,
n3000 with a login<->subnode port forward from 5901 to 5902,
n3042 with a login<->subnode port forward from 5903 to 5901.
This function returns the following:
{ "n3000" : {5901:5900, 5902:5901}, "n3042" : {5901:5903} }
Args:
nodes : A set of SubNode objects with names to inspect
Returns a dictionary with node name as keys and
LoginNodePort (value) <-> SubNodePort (key) dictionary as value.
"""
node_port_map = dict()
if nodes is not None:
for node in nodes:
if "(" not in node.name:
port_map = dict()
cmd = f"ps x | grep ssh | grep {node.name}"
proc = self.run_command(cmd)
while proc.poll() is None:
line = str(proc.stdout.readline(), 'utf-8').strip()
if cmd not in line:
# Match against pattern:
#1974577 ? Ss 0:20 ssh -N -f -L 5902:127.0.0.1:5902 n3065.hyak.local
pattern = re.compile("""
([^\s]+(\s)+){4}
(ssh\s-N\s-f\s-L\s(?P<ln_port>[0-9]+):127.0.0.1:(?P<sn_port>[0-9]+))
""", re.VERBOSE)
match = re.match(pattern, line)
if match is not None:
ln_port = int(match.group("ln_port"))
sn_port = int(match.group("sn_port"))
port_map.update({sn_port:ln_port})
node_port_map.update({node.name:port_map})
return node_port_map
def get_job_port_forward(self, job_id:int, node_name:str, node_port_map:dict):
"""
Returns tuple containing LoginNodePort and SubNodePort for given job ID
and node_name. Returns None on failure.
"""
if self.get_time_left(job_id) is not None:
port_map = node_port_map[node_name]
if port_map is not None:
subnode = SubNode(name=node_name, job_id=job_id, debug=self.debug, sing_container=self.sing_container)
for vnc_port in port_map.keys():
display_number = vnc_port - BASE_VNC_PORT
if self.debug:
logging.debug(f"get_job_port_forward: Checking job {job_id} vnc_port {vnc_port}")
# get PID from VNC pid file
pid = subnode.get_vnc_pid(subnode.name, display_number)
if pid is None:
# try long hostname
pid = subnode.get_vnc_pid(subnode.hostname, display_number)
# if PID is active, then we have a hit for a specific job
if pid is not None and subnode.check_pid(pid):
if self.debug:
logging.debug(f"get_job_port_forward: {job_id} has vnc_port {vnc_port} and login node port {port_map[vnc_port]}")
return (vnc_port,port_map[vnc_port])
return None
def get_time_left(self, job_id:int, job_name="vnc"):
"""
Returns the time remaining for given job ID or None if the job is not
present.
"""
cmd = f'squeue -o "%L %.18i %.8j %.8u %R" | grep {os.getlogin()} | grep {job_name} | grep {job_id}'
proc = self.run_command(cmd)
if proc.poll() is None:
line = str(proc.stdout.readline(), 'utf-8')
return line.split(' ', 1)[0]
return None
def print_props(self):
"""
Print all properties (including subnode properties)
"""
print("Login node properties:")
props = vars(self)
for item in props:
msg = f"{item} : {props[item]}"
print(f"\t{msg}")
if self.debug:
logging.debug(msg)
if item == "subnode" and props[item] is not None:
props[item].print_props()
def print_status(self, job_name:str, node_set=None, node_port_map=None):
"""
Print details of each active VNC job in node_set. VNC port and display
number should be in node_port_map.
"""
print(f"Active {job_name} jobs:")
if node_set is not None:
for node in node_set:
mapped_port = None
if node_port_map and node_port_map[node.name]:
port_forward = self.get_job_port_forward(node.job_id, node.name, node_port_map)
if port_forward:
vnc_port = port_forward[0]
mapped_port = port_forward[1]
node.vnc_display_number = vnc_port + BASE_VNC_PORT
node_port_map.get(node.name).pop(vnc_port)
time_left = self.get_time_left(node.job_id, job_name)
vnc_active = mapped_port is not None
ssh_cmd = f"ssh -N -f -L {mapped_port}:127.0.0.1:{mapped_port} {os.getlogin()}@klone.hyak.uw.edu"
print(f"\tJob ID: {node.job_id}")
print(f"\t\tSubNode: {node.name}")
print(f"\t\tTime left: {time_left}")
print(f"\t\tVNC active: {vnc_active}")
if vnc_active:
print(f"\t\tVNC display number: {vnc_port - BASE_VNC_PORT}")
print(f"\t\tVNC port: {vnc_port}")
print(f"\t\tMapped LoginNode port: {mapped_port}")
print(f"\t\tRun command: {ssh_cmd}")
def repair_ln_sn_port_forwards(self, node_set=None, node_port_map=None):
"""
Re-creates port forwards missing from vnc jobs.
Useful when LoginNode restarts but VNC jobs remain alive.
"""
if node_set is not None:
for node in node_set:
if node_port_map and node_port_map[node.name]:
print(f"{node.name} with job ID {node.job_id} already has valid port forward")
else:
subnode = SubNode(name=node.name, job_id=node.job_id, debug=self.debug, sing_container=self.sing_container)
subnode_pids = subnode.list_pids()
# search for vnc process
proc = subnode.run_command("ps x | grep vnc")
while proc.poll() is None:
line = str(proc.stdout.readline(), 'utf-8').strip()
pid = int(line.split(' ', 1)[0])
# match found
if pid in subnode_pids:
if self.debug:
logging.debug(f"repair_ln_sn_port_forwards: VNC PID {pid} found for job ID {node.job_id}")
pattern = re.compile("""
(vnc\s+:)
(?P<display_number>\d+)
""", re.VERBOSE)
match = re.search(pattern, line)
assert match is not None
vnc_port = BASE_VNC_PORT + int(match.group("display_number"))
u2h_port = self.get_port()
if self.debug:
logging.debug(f"repair_ln_sn_port_forwards: LoginNode({u2h_port})<->JobID({vnc_port})")
if u2h_port is None:
print(f"Error: cannot find available/unused port")
continue
else:
self.subnode = subnode
self.create_port_forward(u2h_port, vnc_port)
def check_auth_keys():
"""
Returns True if a public key (~/.ssh/*.pub) exists in
~/.ssh/authorized_keys and False otherwise.
"""
pubkeys = glob.glob(os.path.expanduser("~/.ssh/*.pub"))
for pubkey in pubkeys:
cmd = f"cat {pubkey} | grep -f {AUTH_KEYS_FILEPATH} &> /dev/null"
if subprocess.call(cmd, shell=True) == 0:
return True
return False
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--partition',
dest='partition',