Skip to content

Commit dd876a8

Browse files
committed
tear down SSH tunnel every time console button is clicked and on suspend, yapf a few things
1 parent 9f7f5d2 commit dd876a8

File tree

3 files changed

+19
-28
lines changed

3 files changed

+19
-28
lines changed

proxstar/__init__.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def vm_power(vmid, action):
215215
vm.reset()
216216
elif action == 'suspend':
217217
vm.suspend()
218+
send_stop_ssh_tunnel(vmid)
218219
elif action == 'resume':
219220
vm.resume()
220221
return '', 200
@@ -238,31 +239,14 @@ def vm_console(vmid):
238239
proxmox = connect_proxmox()
239240
if user.rtp or int(vmid) in user.allowed_vms:
240241
vm = VM(vmid)
242+
stop_ssh_tunnel(vm.id, ssh_tunnels)
241243
port = str(5900 + int(vmid))
242244
token = add_vnc_target(port)
243245
node = "{}.csh.rit.edu".format(vm.node)
244-
tunnel = next((tunnel for tunnel in ssh_tunnels
245-
if tunnel.local_bind_port == int(port)), None)
246-
if tunnel:
247-
if tunnel.ssh_host != node:
248-
print(
249-
"Tunnel already exists for VM {} to the wrong Proxmox node.".
250-
format(vmid))
251-
tunnel.stop()
252-
ssh_tunnels.remove(tunnel)
253-
print("Creating SSH tunnel to {} for VM {}.".format(
254-
node, vmid))
255-
tunnel = start_ssh_tunnel(node, port)
256-
ssh_tunnels.append(tunnel)
257-
vm.start_vnc(port)
258-
else:
259-
print("Tunnel already exists to {} for VM {}.".format(
260-
node, vmid))
261-
else:
262-
print("Creating SSH tunnel to {} for VM {}.".format(node, vmid))
263-
tunnel = start_ssh_tunnel(node, port)
264-
ssh_tunnels.append(tunnel)
265-
vm.start_vnc(port)
246+
print("Creating SSH tunnel to {} for VM {}.".format(node, vm.id))
247+
tunnel = start_ssh_tunnel(node, port)
248+
ssh_tunnels.append(tunnel)
249+
vm.start_vnc(port)
266250
return token, 200
267251
else:
268252
return '', 403

proxstar/mail.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@ def send_vm_expire_email(user, vms):
2424
body = "The following VMs in Proxstar are expiring soon or have already expired:\n\n"
2525
for vm in vms:
2626
if vm[2] == -6:
27-
body += " - {} ({}) has expired (VM has been stopped and will be deleted in 1 day)\n".format(vm[1], vm[0])
27+
body += " - {} ({}) has expired (VM has been stopped and will be deleted in 1 day)\n".format(
28+
vm[1], vm[0])
2829
elif vm[2] < 0:
2930
body += " - {} ({}) has expired (VM has been stopped and will be deleted in {} days)\n".format(
3031
vm[1], vm[0], (7 + int(vm[2])))
3132
elif vm[2] == 0:
32-
body += " - {} ({}) expires today (VM has been stopped and will be deleted in 7 days)\n".format(vm[1], vm[0])
33+
body += " - {} ({}) expires today (VM has been stopped and will be deleted in 7 days)\n".format(
34+
vm[1], vm[0])
3335
elif vm[2] == 1:
3436
body += " - {} ({}) expires in 1 day\n".format(vm[1], vm[0])
3537
else:
36-
body += " - {} ({}) expires in {} days\n".format(vm[1], vm[0], vm[2])
38+
body += " - {} ({}) expires in {} days\n".format(
39+
vm[1], vm[0], vm[2])
3740
body += "\nPlease login to Proxstar (https://proxstar.csh.rit.edu/) and renew any VMs you would like to keep."
3841
send_email(toaddr, subject, body)
3942

@@ -44,8 +47,10 @@ def send_rtp_vm_delete_email(vms):
4447
body = "The following VMs in Proxstar have expired and will be deleted soon:\n\n"
4548
for vm in vms:
4649
if vm[2] == -6:
47-
body += " - {} ({}) will be deleted in 1 day\n".format(vm[1], vm[0])
50+
body += " - {} ({}) will be deleted in 1 day\n".format(
51+
vm[1], vm[0])
4852
else:
49-
body += " - {} ({}) will be deleted in {} days\n".format(vm[1], vm[0], (7 + int(vm[2])))
53+
body += " - {} ({}) will be deleted in {} days\n".format(
54+
vm[1], vm[0], (7 + int(vm[2])))
5055
body += "\nPlease verify this list to ensure there aren't any pools included in Proxstar that shouldn't be."
5156
send_email(toaddr, subject, body)

proxstar/tasks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ def process_expiring_vms_task():
9595
if days <= 0:
9696
vm.stop()
9797
elif days == -7:
98-
print("Deleting {} ({}) as it has been a week since expiration.".format(vm.name, vm.id))
98+
print(
99+
"Deleting {} ({}) as it has been a week since expiration.".
100+
format(vm.name, vm.id))
99101
send_stop_ssh_tunnel(vm.id)
100102
delete_vm_task(vm.id)
101103
if expiring_vms:

0 commit comments

Comments
 (0)