blob: df2798f47da2f93d3e562728879b51255c7f3a15 [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.
sgudzb67ce732018-02-13 17:58:31 +020014from tcp_tests.managers.execute_commands import ExecuteCommandsMixin
Hanna Arhipova17b2c102019-09-06 16:44:17 +030015from tcp_tests.utils import run_jenkins_job
16from tcp_tests.utils import get_jenkins_job_stages
17from tcp_tests import logger
dtsapikov269f5192021-08-12 13:35:43 +040018from tcp_tests.managers.jenkins import client
Hanna Arhipova17b2c102019-09-06 16:44:17 +030019
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
dtsapikov269f5192021-08-12 13:35:43 +040065 jenkins = client.JenkinsClient(host=jenkins_url,
66 username=jenkins_user,
67 password=jenkins_pass)
68 build_number = jenkins.job_info(job_name)['nextBuildNumber']
69
Hanna Arhipova17b2c102019-09-06 16:44:17 +030070 job_result = run_jenkins_job.run_job(
71 host=jenkins_url,
72 username=jenkins_user,
73 password=jenkins_pass,
74 job_name=job_name,
75 **kwargs)
76
77 (description, stages) = get_jenkins_job_stages.get_deployment_result(
78 host=jenkins_url,
79 username=jenkins_user,
80 password=jenkins_pass,
81 job_name=job_name,
82 build_number='lastBuild')
83
84 LOG.info(description)
85 LOG.info('\n'.join(stages))
86
Hanna Arhipova508f6532021-01-27 15:52:45 +020087 job_description = "{description}. \n\n{stages}"\
88 .format(description=description,
89 stages='\n'.join(stages))
90
dtsapikov269f5192021-08-12 13:35:43 +040091 job_log = jenkins.get_build_output(job_name, build_number)
92
93 with open('tmp/' + job_name + str(build_number) + '.log', 'wb') as f:
PGlazovd576bed2022-05-03 16:26:10 +040094 f.write(job_log.encode('utf-8'))
dtsapikov269f5192021-08-12 13:35:43 +040095
Hanna Arhipova17b2c102019-09-06 16:44:17 +030096 if job_result != 'SUCCESS':
97 LOG.warning("{0}\n{1}".format(description, '\n'.join(stages)))
Hanna Arhipova508f6532021-01-27 15:52:45 +020098 return job_result, job_description
Hanna Arhipova17b2c102019-09-06 16:44:17 +030099
100 def get_jenkins_creds(self, tgt):
101 """
102 Requests Jenkins's login parameters from pillars from desired node
103
104 :return: tuple {jenkins_url, jenkins_user, jenkins_pass}
105 """
106 jenkins_host = self._salt.get_single_pillar(
107 tgt=tgt, pillar="jenkins:client:master:host")
108 if jenkins_host is None:
109 raise Exception(
110 "Can't find 'jenkins:client:master' pillar on {tgt} node."
111 .format(tgt=tgt))
112 jenkins_port = self._salt.get_single_pillar(
113 tgt=tgt, pillar="jenkins:client:master:port")
114 jenkins_protocol = self._salt.get_single_pillar(
115 tgt=tgt, pillar="jenkins:client:master:proto")
116 jenkins_url = '{0}://{1}:{2}'.format(jenkins_protocol,
117 jenkins_host,
118 jenkins_port)
119 jenkins_user = self._salt.get_single_pillar(
120 tgt=tgt, pillar="jenkins:client:master:username")
121 jenkins_pass = self._salt.get_single_pillar(
122 tgt=tgt, pillar="jenkins:client:master:password")
123 return jenkins_url, jenkins_user, jenkins_pass
dtsapikovcbff2332022-01-19 19:32:22 +0400124
125 def check_job_exists_on_jenkins(
126 self,
127 job_name,
128 jenkins_tgt='I@docker:client:stack:jenkins and cid01*'
129 ):
130 jenkins_url, jenkins_user, jenkins_pass = self.get_jenkins_creds(
131 tgt=jenkins_tgt)
132
133 jenkins = client.JenkinsClient(host=jenkins_url,
134 username=jenkins_user,
135 password=jenkins_pass)
136 result = jenkins.find_jobs(job_name)
137 return result