sgudz | b67ce73 | 2018-02-13 17:58:31 +0200 | [diff] [blame] | 1 | # Copyright 2017 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 | |
| 15 | from tcp_tests.managers.execute_commands import ExecuteCommandsMixin |
Hanna Arhipova | 17b2c10 | 2019-09-06 16:44:17 +0300 | [diff] [blame] | 16 | from tcp_tests.utils import run_jenkins_job |
| 17 | from tcp_tests.utils import get_jenkins_job_stages |
| 18 | from tcp_tests import logger |
| 19 | |
| 20 | LOG = logger.logger |
sgudz | b67ce73 | 2018-02-13 17:58:31 +0200 | [diff] [blame] | 21 | |
| 22 | |
| 23 | class DrivetrainManager(ExecuteCommandsMixin): |
| 24 | """docstring for DrivetrainManager""" |
| 25 | |
| 26 | __config = None |
| 27 | __underlay = None |
| 28 | |
| 29 | def __init__(self, config, underlay, salt=None): |
| 30 | self.__config = config |
| 31 | self.__underlay = underlay |
| 32 | self._salt = salt |
| 33 | super(DrivetrainManager, self).__init__( |
| 34 | config=config, underlay=underlay) |
| 35 | |
| 36 | def install(self, commands): |
| 37 | self.execute_commands(commands, |
| 38 | label='Install Drivetrain Tools') |
| 39 | self.__config.drivetrain.drivetrain_installed = True |
Hanna Arhipova | 17b2c10 | 2019-09-06 16:44:17 +0300 | [diff] [blame] | 40 | |
Hanna Arhipova | a85bfa6 | 2020-01-03 18:49:15 +0200 | [diff] [blame] | 41 | def start_job_on_jenkins( |
| 42 | self, |
| 43 | job_name, |
| 44 | jenkins_tgt='I@docker:client:stack:jenkins and cid01*', |
| 45 | **kwargs): |
Hanna Arhipova | 17b2c10 | 2019-09-06 16:44:17 +0300 | [diff] [blame] | 46 | """ |
| 47 | Starts job with specific parameters on cluster Jenkins |
| 48 | |
| 49 | Method accept any param: |
| 50 | job_parameters=None, |
| 51 | job_output_prefix='', |
Hanna Arhipova | 508f653 | 2021-01-27 15:52:45 +0200 | [diff] [blame] | 52 | jenkins_tgt='I@salt:master' or |
| 53 | 'I@docker:client:stack:jenkins and cid01*' |
Hanna Arhipova | 17b2c10 | 2019-09-06 16:44:17 +0300 | [diff] [blame] | 54 | start_timeout=1800, |
| 55 | build_timeout=3600 * 4, |
| 56 | verbose=False |
| 57 | |
| 58 | :param job_name: string |
Hanna Arhipova | 508f653 | 2021-01-27 15:52:45 +0200 | [diff] [blame] | 59 | :param jenkins_tgt: string, node to run jenkins in salt-style |
Hanna Arhipova | 17b2c10 | 2019-09-06 16:44:17 +0300 | [diff] [blame] | 60 | :return: string, Result of passed job, "SUCCESS"| "FAILED" | "UNSTABLE" |
| 61 | """ |
| 62 | jenkins_url, jenkins_user, jenkins_pass = self.get_jenkins_creds( |
Hanna Arhipova | a85bfa6 | 2020-01-03 18:49:15 +0200 | [diff] [blame] | 63 | tgt=jenkins_tgt) |
Hanna Arhipova | 17b2c10 | 2019-09-06 16:44:17 +0300 | [diff] [blame] | 64 | |
| 65 | job_result = run_jenkins_job.run_job( |
| 66 | host=jenkins_url, |
| 67 | username=jenkins_user, |
| 68 | password=jenkins_pass, |
| 69 | job_name=job_name, |
| 70 | **kwargs) |
| 71 | |
| 72 | (description, stages) = get_jenkins_job_stages.get_deployment_result( |
| 73 | host=jenkins_url, |
| 74 | username=jenkins_user, |
| 75 | password=jenkins_pass, |
| 76 | job_name=job_name, |
| 77 | build_number='lastBuild') |
| 78 | |
| 79 | LOG.info(description) |
| 80 | LOG.info('\n'.join(stages)) |
| 81 | |
Hanna Arhipova | 508f653 | 2021-01-27 15:52:45 +0200 | [diff] [blame] | 82 | job_description = "{description}. \n\n{stages}"\ |
| 83 | .format(description=description, |
| 84 | stages='\n'.join(stages)) |
| 85 | |
Hanna Arhipova | 17b2c10 | 2019-09-06 16:44:17 +0300 | [diff] [blame] | 86 | if job_result != 'SUCCESS': |
| 87 | LOG.warning("{0}\n{1}".format(description, '\n'.join(stages))) |
Hanna Arhipova | 508f653 | 2021-01-27 15:52:45 +0200 | [diff] [blame] | 88 | return job_result, job_description |
Hanna Arhipova | 17b2c10 | 2019-09-06 16:44:17 +0300 | [diff] [blame] | 89 | |
| 90 | def get_jenkins_creds(self, tgt): |
| 91 | """ |
| 92 | Requests Jenkins's login parameters from pillars from desired node |
| 93 | |
| 94 | :return: tuple {jenkins_url, jenkins_user, jenkins_pass} |
| 95 | """ |
| 96 | jenkins_host = self._salt.get_single_pillar( |
| 97 | tgt=tgt, pillar="jenkins:client:master:host") |
| 98 | if jenkins_host is None: |
| 99 | raise Exception( |
| 100 | "Can't find 'jenkins:client:master' pillar on {tgt} node." |
| 101 | .format(tgt=tgt)) |
| 102 | jenkins_port = self._salt.get_single_pillar( |
| 103 | tgt=tgt, pillar="jenkins:client:master:port") |
| 104 | jenkins_protocol = self._salt.get_single_pillar( |
| 105 | tgt=tgt, pillar="jenkins:client:master:proto") |
| 106 | jenkins_url = '{0}://{1}:{2}'.format(jenkins_protocol, |
| 107 | jenkins_host, |
| 108 | jenkins_port) |
| 109 | jenkins_user = self._salt.get_single_pillar( |
| 110 | tgt=tgt, pillar="jenkins:client:master:username") |
| 111 | jenkins_pass = self._salt.get_single_pillar( |
| 112 | tgt=tgt, pillar="jenkins:client:master:password") |
| 113 | return jenkins_url, jenkins_user, jenkins_pass |