blob: 0700d60a5a81e280c4724f7bb20eef2944cada0a [file] [log] [blame]
Richard Felkl2e9e5452017-10-16 09:52:10 +02001/**
2 *
Richard Felkl859f4dd2018-01-04 23:03:27 +01003 * Build mirror image pipeline
Richard Felkl2e9e5452017-10-16 09:52:10 +02004 *
5 * Expected parameters:
Richard Felkl859f4dd2018-01-04 23:03:27 +01006 * IMAGE_NAME - Name of the result image.
7 * OS_CREDENTIALS_ID - ID of credentials for OpenStack API stored in Jenkins.
8 * OS_PROJECT - Project in OpenStack under the VM will be spawned.
9 * OS_URL - Keystone auth endpoint of the OpenStack.
10 * OS_VERSION - OpenStack version
Richard Felkl859f4dd2018-01-04 23:03:27 +010011 * UPLOAD_URL - URL of an WebDAV used to upload the image after creating.
12 * VM_AVAILABILITY_ZONE - Availability zone in OpenStack in the VM will be spawned.
Richard Felkl859f4dd2018-01-04 23:03:27 +010013 * VM_FLAVOR - Flavor to be used for VM in OpenStack.
14 * VM_FLOATING_IP_POOL - Floating IP pool to be used to assign floating IP to the VM.
15 * VM_IMAGE - Name of the image to be used for VM in OpenStack.
16 * VM_IP - Static IP that is assigned to the VM which belongs to the network used.
Richard Felkl859f4dd2018-01-04 23:03:27 +010017 * VM_NETWORK_ID - ID of the network that VM connects to.
azvyagintseva0bc6142018-03-02 21:24:30 +020018 * EXTRA_VARIABLES - list of key:value variables required by template.json
Richard Felkl859f4dd2018-01-04 23:03:27 +010019 *
Richard Felkl2e9e5452017-10-16 09:52:10 +020020 */
21
22// Load shared libs
Richard Felkl2e9e5452017-10-16 09:52:10 +020023def common = new com.mirantis.mk.Common()
Richard Felkl2e9e5452017-10-16 09:52:10 +020024def openstack = new com.mirantis.mk.Openstack()
azvyagintsev1c79f4d2018-03-02 14:25:37 +020025def git = new com.mirantis.mk.Git()
Richard Felkl2e9e5452017-10-16 09:52:10 +020026def date = new Date()
27def dateTime = date.format("ddMMyyyy-HHmmss")
Richard Felkl2e9e5452017-10-16 09:52:10 +020028def openstackServer = ""
29def rcFile = ""
30def openstackEnv = ""
Richard Felkl802e4462017-12-06 10:08:05 +010031def uploadImageStatus = ""
32def uploadMd5Status = ""
azvyagintseva0bc6142018-03-02 21:24:30 +020033ArrayList extra_vars = job_env.get('EXTRA_VARIABLES', '').readLines()
34def IMAGE_NAME = job_env.get('IMAGE_NAME', "packer-image") + "-" + dateTime
Richard Felkl2e9e5452017-10-16 09:52:10 +020035
azvyagintsev1c79f4d2018-03-02 14:25:37 +020036timeout(time: 8, unit: 'HOURS') {
37 node("python&&disk-xl") {
38 try {
39 def workspace = common.getWorkspace()
40 openstackEnv = "${workspace}/venv"
Richard Felkl2e9e5452017-10-16 09:52:10 +020041
azvyagintsev1c79f4d2018-03-02 14:25:37 +020042 stage("Prepare env") {
43 checkout scm
44 if (!fileExists("${workspace}/tmp")) {
45 sh "mkdir -p ${workspace}/tmp"
Richard Felkl2e9e5452017-10-16 09:52:10 +020046 }
azvyagintsev1c79f4d2018-03-02 14:25:37 +020047 if (!fileExists("${workspace}/images")) {
48 sh "mkdir ${workspace}/images"
49 }
50 if (!fileExists("bin")) {
51 common.infoMsg("Downloading packer")
52 sh "mkdir -p bin"
53 dir("bin") {
azvyagintseva0bc6142018-03-02 21:24:30 +020054 sh "wget --quiet -O ${PACKER_ZIP} ${PACKER_URL}"
azvyagintsev1c79f4d2018-03-02 14:25:37 +020055 sh "echo \"${PACKER_ZIP_MD5} ${PACKER_ZIP}\" >> md5sum"
56 sh "md5sum -c --status md5sum"
57 sh "unzip ${PACKER_ZIP}"
58 }
59 }
60 // clean images dir before building
61 sh(script: "rm -rf ${BUILD_OS}/images/*", returnStatus: true)
62 // clean virtualenv is exists
63 sh(script: "rm -rf ${workspace}/venv", returnStatus: true)
64
65 openstack.setupOpenstackVirtualenv(openstackEnv, OS_VERSION)
66 git.checkoutGitRepository(PACKER_TEMPLATES_REPO_NAME, PACKER_TEMPLATES_REPO_URL, PACKER_TEMPLATES_BRANCH)
67 }
68
69 stage("Build Instance") {
70 rcFile = openstack.createOpenstackEnv(OS_URL, OS_CREDENTIALS_ID, OS_PROJECT, "default", "", "default", "2", "")
71 dir("${workspace}/${PACKER_TEMPLATES_REPO_NAME}/${BUILD_OS}/") {
azvyagintseva0bc6142018-03-02 21:24:30 +020072 withEnv(extra_vars + ["PATH=${env.PATH}:${workspace}/bin",
73 "PACKER_LOG_PATH=${workspace}/packer.log",
74 "PACKER_LOG=1",
75 "TMPDIR=${workspace}/tmp"]) {
azvyagintsev1c79f4d2018-03-02 14:25:37 +020076 if (PACKER_DEBUG.toBoolean()) {
77 PACKER_ARGS = "${PACKER_ARGS} -debug"
78 }
79
azvyagintsev1c79f4d2018-03-02 14:25:37 +020080 sh "source extra.vars ; packer build -only=${BUILD_ONLY} ${PACKER_ARGS} -parallel=false template.json"
81
82 def packerStatus = sh(script: "grep \"Some builds didn't complete successfully and had errors\" ${PACKER_LOG_PATH}", returnStatus: true)
83 // grep returns 0 if find something
84 if (packerStatus != 0) {
85 if (buildTypes.contains("openstack")) {
86 common.infoMsg("Openstack instance complete")
87 }
88 } else {
89 throw new Exception("Packer build failed")
90 }
91 }
92 }
93 }
94
95 stage("Publish image") {
azvyagintseva0bc6142018-03-02 21:24:30 +020096 common.infoMsg("Saving image ${IMAGE_NAME}")
azvyagintsev1c79f4d2018-03-02 14:25:37 +020097 common.retry(3, 5) {
azvyagintseva0bc6142018-03-02 21:24:30 +020098 openstack.runOpenstackCommand("openstack image save --file ${IMAGE_NAME}.qcow2 ${IMAGE_NAME}", rcFile, openstackEnv)
azvyagintsev1c79f4d2018-03-02 14:25:37 +020099 }
azvyagintseva0bc6142018-03-02 21:24:30 +0200100 sh "md5sum ${IMAGE_NAME}.qcow2 > ${IMAGE_NAME}.qcow2.md5"
azvyagintsev1c79f4d2018-03-02 14:25:37 +0200101
azvyagintseva0bc6142018-03-02 21:24:30 +0200102 common.infoMsg("Uploading image ${IMAGE_NAME}")
azvyagintsev1c79f4d2018-03-02 14:25:37 +0200103 common.retry(3, 5) {
azvyagintseva0bc6142018-03-02 21:24:30 +0200104 uploadImageStatus = sh(script: "curl -f -T ${IMAGE_NAME}.qcow2 ${UPLOAD_URL}", returnStatus: true)
azvyagintsev1c79f4d2018-03-02 14:25:37 +0200105 if (uploadImageStatus != 0) {
106 throw new Exception("Image upload failed")
107 }
108 }
109
110 common.retry(3, 5) {
azvyagintseva0bc6142018-03-02 21:24:30 +0200111 uploadMd5Status = sh(script: "curl -f -T ${IMAGE_NAME}.qcow2.md5 ${UPLOAD_URL}", returnStatus: true)
azvyagintsev1c79f4d2018-03-02 14:25:37 +0200112 if (uploadMd5Status != 0) {
113 throw new Exception("MD5 sum upload failed")
114 }
115 }
azvyagintseva0bc6142018-03-02 21:24:30 +0200116 currentBuild.description = "<a href='http://ci.mcp.mirantis.net:8085/images/${IMAGE_NAME}.qcow2'>${IMAGE_NAME}.qcow2</a>"
azvyagintsev1c79f4d2018-03-02 14:25:37 +0200117 }
118
119 } catch (Throwable e) {
120 // If there was an error or exception thrown, the build failed
121 currentBuild.result = "FAILURE"
122 throw e
123 } finally {
124 if (CLEANUP_AFTER) {
125 stage("Cleanup") {
126 if (openstackServer != "") {
azvyagintseva0bc6142018-03-02 21:24:30 +0200127 openstack.runOpenstackCommand("openstack server delete ${IMAGE_NAME}", rcFile, openstackEnv)
128 openstack.runOpenstackCommand("openstack image delete ${IMAGE_NAME}", rcFile, openstackEnv)
azvyagintsev1c79f4d2018-03-02 14:25:37 +0200129 }
130 dir(workspace) {
131 sh "rm -rf ./*"
132 }
133 }
134
135 } else {
136 common.infoMsg("Env has not been cleanup!")
137 common.infoMsg("Packer private key:")
138 dir("${workspace}/${PACKER_TEMPLATES_REPO_NAME}/${BUILD_OS}/") {
139 sh "cat os_${BUILD_OS}.pem"
140 }
141
142 }
Richard Felkl2e9e5452017-10-16 09:52:10 +0200143 }
azvyagintsev1c79f4d2018-03-02 14:25:37 +0200144
145 }
Jakub Josef88aaf832018-01-18 16:18:28 +0100146}