X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2Fhoneycomb%2FHcPersistence.py;h=ffa850fc9ef754ab1e754db060b729fb49015597;hb=refs%2Fchanges%2F80%2F11580%2F10;hp=c76a2760980a68c62b762c40befb73f4a9130eeb;hpb=7a0a0ec3986ad03444cd4864ae8d35019007be60;p=csit.git diff --git a/resources/libraries/python/honeycomb/HcPersistence.py b/resources/libraries/python/honeycomb/HcPersistence.py index c76a276098..ffa850fc9e 100644 --- a/resources/libraries/python/honeycomb/HcPersistence.py +++ b/resources/libraries/python/honeycomb/HcPersistence.py @@ -1,4 +1,4 @@ -# Copyright (c) 2016 Cisco and/or its affiliates. +# Copyright (c) 2018 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -70,7 +70,7 @@ class HcPersistence(object): :type find: string :type replace: string :raises HoneycombError: If persistent configuration couldn't be - modified. + modified. """ argument = "\"s/{0}/{1}/g\"".format(find, replace) @@ -100,5 +100,43 @@ class HcPersistence(object): ssh = SSH() ssh.connect(node) for command in commands: - (_, stdout, _) = ssh.exec_command_sudo(command) - logger.info(stdout) + (_, _, _) = ssh.exec_command_sudo(command) + + @staticmethod + def configure_persistence(node, state): + """Enable or disable Honeycomb configuration data persistence. + + :param node: Honeycomb node. + :param state: Enable or Disable. + :type node: dict + :type state: str + :raises ValueError: If the state argument is incorrect. + :raises HoneycombError: If the operation fails. + """ + + state = state.lower() + if state == "enable": + state = "true" + elif state == "disable": + state = "false" + else: + raise ValueError("Unexpected value of state argument:" + " {0} provided. Must be enable or disable." + .format(state)) + + for setting in ("persist-config", "persist-context"): + # find the setting, replace entire line with 'setting: state' + find = '\\"{setting}\\":'.format(setting=setting) + replace = '\\"{setting}\\": \\"{state}\\",'.format( + setting=setting, state=state) + + argument = '"/{0}/c\\ {1}"'.format(find, replace) + path = "{0}/config/honeycomb.json".format(Const.REMOTE_HC_DIR) + command = "sed -i {0} {1}".format(argument, path) + + ssh = SSH() + ssh.connect(node) + (ret_code, _, stderr) = ssh.exec_command_sudo(command) + if ret_code != 0: + raise HoneycombError("Failed to modify configuration on " + "node {0}, {1}".format(node, stderr))