From c8940da83c54b18126133f7fb54271e40d56ec02 Mon Sep 17 00:00:00 2001 From: Jim Rollenhagen Date: Fri, 17 Jan 2014 15:59:56 -0800 Subject: [PATCH] add run_image command --- teeth_agent/errors.py | 11 +++++++++++ teeth_agent/shell/reboot.sh | 8 ++++++++ teeth_agent/standby.py | 20 ++++++++++++++++---- teeth_agent/tests/standby_agent.py | 21 ++++++++++++++++++--- 4 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 teeth_agent/shell/reboot.sh diff --git a/teeth_agent/errors.py b/teeth_agent/errors.py index ccf911ff5..07e4e8467 100644 --- a/teeth_agent/errors.py +++ b/teeth_agent/errors.py @@ -93,3 +93,14 @@ class ImageWriteError(errors.RESTError): super(ImageWriteError, self).__init__() self.details = 'Writing image to device {} failed with exit code {}.' self.details = self.details.format(device, exit_code) + + +class SystemRebootError(errors.RESTError): + """Error raised when a system cannot reboot.""" + + message = 'Error rebooting system.' + + def __init__(self, exit_code): + super(SystemRebootError, self).__init__() + self.details = 'Reboot script failed with exit code {}.' + self.details = self.details.format(exit_code) diff --git a/teeth_agent/shell/reboot.sh b/teeth_agent/shell/reboot.sh new file mode 100644 index 000000000..ef9832f5a --- /dev/null +++ b/teeth_agent/shell/reboot.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# +# This script reboots by echoing into /proc/sysrq_trigger. + +set -e + +echo "s" > /proc/sysrq-trigger +echo "b" > /proc/sysrq-trigger diff --git a/teeth_agent/standby.py b/teeth_agent/standby.py index ef748d296..308ec9809 100644 --- a/teeth_agent/standby.py +++ b/teeth_agent/standby.py @@ -40,11 +40,15 @@ def _write_local_config_drive(location, data): f.write(json_data) +def _path_to_script(script): + cwd = os.path.dirname(os.path.realpath(__file__)) + return os.path.join(cwd, script) + + def _write_image(image_info, configdrive_dir, device): image = _image_location(image_info) - cwd = os.path.dirname(os.path.realpath(__file__)) - script = os.path.join(cwd, 'shell/makefs.sh') + script = _path_to_script('shell/makefs.sh') command = ['/bin/bash', script, configdrive_dir, image, device] exit_code = subprocess.call(command) @@ -95,6 +99,15 @@ def _verify_image(image_info, image_location): return False +def _run_image(): + script = _path_to_script('shell/reboot.sh') + command = ['/bin/bash', script] + # this should never return if successful + exit_code = subprocess.call(command) + if exit_code != 0: + raise errors.SystemRebootError(exit_code) + + class CacheImagesCommand(base.AsyncCommandResult): def execute(self): # TODO(russellhaering): Actually cache images @@ -116,8 +129,7 @@ class PrepareImageCommand(base.AsyncCommandResult): class RunImageCommand(base.AsyncCommandResult): def execute(self): - # TODO(jimrollenhagen): Actually run image, reboot/kexec/whatever - pass + _run_image() class StandbyAgent(base.BaseTeethAgent): diff --git a/teeth_agent/tests/standby_agent.py b/teeth_agent/tests/standby_agent.py index cf51fc7b2..00cd8dda9 100644 --- a/teeth_agent/tests/standby_agent.py +++ b/teeth_agent/tests/standby_agent.py @@ -16,7 +16,6 @@ limitations under the License. import json import mock -import os import unittest from teeth_agent import errors @@ -133,8 +132,7 @@ class TestBaseTeethAgent(unittest.TestCase): configdrive = 'configdrive' device = '/dev/sda' location = standby._image_location(image_info) - standby_dir = os.path.dirname(os.path.realpath(standby.__file__)) - script = os.path.join(standby_dir, 'shell/makefs.sh') + script = standby._path_to_script('shell/makefs.sh') command = ['/bin/bash', script, configdrive, location, device] call_mock.return_value = 0 @@ -226,3 +224,20 @@ class TestBaseTeethAgent(unittest.TestCase): verified = standby._verify_image(image_info, image_location) self.assertFalse(verified) self.assertEqual(md5_mock.call_count, 1) + + @mock.patch('subprocess.call', autospec=True) + def test_run_image(self, call_mock): + script = standby._path_to_script('shell/reboot.sh') + command = ['/bin/bash', script] + call_mock.return_value = 0 + + standby._run_image() + call_mock.assert_called_once_with(command) + + call_mock.reset_mock() + call_mock.return_value = 1 + + self.assertRaises(errors.SystemRebootError, + standby._run_image) + + call_mock.assert_called_once_with(command)