def start_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.ON: appliance_power_on.delay(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)
def give_to_pool(cls, pool, custom_limit=None): """Give appliances from shepherd to the pool where the maximum count is specified by pool or you can specify a custom limit """ from appliances.tasks import appliance_power_on limit = custom_limit if custom_limit is not None else pool.total_count appliances = [] with transaction.atomic(): for template in pool.possible_templates: for appliance in cls.unassigned().filter( template=template).all()[:limit - len(appliances)]: with appliance.kill_lock: appliance.appliance_pool = pool appliance.save() appliance.set_status("Given to pool {}".format(pool.id)) appliance_power_on.delay(appliance.id) appliances.append(appliance) if len(appliances) == limit: break return len(appliances)
def power_on(user, appliance): """Power on 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.ON: return appliance_power_on.delay(appliance.id).task_id