Adding cluster-issuer annotation for TLS provisioning
[osm/devops.git] / jenkins / ci-pipelines / ci_helper.groovy
1 /* Copyright 2017 Sandvine
2  *
3  * All Rights Reserved.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License"); you may
6  *   not use this file except in compliance with the License. You may obtain
7  *   a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  *   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  *   License for the specific language governing permissions and limitations
15  *   under the License.
16  */
17
18 def get_archive(artifactory_server, mdg, branch, build_name, build_number, pattern='*') {
19     server = Artifactory.server artifactory_server
20
21     println("retrieve archive for ${mdg}/${branch}/${build_name}/${build_number}/${pattern}")
22
23     // if the build name does not contain merge, then this is a patchset/staging job
24     if (!build_name.contains('merge')) {
25         branch += '-staging'
26     }
27     def repo_prefix = 'osm-'
28     def downloadSpec = """{
29      "files": [
30         {
31           "target": "./",
32           "pattern": "${repo_prefix}${mdg}/${branch}/${build_number}/${pattern}",
33           "build": "${build_name}/${build_number}"
34         }
35      ]
36     }"""
37
38     def results = server.download(downloadSpec)
39     // Save the list of URLs that we need to pass to the dockerfiles for build
40     def debian_packages = []
41     for ( result in results.getDependencies()) {
42         if (result.remotePath.contains(".deb")) {
43             debian_packages.add(result.remotePath)
44         }
45     }
46
47     // workaround.  flatten repo to remove specific build num from the directory
48     sh "cp -R ${branch}/${build_number}/* ."
49     sh "rm -rf ${branch}/${build_number}"
50
51     return debian_packages
52 }
53
54 def get_env_value(build_env_file,key) {
55     return sh(returnStdout:true,  script: "cat ${build_env_file} | awk -F= '/${key}/{print \$2}'").trim()
56 }
57
58 def lxc_run(container_name,cmd) {
59     return sh(returnStdout: true, script: "lxc exec ${container_name} -- ${cmd}").trim()
60 }
61
62 def lxc_file_push(container_name,file,destination) {
63     return sh(returnStdout: true, script: "lxc file push ${file} ${container_name}/${destination}").trim()
64 }
65
66 // start a http server
67 // return the http server URL
68 def start_http_server(repo_dir,server_name,port) {
69     sh "docker run -dit --name ${server_name} -p ${port}:80 -v ${repo_dir}:/usr/local/apache2/htdocs/ httpd:2.4"
70     def http_server_ip = sh(returnStdout:true,  script: "docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${server_name}").trim()
71     return "http://${http_server_ip}/"
72 }
73
74 def lxc_get_file(container_name,file,destination) {
75     sh "lxc file pull ${container_name}/${file} ${destination}"
76 }
77
78 def systest_run(container_name, test, source_rc = null) {
79     // need to get the SO IP inside the running container
80     so_ip = lxc_run(container_name,"lxc list SO-ub -c 4|grep eth0 |awk '{print \$2}'")
81     ro_ip = lxc_run(container_name,"lxc list RO -c 4|grep eth0 |awk '{print \$2}'")
82     //container_ip = get_ip_from_container(container_name)
83
84     if ( source_rc ) {
85         pre_source = "/tmp/" + source_rc.substring(source_rc.lastIndexOf('/')+1)
86
87         lxc_file_push(container_name,source_rc,pre_source)
88         result = lxc_run(container_name, "sh -c '. ${pre_source}; make -C devops/systest OSM_HOSTNAME=${so_ip} OSM_RO_HOSTNAME=${ro_ip} ${test}'")
89         echo result
90     }
91     else
92     {
93         result = lxc_run(container_name, "make -C devops/systest OSM_HOSTNAME=${so_ip} OSM_RO_HOSTNAME=${ro_ip} ${test}")
94         echo result
95     }
96     lxc_get_file(container_name, "/root/devops/systest/reports/pytest-${test}.xml",'.')
97 }
98
99 def get_ip_from_container( container_name ) {
100     return sh(returnStdout: true, script: "lxc list ${container_name} -c 4|grep eth0 |awk '{print \$2}'").trim()
101 }
102
103 def archive(artifactory_server,mdg,branch,status) {
104     server = Artifactory.server artifactory_server
105
106     def properties = ""
107     //def properties = "branch=${branch};status=${status}"
108     def repo_prefix = 'osm-'
109
110     // if the build name does not contain merge, then this is a patchset/staging job
111     if ( !JOB_NAME.contains('merge') ) {
112         branch += '-staging'
113     }
114     def uploadSpec = """{
115      "files": [
116         {
117           "pattern": "dists/*.gz",
118           "target": "${repo_prefix}${mdg}/${branch}/${BUILD_NUMBER}/",
119           "props": "${properties}",
120           "flat": false
121         },
122         {
123           "pattern": "dists/*Packages",
124           "target": "${repo_prefix}${mdg}/${branch}/${BUILD_NUMBER}/",
125           "props": "${properties}",
126           "flat": false
127         },
128         {
129           "pattern": "pool/*/*.deb",
130           "target": "${repo_prefix}${mdg}/${branch}/${BUILD_NUMBER}/",
131           "props": "${properties}",
132           "flat": false
133         },
134         {
135           "pattern": "changelog/*",
136           "target": "${repo_prefix}${mdg}/${branch}/${BUILD_NUMBER}/",
137           "props": "${properties}",
138           "flat": false
139         }]
140     }"""
141
142     buildInfo = server.upload(uploadSpec)
143     //buildInfo.retention maxBuilds: 4
144     //buildInfo.retention deleteBuildArtifacts: false
145
146     server.publishBuildInfo(buildInfo)
147
148     // store the build environment into the jenkins artifact storage
149     sh 'env > build.env'
150     archiveArtifacts artifacts: "build.env", fingerprint: true
151 }
152
153
154 //CANNOT use build promotion with OSS version of artifactory
155 // For now, will publish downloaded artifacts into a new repo.
156 def promote_build(artifactory_server,mdg,branch,buildInfo) {
157     println("Promoting build: mdg: ${mdg} branch: ${branch} build: ${buildInfo.name}/${buildInfo.number}")
158
159     server = Artifactory.server artifactory_server
160
161     //def properties = "branch=${branch};status=${status}"
162     def repo_prefix = 'osm-'
163     def build_name = "${mdg}-stage_2 :: ${branch}"
164
165     def promotionConfig = [
166         // Mandatory parameters
167         "buildName"          : buildInfo.name,
168         "buildNumber"        : buildInfo.number,
169         'targetRepo'         : 'osm-release',
170
171         // Optional parameters
172         'comment'            : 'this is the promotion comment',
173         'sourceRepo'         : "${repo_prefix}${mdg}",
174         'status'             : 'Testing',
175         'includeDependencies': true,
176         'copy'               : true,
177         // 'failFast' is true by default.
178         // Set it to false, if you don't want the promotion to abort upon receiving the first error.
179         'failFast'           : true
180     ]
181
182     server.promote promotionConfig
183 }
184
185 def get_mdg_from_project(project) {
186     // split the project.
187     def values = project.split('/')
188     if ( values.size() > 1 ) {
189         return values[1]
190     }
191     // no prefix, likely just the project name then
192     return project
193 }
194
195
196 return this