def mock_list_params(self, path): fl = glob(path) if fl: daemon_log.info("mock_list_params: " + path) return fl else: raise AgentShell.CommandExecutionError( AgentShell.RunResult( 2, "", "error: get_param: param_path '" + path + "': No such file or directory", 0, ), ["lctl", "get_param", "-N", path], )
def _configure_target_priority(primary, ha_label, node): if primary: score = "20" else: score = "10" name = _constraint(ha_label, primary) constraint = ET.Element( "rsc_location", {"id": name, "node": node, "rsc": ha_label, "score": score} ) result = cibcreate("constraints", ET.tostring(constraint)) if result.rc == errno.ENOTUNIQ: console_log.warn("A constraint with the name %s already exists", name) result = AgentShell.RunResult(0, result.stdout, result.stderr, result.timeout) return result
def _configure_target_ha(ha_label, info, enabled=False): if enabled: extra = [] else: extra = ["--disabled"] xmlid = ha_label res = _resource_xml( ha_label, "ocf:lustre:Lustre", { "target": info["bdev"], "mountpoint": info["mntpt"] }, ) if info["device_type"] == "zfs": xmlid = _group_name(ha_label) grp = ET.Element("group", {"id": xmlid}) zpool = info["bdev"].split("/")[0] grp.append( _resource_xml(_zfs_name(ha_label), "ocf:chroma:ZFS", {"pool": zpool})) grp.append(res) res = grp if not enabled: meta = ET.SubElement(res, "meta_attributes", {"id": "{}-{}".format(xmlid, "meta_attributes")}) _nvpair_xml(meta, "target_role", "Stopped") # Create Lustre resource and add target=uuid as an attribute result = cibcreate("resources", ET.tostring(res)) if result.rc != 0 or enabled and not _wait_target(ha_label, True): if result.rc == 0: result = AgentShell.RunResult( -1, "", "Resource ({}) failed to start".format(ha_label), False) console_log.error("Failed to create resource %s:%d: %s", ha_label, result.rc, result.stderr) return result
def mock_get_param_raw(self, path): param = path.replace("/", ".") daemon_log.info("mock_get_params_lines: " + param) data = "" for fn in glob(param): with open(fn, "r") as content_file: data += content_file.read() if data: return data else: raise AgentShell.CommandExecutionError( AgentShell.RunResult( 2, "", "error: get_param: param_path '" + param + "': No such file or directory", 0, ), ["lctl", "get_param", "-n", path], )
def mock_get_param_lines(self, path, filter_f=None): param = path.replace("/", ".") daemon_log.info("mock_get_params_lines: " + param) flist = glob(param) if not flist: raise AgentShell.CommandExecutionError( AgentShell.RunResult( 2, "", "error: get_param: param_path '" + param + "': No such file or directory", 0, ), ["lctl", "get_param", "-n", path], ) for fn in flist: with open(fn, "r") as content_file: for line in content_file: if filter_f: if filter_f(line): yield line.strip() else: yield line.strip()
import mock from django.utils import unittest from chroma_agent.lib.shell import AgentShell from chroma_agent.device_plugins.action_runner import ActionRunnerPlugin, CallbackAfterResponse from chroma_agent.plugin_manager import ActionPluginManager, DevicePlugin from chroma_agent.agent_client import AgentDaemonContext ACTION_ONE_NO_CONTEXT_RETVAL = 'action_one_no_context_return' ACTION_ONE_WITH_CONTEXT_RETVAL = 'action_one_with_context_return' ACTION_TWO_RETVAL = 'action_two_return' subprocesses = { ('subprocess_one', 'subprocess_one_arg'): lambda: AgentShell.RunResult(0, 'subprocess_one_stdout', 'subprocess_one_stderr', False), ('subprocess_two', 'subprocess_two_arg'): lambda: AgentShell.RunResult(-1, 'subprocess_two_stdout', 'subprocess_two_stderr', False) } def action_one_no_context(arg1): """An action which invokes subprocess_one""" assert arg1 == "arg1_test" stdout = AgentShell.try_run(['subprocess_one', 'subprocess_one_arg']) assert stdout == 'subprocess_one_stdout' return ACTION_ONE_NO_CONTEXT_RETVAL def action_one_with_context(agent_daemon_context, arg1): """An action which invokes subprocess_one"""
def yum_util(action, packages=[], fromrepo=None, enablerepo=None, narrow_updates=False): ''' A wrapper to perform yum actions in encapsulated way. :param action: clean, install, remove, update, requires etc :param packages: Packages to install or remove :param fromrepo: The repo the action should be carried out from, others are disabled. :param enablerepo: The repo to enable for the action, others are not disabled or enabled :param narrow_updates: ? :return: No return but throws CommandExecutionError on error. ''' if fromrepo and enablerepo: raise ValueError( "Cannot provide fromrepo and enablerepo simultaneously") repo_arg = [] valid_rc_values = [0] # Some errors values other than 0 are valid. if fromrepo: repo_arg = ['--disablerepo=*', '--enablerepo=%s' % ','.join(fromrepo)] elif enablerepo: repo_arg = ['--enablerepo=%s' % ','.join(enablerepo)] if narrow_updates and action == 'query': repo_arg.extend(['--pkgnarrow=updates', '-a']) if action == 'clean': cmd = ['yum', 'clean', 'all' ] + (repo_arg if repo_arg else ["--enablerepo=*"]) elif action == 'install': cmd = ['yum', 'install', '-y'] + repo_arg + list(packages) elif action == 'remove': cmd = ['yum', 'remove', '-y'] + repo_arg + list(packages) elif action == 'update': cmd = ['yum', 'update', '-y'] + repo_arg + list(packages) elif action == 'requires': cmd = ['repoquery', '--requires'] + repo_arg + list(packages) elif action == 'query': cmd = ['repoquery'] + repo_arg + list(packages) elif action == 'repoquery': cmd = ['repoquery'] + repo_arg + [ '-a', '--qf=%{EPOCH} %{NAME} %{VERSION} %{RELEASE} %{ARCH}' ] elif action == 'check-update': cmd = ['yum', 'check-update', '-q'] + repo_arg + list(packages) valid_rc_values = [ 0, 100 ] # check-update returns 100 if updates are available. else: raise RuntimeError('Unknown yum util action %s' % action) # This is a poor solution for HYD-3855 but not one that carries any known cost. # We sometimes see intermittent failures in test, and possibly out of test, that occur # 1 in 50 (estimate) times. yum commands are idempotent and so trying the command three # times has no downside and changes the estimated chance of fail to 1 in 12500. for hyd_3885 in range(2, -1, -1): rc, stdout, stderr = AgentShell.run_old(cmd) if rc in valid_rc_values: return stdout else: daemon_log.info("HYD-3885 Retrying yum command '%s'" % " ".join(cmd)) if hyd_3885 == 0: daemon_log.info("HYD-3885 Retry yum command failed '%s'" % " ".join(cmd)) raise AgentShell.CommandExecutionError( AgentShell.RunResult(rc, stdout, stderr, False), cmd) # Out of retries so raise for the caller..
from chroma_agent.lib.shell import AgentShell from chroma_agent.device_plugins.action_runner import ( ActionRunnerPlugin, CallbackAfterResponse, ) from chroma_agent.plugin_manager import ActionPluginManager, DevicePlugin from chroma_agent.agent_client import AgentDaemonContext ACTION_ONE_NO_CONTEXT_RETVAL = "action_one_no_context_return" ACTION_ONE_WITH_CONTEXT_RETVAL = "action_one_with_context_return" ACTION_TWO_RETVAL = "action_two_return" subprocesses = { ("subprocess_one", "subprocess_one_arg"): lambda: AgentShell.RunResult(0, "subprocess_one_stdout", "subprocess_one_stderr", False), ("subprocess_two", "subprocess_two_arg"): lambda: AgentShell.RunResult(-1, "subprocess_two_stdout", "subprocess_two_stderr", False), } def action_one_no_context(arg1): """An action which invokes subprocess_one""" assert arg1 == "arg1_test" stdout = AgentShell.try_run(["subprocess_one", "subprocess_one_arg"]) assert stdout == "subprocess_one_stdout" return ACTION_ONE_NO_CONTEXT_RETVAL