Commit ff1f0b5e authored by Peter Chronz's avatar Peter Chronz
Browse files

Implement IOcm plugin

parent 8fcc526b
# -*- coding: utf-8 -*-
import logging
import importlib
import os
from actuator.core.exceptions import ActuatorException
logger = logging.getLogger(__name__)
......@@ -39,3 +42,8 @@ class AbstractPlugin(object):
def _get_package_name(self):
raise NotImplementedError()
def verify_is_root(self):
uid = os.getuid()
if uid != 0:
raise ActuatorException('Need to be root for this plugin.')
# -*- coding: utf-8 -*-
import logging
import subprocess32
import os.path
import actuator.core.settings as settings
from actuator.core.exceptions import ActuatorException
from actuator.core.plugins import AbstractPlugin
logger = logging.getLogger(__name__)
class IOcm(AbstractPlugin):
def activate_config(self, config):
self.verify_is_root()
iocm_method_name = '_turn_iocm_{}'.format(config)
iocm_method = getattr(self, iocm_method_name)
iocm_method()
def _turn_iocm_on(self):
self._call_iocm_script('start')
def _call_iocm_script(self, script_name):
iocm_base_path = settings.settings['plugins']['iocm']['base_path']
iocm_start_script_name = '{}_io_manager.py'.format(script_name)
iocm_start_path = os.path.join(iocm_base_path, iocm_start_script_name)
iocm_command = [iocm_start_path]
subprocess32.check_output(iocm_command)
def _turn_iocm_off(self):
self._call_iocm_script('stop')
def get_state(self, **kwargs):
raise ActuatorException(u'IOcm does not offer a way to read it\'s state.')
def _get_package_name(self):
return 'iocm'
plugin = IOcm()
# -*- coding: utf-8 -*-
import logging
import os
import re
from actuator.core.exceptions import ActuatorException
import actuator.core.settings as settings
from actuator.core.plugins import AbstractPlugin
......@@ -13,7 +11,7 @@ logger = logging.getLogger(__name__)
class IOScheduler(AbstractPlugin):
def activate_config(self, config):
self._verify_is_root()
self.verify_is_root()
block_devices = settings.settings['plugins']['ioscheduler']['devices']
for block_device in block_devices:
self._activate_config_on_device(config, block_device)
......@@ -29,11 +27,6 @@ class IOScheduler(AbstractPlugin):
with open(path, 'w') as scheduler_file:
scheduler_file.write(value)
def _verify_is_root(self):
uid = os.getuid()
if uid != 0:
raise ActuatorException('Need to be root for this plugin.')
def get_state(self, **kwargs):
active_scheduler = self._get_active_scheduler()
return active_scheduler
......
......@@ -2,3 +2,5 @@ plugins:
ioscheduler:
devices: [sda]
taskset:
iocm:
base_path: '/var/lib/iocm'
# -*- coding: utf-8 -*-
import unittest
import mock
import logging
import actuator.plugins.iocm as iocm
import actuator.core.plugins
from actuator.core import settings
from actuator.core.exceptions import ActuatorException
logger = logging.getLogger(__name__)
def check_output_mock(args):
if args[0] == 'taskset':
if '4045' in args:
return 'pid 4045\'s current affinity mask: 42'
elif '4046' in args:
return 'pid 4046\'s current affinity mask: 43'
else:
logger.error('panic!', args)
elif args[0] == 'pgrep':
return '4045\n4046\n'
class IOcmTest(unittest.TestCase):
settings_path = 'actuator/tests/resources/slave-settings.yaml'
def setUp(self):
self.settings = settings.load_settings(self.settings_path)
def test_is_plugin(self):
self.assertIsInstance(iocm.plugin, actuator.core.plugins.AbstractPlugin)
def test_get_state(self):
with self.assertRaises(ActuatorException):
iocm.plugin.get_state()
@mock.patch('subprocess32.check_output')
def test_start_iocm(self, output_mock):
state = 'on'
iocm.plugin.activate_config(state)
output_mock.assert_called_with(['/var/lib/iocm/start_io_manager.py'])
@mock.patch('subprocess32.check_output')
def test_stop_iocm(self, output_mock):
state = 'off'
iocm.plugin.activate_config(state)
output_mock.assert_called_with(['/var/lib/iocm/stop_io_manager.py'])
def test_get_name(self):
name = iocm.plugin.name
self.assertEqual(name, 'iocm')
......@@ -5,6 +5,7 @@ from contextlib import contextmanager
import actuator.core.plugins as plugins
import actuator.core.settings as settings
from actuator.core.exceptions import ActuatorException
class PluginTest(unittest.TestCase):
......@@ -53,3 +54,14 @@ class AbstractPluginTest(unittest.TestCase):
def test_get_package_name(self):
with self._test_method() as plugin:
plugin._get_package_name()
@mock.patch('os.getuid', return_value=1)
def test_verify_is_not_root(self, getuid_mock):
plugin = plugins.AbstractPlugin()
with self.assertRaises(ActuatorException):
plugin.verify_is_root()
@mock.patch('os.getuid', return_value=0)
def test_verify_is_root(self, getuid_mock):
plugin = plugins.AbstractPlugin()
plugin.verify_is_root()
......@@ -46,7 +46,7 @@ def is_rabbitmq_running():
def stop_rabbitmq():
subprocess32.check_call(['killall', '-u', 'rabbitmq'])
subprocess32.call(['killall', '-u', 'rabbitmq'])
class SlaveAgentsMock(object):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment