mirror of
https://github.com/ComputerScienceHouse/proxstar.git
synced 2025-03-09 15:40:09 +00:00
enable vm deletions, wait 7 days before deleting, fix emails, add rtp deletion email
This commit is contained in:
parent
8808389b46
commit
d9658aaf07
4 changed files with 43 additions and 38 deletions
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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():
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue