From d9658aaf0719ae1d73e1372e173361bef747d142 Mon Sep 17 00:00:00 2001 From: Jordan Rodgers Date: Mon, 16 Apr 2018 15:14:36 -0400 Subject: [PATCH] enable vm deletions, wait 7 days before deleting, fix emails, add rtp deletion email --- proxstar/db.py | 10 ---------- proxstar/mail.py | 33 +++++++++++++++++++++++++-------- proxstar/tasks.py | 33 ++++++++++++++------------------- proxstar/vm.py | 5 ++++- 4 files changed, 43 insertions(+), 38 deletions(-) diff --git a/proxstar/db.py b/proxstar/db.py index 6859198..0914744 100644 --- a/proxstar/db.py +++ b/proxstar/db.py @@ -37,16 +37,6 @@ def delete_vm_expire(db, vmid): db.commit() -def get_expired_vms(db): - expired = [] - today = datetime.date.today().strftime('%Y-%m-%d') - expire = db.query(VM_Expiration).filter( - VM_Expiration.expire_date < today).all() - for vm in expire: - expired.append(vm.id) - return expired - - def get_expiring_vms(db): expiring = [] today = datetime.date.today() diff --git a/proxstar/mail.py b/proxstar/mail.py index 0a66b87..049047d 100644 --- a/proxstar/mail.py +++ b/proxstar/mail.py @@ -21,14 +21,31 @@ def send_email(toaddr, subject, body): def send_vm_expire_email(user, vms): toaddr = "{}@csh.rit.edu".format(user) subject = 'Proxstar VM Expiration Notice' - body = "The following VMs in Proxstar are expiring soon:\n\n" + body = "The following VMs in Proxstar are expiring soon or have already expired:\n\n" for vm in vms: - if vm[1] == 0: - body += " - {} expires today (VM has been stopped and will be deleted tomorrow)\n".format( - vm[0], vm[1]) - elif vm[1] == 1: - body += " - {} expires in 1 day\n".format(vm[0]) + if vm[2] == -6: + body += " - {} ({}) has expired (VM has been stopped and will be deleted in 1 day)\n".format(vm[1], vm[0]) + elif vm[2] < 0: + body += " - {} ({}) has expired (VM has been stopped and will be deleted in {} days)\n".format( + vm[1], vm[0], (7 + int(vm[2]))) + elif vm[2] == 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: + body += " - {} ({}) expires in 1 day\n".format(vm[1], vm[0]) else: - body += " - {} expires in {} days\n".format(vm[0], vm[1]) - body += "\nPlease login to Proxstar and renew any VMs you would like to keep." + 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." + send_email(toaddr, subject, body) + + +def send_rtp_vm_delete_email(vms): + toaddr = 'rtp@csh.rit.edu' + subject = 'Proxstar VM Deletion Report' + body = "The following VMs in Proxstar have expired and will be deleted soon:\n\n" + for vm in vms: + if vm[2] == -6: + body += " - {} ({}) will be deleted in 1 day\n".format(vm[1], vm[0]) + else: + 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." send_email(toaddr, subject, body) diff --git a/proxstar/tasks.py b/proxstar/tasks.py index 4821d2e..9320b75 100644 --- a/proxstar/tasks.py +++ b/proxstar/tasks.py @@ -11,6 +11,7 @@ from proxstar.db import * from proxstar.util import * from proxstar.mail import * from proxstar.starrs import * +from proxstar.vnc import send_stop_ssh_tunnel from proxstar.vm import VM, create_vm, clone_vm from proxstar.user import User, get_vms_for_rtp from proxstar.proxmox import connect_proxmox, get_pools @@ -72,21 +73,6 @@ def delete_vm_task(vmid): delete_vm_expire(db, vmid) -def process_expired_vms_task(): - with app.app_context(): - proxmox = connect_proxmox() - starrs = connect_starrs() - expired_vms = get_expired_vms() - print(expired_vms) - - # for vmid in expired_vms: - - # vm = VM(vmid) - # delete_vm(proxmox, starrs, vmid) - # delete_starrs(starrs, vm.name) - # delete_vm_expire(vmid) - - def process_expiring_vms_task(): with app.app_context(): proxmox = connect_proxmox() @@ -96,17 +82,26 @@ def process_expiring_vms_task(): for pool in pools: user = User(pool) expiring_vms = [] + expired_vms = [] vms = user.vms for vm in vms: vm = VM(vm['vmid']) days = (vm.expire - datetime.date.today()).days - if days in [10, 7, 3, 1, 0]: + if days in [10, 7, 3, 1, 0, -1, -2, -3, -4, -5, -6]: name = vm.name - expiring_vms.append([vm.name, days]) - if days == 0: + expiring_vms.append([vm.id, vm.name, days]) + if days <= 0: + expired_vms.append([vm.id, vm.name, days]) + if days <= 0: vm.stop() + elif days == -7: + print("Deleting {} ({}) as it has been a week since expiration.".format(vm.name, vm.id)) + send_stop_ssh_tunnel(vm.id) + delete_vm_task(vm.id) if expiring_vms: - send_vm_expire_email('com6056', expiring_vms) + send_vm_expire_email(pool, expiring_vms) + if expired_vms: + send_rtp_vm_delete_email(expired_vms) def generate_pool_cache_task(): diff --git a/proxstar/vm.py b/proxstar/vm.py index 86f6f59..4e6ea32 100644 --- a/proxstar/vm.py +++ b/proxstar/vm.py @@ -14,7 +14,10 @@ class VM(object): @lazy_property def name(self): - return self.config['name'] + try: + return self.config['name'] + except KeyError: + return self.id @lazy_property def cpu(self):