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