def execute(self, osystem=None, distro_series=None, hwe_kernel=None): """See `NodeAction.execute`.""" if self.node.owner is None: with locks.node_acquire: self.node.acquire(self.user, token=None) if osystem and distro_series: try: self.node.osystem, self.node.distro_series = ( validate_osystem_and_distro_series(osystem, distro_series)) self.node.save() except ValidationError as e: raise NodeActionError(e) try: self.node.hwe_kernel = validate_hwe_kernel( hwe_kernel, self.node.min_hwe_kernel, self.node.architecture, self.node.osystem, self.node.distro_series) self.node.save() except ValidationError as e: raise NodeActionError(e) try: get_curtin_config(self.node) except Exception as e: raise NodeActionError("Failed to retrieve curtin config: %s" % e) try: self.node.start(self.user) except StaticIPAddressExhaustion: raise NodeActionError( "%s: Failed to start, static IP addresses are exhausted." % self.node.hostname) except RPC_EXCEPTIONS + (ExternalProcessError, ) as exception: raise NodeActionError(exception)
def _execute(self): """See `NodeAction.execute`.""" try: self.node.start(self.user) except StaticIPAddressExhaustion: raise NodeActionError( "%s: Failed to start, static IP addresses are exhausted." % self.node.hostname) except RPC_EXCEPTIONS + (ExternalProcessError, ) as exception: raise NodeActionError(exception)
def _execute(self): """See `NodeAction.execute`.""" if self.node.power_state == POWER_STATE.ON: raise NodeActionError( "Unable to be mark fixed because the power is currently on.") if not self.has_commissioning_data(): raise NodeActionError( "Unable to be mark fixed because it has not been commissioned " "successfully.") self.node.mark_fixed(self.user)
def _execute(self): """See `NodeAction.execute`.""" with locks.node_acquire: try: self.node.acquire(self.user, token=None) except ValidationError as e: raise NodeActionError(e)
def _execute(self): """See `NodeAction.execute`.""" try: post_commit_do(RackControllersImporter.schedule, self.node.system_id) except RPC_EXCEPTIONS as exception: raise NodeActionError(exception)
def _execute(self, enable_ssh=False, testing_scripts=[]): try: self.node.start_testing(self.user, enable_ssh=enable_ssh, testing_scripts=testing_scripts) except RPC_EXCEPTIONS + (ExternalProcessError, ) as exception: raise NodeActionError(exception)
def _execute(self): """See `NodeAction.execute`.""" if not self.has_commissioning_data(): raise NodeActionError( "Unable to be mark fixed because it has not been commissioned " "successfully.") self.node.mark_fixed(self.user)
def execute(self, erase=False, secure_erase=False, quick_erase=False): """See `NodeAction.execute`.""" try: self.node.release_or_erase( self.user, erase=erase, secure_erase=secure_erase, quick_erase=quick_erase) except RPC_EXCEPTIONS + (ExternalProcessError,) as exception: raise NodeActionError(exception)
def test_Deploy_raises_NodeActionError_for_no_curtin_config(self): user = factory.make_User() node = factory.make_Node(interface=True, status=NODE_STATUS.ALLOCATED, power_type='manual', owner=user) mock_get_curtin_config = self.patch(node_action_module, 'get_curtin_config') mock_get_curtin_config.side_effect = NodeActionError('error') error = self.assertRaises(NodeActionError, Deploy(node, user).execute) self.assertEqual("Failed to retrieve curtin config: error", str(error))
def action(self, params): """Perform the action on the object.""" obj = self.get_object(params) action_name = params.get("action") actions = compile_node_actions(obj, self.user, request=self.request) action = actions.get(action_name) if action is None: raise NodeActionError("%s action is not available for this node." % action_name) extra_params = params.get("extra", {}) return action.execute(**extra_params)
def action(self, params): """Perform the action on the object.""" # `compile_node_actions` handles the permission checking internally # the default view permission check is enough at this level. obj = self.get_object(params) action_name = params.get("action") actions = compile_node_actions(obj, self.user, request=self.request) action = actions.get(action_name) if action is None: raise NodeActionError( "%s action is not available for this node." % action_name) extra_params = params.get("extra", {}) return action.execute(**extra_params)
def _execute( self, destinations=None, storage=False, interfaces=False, _error_data=None, ): data = { "source": self.node, "destinations": destinations, "storage": storage, "interfaces": interfaces, } form = CloneForm(self.user, data=data) if form.has_error("destinations", "storage") or form.has_error( "destinations", "network"): # Try again with all the bad destinations removed new_destinations = form.strip_failed_destinations() if new_destinations: return self._execute( destinations=new_destinations, storage=storage, interfaces=interfaces, _error_data=form.errors, ) if not form.is_valid(): raise NodeActionError(self._format_errors_as_json(form.errors)) try: form.save() except ValidationError as exc: raise NodeActionError(self._format_errors_as_json(exc.errors)) if _error_data: for name, error_list in _error_data.items(): if name in form.errors: form.errors[name].extend(error_list) else: form.errors[name] = error_list raise NodeActionError(self._format_errors_as_json(form.errors))
def execute( self, enable_ssh=False, skip_networking=False, skip_storage=False, commissioning_scripts=[], testing_scripts=[]): """See `NodeAction.execute`.""" try: self.node.start_commissioning( self.user, enable_ssh=enable_ssh, skip_networking=skip_networking, skip_storage=skip_storage, commissioning_scripts=commissioning_scripts, testing_scripts=testing_scripts) except RPC_EXCEPTIONS + (ExternalProcessError, ValidationError) as e: raise NodeActionError(e)
def test_perform_action_catches_start_action_errors(self): error_text = factory.make_string(prefix="NodeActionError") exc = NodeActionError(error_text) self.patch(PowerOn, "execute").side_effect = exc with transaction.atomic(): user = factory.make_User() factory.make_SSHKey(user) node = factory.make_Node(status=NODE_STATUS.READY, owner=user) form = BulkNodeActionForm(user=user, data=dict(action=PowerOn.name, system_id=[node.system_id])) self.assertTrue(form.is_valid(), form._errors) with transaction.atomic(): done, not_actionable, not_permitted = form.save() self.assertEqual([0, 1, 0], [done, not_actionable, not_permitted])
def execute(self, osystem=None, distro_series=None, hwe_kernel=None): """See `NodeAction.execute`.""" if self.node.owner is None: with locks.node_acquire: try: self.node.acquire(self.user, token=None) except ValidationError as e: raise NodeActionError(e) if osystem and distro_series: try: self.node.osystem, self.node.distro_series = ( validate_osystem_and_distro_series(osystem, distro_series)) self.node.save() except ValidationError as e: raise NodeActionError(e) try: self.node.hwe_kernel = validate_hwe_kernel( hwe_kernel, self.node.min_hwe_kernel, self.node.architecture, self.node.osystem, self.node.distro_series) self.node.save() except ValidationError as e: raise NodeActionError(e) request = self.request if request is None: # Being called from the websocket, just to ensure that the curtin # configuration is valid. The request object does not need to be # an actual request. 'SERVER_NAME' and 'SERVER_PORT' are required # so `build_absolure_uri` can create an actual absolute URI. request = HttpRequest() request.META['SERVER_NAME'] = 'localhost' request.META['SERVER_PORT'] = 5248 try: get_curtin_config(request, self.node) except Exception as e: raise NodeActionError("Failed to retrieve curtin config: %s" % e) try: self.node.start(self.user) except StaticIPAddressExhaustion: raise NodeActionError( "%s: Failed to start, static IP addresses are exhausted." % self.node.hostname) except RPC_EXCEPTIONS + (ExternalProcessError, ) as exception: raise NodeActionError(exception)
def _execute(self): """See `NodeAction.execute`.""" try: self.node.stop_rescue_mode(self.user) except RPC_EXCEPTIONS + (ExternalProcessError, ) as exception: raise NodeActionError(exception)
def _execute(self, osystem=None, distro_series=None, hwe_kernel=None, install_kvm=False): """See `NodeAction.execute`.""" if install_kvm: if not self.user.is_superuser: raise NodeActionError( "You must be a MAAS administrator to deploy a machine " "as a MAAS-managed KVM Pod.") if self.node.owner is None: with locks.node_acquire: try: bridge_all = True if install_kvm else False self.node.acquire(self.user, token=None, bridge_all=bridge_all) except ValidationError as e: raise NodeActionError(e) if install_kvm: try: # KVM Pod installation should default to ubuntu/bionic, since # that was the release it was tested on. if osystem is None: osystem = 'ubuntu' if distro_series is None: distro_series = 'bionic' self.node.osystem, self.node.distro_series = ( validate_osystem_and_distro_series(osystem, distro_series)) self.node.install_kvm = True self.node.save() except ValidationError as e: raise NodeActionError(e) elif osystem and distro_series: try: self.node.osystem, self.node.distro_series = ( validate_osystem_and_distro_series(osystem, distro_series)) self.node.save() except ValidationError as e: raise NodeActionError(e) try: self.node.hwe_kernel = validate_hwe_kernel( hwe_kernel, self.node.min_hwe_kernel, self.node.architecture, self.node.osystem, self.node.distro_series) self.node.save() except ValidationError as e: raise NodeActionError(e) request = self.request if request is None: # `compile_node_actions` is the path by which the node # actions are instantiated. There are other places within the # code that call compile_node_actions without a request object. # In this event, and for future uses of these node actions without # a request being passed in, we need to create one here. # 'SERVER_NAME' and 'SERVER_PORT' are required so # `build_absolure_uri` can create an actual absolute URI so that # the curtin configuration is valid. request = HttpRequest() request.META['SERVER_NAME'] = 'localhost' request.META['SERVER_PORT'] = 5248 try: get_curtin_config(request, self.node) except Exception as e: raise NodeActionError("Failed to retrieve curtin config: %s" % e) try: self.node.start(self.user) except StaticIPAddressExhaustion: raise NodeActionError( "%s: Failed to start, static IP addresses are exhausted." % self.node.hostname) except RPC_EXCEPTIONS + (ExternalProcessError, ) as exception: raise NodeActionError(exception)
def _execute( self, osystem=None, distro_series=None, hwe_kernel=None, install_kvm=False, user_data=None, ): """See `NodeAction.execute`.""" if install_kvm: if not self.user.is_superuser: raise NodeActionError( "You must be a MAAS administrator to deploy a machine " "as a MAAS-managed KVM Pod.") if self.node.owner is None: with locks.node_acquire: try: self.node.acquire(self.user) except ValidationError as e: raise NodeActionError(e) if osystem and distro_series: try: ( self.node.osystem, self.node.distro_series, ) = validate_osystem_and_distro_series(osystem, distro_series) self.node.save() except ValidationError as e: raise NodeActionError(e) else: configs = Config.objects.get_configs( ["default_osystem", "default_distro_series"]) self.node.osystem = configs["default_osystem"] self.node.distro_series = configs["default_distro_series"] self.node.save() try: self.node.hwe_kernel = validate_hwe_kernel( hwe_kernel, self.node.min_hwe_kernel, self.node.architecture, self.node.osystem, self.node.distro_series, ) self.node.save() except ValidationError as e: raise NodeActionError(e) user_data = user_data.encode() if user_data else None request = self.request if request is None: # `compile_node_actions` is the path by which the node # actions are instantiated. There are other places within the # code that call compile_node_actions without a request object. # In this event, and for future uses of these node actions without # a request being passed in, we need to create one here. # 'SERVER_NAME' and 'SERVER_PORT' are required so # `build_absolure_uri` can create an actual absolute URI so that # the curtin configuration is valid. request = HttpRequest() request.META["SERVER_NAME"] = "localhost" request.META["SERVER_PORT"] = 5248 try: get_curtin_config(request, self.node) except Exception as e: raise NodeActionError("Failed to retrieve curtin config: %s" % e) try: self.node.start(self.user, user_data=user_data, install_kvm=install_kvm) except StaticIPAddressExhaustion: raise NodeActionError( "%s: Failed to start, static IP addresses are exhausted." % self.node.hostname) except IPAddressCheckFailed: raise NodeActionError( f"{self.node.hostname}: Failed to start, IP addresses check failed." ) except RPC_EXCEPTIONS + (ExternalProcessError, ) as exception: raise NodeActionError(exception)
def _execute(self): """See `NodeAction.execute`.""" try: self.node.abort_operation(self.user) except RPC_EXCEPTIONS + (ExternalProcessError, ) as exception: raise NodeActionError(exception)