Add osmclient sidecar
[osm/devops.git] / jenkins / ci-pipelines / ci_stage_3.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 properties([
19     parameters([
20         string(defaultValue: env.GERRIT_BRANCH, description: '', name: 'GERRIT_BRANCH'),
21         string(defaultValue: 'system', description: '', name: 'NODE'),
22         string(defaultValue: '', description: '', name: 'BUILD_FROM_SOURCE'),
23         string(defaultValue: 'unstable', description: '', name: 'REPO_DISTRO'),
24         string(defaultValue: '', description: '', name: 'COMMIT_ID'),
25         string(defaultValue: '-stage_2', description: '', name: 'UPSTREAM_SUFFIX'),
26         string(defaultValue: 'pubkey.asc', description: '', name: 'REPO_KEY_NAME'),
27         string(defaultValue: 'release', description: '', name: 'RELEASE'),
28         string(defaultValue: '', description: '', name: 'UPSTREAM_JOB_NAME'),
29         string(defaultValue: '', description: '', name: 'UPSTREAM_JOB_NUMBER'),
30         string(defaultValue: '', description: '', name: 'UPSTREAM_JOB_NUMBER'),
31         string(defaultValue: 'dpkg1', description: '', name: 'GPG_KEY_NAME'),
32         string(defaultValue: 'artifactory-osm', description: '', name: 'ARTIFACTORY_SERVER'),
33         string(defaultValue: 'osm-stage_4', description: '', name: 'DOWNSTREAM_STAGE_NAME'),
34         booleanParam(defaultValue: false, description: '', name: 'SAVE_CONTAINER_ON_FAIL'),
35         booleanParam(defaultValue: false, description: '', name: 'SAVE_CONTAINER_ON_PASS'),
36         booleanParam(defaultValue: false, description: '', name: 'DO_STAGE_4'),
37         booleanParam(defaultValue: false, description: '', name: 'DO_INSTALL'),
38         booleanParam(defaultValue: false, description: '', name: 'DO_SMOKE'),
39         booleanParam(defaultValue: false, description: '', name: 'SAVE_ARTIFACTS_OVERRIDE'),
40     ])
41 ])
42
43 node("${params.NODE}") {
44
45     sh 'env'
46
47     tag_or_branch = params.GERRIT_BRANCH.replaceAll(/\./,"")
48
49     stage("Checkout") {
50         checkout scm
51     }
52
53     ci_helper = load "jenkins/ci-pipelines/ci_helper.groovy"
54
55     def upstream_main_job = params.UPSTREAM_SUFFIX
56
57     // upstream jobs always use merged artifacts
58     upstream_main_job += '-merge'
59     container_name_prefix = "osm-${tag_or_branch}"
60     container_name = "${container_name_prefix}"
61     if ( JOB_NAME.contains('merge') ) {
62         container_name += "-merge"
63     }
64     container_name += "-${BUILD_NUMBER}"
65
66     // Copy the artifacts from the upstream jobs
67     stage("Copy Artifacts") {
68         // cleanup any previous repo
69         sh 'rm -rf repo'
70         dir("repo") {
71             // grab all stable upstream builds based on the
72
73             dir("${RELEASE}") {
74                 def list = ["RO", "openvim", "osmclient", "IM", "devops", "MON", "N2VC", "NBI", "common", "LCM"]
75                 for (component in list) {
76                     step ([$class: 'CopyArtifact',
77                            projectName: "${component}${upstream_main_job}/${GERRIT_BRANCH}"])
78
79                     // grab the build name/number
80                     //options = get_env_from_build('build.env')
81                     build_num = ci_helper.get_env_value('build.env','BUILD_NUMBER')
82
83                     // grab the archives from the stage_2 builds (ie. this will be the artifacts stored based on a merge)
84                     ci_helper.get_archive(params.ARTIFACTORY_SERVER,component,GERRIT_BRANCH, "${component}${upstream_main_job} :: ${GERRIT_BRANCH}", build_num)
85
86                     // cleanup any prevously defined dists
87                     sh "rm -rf dists"
88                 }
89
90                 // check if an upstream artifact based on specific build number has been requested
91                 // This is the case of a merge build and the upstream merge build is not yet complete (it is not deemed
92                 // a successful build yet). The upstream job is calling this downstream job (with the its build artifiact)
93                 if ( params.UPSTREAM_JOB_NAME ) {
94                     step ([$class: 'CopyArtifact',
95                            projectName: "${params.UPSTREAM_JOB_NAME}",
96                            selector: [$class: 'SpecificBuildSelector', buildNumber: "${params.UPSTREAM_JOB_NUMBER}"]
97                           ])
98
99                     //options = get_env_from_build('build.env')
100                     // grab the build name/number
101                     //build_num = sh(returnStdout:true,  script: "cat build.env | awk -F= '/BUILD_NUMBER/{print \$2}'").trim()
102                     build_num = ci_helper.get_env_value('build.env','BUILD_NUMBER')
103                     component = ci_helper.get_mdg_from_project(ci_helper.get_env_value('build.env','GERRIT_PROJECT'))
104
105                     // the upstream job name contains suffix with the project. Need this stripped off
106                     def project_without_branch = params.UPSTREAM_JOB_NAME.split('/')[0]
107
108                     ci_helper.get_archive(params.ARTIFACTORY_SERVER,component,GERRIT_BRANCH, "${project_without_branch} :: ${GERRIT_BRANCH}", build_num)
109
110                     sh "rm -rf dists"
111                 }
112                 
113                 // sign all the components
114                 for (component in list) {
115                     sh "dpkg-sig --sign builder -k ${GPG_KEY_NAME} pool/${component}/*"
116                 }
117
118                 // now create the distro
119                 for (component in list) {
120                     sh "mkdir -p dists/${params.REPO_DISTRO}/${component}/binary-amd64/"
121                     sh "apt-ftparchive packages pool/${component} > dists/${params.REPO_DISTRO}/${component}/binary-amd64/Packages"
122                     sh "gzip -9fk dists/${params.REPO_DISTRO}/${component}/binary-amd64/Packages"
123                 }
124
125                 // create and sign the release file
126                 sh "apt-ftparchive release dists/${params.REPO_DISTRO} > dists/${params.REPO_DISTRO}/Release"
127                 sh "gpg --yes -abs -u ${GPG_KEY_NAME} -o dists/${params.REPO_DISTRO}/Release.gpg dists/${params.REPO_DISTRO}/Release"
128
129                 // copy the public key into the release folder
130                 // this pulls the key from the home dir of the current user (jenkins)
131                 sh "cp ~/${REPO_KEY_NAME} ."
132
133                 // merge the change logs
134                 sh """
135                    rm -f changelog/changelog-osm.html
136                    [ ! -d changelog ] || for mdgchange in \$(ls changelog); do cat changelog/\$mdgchange >> changelog/changelog-osm.html; done
137                    """
138             }
139             // start an apache server to serve up the images
140             http_server_name = "${container_name}-apache"
141
142             pwd = sh(returnStdout:true,  script: 'pwd').trim()
143             repo_base_url = ci_helper.start_http_server(pwd,http_server_name)
144         }
145     }
146
147     error = null
148
149     try {
150         if ( params.DO_INSTALL ) {
151             stage("Install") {
152
153                 //will by default always delete containers on complete
154                 //sh "jenkins/system/delete_old_containers.sh ${container_name_prefix}"
155
156                 commit_id = ''
157                 repo_distro = ''
158                 repo_key_name = ''
159                 release = ''
160
161                 if ( params.COMMIT_ID )
162                 {
163                     commit_id = "-b ${params.COMMIT_ID}"
164                 }
165
166                 if ( params.REPO_DISTRO )
167                 {
168                     repo_distro = "-r ${params.REPO_DISTRO}"
169                 }
170
171                 if ( params.REPO_KEY_NAME )
172                 {
173                     repo_key_name = "-k ${params.REPO_KEY_NAME}"
174                 }
175
176                 if ( params.RELEASE )
177                 {
178                     release = "-R ${params.RELEASE}"
179                 }
180          
181                 sh """
182                     export OSM_USE_LOCAL_DEVOPS=true
183                     export PATH=$PATH:/snap/bin
184                     installers/full_install_osm.sh -y -s ${container_name} --nolxd --nodocker --nojuju --nohostports --nohostclient \
185                                                     ${commit_id} \
186                                                     ${repo_distro} \
187                                                     ${repo_base_url} \
188                                                     ${repo_key_name} \
189                                                     ${release} \
190                                                     ${params.BUILD_FROM_SOURCE}
191                    """
192             }
193         }
194
195         if ( params.DO_SMOKE ) {
196             stage("Smoke") {
197                 ci_helper.systest_run(container_name, 'smoke')
198                 junit '*.xml'
199             }
200         }
201
202         stage_4_archive = false
203         if ( params.DO_STAGE_4 ) {
204             stage("stage_4") {
205                 def downstream_params = [
206                     string(name: 'CONTAINER_NAME', value: container_name),
207                     string(name: 'NODE', value: NODE_NAME.split()[0]),
208                 ]
209                 stage_4_result = build job: "${params.DOWNSTREAM_STAGE_NAME}/${GERRIT_BRANCH}", parameters: downstream_params, propagate: false 
210                 currentBuild.result = stage_4_result.result
211
212                 if ( stage_4_result.getResult().equals('SUCCESS') ) {
213                     stage_4_archive = true;
214                 }
215             }
216         }
217
218         // override to save the artifacts
219         if ( params.SAVE_ARTIFACTS_OVERRIDE || stage_4_archive ) {
220             stage("Archive") {
221                 sh "echo ${container_name} > build_version.txt"
222                 archiveArtifacts artifacts: "build_version.txt", fingerprint: true
223
224                 // Archive the tested repo
225                 dir("repo/${RELEASE}") {
226                     ci_helper.archive(params.ARTIFACTORY_SERVER,RELEASE,GERRIT_BRANCH,'tested')
227                 }
228             }
229         }
230     }
231     catch(caughtError) {
232         println("Caught error!")
233         error = caughtError
234         currentBuild.result = 'FAILURE'
235     }
236     finally {
237         sh "docker stop ${http_server_name}"
238         sh "docker rm ${http_server_name}"
239
240         if ( params.DO_INSTALL ) {
241             if (error) {
242                 if ( !params.SAVE_CONTAINER_ON_FAIL ) {
243                     sh "docker stack rm ${container_name}"
244                     sh "export PATH=$PATH:/snap/bin; juju destroy-controller ${container_name}"
245                 }
246                 throw error 
247             }
248             else {
249                 if ( !params.SAVE_CONTAINER_ON_PASS ) {
250                     sh "docker stack rm ${container_name}"
251                     sh "export PATH=$PATH:/snap/bin; juju destroy-controller ${container_name}"
252                 }
253             }
254         }
255     }
256 }