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