blob: f65892e33f83433643639cb78f35bc84a6eb617b [file] [log] [blame]
sgudzb67ce732018-02-13 17:58:31 +02001# 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
15from tcp_tests.managers.execute_commands import ExecuteCommandsMixin
Hanna Arhipova17b2c102019-09-06 16:44:17 +030016from tcp_tests.utils import run_jenkins_job
17from tcp_tests.utils import get_jenkins_job_stages
18from tcp_tests import logger
19
20LOG = logger.logger
sgudzb67ce732018-02-13 17:58:31 +020021
22
23class 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 Arhipova17b2c102019-09-06 16:44:17 +030040
Hanna Arhipovaa85bfa62020-01-03 18:49:15 +020041 def start_job_on_jenkins(
42 self,
43 job_name,
44 jenkins_tgt='I@docker:client:stack:jenkins and cid01*',
45 **kwargs):
Hanna Arhipova17b2c102019-09-06 16:44:17 +030046 """
47 Starts job with specific parameters on cluster Jenkins
48
49 Method accept any param:
50 job_parameters=None,
51 job_output_prefix='',
Hanna Arhipova508f6532021-01-27 15:52:45 +020052 jenkins_tgt='I@salt:master' or
53 'I@docker:client:stack:jenkins and cid01*'
Hanna Arhipova17b2c102019-09-06 16:44:17 +030054 start_timeout=1800,
55 build_timeout=3600 * 4,
56 verbose=False
57
58 :param job_name: string
Hanna Arhipova508f6532021-01-27 15:52:45 +020059 :param jenkins_tgt: string, node to run jenkins in salt-style
Hanna Arhipova17b2c102019-09-06 16:44:17 +030060 :return: string, Result of passed job, "SUCCESS"| "FAILED" | "UNSTABLE"
61 """
62 jenkins_url, jenkins_user, jenkins_pass = self.get_jenkins_creds(
Hanna Arhipovaa85bfa62020-01-03 18:49:15 +020063 tgt=jenkins_tgt)
Hanna Arhipova17b2c102019-09-06 16:44:17 +030064
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 Arhipova508f6532021-01-27 15:52:45 +020082 job_description = "{description}. \n\n{stages}"\
83 .format(description=description,
84 stages='\n'.join(stages))
85
Hanna Arhipova17b2c102019-09-06 16:44:17 +030086 if job_result != 'SUCCESS':
87 LOG.warning("{0}\n{1}".format(description, '\n'.join(stages)))
Hanna Arhipova508f6532021-01-27 15:52:45 +020088 return job_result, job_description
Hanna Arhipova17b2c102019-09-06 16:44:17 +030089
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