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