1 /* Copyright 2017 Sandvine
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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
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: 'OSMETSI', 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 string(defaultValue: 'testing-daily', description: '', name: 'DOCKER_TAG'),
35 booleanParam(defaultValue: true, description: '', name: 'SAVE_CONTAINER_ON_FAIL'),
36 booleanParam(defaultValue: false, description: '', name: 'SAVE_CONTAINER_ON_PASS'),
37 booleanParam(defaultValue: true, description: '', name: 'SAVE_ARTIFACTS_ON_SMOKE_SUCCESS'),
38 booleanParam(defaultValue: true, description: '', name: 'DO_STAGE_4'),
39 booleanParam(defaultValue: true, description: '', name: 'DO_BUILD'),
40 booleanParam(defaultValue: true, description: '', name: 'DO_INSTALL'),
41 booleanParam(defaultValue: true, description: '', name: 'DO_SMOKE'),
42 booleanParam(defaultValue: true, description: '', name: 'DO_DOCKERPUSH'),
43 booleanParam(defaultValue: false, description: '', name: 'SAVE_ARTIFACTS_OVERRIDE'),
44 string(defaultValue: '/home/jenkins/hive/openstack-etsi.rc', description: '', name: 'HIVE_VIM_1'),
45 booleanParam(defaultValue: false, description: '', name: 'DO_ROBOT'),
46 string(defaultValue: 'sanity', description: 'sanity/regression are the options', name: 'TEST_NAME'),
47 string(defaultValue: '/home/jenkins/hive/robot-systest.cfg', description: '', name: 'ROBOT_VIM'),
48 string(defaultValue: '/home/jenkins/hive/kubeconfig.yaml', description: '', name: 'KUBECONFIG'),
49 string(defaultValue: '/home/jenkins/hive/clouds.yaml', description: '', name: 'CLOUDS'),
50 string(defaultValue: 'Charmed', description: '', name: 'INSTALLER'),
54 def uninstall_osm(stackName) {
56 export OSM_USE_LOCAL_DEVOPS=true
57 export PATH=$PATH:/snap/bin
58 installers/full_install_osm.sh -y -w /tmp/osm -t ${stackName} -s ${stackName} --test --nolxd --nodocker --nojuju --nohostports --nohostclient --uninstall
62 def run_systest(stackName,tagName,testName,envfile=null) {
63 tempdir = sh(returnStdout: true, script: "mktemp -d").trim()
66 sh(script: "touch ${tempdir}/env")
67 envfile="${tempdir}/env"
69 sh "docker run --network net${stackName} --env-file ${envfile} -v ${tempdir}:/usr/share/osm-devops/systest/reports opensourcemano/osmclient:${tagName} make -C /usr/share/osm-devops/systest ${testName}"
70 sh "cp ${tempdir}/* ."
74 def run_robot_systest(tagName,testName,osmHostname,prometheusHostname,prometheus_port=null,envfile=null,kubeconfig=null,clouds=null,hostfile=null,jujuPassword=null) {
75 tempdir = sh(returnStdout: true, script: "mktemp -d").trim()
78 sh(script: "touch ${tempdir}/env")
79 envfile="${tempdir}/env"
81 PROMETHEUS_PORT_VAR = ""
82 if ( prometheusPort != null) {
83 PROMETHEUS_PORT_VAR = "--env PROMETHEUS_PORT="+prometheusPort
87 hostfilemount="-v "+hostfile+":/etc/hosts"
90 JUJU_PASSWORD_VAR = ""
91 if ( jujuPassword != null) {
92 JUJU_PASSWORD_VAR = "--env JUJU_PASSWORD="+jujuPassword
96 sh "docker run --env OSM_HOSTNAME=${osmHostname} --env PROMETHEUS_HOSTNAME=${prometheusHostname} ${PROMETHEUS_PORT_VAR} ${JUJU_PASSWORD_VAR} --env-file ${envfile} -v ${clouds}:/etc/openstack/clouds.yaml -v ${kubeconfig}:/root/.kube/config -v ${tempdir}:/robot-systest/reports ${hostfilemount} opensourcemano/tests:${tagName} -c -t ${testName}"
98 sh "cp ${tempdir}/* ."
99 outputDirectory = sh(returnStdout: true, script: "pwd").trim()
100 println ("Present Directory is : ${outputDirectory}")
102 $class : 'RobotPublisher',
103 outputPath : "${outputDirectory}",
104 outputFileName : "*.xml",
105 disableArchiveOutput : false,
106 reportFileName : "report.html",
107 logFileName : "log.html",
109 unstableThreshold: 0,
110 otherFiles : "*.png",
115 def archive_logs(remote) {
117 sshCommand remote: remote, command: '''mkdir -p logs'''
118 if (useCharmedInstaller) {
119 sshCommand remote: remote, command: '''
120 for container in `kubectl get pods -n osm | grep -v operator | grep -v NAME| awk '{print $1}'`; do
121 logfile=`echo $container | cut -d- -f1`
122 echo "Extracting log for $logfile"
123 kubectl logs -n osm $container 2>&1 > logs/$logfile.log
127 // collect logs from k8s based installer...
131 sshCommand remote: remote, command: '''ls -al logs'''
132 sshGet remote: remote, from: 'logs', into: '.', override: true
134 archiveArtifacts artifacts: '*.log'
137 def get_value(key, output) {
138 for (String line : output.split( '\n' )) {
139 data = line.split( '\\|' )
140 if (data.length > 1) {
141 if ( data[1].trim() == key ) {
142 return data[2].trim()
148 node("${params.NODE}") {
150 INTERNAL_DOCKER_REGISTRY = 'osm.etsi.org:5050/devops/cicd/'
151 SSH_KEY = '~/hive/cicd_rsa'
154 tag_or_branch = params.GERRIT_BRANCH.replaceAll(/\./,"")
160 ci_helper = load "jenkins/ci-pipelines/ci_helper.groovy"
162 def upstream_main_job = params.UPSTREAM_SUFFIX
164 // upstream jobs always use merged artifacts
165 upstream_main_job += '-merge'
166 container_name_prefix = "osm-${tag_or_branch}"
167 container_name = "${container_name_prefix}"
169 keep_artifacts = false
170 if ( JOB_NAME.contains('merge') ) {
171 container_name += "-merge"
173 // On a merge job, we keep artifacts on smoke success
174 keep_artifacts = params.SAVE_ARTIFACTS_ON_SMOKE_SUCCESS
176 container_name += "-${BUILD_NUMBER}"
178 // Copy the artifacts from the upstream jobs
179 stage("Copy Artifacts") {
180 // cleanup any previous repo
183 // grab all stable upstream builds based on the
186 def list = ["RO", "openvim", "osmclient", "IM", "devops", "MON", "N2VC", "NBI", "common", "LCM", "POL", "LW-UI", "NG-UI", "PLA", "tests"]
187 for (component in list) {
188 step ([$class: 'CopyArtifact',
189 projectName: "${component}${upstream_main_job}/${GERRIT_BRANCH}"])
191 // grab the build name/number
192 build_num = ci_helper.get_env_value('build.env','BUILD_NUMBER')
194 // grab the archives from the stage_2 builds (ie. this will be the artifacts stored based on a merge)
195 ci_helper.get_archive(params.ARTIFACTORY_SERVER,component,GERRIT_BRANCH, "${component}${upstream_main_job} :: ${GERRIT_BRANCH}", build_num)
197 // cleanup any prevously defined dists
201 // check if an upstream artifact based on specific build number has been requested
202 // This is the case of a merge build and the upstream merge build is not yet complete (it is not deemed
203 // a successful build yet). The upstream job is calling this downstream job (with the its build artifiact)
204 if ( params.UPSTREAM_JOB_NAME ) {
205 step ([$class: 'CopyArtifact',
206 projectName: "${params.UPSTREAM_JOB_NAME}",
207 selector: [$class: 'SpecificBuildSelector', buildNumber: "${params.UPSTREAM_JOB_NUMBER}"]
210 build_num = ci_helper.get_env_value('build.env','BUILD_NUMBER')
211 component = ci_helper.get_mdg_from_project(ci_helper.get_env_value('build.env','GERRIT_PROJECT'))
213 // the upstream job name contains suffix with the project. Need this stripped off
214 def project_without_branch = params.UPSTREAM_JOB_NAME.split('/')[0]
216 // Remove the previous artifact for this component. Use the new upstream artifact
217 sh "rm -rf pool/${component}"
219 ci_helper.get_archive(params.ARTIFACTORY_SERVER,component,GERRIT_BRANCH, "${project_without_branch} :: ${GERRIT_BRANCH}", build_num)
224 // sign all the components
225 for (component in list) {
226 sh "dpkg-sig --sign builder -k ${GPG_KEY_NAME} pool/${component}/*"
229 // now create the distro
230 for (component in list) {
231 sh "mkdir -p dists/${params.REPO_DISTRO}/${component}/binary-amd64/"
232 sh "apt-ftparchive packages pool/${component} > dists/${params.REPO_DISTRO}/${component}/binary-amd64/Packages"
233 sh "gzip -9fk dists/${params.REPO_DISTRO}/${component}/binary-amd64/Packages"
236 // create and sign the release file
237 sh "apt-ftparchive release dists/${params.REPO_DISTRO} > dists/${params.REPO_DISTRO}/Release"
238 sh "gpg --yes -abs -u ${GPG_KEY_NAME} -o dists/${params.REPO_DISTRO}/Release.gpg dists/${params.REPO_DISTRO}/Release"
240 // copy the public key into the release folder
241 // this pulls the key from the home dir of the current user (jenkins)
242 sh "cp ~/${REPO_KEY_NAME} 'OSM ETSI Release Key.gpg'"
243 sh "cp ~/${REPO_KEY_NAME} ."
245 // merge the change logs
247 rm -f changelog/changelog-osm.html
248 [ ! -d changelog ] || for mdgchange in \$(ls changelog); do cat changelog/\$mdgchange >> changelog/changelog-osm.html; done
250 RELEASE_DIR = sh(returnStdout:true, script: 'pwd').trim()
252 // start an apache server to serve up the images
253 http_server_name = "${container_name}-apache"
255 pwd = sh(returnStdout:true, script: 'pwd').trim()
256 repo_port = sh(script: 'echo $(python -c \'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()\');', returnStdout: true).trim()
257 repo_base_url = ci_helper.start_http_server(pwd,http_server_name,repo_port)
258 NODE_IP_ADDRESS=sh(returnStdout: true, script:
259 "echo ${SSH_CONNECTION} | awk '{print \$3}'").trim()
262 // now pull the devops package and install in temporary location
263 tempdir = sh(returnStdout: true, script: "mktemp -d").trim()
264 osm_devops_dpkg = sh(returnStdout: true, script: "find . -name osm-devops*.deb").trim()
265 sh "dpkg -x ${osm_devops_dpkg} ${tempdir}"
266 OSM_DEVOPS="${tempdir}/usr/share/osm-devops"
267 println("Repo base URL=${repo_base_url}")
274 if ( params.DO_BUILD ) {
276 sh "make -C docker clean"
277 sh "make -C docker -j `nproc` Q= CMD_DOCKER_ARGS= TAG=${container_name} RELEASE=${params.RELEASE} REPOSITORY_BASE=${repo_base_url} REPOSITORY_KEY=${params.REPO_KEY_NAME} REPOSITORY=${params.REPO_DISTRO}"
280 stage("Push to internal registry") {
281 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'gitlab-registry',
282 usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
283 sh "docker login ${INTERNAL_DOCKER_REGISTRY} -u ${USERNAME} -p ${PASSWORD}"
285 sh "make -C docker push INPUT_TAG=${container_name} TAG=${container_name} DOCKER_REGISTRY=${INTERNAL_DOCKER_REGISTRY}"
291 useCharmedInstaller = params.INSTALLER.equalsIgnoreCase("charmed")
293 if ( params.DO_INSTALL ) {
295 stage("Spawn Remote VM") {
296 println("Launching new VM")
297 output=sh(returnStdout: true, script: """#!/bin/sh -e
298 for line in `grep OS ~/hive/robot-systest.cfg | grep -v OS_CLOUD` ; do export \$line ; done
299 openstack server create --flavor m1.xlarge \
300 --image ubuntu18.04 \
302 --nic net-id=osm-ext \
306 server_id = get_value('id', output)
308 if (server_id == null) {
309 println("VM launch output: ")
311 throw new Exception("VM Launch failed")
313 println("Target VM is ${server_id}, waiting for IP address to be assigned")
317 while (IP_ADDRESS == "") {
318 output=sh(returnStdout: true, script: """#!/bin/sh -e
319 for line in `grep OS ~/hive/robot-systest.cfg | grep -v OS_CLOUD` ; do export \$line ; done
320 openstack server show ${server_id}
322 IP_ADDRESS = get_value('addresses', output)
324 IP_ADDRESS = IP_ADDRESS.split('=')[1]
325 println("Waiting for VM at ${IP_ADDRESS} to be reachable")
329 output=sh(returnStdout: true, script: "sleep 1 ; nc -zv ${IP_ADDRESS} 22 2>&1 || true").trim()
330 println("output is [$output]")
331 alive = output.contains("succeeded")
333 println("VM is ready and accepting ssh connections")
342 if ( params.COMMIT_ID )
344 commit_id = "-b ${params.COMMIT_ID}"
347 if ( params.REPO_DISTRO )
349 repo_distro = "-r ${params.REPO_DISTRO}"
352 if ( params.REPO_KEY_NAME )
354 repo_key_name = "-k ${params.REPO_KEY_NAME}"
357 if ( params.RELEASE )
359 release = "-R ${params.RELEASE}"
362 if ( params.REPOSITORY_BASE )
364 repo_base_url = "-u ${params.REPOSITORY_BASE}"
368 repo_base_url = "-u http://${NODE_IP_ADDRESS}:${repo_port}"
371 remote.name = container_name
372 remote.host = IP_ADDRESS
373 remote.user = 'ubuntu'
374 remote.identityFile = SSH_KEY
375 remote.allowAnyHosts = true
376 remote.logLevel = 'INFO'
378 sshCommand remote: remote, command: """
379 wget https://osm-download.etsi.org/ftp/osm-8.0-eight/install_osm.sh
380 chmod +x ./install_osm.sh
383 if ( useCharmedInstaller ) {
385 // Use local proxy for docker hub
386 sshCommand remote: remote, command: '''
387 sudo snap install microk8s --classic --channel=1.19/stable
388 sudo sed -i "s|https://registry-1.docker.io|http://172.21.1.1:5000|" /var/snap/microk8s/current/args/containerd-template.toml
389 sudo systemctl restart snap.microk8s.daemon-containerd.service
390 sudo snap alias microk8s.kubectl kubectl
391 echo export PATH=/snap/bin:\${PATH} > ~/.bashrc
394 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'gitlab-registry',
395 usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
396 sshCommand remote: remote, command: """
397 ./install_osm.sh -y \
400 ${release} -r unstable \
402 --registry ${USERNAME}:${PASSWORD}@${INTERNAL_DOCKER_REGISTRY} \
403 --tag ${container_name}
406 prometheusHostname = "prometheus."+IP_ADDRESS+".xip.io"
408 osmHostname = "nbi."+IP_ADDRESS+".xip.io:443"
410 // Run -k8s installer here
411 // Update to use 172.21.1.1 as the dockerhub proxy
412 // Specify registry to use for installer
413 // set osmHostname, prometheusHostname, prometheusPort as needed
418 stage_archive = false
419 if ( params.DO_SMOKE ) {
420 stage("OSM Health") {
421 if ( useCharmedInstaller ) {
424 stackName = container_name
426 sshCommand remote: remote, command: """
427 /usr/share/osm-devops/installers/osm_health.sh -k -s ${stackName}
432 if ( params.DO_STAGE_4 ) {
433 // override stage_archive to only archive on stable
434 stage_archive = false
436 stage("System Integration Test") {
437 if ( params.DO_ROBOT ) {
438 if( useCharmedInstaller ) {
439 tempdir = sh(returnStdout: true, script: "mktemp -d").trim()
440 sh(script: "touch ${tempdir}/hosts")
441 hostfile="${tempdir}/hosts"
442 sh """cat << EOF > ${hostfile}
444 ${remote.host} prometheus.${remote.host}.xip.io nbi.${remote.host}.xip.io
450 jujuPassword=sshCommand remote: remote, command: """
451 echo `juju gui 2>&1 | grep password | cut -d: -f2`
468 stage("Archive Contailer Logs") {
469 // Archive logs to containers_logs.txt
471 if ( ! currentBuild.result.equals('UNSTABLE') && ! currentBuild.result.equals('FAILURE')) {
472 stage_archive = keep_artifacts
474 println ("Systest test failed, throwing error")
475 error = new Exception("Systest test failed")
476 currentBuild.result = 'FAILURE'
483 // override to save the artifacts
484 if ( params.SAVE_ARTIFACTS_OVERRIDE || stage_archive ) {
486 sh "echo ${container_name} > build_version.txt"
487 archiveArtifacts artifacts: "build_version.txt", fingerprint: true
489 // Archive the tested repo
490 dir("${RELEASE_DIR}") {
491 ci_helper.archive(params.ARTIFACTORY_SERVER,RELEASE,GERRIT_BRANCH,'tested')
493 if ( params.DO_DOCKERPUSH ) {
494 stage("Docker Push") {
495 sh "make -C docker push INPUT_TAG=${container_name} TAG=${params.DOCKER_TAG}"
498 stage("Snap promotion") {
499 def snaps = ["osmclient"]
500 for (snap in snaps) {
502 if (BRANCH_NAME.startsWith("v")) {
503 channel=BRANCH_NAME.substring(1)+"/"
504 } else if (BRANCH_NAME!="master") {
505 channel+="/"+BRANCH_NAME.replaceAll('/','-')
507 track=channel+"edge\\*"
508 edge_rev=sh(returnStdout: true,
509 script: "sudo docker run -v ~/.snapcraft:/snapcraft -v ${WORKSPACE}:/build " +
510 "-w /build snapcore/snapcraft:stable /bin/bash -c " +
511 "\"snapcraft login --with /snapcraft/config &>/dev/null && " +
512 "snapcraft revisions $snap\" | " +
513 "grep \" $track\" | tail -1 | awk '{print \$1}'").trim()
514 track=channel+"beta\\*"
515 beta_rev=sh(returnStdout: true,
516 script: "sudo docker run -v ~/.snapcraft:/snapcraft -v ${WORKSPACE}:/build " +
517 "-w /build snapcore/snapcraft:stable /bin/bash -c " +
518 "\"snapcraft login --with /snapcraft/config &>/dev/null && " +
519 "snapcraft revisions $snap\" | " +
520 "grep \" $track\" | tail -1 | awk '{print \$1}'").trim()
522 if ( edge_rev != beta_rev ) {
523 print "Promoting $edge_rev to beta in place of $beta_rev"
524 beta_track=channel+"beta"
525 sh("sudo docker run -v ~/.snapcraft:/snapcraft -v ${WORKSPACE}:/build " +
526 "-w /build snapcore/snapcraft:stable /bin/bash -c " +
527 "\"snapcraft login --with /snapcraft/config &>/dev/null && " +
528 "snapcraft release $snap $edge_rev $beta_track\"")
536 catch(Exception ex) {
538 currentBuild.result = 'FAILURE'
539 println("Caught error: "+ex)
542 println("Entered finally block")
543 if ( params.DO_INSTALL && server_id != null) {
545 if (error && params.SAVE_CONTAINER_ON_FAIL ) {
548 if (!error && params.SAVE_CONTAINER_ON_PASS ) {
553 if (server_id != null) {
554 println("Deleting VM: $server_id")
556 for line in `grep OS ~/hive/robot-systest.cfg | grep -v OS_CLOUD` ; do export \$line ; done
557 openstack server delete ${server_id}
560 println("Saved VM $server_id in ETSI VIM")
564 sh "docker stop ${http_server_name} || true"
565 sh "docker rm ${http_server_name} || true"