1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Implementation of keywords for managing Honeycomb persistence files."""
16 from robot.api import logger
18 from resources.libraries.python.Constants import Constants as Const
19 from resources.libraries.python.honeycomb.HoneycombUtil import HoneycombError
20 from resources.libraries.python.ssh import SSH
21 from resources.libraries.python.topology import NodeType
24 class HcPersistence(object):
25 """Implements keywords for managing Honeycomb persistence files.
27 The keywords implemented in this class make possible to:
28 - find and replace strings in config.json persistence file
35 def clear_persisted_honeycomb_config(*nodes):
36 """Remove configuration data persisted from last Honeycomb session.
37 Default configuration will be used instead.
39 :param nodes: List of DUTs to execute on.
41 :raises HoneycombError: If persisted configuration could not be removed.
43 cmd = "rm -rf {}/*".format(Const.REMOTE_HC_PERSIST)
45 if node['type'] == NodeType.DUT:
48 (ret_code, _, stderr) = ssh.exec_command_sudo(cmd)
50 if "No such file or directory" not in stderr:
51 raise HoneycombError('Could not clear persisted '
52 'configuration on node {0}, {1}'
53 .format(node['host'], stderr))
55 logger.info("Persistence data was not present on node"
56 " {0}".format(node['host']))
58 logger.info("Persistence files removed on node {0}"
59 .format(node['host']))
62 def modify_persistence_files(node, find, replace):
63 """Searches contents of persistence file data.json for the provided
64 string, and replaces all occurrences with another string.
66 :param node: Honeycomb node.
67 :param find: Text to find in file.
68 :param replace: String to replace anything found with.
72 :raises HoneycombError: If persistent configuration couldn't be
76 argument = "\"s/{0}/{1}/g\"".format(find, replace)
77 path = "{0}/config/data.json".format(Const.REMOTE_HC_PERSIST)
78 command = "sed -i {0} {1}".format(argument, path)
82 (ret_code, _, stderr) = ssh.exec_command_sudo(command)
84 raise HoneycombError("Failed to modify persistence file on node"
85 " {0}, {1}".format(node, stderr))
88 def log_persisted_configuration(node):
89 """Read contents of Honeycomb persistence files and print them to log.
91 :param node: Honeycomb node.
96 "cat {0}/config/data.json".format(Const.REMOTE_HC_PERSIST),
97 "cat {0}/context/data.json".format(Const.REMOTE_HC_PERSIST),
102 for command in commands:
103 (_, _, _) = ssh.exec_command_sudo(command)
106 def configure_persistence(node, state):
107 """Enable or disable Honeycomb configuration data persistence.
109 :param node: Honeycomb node.
110 :param state: Enable or Disable.
113 :raises ValueError: If the state argument is incorrect.
114 :raises HoneycombError: If the operation fails.
117 state = state.lower()
118 if state == "enable":
120 elif state == "disable":
123 raise ValueError("Unexpected value of state argument:"
124 " {0} provided. Must be enable or disable."
127 for setting in ("persist-config", "persist-context"):
128 # find the setting, replace entire line with 'setting: state'
129 find = '\\"{setting}\\":'.format(setting=setting)
130 replace = '\\"{setting}\\": \\"{state}\\",'.format(
131 setting=setting, state=state)
133 argument = '"/{0}/c\\ {1}"'.format(find, replace)
134 path = "{0}/config/honeycomb.json".format(Const.REMOTE_HC_DIR)
135 command = "sed -i {0} {1}".format(argument, path)
139 (ret_code, _, stderr) = ssh.exec_command_sudo(command)
141 raise HoneycombError("Failed to modify configuration on "
142 "node {0}, {1}".format(node, stderr))