Hanna Arhipova | 94a8abe | 2019-08-22 14:11:46 +0300 | [diff] [blame] | 1 | # Copyright 2016 Mirantis, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | from tcp_tests import logger |
| 16 | from tcp_tests.managers.execute_commands import ExecuteCommandsMixin |
| 17 | |
| 18 | LOG = logger.logger |
| 19 | |
| 20 | |
| 21 | class ReclassManager(ExecuteCommandsMixin): |
| 22 | """docstring for ReclassManager""" |
| 23 | |
| 24 | __config = None |
| 25 | __underlay = None |
| 26 | reclass_tools_cmd = ". venv-reclass-tools/bin/activate; reclass-tools " |
| 27 | tgt = "cfg01" # place where the reclass-tools installed |
| 28 | |
| 29 | def __init__(self, config, underlay): |
| 30 | self.__config = config |
| 31 | self.__underlay = underlay |
| 32 | |
| 33 | reclass_node = [node_name |
| 34 | for node_name in self.__underlay.node_names() |
| 35 | if self.tgt in node_name] |
| 36 | self.ssh = self.__underlay.remote(node_name=reclass_node[0]) |
| 37 | |
| 38 | super(ReclassManager, self).__init__(config=config, underlay=underlay) |
| 39 | |
| 40 | def check_existence(self, key): |
| 41 | if key in self.ssh.check_call( |
| 42 | "{reclass_tools} get-key {key} /srv/salt/reclass/classes" |
| 43 | .format( |
| 44 | reclass_tools=self.reclass_tools_cmd, |
| 45 | key=key |
| 46 | )): |
| 47 | LOG.warning("({}) key already exists in reclass".format(key)) |
| 48 | return True |
| 49 | return False |
| 50 | |
| 51 | def add_key(self, key, value, short_path): |
| 52 | """ |
| 53 | Shows alert if key exists |
| 54 | |
| 55 | :param key: string, parameters which will be added or updated |
| 56 | :param value: value of key |
| 57 | :param short_path: path to reclass yaml file. |
| 58 | It takes into account default path where the reclass locates. |
| 59 | May look like cluster/*/cicd/control/leader.yml |
| 60 | :return: None |
| 61 | """ |
| 62 | self.check_existence(key) |
| 63 | self.ssh.check_call( |
| 64 | "{reclass_tools} add-key {key} {value} \ |
| 65 | /srv/salt/reclass/classes/{path}".format( |
| 66 | reclass_tools=self.reclass_tools_cmd, |
| 67 | key=key, |
| 68 | value=value, |
| 69 | path=short_path |
| 70 | )) |
| 71 | |
Dmitriy Kruglov | 07977de | 2019-09-02 13:15:18 +0200 | [diff] [blame^] | 72 | def get_key(self, key, short_path): |
| 73 | """Find a key in a YAML |
| 74 | |
| 75 | :param key: string, parameter to add |
| 76 | :param short_path: path to reclass yaml file. |
| 77 | It takes into account default path where the reclass is located. |
| 78 | May look like cluster/*/cicd/control/leader.yml |
| 79 | :return: str, key if found |
| 80 | """ |
| 81 | return self.ssh.check_call( |
| 82 | "{reclass_tools} get-key {key} /srv/salt/reclass/classes".format( |
| 83 | reclass_tools=self.reclass_tools_cmd, key=key)) |
| 84 | |
Hanna Arhipova | 94a8abe | 2019-08-22 14:11:46 +0300 | [diff] [blame] | 85 | def add_bool_key(self, key, value, short_path): |
| 86 | """ |
| 87 | Shows alert if key exists |
| 88 | |
| 89 | :param key: string, parameters which will be added or updated |
| 90 | :param value: value of key |
| 91 | :param short_path: path to reclass yaml file. |
| 92 | It takes into account default path where the reclass locates. |
| 93 | May look like cluster/*/cicd/control/leader.yml |
| 94 | :return: None |
| 95 | """ |
| 96 | self.check_existence(key) |
| 97 | self.ssh.check_call( |
| 98 | "{reclass_tools} add-bool-key {key} {value} \ |
| 99 | /srv/salt/reclass/classes/{path}".format( |
| 100 | reclass_tools=self.reclass_tools_cmd, |
| 101 | key=key, |
| 102 | value=value, |
| 103 | path=short_path |
| 104 | ), raise_on_err=False) |
| 105 | |
| 106 | def add_class(self, value, short_path): |
| 107 | """ |
| 108 | Shows warning if class exists |
| 109 | :param value: role to add to 'classes' parameter in the reclass |
| 110 | :param short_path: path to reclass yaml file. |
| 111 | It takes into account default path where the reclass locates. |
| 112 | May look like cluster/*/cicd/control/leader.yml |
| 113 | :return: None |
| 114 | """ |
| 115 | if value in self.ssh.check_call( |
| 116 | "{reclass_tools} get-key classes \ |
| 117 | /srv/salt/reclass/classes/{path}".format( |
| 118 | reclass_tools=self.reclass_tools_cmd, |
| 119 | value=value, |
| 120 | path=short_path |
| 121 | )): |
| 122 | LOG.warning("Class {} already exists in {}".format( |
| 123 | value, |
| 124 | short_path |
| 125 | )) |
| 126 | |
| 127 | self.ssh.check_call( |
| 128 | "{reclass_tools} add-key classes {value} \ |
| 129 | /srv/salt/reclass/classes/{path} --merge".format( |
| 130 | reclass_tools=self.reclass_tools_cmd, |
| 131 | value=value, |
| 132 | path=short_path |
| 133 | )) |