forked from COSIMA/cosima-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvdi_ssh.py
66 lines (54 loc) · 2.06 KB
/
vdi_ssh.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
#!/usr/bin/python3
"""
Script to launch a VDI session (or connect to already running session)
and start an ssh tunnel to that session.
"""
import subprocess
import re
import sys
import webbrowser
import time
params = {'User' : 'jm0634',
'IdentityFile' : '~/.ssh/MassiveLauncherKey',
}
def ssh(func):
cmd = "ssh vdi " + func
ret = subprocess.run(cmd.split(), stdout=subprocess.PIPE)
return ret
def session(func):
cmd = '/opt/vdi/bin/session-ctl --configver=20151620513 '
cmd += func
return ssh(cmd)
print("Verifying SSH keys to VDI are configured...", end='' )
r = session('hello --partition main')
if r.returncode != 0:
print("Error with ssh keys and VDI. Please edit params dictionary in script.")
sys.exit(1)
print("OK")
print("Determine if VDI session is already running...", end='')
r = session('list-avail --partition main')
m = re.match('^#~#id=(?P<jobid>(?P<jobidNumber>.*?))#~#state=(?P<state>.*?)(?:#~#time_rem=(?P<remainingWalltime>.*?))?#~#', r.stdout.decode())
if m is not None:
params.update(m.groupdict())
w = int(params['remainingWalltime'])
remainingWalltime = '{:02}:{:02}:{:02}'.format(w // 3600, w % 3600 // 60, w % 60)
print(remainingWalltime, 'time remaining')
# should give use option of starting a new session of the remaining walltime is short
else:
print('No')
print("Launching new VDI session...", end='')
r = session('launch --partition main')
m = re.match('^#~#id=(?P<jobid>(?P<jobidNumber>.*?))#~#', r.stdout.decode())
params.update(m.groupdict())
time.sleep(2) # instead of waiting, should check for confirmation
# use has-started
print("Determine jobid for VDI session...{jobid}".format(**params))
print("Get execHost for VDI session...", end='')
r = session('get-host --jobid {jobid}'.format(**params))
m = re.search('^#~#host=(?P<execHost>.*?)#~#', r.stdout.decode())
params.update(m.groupdict())
print('{execHost}'.format(**params))
print ("Openning SSH to VDI...")
cmd = 'ssh -t {execHost} -l {User}'.format(**params)
p = subprocess.Popen(cmd.split())
p.wait()