blob: d8886c1577182a8de6b4cfce123f395485e33a25 [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.
13 * VM_CONNECT_RETRIES - Number of retries for SSH connection to the VM after it’s spawned after 8 minutes.
Richard Felkl859f4dd2018-01-04 23:03:27 +010014 * VM_FLAVOR - Flavor to be used for VM in OpenStack.
15 * VM_FLOATING_IP_POOL - Floating IP pool to be used to assign floating IP to the VM.
16 * VM_IMAGE - Name of the image to be used for VM in OpenStack.
17 * VM_IP - Static IP that is assigned to the VM which belongs to the network used.
Richard Felkl859f4dd2018-01-04 23:03:27 +010018 * VM_NETWORK_ID - ID of the network that VM connects to.
19 *
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 = ""
Richard Felkl2e9e5452017-10-16 09:52:10 +020033
azvyagintsev1c79f4d2018-03-02 14:25:37 +020034timeout(time: 8, unit: 'HOURS') {
35 node("python&&disk-xl") {
36 try {
37 def workspace = common.getWorkspace()
38 openstackEnv = "${workspace}/venv"
Richard Felkl2e9e5452017-10-16 09:52:10 +020039
azvyagintsev1c79f4d2018-03-02 14:25:37 +020040 stage("Prepare env") {
41 checkout scm
42 if (!fileExists("${workspace}/tmp")) {
43 sh "mkdir -p ${workspace}/tmp"
Richard Felkl2e9e5452017-10-16 09:52:10 +020044 }
azvyagintsev1c79f4d2018-03-02 14:25:37 +020045 if (!fileExists("${workspace}/images")) {
46 sh "mkdir ${workspace}/images"
47 }
48 if (!fileExists("bin")) {
49 common.infoMsg("Downloading packer")
50 sh "mkdir -p bin"
51 dir("bin") {
52 sh "wget -O ${PACKER_ZIP} ${PACKER_URL}"
53 sh "echo \"${PACKER_ZIP_MD5} ${PACKER_ZIP}\" >> md5sum"
54 sh "md5sum -c --status md5sum"
55 sh "unzip ${PACKER_ZIP}"
56 }
57 }
58 // clean images dir before building
59 sh(script: "rm -rf ${BUILD_OS}/images/*", returnStatus: true)
60 // clean virtualenv is exists
61 sh(script: "rm -rf ${workspace}/venv", returnStatus: true)
62
63 openstack.setupOpenstackVirtualenv(openstackEnv, OS_VERSION)
64 git.checkoutGitRepository(PACKER_TEMPLATES_REPO_NAME, PACKER_TEMPLATES_REPO_URL, PACKER_TEMPLATES_BRANCH)
65 }
66
67 stage("Build Instance") {
68 rcFile = openstack.createOpenstackEnv(OS_URL, OS_CREDENTIALS_ID, OS_PROJECT, "default", "", "default", "2", "")
69 dir("${workspace}/${PACKER_TEMPLATES_REPO_NAME}/${BUILD_OS}/") {
70 withEnv(["PATH=${env.PATH}:${workspace}/bin",
71 "PACKER_LOG_PATH=${workspace}/packer.log",
72 "PACKER_LOG=1",
73 "TMPDIR=${workspace}/tmp",
74 "VM_FLOATING_IP_POOL=${VM_FLOATING_IP_POOL}",
75 "ssh_keypair_name=mcp-offline-keypair-${dateTime}",
76 "ssh_private_key_file=${workspace}/id_rsa",
77 "IMAGE_NAME=${IMAGE_NAME}-${dateTime}"]) {
78 if (PACKER_DEBUG.toBoolean()) {
79 PACKER_ARGS = "${PACKER_ARGS} -debug"
80 }
81
82 sh "printenv | sort -u"
83 sh "echo ${EXTRA_VARIABLES} > extra.vars"
84 sh "source extra.vars ; packer build -only=${BUILD_ONLY} ${PACKER_ARGS} -parallel=false template.json"
85
86 def packerStatus = sh(script: "grep \"Some builds didn't complete successfully and had errors\" ${PACKER_LOG_PATH}", returnStatus: true)
87 // grep returns 0 if find something
88 if (packerStatus != 0) {
89 if (buildTypes.contains("openstack")) {
90 common.infoMsg("Openstack instance complete")
91 }
92 } else {
93 throw new Exception("Packer build failed")
94 }
95 }
96 }
97 }
98
99 stage("Publish image") {
100 common.infoMsg("Saving image ${IMAGE_NAME}-${dateTime}")
101 common.retry(3, 5) {
102 openstack.runOpenstackCommand("openstack image save --file ${IMAGE_NAME}-${dateTime}.qcow2 ${IMAGE_NAME}-${dateTime}", rcFile, openstackEnv)
103 }
104 sh "md5sum ${IMAGE_NAME}-${dateTime}.qcow2 > ${IMAGE_NAME}-${dateTime}.qcow2.md5"
105
106 common.infoMsg("Uploading image ${IMAGE_NAME}-${dateTime}")
107 common.retry(3, 5) {
108 uploadImageStatus = sh(script: "curl -f -T ${IMAGE_NAME}-${dateTime}.qcow2 ${UPLOAD_URL}", returnStatus: true)
109 if (uploadImageStatus != 0) {
110 throw new Exception("Image upload failed")
111 }
112 }
113
114 common.retry(3, 5) {
115 uploadMd5Status = sh(script: "curl -f -T ${IMAGE_NAME}-${dateTime}.qcow2.md5 ${UPLOAD_URL}", returnStatus: true)
116 if (uploadMd5Status != 0) {
117 throw new Exception("MD5 sum upload failed")
118 }
119 }
120 currentBuild.description = "<a href='http://ci.mcp.mirantis.net:8085/images/${IMAGE_NAME}-${dateTime}.qcow2'>${IMAGE_NAME}-${dateTime}.qcow2</a>"
121 }
122
123 } catch (Throwable e) {
124 // If there was an error or exception thrown, the build failed
125 currentBuild.result = "FAILURE"
126 throw e
127 } finally {
128 if (CLEANUP_AFTER) {
129 stage("Cleanup") {
130 if (openstackServer != "") {
131 openstack.runOpenstackCommand("openstack server delete ${IMAGE_NAME}-${dateTime}", rcFile, openstackEnv)
132 openstack.runOpenstackCommand("openstack image delete ${IMAGE_NAME}-${dateTime}", rcFile, openstackEnv)
133 }
134 dir(workspace) {
135 sh "rm -rf ./*"
136 }
137 }
138
139 } else {
140 common.infoMsg("Env has not been cleanup!")
141 common.infoMsg("Packer private key:")
142 dir("${workspace}/${PACKER_TEMPLATES_REPO_NAME}/${BUILD_OS}/") {
143 sh "cat os_${BUILD_OS}.pem"
144 }
145
146 }
Richard Felkl2e9e5452017-10-16 09:52:10 +0200147 }
azvyagintsev1c79f4d2018-03-02 14:25:37 +0200148
149 }
Jakub Josef88aaf832018-01-18 16:18:28 +0100150}