def suspend_appliance(request, appliance_id): if not request.user.is_authenticated(): return go_home(request) try: appliance = Appliance.objects.get(id=appliance_id) except ObjectDoesNotExist: messages.error(request, 'Appliance with ID {} does not exist!.'.format(appliance_id)) return go_back_or_home(request) if not can_operate_appliance_or_pool(appliance, request.user): messages.error(request, 'This appliance belongs either to some other user or nobody.') return go_back_or_home(request) if appliance.power_state != Appliance.Power.SUSPENDED: appliance_suspend.delay(appliance.id) messages.success(request, 'Initiated suspend of appliance.') return go_back_or_home(request) else: messages.info(request, 'Appliance was already suspended.') return go_back_or_home(request)
def suspend(user, appliance): """Suspend the appliance. If task is called, an id is returned, otherwise None. You can specify appliance by IP address, id or name. """ appliance = get_appliance(appliance, user) if appliance.power_state == Appliance.Power.OFF: return False elif appliance.power_state != Appliance.Power.SUSPENDED: return appliance_suspend.delay(appliance.id).task_id
def suspend(user, appliance): """Suspend the appliance. If task is called, an id is returned, otherwise None. You can specify appliance by IP address, id or name. """ appliance = get_appliance(appliance) if appliance.owner is None: if not user.is_staff: raise Exception("Only staff can operate with nonowned appliances") elif appliance.owner != user: raise Exception("This appliance belongs to a different user!") if appliance.power_state == Appliance.Power.OFF: return False elif appliance.power_state != Appliance.Power.SUSPENDED: return appliance_suspend.delay(appliance.id).task_id
def appliance_action(request, appliance_id, action, x=None): try: appliance = Appliance.objects.get(id=appliance_id) except ObjectDoesNotExist: messages.warning( request, 'Appliance with ID {} does not exist!.'.format(appliance_id)) return go_back_or_home(request) if not can_operate_appliance_or_pool(appliance, request.user): messages.warning( request, 'This appliance belongs either to some other user or nobody.') return go_back_or_home(request) if action == "start": if appliance.power_state != Appliance.Power.ON: chain(appliance_power_on.si(appliance.id), (wait_appliance_ready if appliance.preconfigured else mark_appliance_ready).si(appliance.id))() messages.success(request, 'Initiated launch of appliance.') return go_back_or_home(request) else: messages.info(request, 'Appliance was already powered on.') return go_back_or_home(request) elif action == "reboot": if appliance.power_state == Appliance.Power.ON: chain(appliance_reboot.si(appliance.id), mark_appliance_ready.si(appliance.id))() messages.success(request, 'Initiated reboot of appliance.') return go_back_or_home(request) else: messages.warning(request, 'Only powered on appliances can be rebooted') return go_back_or_home(request) elif action == "stop": if appliance.power_state != Appliance.Power.OFF: appliance_power_off.delay(appliance.id) messages.success(request, 'Initiated stop of appliance.') return go_back_or_home(request) else: messages.info(request, 'Appliance was already powered off.') return go_back_or_home(request) elif action == "suspend": if appliance.power_state != Appliance.Power.SUSPENDED: appliance_suspend.delay(appliance.id) messages.success(request, 'Initiated suspend of appliance.') return go_back_or_home(request) else: messages.info(request, 'Appliance was already suspended.') return go_back_or_home(request) elif action == "kill": Appliance.kill(appliance) messages.success(request, 'Kill initiated.') return go_back_or_home(request) elif action == "dont_expire": if not request.user.is_superuser: messages.warning( request, 'Disabling expiration time is allowed only for superusers.') return go_back_or_home(request) with transaction.atomic(): appliance.leased_until = None appliance.save() messages.success(request, 'Lease disabled successfully. Be careful.') return go_back_or_home(request) elif action == "set_lease": if not can_operate_appliance_or_pool(appliance, request.user): messages.warning( request, 'This appliance belongs either to some other user or nobody.') return go_back_or_home(request) appliance.prolong_lease(time=int(x)) messages.success(request, 'Lease prolonged successfully.') return go_back_or_home(request) else: messages.warning(request, "Unknown action '{}'".format(action))
def appliance_action(request, appliance_id, action, x=None): try: appliance = Appliance.objects.get(id=appliance_id) except ObjectDoesNotExist: messages.warning(request, 'Appliance with ID {} does not exist!.'.format(appliance_id)) return go_back_or_home(request) if not can_operate_appliance_or_pool(appliance, request.user): messages.warning(request, 'This appliance belongs either to some other user or nobody.') return go_back_or_home(request) if action == "start": if appliance.power_state != Appliance.Power.ON: chain( appliance_power_on.si(appliance.id), (wait_appliance_ready if appliance.preconfigured else mark_appliance_ready).si( appliance.id))() messages.success(request, 'Initiated launch of appliance.') return go_back_or_home(request) else: messages.info(request, 'Appliance was already powered on.') return go_back_or_home(request) elif action == "reboot": if appliance.power_state == Appliance.Power.ON: chain( appliance_reboot.si(appliance.id), mark_appliance_ready.si(appliance.id))() messages.success(request, 'Initiated reboot of appliance.') return go_back_or_home(request) else: messages.warning(request, 'Only powered on appliances can be rebooted') return go_back_or_home(request) elif action == "stop": if appliance.power_state != Appliance.Power.OFF: appliance_power_off.delay(appliance.id) messages.success(request, 'Initiated stop of appliance.') return go_back_or_home(request) else: messages.info(request, 'Appliance was already powered off.') return go_back_or_home(request) elif action == "suspend": if appliance.power_state != Appliance.Power.SUSPENDED: appliance_suspend.delay(appliance.id) messages.success(request, 'Initiated suspend of appliance.') return go_back_or_home(request) else: messages.info(request, 'Appliance was already suspended.') return go_back_or_home(request) elif action == "kill": Appliance.kill(appliance) messages.success(request, 'Kill initiated.') return go_back_or_home(request) elif action == "dont_expire": if not request.user.is_superuser: messages.warning(request, 'Disabling expiration time is allowed only for superusers.') return go_back_or_home(request) with transaction.atomic(): appliance.leased_until = None appliance.save() messages.success(request, 'Lease disabled successfully. Be careful.') return go_back_or_home(request) elif action == "set_lease": if not can_operate_appliance_or_pool(appliance, request.user): messages.warning(request, 'This appliance belongs either to some other user or nobody.') return go_back_or_home(request) appliance.prolong_lease(time=int(x)) messages.success(request, 'Lease prolonged successfully.') return go_back_or_home(request) else: messages.warning(request, "Unknown action '{}'".format(action))