Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [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 | |
Dmitry Tyzhnenko | b864183 | 2017-11-07 17:02:47 +0200 | [diff] [blame] | 15 | import netaddr |
Dennis Dmitriev | ad5f858 | 2019-04-12 13:15:08 +0300 | [diff] [blame] | 16 | import pkg_resources |
Dmitry Tyzhnenko | b864183 | 2017-11-07 17:02:47 +0200 | [diff] [blame] | 17 | |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 18 | from collections import defaultdict |
Hanna Arhipova | 7a593da | 2021-06-25 11:16:09 +0300 | [diff] [blame] | 19 | from polling import poll |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 20 | |
| 21 | from datetime import datetime |
Dennis Dmitriev | b8115f5 | 2017-12-15 13:09:56 +0200 | [diff] [blame] | 22 | from pepper import libpepper |
| 23 | from tcp_tests.helpers import utils |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 24 | from tcp_tests import logger |
Dmitry Tyzhnenko | 35413c0 | 2018-03-05 14:12:37 +0200 | [diff] [blame] | 25 | from tcp_tests import settings |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 26 | from tcp_tests.managers.execute_commands import ExecuteCommandsMixin |
| 27 | |
| 28 | LOG = logger.logger |
| 29 | |
| 30 | |
| 31 | class SaltManager(ExecuteCommandsMixin): |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 32 | """docstring for SaltManager""" |
| 33 | |
Dmitry Tyzhnenko | bc0f826 | 2017-04-28 15:39:26 +0300 | [diff] [blame] | 34 | __config = None |
| 35 | __underlay = None |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 36 | _map = { |
| 37 | 'enforceState': 'enforce_state', |
| 38 | 'enforceStates': 'enforce_states', |
| 39 | 'runState': 'run_state', |
| 40 | 'runStates': 'run_states', |
| 41 | } |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 42 | |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 43 | def __init__(self, config, underlay, host=None, port='6969', |
| 44 | username=None, password=None): |
Dmitry Tyzhnenko | bc0f826 | 2017-04-28 15:39:26 +0300 | [diff] [blame] | 45 | self.__config = config |
| 46 | self.__underlay = underlay |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 47 | self.__port = port |
| 48 | self.__host = host |
| 49 | self.__api = None |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 50 | self.__user = username or settings.SALT_USER |
| 51 | self.__password = password or settings.SALT_PASSWORD |
Dennis Dmitriev | eac3aab | 2017-07-12 16:36:41 +0300 | [diff] [blame] | 52 | self._salt = self |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 53 | |
Dmitry Tyzhnenko | bc0f826 | 2017-04-28 15:39:26 +0300 | [diff] [blame] | 54 | super(SaltManager, self).__init__(config=config, underlay=underlay) |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 55 | |
| 56 | def install(self, commands): |
Dina Belova | e6fdffb | 2017-09-19 13:58:34 -0700 | [diff] [blame] | 57 | # if self.__config.salt.salt_master_host == '0.0.0.0': |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 58 | # # Temporary workaround. Underlay should be extended with roles |
| 59 | # salt_nodes = self.__underlay.node_names() |
| 60 | # self.__config.salt.salt_master_host = \ |
| 61 | # self.__underlay.host_by_node_name(salt_nodes[0]) |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 62 | |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 63 | self.execute_commands(commands=commands, |
| 64 | label="Install and configure salt") |
Dennis Dmitriev | ad5f858 | 2019-04-12 13:15:08 +0300 | [diff] [blame] | 65 | self.create_env_salt() |
| 66 | self.create_env_jenkins_day01() |
Oleksii Butenko | 3b3bab1 | 2019-05-23 17:30:15 +0300 | [diff] [blame] | 67 | # self.create_env_jenkins_cicd() |
| 68 | # self.create_env_k8s() |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 69 | |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 70 | def change_creds(self, username, password): |
| 71 | self.__user = username |
| 72 | self.__password = password |
| 73 | |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 74 | @property |
| 75 | def port(self): |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 76 | return self.__port |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 77 | |
| 78 | @property |
| 79 | def host(self): |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 80 | if self.__host: |
| 81 | return self.__host |
| 82 | else: |
Dina Belova | e6fdffb | 2017-09-19 13:58:34 -0700 | [diff] [blame] | 83 | # TODO(ddmitriev): consider to add a check and raise |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 84 | # exception if 'salt_master_host' is not initialized. |
| 85 | return self.__config.salt.salt_master_host |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 86 | |
| 87 | @property |
| 88 | def api(self): |
| 89 | def login(): |
| 90 | LOG.info("Authentication in Salt API") |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 91 | self.__api.login( |
| 92 | username=self.__user, |
| 93 | password=self.__password, |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 94 | eauth='pam') |
| 95 | return datetime.now() |
| 96 | |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 97 | if self.__api: |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 98 | if (datetime.now() - self.__session_start).seconds < 5 * 60: |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 99 | return self.__api |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 100 | else: |
| 101 | # FIXXME: Change to debug |
| 102 | LOG.info("Session's expired") |
| 103 | self.__session_start = login() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 104 | return self.__api |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 105 | |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 106 | url = "http://{host}:{port}".format( |
| 107 | host=self.host, port=self.port) |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 108 | LOG.info("Connecting to Salt API {0}".format(url)) |
Dennis Dmitriev | b8115f5 | 2017-12-15 13:09:56 +0200 | [diff] [blame] | 109 | self.__api = libpepper.Pepper(url) |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 110 | self.__session_start = login() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 111 | return self.__api |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 112 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 113 | def local(self, tgt, fun, args=None, kwargs=None, timeout=None): |
| 114 | return self.api.local(tgt, fun, args, kwargs, timeout=timeout, |
| 115 | expr_form='compound') |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 116 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 117 | def local_async(self, tgt, fun, args=None, kwargs=None, timeout=None): |
| 118 | return self.api.local_async(tgt, fun, args, kwargs, timeout=timeout) |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 119 | |
Hanna Arhipova | 7a593da | 2021-06-25 11:16:09 +0300 | [diff] [blame] | 120 | def runner(self, fun, args=None, **kwargs): |
| 121 | response = self.api.runner(fun, arg=args, **kwargs) |
| 122 | return response.get("return") |
| 123 | |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 124 | def lookup_result(self, jid): |
| 125 | return self.api.lookup_jid(jid) |
| 126 | |
| 127 | def check_result(self, r): |
| 128 | if len(r.get('return', [])) == 0: |
| 129 | raise LookupError("Result is empty or absent") |
| 130 | |
| 131 | result = r['return'][0] |
Dmitry Tyzhnenko | bc0f826 | 2017-04-28 15:39:26 +0300 | [diff] [blame] | 132 | if len(result) == 0: |
| 133 | raise LookupError("Result is empty or absent") |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 134 | LOG.info("Job has result for %s nodes", result.keys()) |
| 135 | fails = defaultdict(list) |
| 136 | for h in result: |
| 137 | host_result = result[h] |
| 138 | LOG.info("On %s executed:", h) |
| 139 | if isinstance(host_result, list): |
| 140 | fails[h].append(host_result) |
| 141 | continue |
| 142 | for t in host_result: |
| 143 | task = host_result[t] |
| 144 | if task['result'] is False: |
| 145 | fails[h].append(task) |
| 146 | LOG.error("%s - %s", t, task['result']) |
| 147 | else: |
| 148 | LOG.info("%s - %s", t, task['result']) |
| 149 | |
| 150 | return fails if fails else None |
| 151 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 152 | def enforce_state(self, tgt, state, args=None, kwargs=None, timeout=None): |
| 153 | r = self.local(tgt=tgt, fun='state.sls', args=state, timeout=timeout) |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 154 | f = self.check_result(r) |
| 155 | return r, f |
| 156 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 157 | def enforce_states(self, tgt, state, args=None, kwargs=None, timeout=None): |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 158 | rets = [] |
| 159 | for s in state: |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 160 | r = self.enforce_state(tgt=tgt, state=s, timeout=timeout) |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 161 | rets.append(r) |
| 162 | return rets |
| 163 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 164 | def run_state(self, tgt, state, args=None, kwargs=None, timeout=None): |
| 165 | return self.local(tgt=tgt, fun=state, args=args, kwargs=kwargs, |
| 166 | timeout=timeout), None |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 167 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 168 | def run_states(self, tgt, state, args=None, kwargs=None, timeout=None): |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 169 | rets = [] |
| 170 | for s in state: |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 171 | r = self.run_state(tgt=tgt, state=s, args=args, kwargs=kwargs, |
| 172 | timeout=timeout) |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 173 | rets.append(r) |
| 174 | return rets |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 175 | |
| 176 | def get_pillar(self, tgt, pillar): |
| 177 | result = self.local(tgt=tgt, fun='pillar.get', args=pillar) |
| 178 | return result['return'] |
Dmitry Tyzhnenko | b864183 | 2017-11-07 17:02:47 +0200 | [diff] [blame] | 179 | |
Dennis Dmitriev | 2a49873 | 2018-12-21 18:30:23 +0200 | [diff] [blame] | 180 | def get_single_pillar(self, tgt, pillar): |
| 181 | """Get a scalar value from a single node |
| 182 | |
| 183 | :return: pillar value |
| 184 | """ |
| 185 | |
| 186 | result = self.get_pillar(tgt, pillar) |
| 187 | nodes = result[0].keys() |
| 188 | |
| 189 | if not nodes: |
| 190 | raise LookupError("No minions selected " |
| 191 | "for the target '{0}'".format(tgt)) |
| 192 | if len(nodes) > 1: |
| 193 | raise LookupError("Too many minions selected " |
| 194 | "for the target '{0}' , expected one: {1}" |
| 195 | .format(tgt, nodes)) |
| 196 | return result[0][nodes[0]] |
| 197 | |
Dennis Dmitriev | 2d643bc | 2017-12-04 12:23:47 +0200 | [diff] [blame] | 198 | def get_grains(self, tgt, grains): |
| 199 | result = self.local(tgt=tgt, fun='grains.get', args=grains) |
| 200 | return result['return'] |
| 201 | |
Hanna Arhipova | 7a593da | 2021-06-25 11:16:09 +0300 | [diff] [blame] | 202 | def wait_jobs_completed(self, timeout=60, interval=5): |
| 203 | """ |
| 204 | :param timeout: int, time seconds to wait |
| 205 | :param interval: int, time in second between attempts |
| 206 | :return: None |
| 207 | """ |
| 208 | # TODO(harhipova) PROD-36434 : need to check that last job completed |
| 209 | # with successful result |
| 210 | poll( |
| 211 | target=lambda: self.runner('jobs.active')[0] == {}, |
| 212 | timeout=timeout, |
| 213 | step=interval |
| 214 | ) |
| 215 | |
Dmitry Tyzhnenko | b864183 | 2017-11-07 17:02:47 +0200 | [diff] [blame] | 216 | def get_ssh_data(self): |
| 217 | """Generate ssh config for Underlay |
| 218 | |
| 219 | :param roles: list of strings |
| 220 | """ |
| 221 | |
| 222 | pool_name = self.__config.underlay.net_mgmt |
| 223 | pool_net = netaddr.IPNetwork(self.__config.underlay.address_pools[ |
| 224 | self.__config.underlay.net_mgmt]) |
| 225 | hosts = self.local('*', 'grains.item', ['host', 'ipv4']) |
| 226 | |
| 227 | if len(hosts.get('return', [])) == 0: |
| 228 | raise LookupError("Hosts is empty or absent") |
| 229 | hosts = hosts['return'][0] |
| 230 | if len(hosts) == 0: |
| 231 | raise LookupError("Hosts is empty or absent") |
| 232 | |
Dennis Dmitriev | 83cc1d5 | 2018-11-09 15:35:30 +0200 | [diff] [blame] | 233 | def host(minion_id, ip): |
Dmitry Tyzhnenko | b864183 | 2017-11-07 17:02:47 +0200 | [diff] [blame] | 234 | return { |
| 235 | 'roles': ['salt_minion'], |
| 236 | 'keys': [ |
| 237 | k['private'] for k in self.__config.underlay.ssh_keys |
| 238 | ], |
Dennis Dmitriev | 83cc1d5 | 2018-11-09 15:35:30 +0200 | [diff] [blame] | 239 | 'node_name': minion_id, |
| 240 | 'minion_id': minion_id, |
Dmitry Tyzhnenko | b864183 | 2017-11-07 17:02:47 +0200 | [diff] [blame] | 241 | 'host': ip, |
| 242 | 'address_pool': pool_name, |
| 243 | 'login': settings.SSH_NODE_CREDENTIALS['login'], |
| 244 | 'password': settings.SSH_NODE_CREDENTIALS['password'] |
| 245 | } |
| 246 | |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 247 | try: |
| 248 | ret = [ |
| 249 | host(k, next(i for i in v['ipv4'] if i in pool_net)) |
| 250 | for k, v in hosts.items() |
| 251 | if next(i for i in v['ipv4'] if i in pool_net)] |
| 252 | LOG.debug("Fetched ssh data from salt grains - {}".format(ret)) |
| 253 | return ret |
| 254 | except StopIteration: |
| 255 | msg = ("Can't match nodes ip address with network cidr\n" |
| 256 | "Managment network - {net}\n" |
| 257 | "Host with address - {host_list}".format( |
| 258 | net=pool_net, |
| 259 | host_list={k: v['ipv4'] for k, v in hosts.items()})) |
| 260 | raise StopIteration(msg) |
Dennis Dmitriev | 2d643bc | 2017-12-04 12:23:47 +0200 | [diff] [blame] | 261 | |
Dennis Dmitriev | 83cc1d5 | 2018-11-09 15:35:30 +0200 | [diff] [blame] | 262 | def update_ssh_data_from_minions(self): |
| 263 | """Combine existing underlay.ssh with VCP salt minions""" |
| 264 | salt_nodes = self.get_ssh_data() |
| 265 | |
| 266 | for salt_node in salt_nodes: |
| 267 | nodes = [n for n in self.__config.underlay.ssh |
| 268 | if salt_node['host'] == n['host'] |
| 269 | and salt_node['address_pool'] == n['address_pool']] |
| 270 | if nodes: |
| 271 | # Assume that there can be only one node with such IP address |
| 272 | # Just update minion_id for this node |
| 273 | nodes[0]['minion_id'] = salt_node['minion_id'] |
| 274 | else: |
| 275 | # New node, add to config.underlay.ssh |
| 276 | self.__config.underlay.ssh.append(salt_node) |
| 277 | |
| 278 | self.__underlay.config_ssh = [] |
| 279 | self.__underlay.add_config_ssh(self.__config.underlay.ssh) |
| 280 | |
Dennis Dmitriev | 2d643bc | 2017-12-04 12:23:47 +0200 | [diff] [blame] | 281 | def service_status(self, tgt, service): |
| 282 | result = self.local(tgt=tgt, fun='service.status', args=service) |
| 283 | return result['return'] |
| 284 | |
| 285 | def service_restart(self, tgt, service): |
| 286 | result = self.local(tgt=tgt, fun='service.restart', args=service) |
| 287 | return result['return'] |
| 288 | |
| 289 | def service_stop(self, tgt, service): |
| 290 | result = self.local(tgt=tgt, fun='service.stop', args=service) |
| 291 | return result['return'] |
Dennis Dmitriev | b8115f5 | 2017-12-15 13:09:56 +0200 | [diff] [blame] | 292 | |
Dmitriy Kruglov | 07977de | 2019-09-02 13:15:18 +0200 | [diff] [blame] | 293 | def pkg_install(self, tgt, pkg): |
| 294 | result = self.local(tgt=tgt, fun='pkg.install', args=pkg) |
| 295 | return result['return'] |
| 296 | |
| 297 | def pkg_info_installed(self, tgt, pkg): |
| 298 | result = self.local(tgt=tgt, fun='pkg.info_installed', args=pkg) |
| 299 | return result['return'] |
| 300 | |
Victor Ryzhenkin | 9504688 | 2018-12-29 19:18:40 +0400 | [diff] [blame] | 301 | def cmd_run(self, tgt, cmd): |
| 302 | result = self.local(tgt=tgt, fun='cmd.run', args=cmd) |
| 303 | return result['return'] |
| 304 | |
Hanna Arhipova | 653d556 | 2020-08-03 15:32:09 +0300 | [diff] [blame] | 305 | def file_write(self, tgt, filename, content): |
| 306 | result = self.local(tgt=tgt, |
| 307 | fun='file.write', |
| 308 | args=[filename, content]) |
| 309 | return result['return'] |
| 310 | |
| 311 | def file_makedirs(self, tgt, path): |
| 312 | if path[-1] != "/": |
| 313 | path += "/" |
| 314 | result = self.local(tgt=tgt, fun='file.makedirs', args=path) |
| 315 | return result['return'] |
| 316 | |
Dennis Dmitriev | fa1774a | 2019-05-28 15:27:44 +0300 | [diff] [blame] | 317 | @utils.retry(10, exception=libpepper.PepperException) |
Dennis Dmitriev | 427e415 | 2019-05-08 15:12:43 +0300 | [diff] [blame] | 318 | def sync_time(self, tgt='*'): |
Dennis Dmitriev | b8115f5 | 2017-12-15 13:09:56 +0200 | [diff] [blame] | 319 | LOG.info("NTP time sync on the salt minions '{0}'".format(tgt)) |
| 320 | # Force authentication update on the next API access |
| 321 | # because previous authentication most probably is not valid |
| 322 | # before or after time sync. |
| 323 | self.__api = None |
Dmitry Tyzhnenko | 80ce020 | 2019-02-07 13:27:19 +0200 | [diff] [blame] | 324 | if not settings.SKIP_SYNC_TIME: |
Andrew Baraniuk | 5da5c58 | 2019-06-20 12:12:45 +0300 | [diff] [blame] | 325 | cmd = ('chmod -x /usr/sbin/ntpd' |
| 326 | 'service ntp stop;' |
Dennis Dmitriev | fa1774a | 2019-05-28 15:27:44 +0300 | [diff] [blame] | 327 | # note: maas-rackd will return 'pool' after start |
Andrew Baraniuk | ab06b7a | 2019-06-18 13:53:00 +0300 | [diff] [blame] | 328 | 'sed -i \'s/^pool ntp.ubuntu.com/server ntp.cesnet.cz/g\' ' |
| 329 | '/etc/ntp/maas.conf;' |
Andrew Baraniuk | 5da5c58 | 2019-06-20 12:12:45 +0300 | [diff] [blame] | 330 | 'sed -i \'s/^pool ntp.ubuntu.com/server ntp.cesnet.cz/g\' ' |
| 331 | '/etc/ntp.conf;' |
Dennis Dmitriev | 427e415 | 2019-05-08 15:12:43 +0300 | [diff] [blame] | 332 | 'if [ -x /usr/sbin/ntpdate ]; then' |
Andrew Baraniuk | 5da5c58 | 2019-06-20 12:12:45 +0300 | [diff] [blame] | 333 | ' ntpdate -s ntp.cesnet.cz;' |
Dennis Dmitriev | 427e415 | 2019-05-08 15:12:43 +0300 | [diff] [blame] | 334 | 'else' |
| 335 | ' ntpd -gq;' |
| 336 | 'fi;' |
Andrew Baraniuk | 5da5c58 | 2019-06-20 12:12:45 +0300 | [diff] [blame] | 337 | 'chmod +x /usr/sbin/ntpd' |
Dennis Dmitriev | 427e415 | 2019-05-08 15:12:43 +0300 | [diff] [blame] | 338 | 'service ntp start;' |
Andrew Baraniuk | 5da5c58 | 2019-06-20 12:12:45 +0300 | [diff] [blame] | 339 | 'sleep 3; ntpq -pn;') |
Dmitry Tyzhnenko | 80ce020 | 2019-02-07 13:27:19 +0200 | [diff] [blame] | 340 | self.run_state( |
| 341 | tgt, |
Dennis Dmitriev | fa1774a | 2019-05-28 15:27:44 +0300 | [diff] [blame] | 342 | 'cmd.run', cmd, timeout=360) # noqa |
Dennis Dmitriev | b8115f5 | 2017-12-15 13:09:56 +0200 | [diff] [blame] | 343 | new_time_res = self.run_state(tgt, 'cmd.run', 'date') |
| 344 | for node_name, time in sorted(new_time_res[0]['return'][0].items()): |
| 345 | LOG.info("{0}: {1}".format(node_name, time)) |
| 346 | self.__api = None |
Dennis Dmitriev | ad5f858 | 2019-04-12 13:15:08 +0300 | [diff] [blame] | 347 | |
| 348 | def create_env_salt(self): |
| 349 | """Creates static utils/env_salt file""" |
| 350 | |
| 351 | env_salt_filename = pkg_resources.resource_filename( |
| 352 | settings.__name__, 'utils/env_salt') |
| 353 | with open(env_salt_filename, 'w') as f: |
| 354 | f.write( |
| 355 | 'export SALT_MASTER_IP={host}\n' |
| 356 | 'export SALTAPI_URL=http://{host}:{port}/\n' |
| 357 | 'export SALTAPI_USER="{user}"\n' |
| 358 | 'export SALTAPI_PASS="{password}"\n' |
| 359 | 'export SALTAPI_EAUTH="pam"\n' |
| 360 | 'echo "export SALT_MASTER_IP=${{SALT_MASTER_IP}}"\n' |
| 361 | 'echo "export SALTAPI_URL=${{SALTAPI_URL}}"\n' |
| 362 | 'echo "export SALTAPI_USER=${{SALTAPI_USER}}"\n' |
| 363 | 'echo "export SALTAPI_PASS=${{SALTAPI_PASS}}"\n' |
| 364 | 'echo "export SALTAPI_EAUTH=${{SALTAPI_EAUTH}}"\n' |
| 365 | .format(host=self.host, port=self.port, |
| 366 | user=self.__user, password=self.__password) |
| 367 | ) |
| 368 | |
| 369 | def create_env_jenkins_day01(self): |
| 370 | """Creates static utils/env_jenkins_day01 file""" |
| 371 | |
| 372 | env_jenkins_day01_filename = pkg_resources.resource_filename( |
| 373 | settings.__name__, 'utils/env_jenkins_day01') |
| 374 | |
| 375 | tgt = 'I@docker:client:stack:jenkins and cfg01*' |
| 376 | jenkins_params = self.get_single_pillar( |
| 377 | tgt=tgt, pillar="jenkins:client:master") |
obutenko | ca85840 | 2019-07-04 18:31:39 +0300 | [diff] [blame] | 378 | jenkins_host = jenkins_params.get('host') |
| 379 | jenkins_port = jenkins_params.get('port') |
| 380 | jenkins_protocol = jenkins_params.get('proto', 'http') |
| 381 | jenkins_user = jenkins_params.get('username', 'admin') |
| 382 | jenkins_pass = jenkins_params.get('password') |
| 383 | |
| 384 | if not all([jenkins_host, |
| 385 | jenkins_port, |
| 386 | jenkins_protocol, |
| 387 | jenkins_user, |
| 388 | jenkins_pass]): |
| 389 | raise LookupError( |
| 390 | "Some of the required parameters for Jenkins not set in the " |
| 391 | "pillar jenkins:client:master on {0}: {1}" |
| 392 | .format(tgt, jenkins_params)) |
Dennis Dmitriev | ad5f858 | 2019-04-12 13:15:08 +0300 | [diff] [blame] | 393 | |
| 394 | with open(env_jenkins_day01_filename, 'w') as f: |
| 395 | f.write( |
obutenko | ca85840 | 2019-07-04 18:31:39 +0300 | [diff] [blame] | 396 | 'export JENKINS_URL={protocol}://{host}:{port}\n' |
Dennis Dmitriev | ad5f858 | 2019-04-12 13:15:08 +0300 | [diff] [blame] | 397 | 'export JENKINS_USER={user}\n' |
| 398 | 'export JENKINS_PASS={password}\n' |
| 399 | 'export JENKINS_START_TIMEOUT=60\n' |
| 400 | 'export JENKINS_BUILD_TIMEOUT=1800\n' |
| 401 | 'echo "export JENKINS_URL=${{JENKINS_URL}}' |
| 402 | ' # Jenkins API URL"\n' |
| 403 | 'echo "export JENKINS_USER=${{JENKINS_USER}}' |
| 404 | ' # Jenkins API username"\n' |
| 405 | 'echo "export JENKINS_PASS=${{JENKINS_PASS}}' |
| 406 | ' # Jenkins API password or token"n\n' |
| 407 | 'echo "export JENKINS_START_TIMEOUT=${{JENKINS_START_TIMEOUT}}' |
| 408 | ' # Timeout waiting for job in queue to start building"\n' |
| 409 | 'echo "export JENKINS_BUILD_TIMEOUT=${{JENKINS_BUILD_TIMEOUT}}' |
| 410 | ' # Timeout waiting for building job to complete"\n' |
obutenko | ca85840 | 2019-07-04 18:31:39 +0300 | [diff] [blame] | 411 | .format(host=jenkins_host, port=jenkins_port, |
| 412 | protocol=jenkins_protocol, user=jenkins_user, |
| 413 | password=jenkins_pass) |
Dennis Dmitriev | ad5f858 | 2019-04-12 13:15:08 +0300 | [diff] [blame] | 414 | ) |
| 415 | |
| 416 | def create_env_jenkins_cicd(self): |
| 417 | """Creates static utils/env_jenkins_cicd file""" |
| 418 | |
| 419 | env_jenkins_cicd_filename = pkg_resources.resource_filename( |
| 420 | settings.__name__, 'utils/env_jenkins_cicd') |
Oleksii Butenko | 3b3bab1 | 2019-05-23 17:30:15 +0300 | [diff] [blame] | 421 | |
| 422 | tgt = 'I@docker:client:stack:jenkins and cid01*' |
| 423 | try: |
| 424 | jenkins_params = self.get_single_pillar( |
| 425 | tgt=tgt, pillar="jenkins:client:master") |
| 426 | except LookupError as e: |
| 427 | LOG.error("Skipping creation {0} because cannot get Jenkins CICD " |
| 428 | "parameters from '{1}': {2}" |
| 429 | .format(env_jenkins_cicd_filename, tgt, e.message)) |
Dennis Dmitriev | ad5f858 | 2019-04-12 13:15:08 +0300 | [diff] [blame] | 430 | return |
Oleksii Butenko | 3b3bab1 | 2019-05-23 17:30:15 +0300 | [diff] [blame] | 431 | |
obutenko | ca85840 | 2019-07-04 18:31:39 +0300 | [diff] [blame] | 432 | jenkins_host = jenkins_params.get('host') |
| 433 | jenkins_port = jenkins_params.get('port') |
| 434 | jenkins_protocol = jenkins_params.get('proto', 'http') |
| 435 | jenkins_user = jenkins_params.get('username', 'admin') |
| 436 | jenkins_pass = jenkins_params.get('password') |
| 437 | |
| 438 | if not all([jenkins_host, |
| 439 | jenkins_port, |
| 440 | jenkins_protocol, |
| 441 | jenkins_user, |
| 442 | jenkins_pass]): |
| 443 | raise LookupError( |
| 444 | "Some of the required parameters for Jenkins not set in the " |
| 445 | "pillar jenkins:client:master on {0}: {1}" |
| 446 | .format(tgt, jenkins_params)) |
Dennis Dmitriev | ad5f858 | 2019-04-12 13:15:08 +0300 | [diff] [blame] | 447 | |
| 448 | with open(env_jenkins_cicd_filename, 'w') as f: |
| 449 | f.write( |
obutenko | ca85840 | 2019-07-04 18:31:39 +0300 | [diff] [blame] | 450 | 'export JENKINS_URL={protocol}://{host}:{port}\n' |
Dennis Dmitriev | ad5f858 | 2019-04-12 13:15:08 +0300 | [diff] [blame] | 451 | 'export JENKINS_USER={user}\n' |
| 452 | 'export JENKINS_PASS={password}\n' |
| 453 | 'export JENKINS_START_TIMEOUT=60\n' |
| 454 | 'export JENKINS_BUILD_TIMEOUT=1800\n' |
| 455 | 'echo "export JENKINS_URL=${{JENKINS_URL}}' |
| 456 | ' # Jenkins API URL"\n' |
| 457 | 'echo "export JENKINS_USER=${{JENKINS_USER}}' |
| 458 | ' # Jenkins API username"\n' |
| 459 | 'echo "export JENKINS_PASS=${{JENKINS_PASS}}' |
| 460 | ' # Jenkins API password or token"n\n' |
| 461 | 'echo "export JENKINS_START_TIMEOUT=${{JENKINS_START_TIMEOUT}}' |
| 462 | ' # Timeout waiting for job in queue to start building"\n' |
| 463 | 'echo "export JENKINS_BUILD_TIMEOUT=${{JENKINS_BUILD_TIMEOUT}}' |
| 464 | ' # Timeout waiting for building job to complete"\n' |
| 465 | .format(host=jenkins_host, port=jenkins_port, |
obutenko | ca85840 | 2019-07-04 18:31:39 +0300 | [diff] [blame] | 466 | protocol=jenkins_protocol, user=jenkins_user, |
| 467 | password=jenkins_pass) |
Dennis Dmitriev | ad5f858 | 2019-04-12 13:15:08 +0300 | [diff] [blame] | 468 | ) |
| 469 | |
| 470 | def create_env_k8s(self): |
| 471 | """Creates static utils/env_k8s file""" |
| 472 | |
| 473 | env_k8s_filename = pkg_resources.resource_filename( |
| 474 | settings.__name__, 'utils/env_k8s') |
Oleksii Butenko | 3b3bab1 | 2019-05-23 17:30:15 +0300 | [diff] [blame] | 475 | |
| 476 | tgt = 'I@haproxy:proxy:enabled:true and I@kubernetes:master and *01*' |
| 477 | try: |
| 478 | haproxy_params = self.get_single_pillar( |
| 479 | tgt=tgt, pillar="haproxy:proxy:listen:k8s_secure:binds")[0] |
| 480 | k8s_params = self.get_single_pillar( |
| 481 | tgt=tgt, pillar="kubernetes:master:admin") |
| 482 | except LookupError as e: |
| 483 | LOG.error("Skipping creation {0} because cannot get Kubernetes " |
| 484 | "parameters from '{1}': {2}" |
| 485 | .format(env_k8s_filename, tgt, e.message)) |
Dennis Dmitriev | ad5f858 | 2019-04-12 13:15:08 +0300 | [diff] [blame] | 486 | return |
Oleksii Butenko | 3b3bab1 | 2019-05-23 17:30:15 +0300 | [diff] [blame] | 487 | |
Dennis Dmitriev | ad5f858 | 2019-04-12 13:15:08 +0300 | [diff] [blame] | 488 | kube_host = haproxy_params['address'] |
| 489 | kube_apiserver_port = haproxy_params['port'] |
| 490 | kubernetes_admin_user = k8s_params['username'] |
| 491 | kubernetes_admin_password = k8s_params['password'] |
| 492 | |
| 493 | with open(env_k8s_filename, 'w') as f: |
| 494 | f.write( |
| 495 | 'export kube_host={host}\n' |
| 496 | 'export kube_apiserver_port={port}\n' |
| 497 | 'export kubernetes_admin_user={user}\n' |
| 498 | 'export kubernetes_admin_password={password}\n' |
| 499 | 'echo "export kube_host=${{kube_host}}' |
| 500 | ' # Kube API host"\n' |
| 501 | 'echo "export kube_apiserver_port=${{kube_apiserver_port}}' |
| 502 | ' # Kube API port"\n' |
| 503 | 'echo "export kubernetes_admin_user=${{kubernetes_admin_user}}' |
| 504 | ' # Kube API username"\n' |
| 505 | 'echo "export kubernetes_admin_password=' |
| 506 | '${{kubernetes_admin_password}} # Kube API password"n\n' |
| 507 | .format(host=kube_host, port=kube_apiserver_port, |
| 508 | user=kubernetes_admin_user, |
| 509 | password=kubernetes_admin_password) |
| 510 | ) |