tear down SSH tunnel every time console button is clicked and on suspend, yapf a few things

This commit is contained in:
Jordan Rodgers 2018-04-26 02:59:09 -04:00
parent 9f7f5d27ca
commit dd876a8b2c
3 changed files with 19 additions and 28 deletions

View file

@ -215,6 +215,7 @@ def vm_power(vmid, action):
vm.reset() vm.reset()
elif action == 'suspend': elif action == 'suspend':
vm.suspend() vm.suspend()
send_stop_ssh_tunnel(vmid)
elif action == 'resume': elif action == 'resume':
vm.resume() vm.resume()
return '', 200 return '', 200
@ -238,31 +239,14 @@ def vm_console(vmid):
proxmox = connect_proxmox() proxmox = connect_proxmox()
if user.rtp or int(vmid) in user.allowed_vms: if user.rtp or int(vmid) in user.allowed_vms:
vm = VM(vmid) vm = VM(vmid)
stop_ssh_tunnel(vm.id, ssh_tunnels)
port = str(5900 + int(vmid)) port = str(5900 + int(vmid))
token = add_vnc_target(port) token = add_vnc_target(port)
node = "{}.csh.rit.edu".format(vm.node) node = "{}.csh.rit.edu".format(vm.node)
tunnel = next((tunnel for tunnel in ssh_tunnels print("Creating SSH tunnel to {} for VM {}.".format(node, vm.id))
if tunnel.local_bind_port == int(port)), None) tunnel = start_ssh_tunnel(node, port)
if tunnel: ssh_tunnels.append(tunnel)
if tunnel.ssh_host != node: vm.start_vnc(port)
print(
"Tunnel already exists for VM {} to the wrong Proxmox node.".
format(vmid))
tunnel.stop()
ssh_tunnels.remove(tunnel)
print("Creating SSH tunnel to {} for VM {}.".format(
node, vmid))
tunnel = start_ssh_tunnel(node, port)
ssh_tunnels.append(tunnel)
vm.start_vnc(port)
else:
print("Tunnel already exists to {} for VM {}.".format(
node, vmid))
else:
print("Creating SSH tunnel to {} for VM {}.".format(node, vmid))
tunnel = start_ssh_tunnel(node, port)
ssh_tunnels.append(tunnel)
vm.start_vnc(port)
return token, 200 return token, 200
else: else:
return '', 403 return '', 403

View file

@ -24,16 +24,19 @@ def send_vm_expire_email(user, vms):
body = "The following VMs in Proxstar are expiring soon or have already expired:\n\n" body = "The following VMs in Proxstar are expiring soon or have already expired:\n\n"
for vm in vms: for vm in vms:
if vm[2] == -6: if vm[2] == -6:
body += " - {} ({}) has expired (VM has been stopped and will be deleted in 1 day)\n".format(vm[1], vm[0]) body += " - {} ({}) has expired (VM has been stopped and will be deleted in 1 day)\n".format(
vm[1], vm[0])
elif vm[2] < 0: elif vm[2] < 0:
body += " - {} ({}) has expired (VM has been stopped and will be deleted in {} days)\n".format( body += " - {} ({}) has expired (VM has been stopped and will be deleted in {} days)\n".format(
vm[1], vm[0], (7 + int(vm[2]))) vm[1], vm[0], (7 + int(vm[2])))
elif vm[2] == 0: elif vm[2] == 0:
body += " - {} ({}) expires today (VM has been stopped and will be deleted in 7 days)\n".format(vm[1], vm[0]) body += " - {} ({}) expires today (VM has been stopped and will be deleted in 7 days)\n".format(
vm[1], vm[0])
elif vm[2] == 1: elif vm[2] == 1:
body += " - {} ({}) expires in 1 day\n".format(vm[1], vm[0]) body += " - {} ({}) expires in 1 day\n".format(vm[1], vm[0])
else: else:
body += " - {} ({}) expires in {} days\n".format(vm[1], vm[0], vm[2]) body += " - {} ({}) expires in {} days\n".format(
vm[1], vm[0], vm[2])
body += "\nPlease login to Proxstar (https://proxstar.csh.rit.edu/) and renew any VMs you would like to keep." body += "\nPlease login to Proxstar (https://proxstar.csh.rit.edu/) and renew any VMs you would like to keep."
send_email(toaddr, subject, body) send_email(toaddr, subject, body)
@ -44,8 +47,10 @@ def send_rtp_vm_delete_email(vms):
body = "The following VMs in Proxstar have expired and will be deleted soon:\n\n" body = "The following VMs in Proxstar have expired and will be deleted soon:\n\n"
for vm in vms: for vm in vms:
if vm[2] == -6: if vm[2] == -6:
body += " - {} ({}) will be deleted in 1 day\n".format(vm[1], vm[0]) body += " - {} ({}) will be deleted in 1 day\n".format(
vm[1], vm[0])
else: else:
body += " - {} ({}) will be deleted in {} days\n".format(vm[1], vm[0], (7 + int(vm[2]))) body += " - {} ({}) will be deleted in {} days\n".format(
vm[1], vm[0], (7 + int(vm[2])))
body += "\nPlease verify this list to ensure there aren't any pools included in Proxstar that shouldn't be." body += "\nPlease verify this list to ensure there aren't any pools included in Proxstar that shouldn't be."
send_email(toaddr, subject, body) send_email(toaddr, subject, body)

View file

@ -95,7 +95,9 @@ def process_expiring_vms_task():
if days <= 0: if days <= 0:
vm.stop() vm.stop()
elif days == -7: elif days == -7:
print("Deleting {} ({}) as it has been a week since expiration.".format(vm.name, vm.id)) print(
"Deleting {} ({}) as it has been a week since expiration.".
format(vm.name, vm.id))
send_stop_ssh_tunnel(vm.id) send_stop_ssh_tunnel(vm.id)
delete_vm_task(vm.id) delete_vm_task(vm.id)
if expiring_vms: if expiring_vms: