blob: fbbb9b82ca8b12f61cd14f84efcbbdf9bbe7a821 [file] [log] [blame]
Dennis Dmitriev010f4cd2016-11-01 20:43:51 +02001# 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 Tyzhnenkob8641832017-11-07 17:02:47 +020015import netaddr
Dennis Dmitrievad5f8582019-04-12 13:15:08 +030016import pkg_resources
Dmitry Tyzhnenkob8641832017-11-07 17:02:47 +020017
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030018from collections import defaultdict
Hanna Arhipova7a593da2021-06-25 11:16:09 +030019from polling import poll
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030020
21from datetime import datetime
Dennis Dmitrievb8115f52017-12-15 13:09:56 +020022from pepper import libpepper
23from tcp_tests.helpers import utils
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030024from tcp_tests import logger
Dmitry Tyzhnenko35413c02018-03-05 14:12:37 +020025from tcp_tests import settings
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030026from tcp_tests.managers.execute_commands import ExecuteCommandsMixin
27
28LOG = logger.logger
29
30
31class SaltManager(ExecuteCommandsMixin):
Dennis Dmitriev010f4cd2016-11-01 20:43:51 +020032 """docstring for SaltManager"""
33
Dmitry Tyzhnenkobc0f8262017-04-28 15:39:26 +030034 __config = None
35 __underlay = None
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030036 _map = {
37 'enforceState': 'enforce_state',
38 'enforceStates': 'enforce_states',
39 'runState': 'run_state',
40 'runStates': 'run_states',
41 }
Dennis Dmitriev010f4cd2016-11-01 20:43:51 +020042
Dmitry Tyzhnenko5a5d8da2017-12-14 14:14:42 +020043 def __init__(self, config, underlay, host=None, port='6969',
44 username=None, password=None):
Dmitry Tyzhnenkobc0f8262017-04-28 15:39:26 +030045 self.__config = config
46 self.__underlay = underlay
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +030047 self.__port = port
48 self.__host = host
49 self.__api = None
Dmitry Tyzhnenko5a5d8da2017-12-14 14:14:42 +020050 self.__user = username or settings.SALT_USER
51 self.__password = password or settings.SALT_PASSWORD
Dennis Dmitrieveac3aab2017-07-12 16:36:41 +030052 self._salt = self
Dennis Dmitriev010f4cd2016-11-01 20:43:51 +020053
Dmitry Tyzhnenkobc0f8262017-04-28 15:39:26 +030054 super(SaltManager, self).__init__(config=config, underlay=underlay)
Dennis Dmitriev010f4cd2016-11-01 20:43:51 +020055
56 def install(self, commands):
Dina Belovae6fdffb2017-09-19 13:58:34 -070057 # if self.__config.salt.salt_master_host == '0.0.0.0':
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +030058 # # 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 Dmitriev010f4cd2016-11-01 20:43:51 +020062
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030063 self.execute_commands(commands=commands,
64 label="Install and configure salt")
Dennis Dmitrievad5f8582019-04-12 13:15:08 +030065 self.create_env_salt()
66 self.create_env_jenkins_day01()
Oleksii Butenko3b3bab12019-05-23 17:30:15 +030067 # self.create_env_jenkins_cicd()
68 # self.create_env_k8s()
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030069
Dmitry Tyzhnenko5a5d8da2017-12-14 14:14:42 +020070 def change_creds(self, username, password):
71 self.__user = username
72 self.__password = password
73
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030074 @property
75 def port(self):
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +030076 return self.__port
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030077
78 @property
79 def host(self):
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +030080 if self.__host:
81 return self.__host
82 else:
Dina Belovae6fdffb2017-09-19 13:58:34 -070083 # TODO(ddmitriev): consider to add a check and raise
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +030084 # exception if 'salt_master_host' is not initialized.
85 return self.__config.salt.salt_master_host
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030086
87 @property
88 def api(self):
89 def login():
90 LOG.info("Authentication in Salt API")
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +030091 self.__api.login(
92 username=self.__user,
93 password=self.__password,
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030094 eauth='pam')
95 return datetime.now()
96
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +030097 if self.__api:
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030098 if (datetime.now() - self.__session_start).seconds < 5 * 60:
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +030099 return self.__api
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300100 else:
101 # FIXXME: Change to debug
102 LOG.info("Session's expired")
103 self.__session_start = login()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300104 return self.__api
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300105
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300106 url = "http://{host}:{port}".format(
107 host=self.host, port=self.port)
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300108 LOG.info("Connecting to Salt API {0}".format(url))
Dennis Dmitrievb8115f52017-12-15 13:09:56 +0200109 self.__api = libpepper.Pepper(url)
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300110 self.__session_start = login()
Dennis Dmitriev2d60c8e2017-05-12 18:34:01 +0300111 return self.__api
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300112
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000113 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 Tyzhnenko2b730a02017-04-07 19:31:32 +0300116
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000117 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 Tyzhnenko2b730a02017-04-07 19:31:32 +0300119
Hanna Arhipova7a593da2021-06-25 11:16:09 +0300120 def runner(self, fun, args=None, **kwargs):
121 response = self.api.runner(fun, arg=args, **kwargs)
122 return response.get("return")
123
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300124 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 Tyzhnenkobc0f8262017-04-28 15:39:26 +0300132 if len(result) == 0:
133 raise LookupError("Result is empty or absent")
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300134 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 Dmitrievb6bcc5c2018-09-26 11:07:53 +0000152 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 Tyzhnenko2b730a02017-04-07 19:31:32 +0300154 f = self.check_result(r)
155 return r, f
156
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000157 def enforce_states(self, tgt, state, args=None, kwargs=None, timeout=None):
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300158 rets = []
159 for s in state:
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000160 r = self.enforce_state(tgt=tgt, state=s, timeout=timeout)
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300161 rets.append(r)
162 return rets
163
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000164 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 Tyzhnenko2b730a02017-04-07 19:31:32 +0300167
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000168 def run_states(self, tgt, state, args=None, kwargs=None, timeout=None):
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300169 rets = []
170 for s in state:
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000171 r = self.run_state(tgt=tgt, state=s, args=args, kwargs=kwargs,
172 timeout=timeout)
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +0300173 rets.append(r)
174 return rets
Artem Panchenko0594cd72017-06-12 13:25:26 +0300175
176 def get_pillar(self, tgt, pillar):
177 result = self.local(tgt=tgt, fun='pillar.get', args=pillar)
178 return result['return']
Dmitry Tyzhnenkob8641832017-11-07 17:02:47 +0200179
Dennis Dmitriev2a498732018-12-21 18:30:23 +0200180 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 Dmitriev2d643bc2017-12-04 12:23:47 +0200198 def get_grains(self, tgt, grains):
199 result = self.local(tgt=tgt, fun='grains.get', args=grains)
200 return result['return']
201
Hanna Arhipova7a593da2021-06-25 11:16:09 +0300202 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 Tyzhnenkob8641832017-11-07 17:02:47 +0200216 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 Dmitriev83cc1d52018-11-09 15:35:30 +0200233 def host(minion_id, ip):
Dmitry Tyzhnenkob8641832017-11-07 17:02:47 +0200234 return {
235 'roles': ['salt_minion'],
236 'keys': [
237 k['private'] for k in self.__config.underlay.ssh_keys
238 ],
Dennis Dmitriev83cc1d52018-11-09 15:35:30 +0200239 'node_name': minion_id,
240 'minion_id': minion_id,
Dmitry Tyzhnenkob8641832017-11-07 17:02:47 +0200241 'host': ip,
242 'address_pool': pool_name,
243 'login': settings.SSH_NODE_CREDENTIALS['login'],
244 'password': settings.SSH_NODE_CREDENTIALS['password']
245 }
246
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200247 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 Dmitriev2d643bc2017-12-04 12:23:47 +0200261
Dennis Dmitriev83cc1d52018-11-09 15:35:30 +0200262 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 Dmitriev2d643bc2017-12-04 12:23:47 +0200281 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 Dmitrievb8115f52017-12-15 13:09:56 +0200292
Dmitriy Kruglov07977de2019-09-02 13:15:18 +0200293 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 Ryzhenkin95046882018-12-29 19:18:40 +0400301 def cmd_run(self, tgt, cmd):
302 result = self.local(tgt=tgt, fun='cmd.run', args=cmd)
303 return result['return']
304
Hanna Arhipova653d5562020-08-03 15:32:09 +0300305 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 Dmitrievfa1774a2019-05-28 15:27:44 +0300317 @utils.retry(10, exception=libpepper.PepperException)
Dennis Dmitriev427e4152019-05-08 15:12:43 +0300318 def sync_time(self, tgt='*'):
Dennis Dmitrievb8115f52017-12-15 13:09:56 +0200319 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 Tyzhnenko80ce0202019-02-07 13:27:19 +0200324 if not settings.SKIP_SYNC_TIME:
Andrew Baraniuk5da5c582019-06-20 12:12:45 +0300325 cmd = ('chmod -x /usr/sbin/ntpd'
326 'service ntp stop;'
Dennis Dmitrievfa1774a2019-05-28 15:27:44 +0300327 # note: maas-rackd will return 'pool' after start
Andrew Baraniukab06b7a2019-06-18 13:53:00 +0300328 'sed -i \'s/^pool ntp.ubuntu.com/server ntp.cesnet.cz/g\' '
329 '/etc/ntp/maas.conf;'
Andrew Baraniuk5da5c582019-06-20 12:12:45 +0300330 'sed -i \'s/^pool ntp.ubuntu.com/server ntp.cesnet.cz/g\' '
331 '/etc/ntp.conf;'
Dennis Dmitriev427e4152019-05-08 15:12:43 +0300332 'if [ -x /usr/sbin/ntpdate ]; then'
Andrew Baraniuk5da5c582019-06-20 12:12:45 +0300333 ' ntpdate -s ntp.cesnet.cz;'
Dennis Dmitriev427e4152019-05-08 15:12:43 +0300334 'else'
335 ' ntpd -gq;'
336 'fi;'
Andrew Baraniuk5da5c582019-06-20 12:12:45 +0300337 'chmod +x /usr/sbin/ntpd'
Dennis Dmitriev427e4152019-05-08 15:12:43 +0300338 'service ntp start;'
Andrew Baraniuk5da5c582019-06-20 12:12:45 +0300339 'sleep 3; ntpq -pn;')
Dmitry Tyzhnenko80ce0202019-02-07 13:27:19 +0200340 self.run_state(
341 tgt,
Dennis Dmitrievfa1774a2019-05-28 15:27:44 +0300342 'cmd.run', cmd, timeout=360) # noqa
Dennis Dmitrievb8115f52017-12-15 13:09:56 +0200343 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 Dmitrievad5f8582019-04-12 13:15:08 +0300347
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")
obutenkoca858402019-07-04 18:31:39 +0300378 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 Dmitrievad5f8582019-04-12 13:15:08 +0300393
394 with open(env_jenkins_day01_filename, 'w') as f:
395 f.write(
obutenkoca858402019-07-04 18:31:39 +0300396 'export JENKINS_URL={protocol}://{host}:{port}\n'
Dennis Dmitrievad5f8582019-04-12 13:15:08 +0300397 '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'
obutenkoca858402019-07-04 18:31:39 +0300411 .format(host=jenkins_host, port=jenkins_port,
412 protocol=jenkins_protocol, user=jenkins_user,
413 password=jenkins_pass)
Dennis Dmitrievad5f8582019-04-12 13:15:08 +0300414 )
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 Butenko3b3bab12019-05-23 17:30:15 +0300421
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 Dmitrievad5f8582019-04-12 13:15:08 +0300430 return
Oleksii Butenko3b3bab12019-05-23 17:30:15 +0300431
obutenkoca858402019-07-04 18:31:39 +0300432 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 Dmitrievad5f8582019-04-12 13:15:08 +0300447
448 with open(env_jenkins_cicd_filename, 'w') as f:
449 f.write(
obutenkoca858402019-07-04 18:31:39 +0300450 'export JENKINS_URL={protocol}://{host}:{port}\n'
Dennis Dmitrievad5f8582019-04-12 13:15:08 +0300451 '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,
obutenkoca858402019-07-04 18:31:39 +0300466 protocol=jenkins_protocol, user=jenkins_user,
467 password=jenkins_pass)
Dennis Dmitrievad5f8582019-04-12 13:15:08 +0300468 )
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 Butenko3b3bab12019-05-23 17:30:15 +0300475
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 Dmitrievad5f8582019-04-12 13:15:08 +0300486 return
Oleksii Butenko3b3bab12019-05-23 17:30:15 +0300487
Dennis Dmitrievad5f8582019-04-12 13:15:08 +0300488 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 )