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. |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 14 | # import time |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 15 | |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 16 | from collections import defaultdict |
| 17 | |
| 18 | from datetime import datetime |
| 19 | from pepper.libpepper import Pepper |
| 20 | from tcp_tests import settings |
| 21 | from tcp_tests import logger |
| 22 | from tcp_tests.managers.execute_commands import ExecuteCommandsMixin |
| 23 | |
| 24 | LOG = logger.logger |
| 25 | |
| 26 | |
| 27 | class SaltManager(ExecuteCommandsMixin): |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 28 | """docstring for SaltManager""" |
| 29 | |
Dmitry Tyzhnenko | bc0f826 | 2017-04-28 15:39:26 +0300 | [diff] [blame] | 30 | __config = None |
| 31 | __underlay = None |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 32 | _map = { |
| 33 | 'enforceState': 'enforce_state', |
| 34 | 'enforceStates': 'enforce_states', |
| 35 | 'runState': 'run_state', |
| 36 | 'runStates': 'run_states', |
| 37 | } |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 38 | |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 39 | def __init__(self, config, underlay, host=None, port='6969'): |
Dmitry Tyzhnenko | bc0f826 | 2017-04-28 15:39:26 +0300 | [diff] [blame] | 40 | self.__config = config |
| 41 | self.__underlay = underlay |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 42 | self.__port = port |
| 43 | self.__host = host |
| 44 | self.__api = None |
| 45 | self.__user = settings.SALT_USER |
| 46 | self.__password = settings.SALT_PASSWORD |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 47 | |
Dmitry Tyzhnenko | bc0f826 | 2017-04-28 15:39:26 +0300 | [diff] [blame] | 48 | super(SaltManager, self).__init__(config=config, underlay=underlay) |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 49 | |
| 50 | def install(self, commands): |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 51 | #if self.__config.salt.salt_master_host == '0.0.0.0': |
| 52 | # # Temporary workaround. Underlay should be extended with roles |
| 53 | # salt_nodes = self.__underlay.node_names() |
| 54 | # self.__config.salt.salt_master_host = \ |
| 55 | # self.__underlay.host_by_node_name(salt_nodes[0]) |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 56 | |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 57 | self.execute_commands(commands=commands, |
| 58 | label="Install and configure salt") |
| 59 | |
| 60 | @property |
| 61 | def port(self): |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 62 | return self.__port |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 63 | |
| 64 | @property |
| 65 | def host(self): |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 66 | if self.__host: |
| 67 | return self.__host |
| 68 | else: |
| 69 | #TODO(ddmitriev): consider to add a check and raise |
| 70 | # exception if 'salt_master_host' is not initialized. |
| 71 | return self.__config.salt.salt_master_host |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 72 | |
| 73 | @property |
| 74 | def api(self): |
| 75 | def login(): |
| 76 | LOG.info("Authentication in Salt API") |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 77 | self.__api.login( |
| 78 | username=self.__user, |
| 79 | password=self.__password, |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 80 | eauth='pam') |
| 81 | return datetime.now() |
| 82 | |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 83 | if self.__api: |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 84 | if (datetime.now() - self.__session_start).seconds < 5 * 60: |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 85 | return self.__api |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 86 | else: |
| 87 | # FIXXME: Change to debug |
| 88 | LOG.info("Session's expired") |
| 89 | self.__session_start = login() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 90 | return self.__api |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 91 | |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 92 | url = "http://{host}:{port}".format( |
| 93 | host=self.host, port=self.port) |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 94 | LOG.info("Connecting to Salt API {0}".format(url)) |
| 95 | self.__api = Pepper(url) |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 96 | self.__session_start = login() |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 97 | return self.__api |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 98 | |
| 99 | def local(self, tgt, fun, args=None, kwargs=None): |
| 100 | return self.api.local(tgt, fun, args, kwargs, expr_form='compound') |
| 101 | |
| 102 | def local_async(self, tgt, fun, args=None, kwargs=None): |
| 103 | return self.api.local_async(tgt, fun, args, kwargs) |
| 104 | |
| 105 | def lookup_result(self, jid): |
| 106 | return self.api.lookup_jid(jid) |
| 107 | |
| 108 | def check_result(self, r): |
| 109 | if len(r.get('return', [])) == 0: |
| 110 | raise LookupError("Result is empty or absent") |
| 111 | |
| 112 | result = r['return'][0] |
Dmitry Tyzhnenko | bc0f826 | 2017-04-28 15:39:26 +0300 | [diff] [blame] | 113 | if len(result) == 0: |
| 114 | raise LookupError("Result is empty or absent") |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 115 | LOG.info("Job has result for %s nodes", result.keys()) |
| 116 | fails = defaultdict(list) |
| 117 | for h in result: |
| 118 | host_result = result[h] |
| 119 | LOG.info("On %s executed:", h) |
| 120 | if isinstance(host_result, list): |
| 121 | fails[h].append(host_result) |
| 122 | continue |
| 123 | for t in host_result: |
| 124 | task = host_result[t] |
| 125 | if task['result'] is False: |
| 126 | fails[h].append(task) |
| 127 | LOG.error("%s - %s", t, task['result']) |
| 128 | else: |
| 129 | LOG.info("%s - %s", t, task['result']) |
| 130 | |
| 131 | return fails if fails else None |
| 132 | |
| 133 | def enforce_state(self, tgt, state, args=None, kwargs=None): |
| 134 | r = self.local(tgt=tgt, fun='state.sls', args=state) |
| 135 | f = self.check_result(r) |
| 136 | return r, f |
| 137 | |
| 138 | def enforce_states(self, tgt, state, args=None, kwargs=None): |
| 139 | rets = [] |
| 140 | for s in state: |
| 141 | r = self.enforce_state(tgt=tgt, state=s) |
| 142 | rets.append(r) |
| 143 | return rets |
| 144 | |
| 145 | def run_state(self, tgt, state, args=None, kwargs=None): |
| 146 | return self.local(tgt=tgt, fun=state, args=args, kwargs=kwargs), None |
| 147 | |
| 148 | def run_states(self, tgt, state, args=None, kwargs=None): |
| 149 | rets = [] |
| 150 | for s in state: |
| 151 | r = self.run_state(tgt=tgt, state=s, args=args, kwargs=kwargs) |
| 152 | rets.append(r) |
| 153 | return rets |