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