blob: c9c063b4168594e4d939da0caf307bae6e98e0e0 [file] [log] [blame]
Dennis Dmitriev8565c342019-02-11 23:45:03 +02001#!/usr/bin/env python
2# Copyright 2019 Mirantis, Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16import argparse
17import os
18import sys
19
20sys.path.append(os.getcwd())
21try:
22 from tcp_tests.managers.jenkins.client import JenkinsClient
23except ImportError:
24 print("ImportError: Run the application from the tcp-qa directory or "
25 "set the PYTHONPATH environment variable to directory which contains"
26 " ./tcp_tests")
27 sys.exit(1)
28
29
30def load_params():
31 """
32 Parse CLI arguments and environment variables
33
34 Returns: ArgumentParser instance
35 """
36 env_host = os.environ.get('JENKINS_URL', None)
37 env_username = os.environ.get('JENKINS_USER', None)
38 env_password = os.environ.get('JENKINS_PASS', None)
39 env_job_name = os.environ.get('JOB_NAME', None)
40 env_build_number = os.environ.get('BUILD_NUMBER', 'lastBuild')
41
42 parser = argparse.ArgumentParser(description=(
43 'Host, username and password may be specified either by the command '
44 'line arguments or using environment variables: JENKINS_URL, '
45 'JENKINS_USER, JENKINS_PASS. \nCommand line arguments have the highest'
46 ' priority, after that the environment variables are used as defaults.'
47 ))
48 parser.add_argument('--host',
49 metavar='JENKINS_URL',
50 help='Jenkins Host',
51 default=env_host)
52 parser.add_argument('--username',
53 metavar='JENKINS_USER',
54 help='Jenkins Username',
55 default=env_username)
56 parser.add_argument('--password',
57 metavar='JENKINS_PASS',
58 help='Jenkins Password or API token',
59 default=env_password)
60 parser.add_argument('--job-name',
61 metavar='JOB_NAME',
62 help='Jenkins job name',
63 default=env_job_name)
64 parser.add_argument('--build-number',
65 metavar='BUILD_NUMBER',
66 help='Jenkins job build number',
67 default=env_build_number)
68 parser.add_argument('--artifact-path',
69 help='Relative path of the artifact in Jenkins',
70 default=None,
71 type=str)
72 parser.add_argument('--destination-name',
73 help='Local filename for the saving artifact',
74 default=None,
75 type=str)
76 return parser
77
78
79def download_artifact(host, username, password,
80 job_name, build_number,
81 artifact_path, destination_name):
82
83 jenkins = JenkinsClient(
84 host=host,
85 username=username,
86 password=password)
87
88 content = jenkins.get_artifact(job_name, build_number,
89 artifact_path, destination_name)
90
91 with open(destination_name, 'wb') as f:
92 f.write(content)
93
94
95def main(args=None):
96 parser = load_params()
97 opts = parser.parse_args()
98
Pavel Glazov6ed8d442022-09-20 12:50:18 +040099 if (opts.host is None or opts.job_name is None or
100 opts.artifact_path is None or opts.destination_name is None):
Dennis Dmitriev8565c342019-02-11 23:45:03 +0200101 print("JENKINS_URL, job_name and destination_name are required!")
102 parser.print_help()
103 return 10
104 else:
105 download_artifact(
106 opts.host,
107 opts.username,
108 opts.password,
109 opts.job_name,
110 opts.build_number,
111 opts.artifact_path,
112 opts.destination_name)
113
114
115if __name__ == "__main__":
116 sys.exit(main())