blob: 0e1133c025663ca49f09644d5402c8e087ddb1a4 [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.
14
15from tcp_tests import logger
16from tcp_tests.managers.execute_commands import ExecuteCommandsMixin
17
18LOG = logger.logger
19
20
21class 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
72 def add_bool_key(self, key, value, short_path):
73 """
74 Shows alert if key exists
75
76 :param key: string, parameters which will be added or updated
77 :param value: value of key
78 :param short_path: path to reclass yaml file.
79 It takes into account default path where the reclass locates.
80 May look like cluster/*/cicd/control/leader.yml
81 :return: None
82 """
83 self.check_existence(key)
84 self.ssh.check_call(
85 "{reclass_tools} add-bool-key {key} {value} \
86 /srv/salt/reclass/classes/{path}".format(
87 reclass_tools=self.reclass_tools_cmd,
88 key=key,
89 value=value,
90 path=short_path
91 ), raise_on_err=False)
92
93 def add_class(self, value, short_path):
94 """
95 Shows warning if class exists
96 :param value: role to add to 'classes' parameter in the reclass
97 :param short_path: path to reclass yaml file.
98 It takes into account default path where the reclass locates.
99 May look like cluster/*/cicd/control/leader.yml
100 :return: None
101 """
102 if value in self.ssh.check_call(
103 "{reclass_tools} get-key classes \
104 /srv/salt/reclass/classes/{path}".format(
105 reclass_tools=self.reclass_tools_cmd,
106 value=value,
107 path=short_path
108 )):
109 LOG.warning("Class {} already exists in {}".format(
110 value,
111 short_path
112 ))
113
114 self.ssh.check_call(
115 "{reclass_tools} add-key classes {value} \
116 /srv/salt/reclass/classes/{path} --merge".format(
117 reclass_tools=self.reclass_tools_cmd,
118 value=value,
119 path=short_path
120 ))