def get(self, request, *args, **kwargs): """ Return form for adding an annotation. """ fqdn = request.GET.get('fqdn', None) try: result = get_machine(fqdn, redirect_to='api:annotation_add', data=request.GET) if isinstance(result, Serializer): return result.as_json elif isinstance(result, HttpResponseRedirect): return result machine = result except Exception as e: return ErrorMessage(str(e)).as_json if isinstance(request.user, AnonymousUser) or not request.auth: return AuthRequiredSerializer().as_json form = AnnotationAPIForm(machine=machine) input = InputSerializer(form.as_dict(), self.URL_POST.format(fqdn=machine.fqdn), form.get_order()) return input.as_json
def get(self, request, *args, **kwargs): """ Return form for adding a remotepower. """ fqdn = request.GET.get('fqdn', None) try: result = get_machine(fqdn, redirect_to='api:remotepower_add', data=request.GET) if isinstance(result, Serializer): return result.as_json elif isinstance(result, HttpResponseRedirect): return result machine = result except Exception as e: return ErrorMessage(str(e)).as_json if isinstance(request.user, AnonymousUser) or not request.auth: return AuthRequiredSerializer().as_json if not request.user.is_superuser: return ErrorMessage( "Only superusers are allowed to perform this action!").as_json if machine.has_remotepower(): return InfoMessage("Machine has already a remote power.").as_json form = RemotePowerAPIForm(machine=machine) input = InputSerializer(form.as_dict(), self.URL_POST.format(fqdn=machine.fqdn), form.get_order()) return input.as_json
def get(self, request, *args, **kwargs): """ Return form for adding a virtual machine. """ architectures = self._get_available_architectures() architecture = request.GET.get('arch', None) if architecture.lower() not in architectures: return Message("Available architectures: {}".format( '|'.join(architectures))).as_json if isinstance(request.user, AnonymousUser) or not request.auth: return AuthRequiredSerializer().as_json hosts = Machine.api.filter(vm_dedicated_host=True, architecture__name=architecture) host = None for host_ in hosts: if host_.virtualization_api and ( host_.get_virtual_machines().count() < host_.vm_max): host = host_ break if host is None: return ErrorMessage("No virtual machine hosts left!").as_json form = VirtualMachineAPIForm( virtualization_api=host.virtualization_api) input = InputSerializer(form.as_dict(host), self.URL_POST.format(arch=architecture), form.get_order()) return input.as_json
def get(self, request, *args, **kwargs): """ Return reservation form for valid machine. """ fqdn = request.GET.get('fqdn', None) try: result = get_machine(fqdn, redirect_to='api:reserve', data=request.GET) if isinstance(result, Serializer): return result.as_json elif isinstance(result, HttpResponseRedirect): return result machine = result except Exception as e: return ErrorMessage(str(e)).as_json if isinstance(request.user, AnonymousUser) or not request.auth: return AuthRequiredSerializer().as_json form = ReserveMachineAPIForm(username=request.user.username, reason=machine.reserved_reason) input = InputSerializer(form.as_dict(), self.URL_POST.format(id=machine.pk), form.get_order()) return input.as_json
def get(self, request, *args, **kwargs): """ Show server configuration. """ if isinstance(request.user, AnonymousUser) or not request.auth: return AuthRequiredSerializer().as_json if not request.user.is_superuser: return ErrorMessage( "Only superusers are allowed to perform this action!").as_json config = ServerConfig.objects.all() if config.count() == 0: return InfoMessage("No configurations available.").as_json theader = [{'key': 'Key'}, {'value': 'Value'}] response = { 'header': { 'type': 'TABLE', 'theader': theader }, 'data': [] } for item in config: response['data'].append({'key': item.key, 'value': item.value}) return JsonResponse(response)
def get(self, request, *args, **kwargs): """ Release a machine. """ fqdn = request.GET.get('fqdn', None) try: result = get_machine(fqdn, redirect_to='api:release', data=request.GET) if isinstance(result, Serializer): return result.as_json elif isinstance(result, HttpResponseRedirect): return result machine = result except Exception as e: return ErrorMessage(str(e)).as_json if isinstance(request.user, AnonymousUser) or not request.auth: return AuthRequiredSerializer().as_json try: machine.release(user=request.user) return Message('OK.').as_json except Exception as e: return ErrorMessage(str(e)).as_json
def get(self, request, *args, **kwargs): """ Perform machine power cycle. """ fqdn = request.GET.get('fqdn', None) action = request.GET.get('action', None) if action.lower() not in RemotePower.Action.as_list: return ErrorMessage("Unknown action '{}'!".format(action)).as_json try: result = get_machine( fqdn, redirect_to='api:powercycle', data=request.GET ) if isinstance(result, Serializer): return result.as_json elif isinstance(result, HttpResponseRedirect): return result machine = result except Exception as e: return ErrorMessage(str(e)).as_json if isinstance(request.user, AnonymousUser) or not request.auth: return AuthRequiredSerializer().as_json if not machine.has_remotepower(): return ErrorMessage("Machine has no remote power!").as_json try: result = machine.powercycle(action.lower(), user=request.user) if action.lower() == RemotePower.Action.STATUS: return Message("Status: {} ({})".format( result.capitalize(), machine.remotepower.name )).as_json if result: return Message("OK.").as_json else: return ErrorMessage("Something went wrong!").as_json except Exception as e: return ErrorMessage(str(e)).as_json return ErrorMessage("Something went wrong!").as_json
def get(self, request, *args, **kwargs): """ Return form for adding a machine. """ if isinstance(request.user, AnonymousUser) or not request.auth: return AuthRequiredSerializer().as_json if not request.user.is_superuser: return ErrorMessage( "Only superusers are allowed to perform this action!").as_json form = MachineAPIForm() input = InputSerializer(form.as_dict(), self.URL_POST, form.get_order()) return input.as_json
def _setup(self, request, machine, distribution): """ Trigger machine setup for `machine` with `distribution`. """ if isinstance(request.user, AnonymousUser) or not request.auth: return AuthRequiredSerializer().as_json machinegroup = None if machine.group and not machine.group.setup_use_architecture: machinegroup = machine.group.name valid = machine.fqdn_domain.is_valid_setup_choice( distribution, machine.architecture.name, machinegroup=machinegroup) if not valid: return ErrorMessage( "Unknown choice '{}'! Use 'SETUP <fqdn> list'.".format( distribution)).as_json try: result = machine.setup(distribution) if result: message = "OK." if not machine.has_remotepower(): message += " This machine has no remote power - "\ "a manuall reboot may be required." return Message(message).as_json else: return ErrorMessage( "Machine has no setup capability! Please contact '{}'.". format(machine.get_support_contact())).as_json except Exception as e: return ErrorMessage(str(e)).as_json
def get(self, request, *args, **kwargs): """ Trigger regeneration of machine-related/service files. """ fqdn = request.GET.get('fqdn', None) option = request.GET.get('option', None) service = request.GET.get('service', None) if service and (service.lower() in [Regenerate.COBBLER, Regenerate.SERIALCONSOLE]): if isinstance(request.user, AnonymousUser) or not request.auth: return AuthRequiredSerializer().as_json if not request.user.is_superuser: return ErrorMessage( "Only superusers are allowed to perform this action!" ).as_json # regenerate Cobbler entries if service.lower() == Regenerate.COBBLER: signal_cobbler_regenerate.send(sender=None, domain_id=None) return Message("Regenerate Cobbler entries...").as_json # regenerate serial console entries iterating over all cscreen servers elif service.lower() == Regenerate.SERIALCONSOLE: machines = SerialConsole.objects.all().values_list( 'cscreen_server__fqdn', flat=True) for fqdn in machines.distinct(): signal_serialconsole_regenerate.send( sender=None, cscreen_server_fqdn=fqdn) return Message("Regenerate serial console entries...").as_json elif (fqdn is not None) and (option is not None): try: result = get_machine(fqdn, redirect_to='api:regenerate', data=request.GET) if isinstance(result, Serializer): return result.as_json elif isinstance(result, HttpResponseRedirect): return result machine = result except Exception as e: return ErrorMessage(str(e)).as_json if option.lower() not in [Regenerate.MOTD]: return ErrorMessage( "Unknown option '{}'!".format(option)).as_json if isinstance(request.user, AnonymousUser) or not request.auth: return AuthRequiredSerializer().as_json try: if option.lower() == Regenerate.MOTD: machine.update_motd(user=request.user) return Message("OK.").as_json except Exception as e: return ErrorMessage(str(e)).as_json return ErrorMessage("Unknown service '{}'!".format(service)).as_json