def process_expiring_vms_task(): with app.app_context(): proxmox = connect_proxmox() db = connect_db() connect_starrs() pools = get_pools(proxmox, db) expired_vms = [] for pool in pools: user = User(pool) expiring_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, -1, -2, -3, -4, -5, -6]: expiring_vms.append([vm.id, vm.name, days]) if days <= 0: expired_vms.append([vm.id, vm.name, days]) vm.stop() elif days <= -7: logging.info( 'Deleting {} ({}) as it has been at least 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(pool, expiring_vms) if expired_vms: send_rtp_vm_delete_email(expired_vms)
def delete(vmid): user = User(session['userinfo']['preferred_username']) connect_proxmox() if user.rtp or int(vmid) in user.allowed_vms: send_stop_ssh_tunnel(vmid) # Submit the delete VM task to RQ q.enqueue(delete_vm_task, vmid) return '', 200 else: return '', 403
def vm_power(vmid, action): user = User(session['userinfo']['preferred_username']) connect_proxmox() if user.rtp or int(vmid) in user.allowed_vms: vm = VM(vmid) if action == 'start': vmconfig = vm.config usage_check = user.check_usage(vmconfig['cores'], vmconfig['memory'], 0) if usage_check: return usage_check vm.start() elif action == 'stop': vm.stop() send_stop_ssh_tunnel(vmid) elif action == 'shutdown': vm.shutdown() send_stop_ssh_tunnel(vmid) elif action == 'reset': vm.reset() elif action == 'suspend': vm.suspend() send_stop_ssh_tunnel(vmid) elif action == 'resume': vm.resume() return '', 200 else: return '', 403