Skip to content

Commit be36ed6

Browse files
rameshg87jimrollenhagen
authored andcommitted
Add power_off command in standby extension
This commit adds a new command power_off to standby extension which runs shutdown -h now on the system. This commit also adds mappings for /proc and /sys in cloud-config.yml for the agent service spawned. Partial-Bug: #1451310 Change-Id: I2a5f984af26bbbe03002bb8c367c8c6af8d91434
1 parent 0a416af commit be36ed6

4 files changed

Lines changed: 44 additions & 8 deletions

File tree

imagebuild/coreos/oem/cloud-config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ coreos:
5858
--machine=ironic_python_agent \
5959
--bind=/dev:/dev \
6060
--bind=/dev/pts:/dev/pts \
61+
--bind=/proc:/proc \
62+
--bind=/sys:/sys \
6163
--bind=/usr/share/oem:/mnt \
6264
--user=root \
6365
--keep-unit \

ironic_python_agent/extensions/standby.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,21 @@ def prepare_image(self,
238238
return 'image ({0}) written to device {1}'.format(image_info['id'],
239239
device)
240240

241-
@base.async_command('run_image')
242-
def run_image(self):
243-
script = _path_to_script('shell/reboot.sh')
244-
LOG.info('Rebooting system')
245-
command = ['/bin/bash', script]
241+
def _run_shutdown_script(self, parameter):
242+
script = _path_to_script('shell/shutdown.sh')
243+
command = ['/bin/bash', script, parameter]
246244
# this should never return if successful
247245
try:
248246
stdout, stderr = utils.execute(*command, check_exit_code=[0])
249247
except processutils.ProcessExecutionError as e:
250248
raise errors.SystemRebootError(e.exit_code, e.stdout, e.stderr)
249+
250+
@base.async_command('run_image')
251+
def run_image(self):
252+
LOG.info('Rebooting system')
253+
self._run_shutdown_script('-r')
254+
255+
@base.async_command('power_off')
256+
def power_off(self):
257+
LOG.info('Powering off system')
258+
self._run_shutdown_script('-h')
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ set -e
2222
echo "1" > /proc/sys/kernel/sysrq
2323

2424
echo "s" > /proc/sysrq-trigger
25-
echo "b" > /proc/sysrq-trigger
25+
if [[ $1 = '-h' ]]; then
26+
echo "o" > /proc/sysrq-trigger
27+
elif [[ $1 = '-r' ]]; then
28+
echo "b" > /proc/sysrq-trigger
29+
fi

ironic_python_agent/tests/extensions/standby.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ def test_prepare_image_no_configdrive(self,
451451

452452
@mock.patch('ironic_python_agent.utils.execute', autospec=True)
453453
def test_run_image(self, execute_mock):
454-
script = standby._path_to_script('shell/reboot.sh')
455-
command = ['/bin/bash', script]
454+
script = standby._path_to_script('shell/shutdown.sh')
455+
command = ['/bin/bash', script, '-r']
456456
execute_mock.return_value = ('', '')
457457

458458
success_result = self.agent_extension.run_image()
@@ -474,3 +474,25 @@ def test_run_image(self, execute_mock):
474474
def test_path_to_script(self):
475475
script = standby._path_to_script('shell/reboot.sh')
476476
self.assertTrue(script.endswith('extensions/../shell/reboot.sh'))
477+
478+
@mock.patch('ironic_python_agent.utils.execute', autospec=True)
479+
def test_power_off(self, execute_mock):
480+
script = standby._path_to_script('shell/shutdown.sh')
481+
command = ['/bin/bash', script, '-h']
482+
execute_mock.return_value = ('', '')
483+
484+
success_result = self.agent_extension.power_off()
485+
success_result.join()
486+
487+
execute_mock.assert_called_once_with(*command, check_exit_code=[0])
488+
self.assertEqual('SUCCEEDED', success_result.command_status)
489+
490+
execute_mock.reset_mock()
491+
execute_mock.return_value = ('', '')
492+
execute_mock.side_effect = processutils.ProcessExecutionError
493+
494+
failed_result = self.agent_extension.power_off()
495+
failed_result.join()
496+
497+
execute_mock.assert_called_once_with(*command, check_exit_code=[0])
498+
self.assertEqual('FAILED', failed_result.command_status)

0 commit comments

Comments
 (0)