blob: f8895c6e283cba902fde6f3f43fc7e6501d6400b [file] [log] [blame]
Hanna Arhipova94a8abe2019-08-22 14:11:46 +03001# 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.
Hanna Arhipova94a8abe2019-08-22 14:11:46 +030014from tcp_tests import logger
15from tcp_tests.managers.execute_commands import ExecuteCommandsMixin
16
17LOG = logger.logger
18
19
20class ReclassManager(ExecuteCommandsMixin):
21 """docstring for ReclassManager"""
22
23 __config = None
24 __underlay = None
25 reclass_tools_cmd = ". venv-reclass-tools/bin/activate; reclass-tools "
26 tgt = "cfg01" # place where the reclass-tools installed
27
28 def __init__(self, config, underlay):
29 self.__config = config
30 self.__underlay = underlay
31
32 reclass_node = [node_name
33 for node_name in self.__underlay.node_names()
34 if self.tgt in node_name]
35 self.ssh = self.__underlay.remote(node_name=reclass_node[0])
36
37 super(ReclassManager, self).__init__(config=config, underlay=underlay)
38
39 def check_existence(self, key):
40 if key in self.ssh.check_call(
41 "{reclass_tools} get-key {key} /srv/salt/reclass/classes"
42 .format(
43 reclass_tools=self.reclass_tools_cmd,
44 key=key
45 )):
46 LOG.warning("({}) key already exists in reclass".format(key))
47 return True
48 return False
49
50 def add_key(self, key, value, short_path):
51 """
52 Shows alert if key exists
53
54 :param key: string, parameters which will be added or updated
55 :param value: value of key
56 :param short_path: path to reclass yaml file.
57 It takes into account default path where the reclass locates.
58 May look like cluster/*/cicd/control/leader.yml
59 :return: None
60 """
61 self.check_existence(key)
62 self.ssh.check_call(
63 "{reclass_tools} add-key {key} {value} \
64 /srv/salt/reclass/classes/{path}".format(
65 reclass_tools=self.reclass_tools_cmd,
66 key=key,
67 value=value,
68 path=short_path
69 ))
70
Hanna Arhipova19429962019-10-17 15:16:49 +030071 def get_key(self, key, file_name):
Dmitriy Kruglov07977de2019-09-02 13:15:18 +020072 """Find a key in a YAML
73
74 :param key: string, parameter to add
Hanna Arhipova19429962019-10-17 15:16:49 +030075 :param file_name: name of YAML file to find a key
Dmitriy Kruglov07977de2019-09-02 13:15:18 +020076 :return: str, key if found
77 """
Hanna Arhipova19429962019-10-17 15:16:49 +030078 request_key = self.ssh.check_call(
79 "{reclass_tools} get-key {key} /srv/salt/reclass/*/{file_name}".
80 format(reclass_tools=self.reclass_tools_cmd,
81 key=key,
82 file_name=file_name))['stdout']
83
84 # Reclass-tools returns result to stdout, so we get it as
85 # ['\n',
86 # '---\n',
87 # '# Found parameters._param.jenkins_pipelines_branch in \
88 # /srv/salt/reclass/classes/cluster/../infra/init.yml\n',
89 # 'release/proposed/2019.2.0\n',
90 # '...\n',
91 # '\n']
92 # So we have no chance to get value without dirty code like `stdout[3]`
93
94 LOG.info("From reclass.get_key {}".format(request_key))
95 if len(request_key) < 4:
96 assert "Can't find {key} in {file_name}. Got stdout {stdout}".\
97 format(
98 key=key,
99 file_name=file_name,
100 stdout=request_key
101 )
102 value = request_key[3].strip('\n')
103 LOG.info("From reclass.get_key {}".format(value))
104 return value
Dmitriy Kruglov07977de2019-09-02 13:15:18 +0200105
Hanna Arhipova94a8abe2019-08-22 14:11:46 +0300106 def add_bool_key(self, key, value, short_path):
107 """
108 Shows alert if key exists
109
110 :param key: string, parameters which will be added or updated
111 :param value: value of key
112 :param short_path: path to reclass yaml file.
113 It takes into account default path where the reclass locates.
114 May look like cluster/*/cicd/control/leader.yml
115 :return: None
116 """
117 self.check_existence(key)
118 self.ssh.check_call(
119 "{reclass_tools} add-bool-key {key} {value} \
120 /srv/salt/reclass/classes/{path}".format(
121 reclass_tools=self.reclass_tools_cmd,
122 key=key,
123 value=value,
124 path=short_path
125 ), raise_on_err=False)
126
127 def add_class(self, value, short_path):
128 """
129 Shows warning if class exists
130 :param value: role to add to 'classes' parameter in the reclass
131 :param short_path: path to reclass yaml file.
132 It takes into account default path where the reclass locates.
133 May look like cluster/*/cicd/control/leader.yml
134 :return: None
135 """
136 if value in self.ssh.check_call(
137 "{reclass_tools} get-key classes \
138 /srv/salt/reclass/classes/{path}".format(
139 reclass_tools=self.reclass_tools_cmd,
140 value=value,
141 path=short_path
142 )):
143 LOG.warning("Class {} already exists in {}".format(
144 value,
145 short_path
146 ))
147
148 self.ssh.check_call(
149 "{reclass_tools} add-key classes {value} \
150 /srv/salt/reclass/classes/{path} --merge".format(
151 reclass_tools=self.reclass_tools_cmd,
152 value=value,
153 path=short_path
154 ))
Ekaterina Chernovaa6087342019-08-26 13:14:42 +0300155
156 def delete_key(self, key, short_path):
157 """
158 Remove key from the provided file
159
160 :param value: string, parameter which will be deleted
161 :param short_path: string,, path to reclass yaml file.
162 It takes into account default path where the reclass locates.
163 May look like cluster/*/cicd/control/leader.yml
164 :return: None
165 """
166 self.ssh.check_call(
167 "{reclass_tools} del-key {key} \
168 /srv/salt/reclass/classes/{path}".format(
169 reclass_tools=self.reclass_tools_cmd,
170 key=key,
171 path=short_path
172 ))