def _run_shutdown_command(self, command): """Run the shutdown or reboot command :param command: A string having the command to be run. :raises: InvalidCommandParamsError if the passed command is not equal to poweroff or reboot. :raises: SystemRebootError if the command errors out with an unsuccessful exit code. """ if command not in ('reboot', 'poweroff'): msg = (('Expected the command "poweroff" or "reboot" ' 'but received "%s".') % command) raise errors.InvalidCommandParamsError(msg) try: self.sync() except errors.CommandExecutionError as e: LOG.warning('Failed to sync file system buffers: % s', e) try: _, stderr = utils.execute(command, use_standard_locale=True, check_exit_code=[0]) if 'ignoring request.' in stderr: LOG.debug( '%s command failed with error %s, ' 'falling back to sysrq-trigger.', command, stderr) if command == 'poweroff': utils.execute("echo o > /proc/sysrq-trigger", shell=True) elif command == 'reboot': utils.execute("echo b > /proc/sysrq-trigger", shell=True) except processutils.ProcessExecutionError as e: raise errors.SystemRebootError(e.exit_code, e.stdout, e.stderr)
def _run_shutdown_script(self, parameter): script = _path_to_script('shell/shutdown.sh') command = ['/bin/bash', script, parameter] # this should never return if successful try: stdout, stderr = utils.execute(*command, check_exit_code=[0]) except processutils.ProcessExecutionError as e: raise errors.SystemRebootError(e.exit_code, e.stdout, e.stderr)
def run_image(self): script = _path_to_script('shell/reboot.sh') LOG.info('Rebooting system') command = ['/bin/bash', script] # this should never return if successful try: stdout, stderr = utils.execute(*command, check_exit_code=[0]) except processutils.ProcessExecutionError as e: raise errors.SystemRebootError(e.exit_code, e.stdout, e.stderr)
def _run_shutdown_command(self, command): """Run the shutdown or reboot command :param command: A string having the command to be run. :raises: InvalidCommandParamsError if the passed command is not equal to poweroff or reboot. :raises: SystemRebootError if the command errors out with an unsuccessful exit code. """ # TODO(TheJulia): When we have deploy/clean steps, we should remove # this upon shutdown. The clock sync deploy step can run before # completing other operations. self._sync_clock(ignore_errors=True) if command not in ('reboot', 'poweroff'): msg = (('Expected the command "poweroff" or "reboot" ' 'but received "%s".') % command) raise errors.InvalidCommandParamsError(msg) try: self.sync() except errors.CommandExecutionError as e: LOG.warning('Failed to sync file system buffers: % s', e) try: _, stderr = utils.execute(command, use_standard_locale=True) except processutils.ProcessExecutionError as e: LOG.warning('%s command failed with error %s, ' 'falling back to sysrq-trigger', command, e) else: if 'ignoring request' in stderr: LOG.warning('%s command has been ignored, ' 'falling back to sysrq-trigger', command) else: return try: if command == 'poweroff': utils.execute("echo o > /proc/sysrq-trigger", shell=True) elif command == 'reboot': utils.execute("echo b > /proc/sysrq-trigger", shell=True) except processutils.ProcessExecutionError as e: raise errors.SystemRebootError(e.exit_code, e.stdout, e.stderr)
def _run_shutdown_script(self, parameter): """Runs the agent's shutdown script with the specified parameter. The script only takes the following parameters: -h : 'halts' the machine by turning the power off. -r : 'runs' the installed image by rebooting the machine. Only one parameter should be specified. :param parameter: The parameter to send to the shutdown script. :raises: SystemRebootError if calling the shutdown script returns an unsuccessful exit code. """ script = _path_to_script('shell/shutdown.sh') command = ['/bin/bash', script, parameter] # this should never return if successful try: stdout, stderr = utils.execute(*command, check_exit_code=[0]) except processutils.ProcessExecutionError as e: raise errors.SystemRebootError(e.exit_code, e.stdout, e.stderr)
def test_error_classes(self): cases = [ (errors.InvalidContentError(DETAILS), SAME_DETAILS), (errors.NotFound(), SAME_CL_DETAILS), (errors.CommandExecutionError(DETAILS), SAME_DETAILS), (errors.InvalidCommandError(DETAILS), SAME_DETAILS), (errors.InvalidCommandParamsError(DETAILS), SAME_DETAILS), (errors.RequestedObjectNotFoundError('type_descr', 'obj_id'), DIFF_CL_DETAILS), (errors.IronicAPIError(DETAILS), SAME_DETAILS), (errors.HeartbeatError(DETAILS), SAME_DETAILS), (errors.LookupNodeError(DETAILS), SAME_DETAILS), (errors.LookupAgentIPError(DETAILS), SAME_DETAILS), (errors.LookupAgentInterfaceError(DETAILS), SAME_DETAILS), (errors.ImageDownloadError('image_id', DETAILS), DIFF_CL_DETAILS), (errors.ImageChecksumError('image_id', '/foo/image_id', 'incorrect', 'correct'), DIFF_CL_DETAILS), (errors.ImageWriteError('device', 'exit_code', 'stdout', 'stderr'), DIFF_CL_DETAILS), (errors.ConfigDriveTooLargeError('filename', 'filesize'), DIFF_CL_DETAILS), (errors.ConfigDriveWriteError('device', 'exit_code', 'stdout', 'stderr'), DIFF_CL_DETAILS), (errors.SystemRebootError('exit_code', 'stdout', 'stderr'), DIFF_CL_DETAILS), (errors.BlockDeviceEraseError(DETAILS), SAME_DETAILS), (errors.BlockDeviceError(DETAILS), SAME_DETAILS), (errors.VirtualMediaBootError(DETAILS), SAME_DETAILS), (errors.UnknownNodeError(), DEFAULT_DETAILS), (errors.UnknownNodeError(DETAILS), SAME_DETAILS), (errors.HardwareManagerNotFound(), DEFAULT_DETAILS), (errors.HardwareManagerNotFound(DETAILS), SAME_DETAILS), (errors.HardwareManagerMethodNotFound('method'), DIFF_CL_DETAILS), (errors.IncompatibleHardwareMethodError(), DEFAULT_DETAILS), (errors.IncompatibleHardwareMethodError(DETAILS), SAME_DETAILS), ] for (obj, check_details) in cases: self._test_class(obj, check_details)