1e724ff7a522c47d9514c130ed03a068a1263c09
[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     server.download(downloadSpec)
39     // workaround.  flatten repo to remove specific build num from the directory
40     sh "cp -R ${branch}/${build_num}/* ."
41     sh "rm -rf ${branch}/${build_num}"
42 }
43
44 def get_env_value(build_env_file,key) {
45     return sh(returnStdout:true,  script: "cat ${build_env_file} | awk -F= '/${key}/{print \$2}'").trim()
46 }
47
48 def lxc_run(container_name,cmd) {
49     return sh(returnStdout: true, script: "lxc exec ${container_name} -- ${cmd}").trim()
50 }
51
52 def lxc_file_push(container_name,file,destination) {
53     return sh(returnStdout: true, script: "lxc file push ${file} ${container_name}/${destination}").trim()
54 }
55
56 // start a http server
57 // return the http server URL
58 def start_http_server(repo_dir,server_name) {
59     sh "docker run -dit --name ${server_name} -v ${repo_dir}:/usr/local/apache2/htdocs/ httpd:2.4"
60     def http_server_ip = sh(returnStdout:true,  script: "docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${server_name}").trim()
61     return "http://${http_server_ip}/"
62 }
63
64 def lxc_get_file(container_name,file,destination) {
65     sh "lxc file pull ${container_name}/${file} ${destination}"
66 }
67
68 def systest_run(container_name, test, source_rc = null) {
69     // need to get the SO IP inside the running container
70     so_ip = lxc_run(container_name,"lxc list SO-ub -c 4|grep eth0 |awk '{print \$2}'")
71     ro_ip = lxc_run(container_name,"lxc list RO -c 4|grep eth0 |awk '{print \$2}'")
72     //container_ip = get_ip_from_container(container_name)
73  
74     if ( source_rc ) {
75         pre_source = "/tmp/" + source_rc.substring(source_rc.lastIndexOf('/')+1)
76  
77         lxc_file_push(container_name,source_rc,pre_source)
78         result = lxc_run(container_name, "sh -c '. ${pre_source}; make -C devops/systest OSM_HOSTNAME=${so_ip} OSM_RO_HOSTNAME=${ro_ip} ${test}'")
79         echo result
80     }
81     else
82     {
83         result = lxc_run(container_name, "make -C devops/systest OSM_HOSTNAME=${so_ip} OSM_RO_HOSTNAME=${ro_ip} ${test}")
84         echo result
85     }
86     lxc_get_file(container_name, "/root/devops/systest/reports/pytest-${test}.xml",'.')
87 }
88
89 def get_ip_from_container( container_name ) {
90     return sh(returnStdout: true, script: "lxc list ${container_name} -c 4|grep eth0 |awk '{print \$2}'").trim()
91 }
92
93 def archive(artifactory_server,mdg,branch,status) {
94     server = Artifactory.server artifactory_server
95
96     def properties = ""
97     //def properties = "branch=${branch};status=${status}"
98     def repo_prefix = 'osm-'
99
100     // if the build name does not contain merge, then this is a patchset/staging job
101     if ( !JOB_NAME.contains('merge') ) {
102         branch += '-staging'
103     }
104     def uploadSpec = """{
105      "files": [
106         {
107           "pattern": "dists/*.gz",
108           "target": "${repo_prefix}${mdg}/${branch}/${BUILD_NUMBER}/",
109           "props": "${properties}",
110           "flat": false
111         },
112         {
113           "pattern": "dists/*Packages",
114           "target": "${repo_prefix}${mdg}/${branch}/${BUILD_NUMBER}/",
115           "props": "${properties}",
116           "flat": false
117         },
118         {
119           "pattern": "pool/*/*.deb",
120           "target": "${repo_prefix}${mdg}/${branch}/${BUILD_NUMBER}/",
121           "props": "${properties}",
122           "flat": false
123         },
124         {
125           "pattern": "changelog/*",
126           "target": "${repo_prefix}${mdg}/${branch}/${BUILD_NUMBER}/",
127           "props": "${properties}",
128           "flat": false
129         }]
130     }"""
131
132     buildInfo = server.upload(uploadSpec)
133     //buildInfo.retention maxBuilds: 4
134     //buildInfo.retention deleteBuildArtifacts: false
135
136     server.publishBuildInfo(buildInfo)
137
138     // store the build environment into the jenkins artifact storage
139     sh 'env > build.env'
140     archiveArtifacts artifacts: "build.env", fingerprint: true
141 }
142
143
144 //CANNOT use build promotion with OSS version of artifactory
145 // For now, will publish downloaded artifacts into a new repo.
146 def promote_build(artifactory_server,mdg,branch,buildInfo) {
147     println("Promoting build: mdg: ${mdg} branch: ${branch} build: ${buildInfo.name}/${buildInfo.number}")
148
149     server = Artifactory.server artifactory_server
150
151     //def properties = "branch=${branch};status=${status}"
152     def repo_prefix = 'osm-'
153     def build_name = "${mdg}-stage_2 :: ${branch}"
154
155     def promotionConfig = [
156         // Mandatory parameters
157         "buildName"          : buildInfo.name,
158         "buildNumber"        : buildInfo.number,
159         'targetRepo'         : 'osm-release',
160      
161         // Optional parameters
162         'comment'            : 'this is the promotion comment',
163         'sourceRepo'         : "${repo_prefix}${mdg}",
164         'status'             : 'Testing',
165         'includeDependencies': true,
166         'copy'               : true,
167         // 'failFast' is true by default.
168         // Set it to false, if you don't want the promotion to abort upon receiving the first error.
169         'failFast'           : true
170     ]
171
172     server.promote promotionConfig
173 }
174
175 def get_mdg_from_project(project) {
176     // split the project.
177     def values = project.split('/')
178     if ( values.size() > 1 ) {
179         return values[1]
180     }
181     // no prefix, likely just the project name then
182     return project
183 }
184
185
186 return this