blob: 137dd335555625fbd8d9cb5d1327fa761f8eae59 [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 "
Hanna Arhipovab2522692020-09-23 15:25:11 +030026 tgt = "cfg01" # place where the reclass-tools installed
Hanna Arhipova94a8abe2019-08-22 14:11:46 +030027
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(
Hanna Arhipovab2522692020-09-23 15:25:11 +030041 "{reclass_tools} get-key {key} /srv/salt/reclass/classes".
42 format(reclass_tools=self.reclass_tools_cmd,
43 key=key)):
Hanna Arhipova94a8abe2019-08-22 14:11:46 +030044 LOG.warning("({}) key already exists in reclass".format(key))
45 return True
46 return False
47
48 def add_key(self, key, value, short_path):
49 """
50 Shows alert if key exists
51
52 :param key: string, parameters which will be added or updated
53 :param value: value of key
54 :param short_path: path to reclass yaml file.
55 It takes into account default path where the reclass locates.
56 May look like cluster/*/cicd/control/leader.yml
57 :return: None
58 """
59 self.check_existence(key)
60 self.ssh.check_call(
61 "{reclass_tools} add-key {key} {value} \
62 /srv/salt/reclass/classes/{path}".format(
63 reclass_tools=self.reclass_tools_cmd,
64 key=key,
65 value=value,
66 path=short_path
Hanna Arhipovab2522692020-09-23 15:25:11 +030067 ))
Hanna Arhipova94a8abe2019-08-22 14:11:46 +030068
Hanna Arhipova19429962019-10-17 15:16:49 +030069 def get_key(self, key, file_name):
Dmitriy Kruglov07977de2019-09-02 13:15:18 +020070 """Find a key in a YAML
71
72 :param key: string, parameter to add
Hanna Arhipova19429962019-10-17 15:16:49 +030073 :param file_name: name of YAML file to find a key
Dmitriy Kruglov07977de2019-09-02 13:15:18 +020074 :return: str, key if found
75 """
Hanna Arhipovab2522692020-09-23 15:25:11 +030076 LOG.info("Try to get '{key}' key from '{file}' file".format(
77 file=file_name,
78 key=key
79 ))
Hanna Arhipova19429962019-10-17 15:16:49 +030080 request_key = self.ssh.check_call(
81 "{reclass_tools} get-key {key} /srv/salt/reclass/*/{file_name}".
82 format(reclass_tools=self.reclass_tools_cmd,
83 key=key,
84 file_name=file_name))['stdout']
85
86 # Reclass-tools returns result to stdout, so we get it as
87 # ['\n',
88 # '---\n',
89 # '# Found parameters._param.jenkins_pipelines_branch in \
90 # /srv/salt/reclass/classes/cluster/../infra/init.yml\n',
91 # 'release/proposed/2019.2.0\n',
92 # '...\n',
93 # '\n']
94 # So we have no chance to get value without dirty code like `stdout[3]`
95
Hanna Arhipovab2522692020-09-23 15:25:11 +030096 LOG.info("Raw output from reclass.get_key {}".format(request_key))
Hanna Arhipova19429962019-10-17 15:16:49 +030097 if len(request_key) < 4:
Hanna Arhipovab2522692020-09-23 15:25:11 +030098 print("Can't find {key} in {file_name}. Got stdout {stdout}".
99 format(key=key,
100 file_name=file_name,
101 stdout=request_key))
102 return None
Hanna Arhipova19429962019-10-17 15:16:49 +0300103 value = request_key[3].strip('\n')
104 LOG.info("From reclass.get_key {}".format(value))
105 return value
Dmitriy Kruglov07977de2019-09-02 13:15:18 +0200106
Hanna Arhipova94a8abe2019-08-22 14:11:46 +0300107 def add_bool_key(self, key, value, short_path):
108 """
109 Shows alert if key exists
110
111 :param key: string, parameters which will be added or updated
112 :param value: value of key
113 :param short_path: path to reclass yaml file.
114 It takes into account default path where the reclass locates.
115 May look like cluster/*/cicd/control/leader.yml
116 :return: None
117 """
118 self.check_existence(key)
119 self.ssh.check_call(
120 "{reclass_tools} add-bool-key {key} {value} \
121 /srv/salt/reclass/classes/{path}".format(
122 reclass_tools=self.reclass_tools_cmd,
123 key=key,
124 value=value,
125 path=short_path
126 ), raise_on_err=False)
127
128 def add_class(self, value, short_path):
129 """
130 Shows warning if class exists
131 :param value: role to add to 'classes' parameter in the reclass
132 :param short_path: path to reclass yaml file.
133 It takes into account default path where the reclass locates.
134 May look like cluster/*/cicd/control/leader.yml
135 :return: None
136 """
137 if value in self.ssh.check_call(
138 "{reclass_tools} get-key classes \
139 /srv/salt/reclass/classes/{path}".format(
140 reclass_tools=self.reclass_tools_cmd,
141 value=value,
142 path=short_path
143 )):
144 LOG.warning("Class {} already exists in {}".format(
145 value,
146 short_path
147 ))
148
149 self.ssh.check_call(
150 "{reclass_tools} add-key classes {value} \
151 /srv/salt/reclass/classes/{path} --merge".format(
152 reclass_tools=self.reclass_tools_cmd,
153 value=value,
154 path=short_path
155 ))
Ekaterina Chernovaa6087342019-08-26 13:14:42 +0300156
157 def delete_key(self, key, short_path):
158 """
159 Remove key from the provided file
160
161 :param value: string, parameter which will be deleted
162 :param short_path: string,, path to reclass yaml file.
163 It takes into account default path where the reclass locates.
164 May look like cluster/*/cicd/control/leader.yml
165 :return: None
166 """
167 self.ssh.check_call(
168 "{reclass_tools} del-key {key} \
169 /srv/salt/reclass/classes/{path}".format(
170 reclass_tools=self.reclass_tools_cmd,
171 key=key,
172 path=short_path
173 ))