enable vm deletions, wait 7 days before deleting, fix emails, add rtp deletion email

This commit is contained in:
Jordan Rodgers 2018-04-16 15:14:36 -04:00
parent 8808389b46
commit d9658aaf07
4 changed files with 43 additions and 38 deletions

View file

@ -37,16 +37,6 @@ def delete_vm_expire(db, vmid):
db.commit() 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): def get_expiring_vms(db):
expiring = [] expiring = []
today = datetime.date.today() today = datetime.date.today()

View file

@ -21,14 +21,31 @@ def send_email(toaddr, subject, body):
def send_vm_expire_email(user, vms): def send_vm_expire_email(user, vms):
toaddr = "{}@csh.rit.edu".format(user) toaddr = "{}@csh.rit.edu".format(user)
subject = 'Proxstar VM Expiration Notice' 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: for vm in vms:
if vm[1] == 0: if vm[2] == -6:
body += " - {} expires today (VM has been stopped and will be deleted tomorrow)\n".format( body += " - {} ({}) has expired (VM has been stopped and will be deleted in 1 day)\n".format(vm[1], vm[0])
vm[0], vm[1]) elif vm[2] < 0:
elif vm[1] == 1: body += " - {} ({}) has expired (VM has been stopped and will be deleted in {} days)\n".format(
body += " - {} expires in 1 day\n".format(vm[0]) 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: else:
body += " - {} expires in {} days\n".format(vm[0], vm[1]) body += " - {} ({}) expires in {} days\n".format(vm[1], vm[0], vm[2])
body += "\nPlease login to Proxstar 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)
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) send_email(toaddr, subject, body)

View file

@ -11,6 +11,7 @@ from proxstar.db import *
from proxstar.util import * from proxstar.util import *
from proxstar.mail import * from proxstar.mail import *
from proxstar.starrs import * from proxstar.starrs import *
from proxstar.vnc import send_stop_ssh_tunnel
from proxstar.vm import VM, create_vm, clone_vm from proxstar.vm import VM, create_vm, clone_vm
from proxstar.user import User, get_vms_for_rtp from proxstar.user import User, get_vms_for_rtp
from proxstar.proxmox import connect_proxmox, get_pools from proxstar.proxmox import connect_proxmox, get_pools
@ -72,21 +73,6 @@ def delete_vm_task(vmid):
delete_vm_expire(db, 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(): def process_expiring_vms_task():
with app.app_context(): with app.app_context():
proxmox = connect_proxmox() proxmox = connect_proxmox()
@ -96,17 +82,26 @@ def process_expiring_vms_task():
for pool in pools: for pool in pools:
user = User(pool) user = User(pool)
expiring_vms = [] expiring_vms = []
expired_vms = []
vms = user.vms vms = user.vms
for vm in vms: for vm in vms:
vm = VM(vm['vmid']) vm = VM(vm['vmid'])
days = (vm.expire - datetime.date.today()).days 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 name = vm.name
expiring_vms.append([vm.name, days]) expiring_vms.append([vm.id, vm.name, days])
if days == 0: if days <= 0:
expired_vms.append([vm.id, vm.name, days])
if days <= 0:
vm.stop() 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: 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(): def generate_pool_cache_task():

View file

@ -14,7 +14,10 @@ class VM(object):
@lazy_property @lazy_property
def name(self): def name(self):
return self.config['name'] try:
return self.config['name']
except KeyError:
return self.id
@lazy_property @lazy_property
def cpu(self): def cpu(self):