def clean(self): """Add the machine to cleaned data for further processing.""" cleaned_data = super(RemotePowerAPIForm, self).clean() cleaned_data['machine'] = self.machine remotepower = RemotePower(**cleaned_data) remotepower.clean() return cleaned_data
def post(self, request, *args, **kwargs): """Add remote power to machine.""" if not request.user.is_superuser: return ErrorMessage( "Only superusers are allowed to perform this action!").as_json try: fqdn = request.path.split("/")[-1] 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 machine.has_remotepower(): return InfoMessage("Machine has already a remote power.").as_json data = json.loads(request.body.decode('utf-8'))['form'] form = RemotePowerAPIForm(data, machine=machine) if form.is_valid(): try: remotepower = RemotePower(**form.cleaned_data) remotepower.save() except Exception as e: logger.exception(e) return ErrorMessage("Something went wrong!").as_json return Message('Ok.').as_json return ErrorMessage("\n{}".format( format_cli_form_errors(form))).as_json
def create(self, *args, **kwargs): """ Create a virtual machine. Method returns a new `Machine` object and calls the subclass to actually create the virtual machine physically. """ from orthos2.data.models import (Architecture, Machine, RemotePower, SerialConsole, SerialConsoleType, System) from django.contrib.auth.models import User vm = Machine() vm.unsaved_networkinterfaces = [] vm.architecture = Architecture.objects.get(name=kwargs['architecture']) vm.system = System.objects.get(pk=kwargs['system']) self._create(vm, *args, **kwargs) vm.mac_address = vm.unsaved_networkinterfaces[0].mac_address vm.check_connectivity = Machine.Connectivity.ALL vm.collect_system_information = True vm.save() for networkinterface in vm.unsaved_networkinterfaces[1:]: networkinterface.machine = vm networkinterface.save() vm.remotepower = RemotePower(fence_name="virsh") vm.remotepower.save() if self.host.has_serialconsole(): stype = SerialConsoleType.objects.get(name='libvirt/qemu') if not stype: raise Exception("Bug: SerialConsoleType not found") vm.serialconsole = SerialConsole(stype=stype, baud_rate=115200) vm.serialconsole.save() if vm.vnc['enabled']: vm.annotations.create(text='VNC enabled: {}:{}'.format( self.host.fqdn, vm.vnc['port']), reporter=User.objects.get(username='******')) return vm