Esempio n. 1
0
def delete_vm_task(vmid):
    with app.app_context():
        db = connect_db()
        starrs = connect_starrs()
        vm = VM(vmid)
        # do this before deleting the VM since it is hard to reconcile later
        retry = 0
        while retry < 3:
            try:
                delete_starrs(starrs, vm.name)
                break
            except:
                retry += 1
                time.sleep(3)
                continue
        if vm.status != 'stopped':
            vm.stop()
            retry = 0
            while retry < 10:
                time.sleep(3)
                if vm.status == 'stopped':
                    break
                retry += 1
        vm.delete()
        delete_vm_expire(db, vmid)
Esempio n. 2
0
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)
Esempio n. 3
0
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
Esempio n. 4
0
def delete_vm_task(vmid):
    with app.app_context():
        db = connect_db()
        starrs = connect_starrs()
        vm = VM(vmid)
        if vm.status != 'stopped':
            vm.stop()
            retry = 0
            while retry < 10:
                time.sleep(3)
                if vm.status == 'stopped':
                    break
                retry += 1
        vm.delete()
        delete_starrs(starrs, vm.name)
        delete_vm_expire(db, vmid)
Esempio n. 5
0
def process_expiring_vms_task():
    with app.app_context():
        proxmox = connect_proxmox()
        db = connect_db()
        starrs = connect_starrs()
        pools = get_pools(proxmox, db)
        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]:
                    name = vm.name
                    expiring_vms.append([vm.name, days])
                    if days == 0:
                        vm.stop()
            if expiring_vms:
                send_vm_expire_email('com6056', expiring_vms)