add run_image command

This commit is contained in:
Jim Rollenhagen 2014-01-17 15:59:56 -08:00
parent dbd3365a85
commit c8940da83c
4 changed files with 53 additions and 7 deletions

View File

@ -93,3 +93,14 @@ class ImageWriteError(errors.RESTError):
super(ImageWriteError, self).__init__() super(ImageWriteError, self).__init__()
self.details = 'Writing image to device {} failed with exit code {}.' self.details = 'Writing image to device {} failed with exit code {}.'
self.details = self.details.format(device, 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)

View File

@ -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

View File

@ -40,11 +40,15 @@ def _write_local_config_drive(location, data):
f.write(json_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): def _write_image(image_info, configdrive_dir, device):
image = _image_location(image_info) image = _image_location(image_info)
cwd = os.path.dirname(os.path.realpath(__file__)) script = _path_to_script('shell/makefs.sh')
script = os.path.join(cwd, 'shell/makefs.sh')
command = ['/bin/bash', script, configdrive_dir, image, device] command = ['/bin/bash', script, configdrive_dir, image, device]
exit_code = subprocess.call(command) exit_code = subprocess.call(command)
@ -95,6 +99,15 @@ def _verify_image(image_info, image_location):
return False 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): class CacheImagesCommand(base.AsyncCommandResult):
def execute(self): def execute(self):
# TODO(russellhaering): Actually cache images # TODO(russellhaering): Actually cache images
@ -116,8 +129,7 @@ class PrepareImageCommand(base.AsyncCommandResult):
class RunImageCommand(base.AsyncCommandResult): class RunImageCommand(base.AsyncCommandResult):
def execute(self): def execute(self):
# TODO(jimrollenhagen): Actually run image, reboot/kexec/whatever _run_image()
pass
class StandbyAgent(base.BaseTeethAgent): class StandbyAgent(base.BaseTeethAgent):

View File

@ -16,7 +16,6 @@ limitations under the License.
import json import json
import mock import mock
import os
import unittest import unittest
from teeth_agent import errors from teeth_agent import errors
@ -133,8 +132,7 @@ class TestBaseTeethAgent(unittest.TestCase):
configdrive = 'configdrive' configdrive = 'configdrive'
device = '/dev/sda' device = '/dev/sda'
location = standby._image_location(image_info) location = standby._image_location(image_info)
standby_dir = os.path.dirname(os.path.realpath(standby.__file__)) script = standby._path_to_script('shell/makefs.sh')
script = os.path.join(standby_dir, 'shell/makefs.sh')
command = ['/bin/bash', script, configdrive, location, device] command = ['/bin/bash', script, configdrive, location, device]
call_mock.return_value = 0 call_mock.return_value = 0
@ -226,3 +224,20 @@ class TestBaseTeethAgent(unittest.TestCase):
verified = standby._verify_image(image_info, image_location) verified = standby._verify_image(image_info, image_location)
self.assertFalse(verified) self.assertFalse(verified)
self.assertEqual(md5_mock.call_count, 1) 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)