azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Build mirror/aptly/apt01 image pipeline |
| 4 | * |
| 5 | * PACKER_URL = 'https://releases.hashicorp.com/packer/1.2.4/packer_1.2.4_linux_amd64.zip' |
| 6 | * PACKER_ZIP_MD5 = '997274e80ae41102eecf9df2e5b67860' |
| 7 | * PACKER_ARGS = '-debug' |
| 8 | * BUILD_ONLY = 'openstack|qemu' |
| 9 | * CLEANUP_AFTER = wipe workspace AFTER build |
| 10 | * IMAGE_NAME = Name of the result image. |
| 11 | * TIMESTAMP_INAME = True|false. If true - image will be uploaded to backend with IMAGE_NAME + timestamp |
| 12 | * |
| 13 | * OPENSTACK_OPENRC_YAML: aml of key:value variables required for openstack venv |
| 14 | * example: |
| 15 | * --- |
| 16 | * OS_TENANT_ID: 'xxxx' |
| 17 | * OS_TENANT_NAME: "mcp-oscore-ci" |
| 18 | * |
| 19 | * EXTRA_VARIABLES_YAML - Yaml of key:value variables required for template.json |
| 20 | * example: |
| 21 | * --- |
| 22 | * IMAGE_NAME: 'ubuntu-16.04-proposed' |
| 23 | * |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 24 | * CREDENTIALS_ID = Global jenkins cred for clone DEFAULT_GIT_URL |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 25 | * DEFAULT_GIT_URL |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 26 | * REFSPEC |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 27 | * |
| 28 | * OS_VERSION = OpenStack version |
| 29 | * OS_CREDENTIALS_ID = ID of credentials for OpenStack API stored in Jenkins. |
| 30 | * OS_URL = Keystone auth endpoint of the OpenStack. |
| 31 | * OS_PROJECT = |
| 32 | * |
Denis Egorenko | c0aba19 | 2019-01-23 18:03:53 +0400 | [diff] [blame] | 33 | * PUBLISH_BACKEND = local,glance,http |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 34 | * UPLOAD_URL = URL of an WebDAV used to upload the image after creating.(Only in case PUBLISH_BACKEND == http) |
| 35 | */ |
| 36 | |
| 37 | // Load shared libs |
| 38 | def common = new com.mirantis.mk.Common() |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 39 | def openstack = new com.mirantis.mk.Openstack() |
azvyagintsev | f2c70cc | 2018-07-12 12:19:11 +0300 | [diff] [blame] | 40 | def gerrit = new com.mirantis.mk.Gerrit() |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 41 | def date = new Date() |
| 42 | def dateTime = date.format("ddMMyyyy-HHmmss") |
| 43 | // |
| 44 | def job_env = env.getEnvironment().findAll { k, v -> v } |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 45 | gerritCredentials = env.CREDENTIALS_ID ?: 'gerrit' |
Vladimir Khlyunev | 101e65e | 2023-01-19 14:44:55 +0400 | [diff] [blame^] | 46 | gerritCredentialsHttp = env.GERRIT_HTTP_CREDENTIALS_ID ?: 'mcp-jenkins-http' |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 47 | |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 48 | ///// |
| 49 | extra_vars = readYaml text: job_env.get('EXTRA_VARIABLES_YAML','').trim() |
azvyagintsev | f2c70cc | 2018-07-12 12:19:11 +0300 | [diff] [blame] | 50 | // FIXME: os_openrc should be refactored. |
| 51 | os_openrc = readYaml text: job_env.get('OPENSTACK_OPENRC_YAML','').trim() |
azvyagintsev | 4f9239d | 2018-07-18 16:47:15 +0300 | [diff] [blame] | 52 | if (job_env.get('TIMESTAMP_INAME', false).toBoolean()) { |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 53 | imageName = job_env.IMAGE_NAME + "-" + dateTime |
| 54 | } else { |
| 55 | imageName = job_env.IMAGE_NAME |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 56 | } |
| 57 | // Overwrite IMAGE_NAME in template.json with expected |
azvyagintsev | f2c70cc | 2018-07-12 12:19:11 +0300 | [diff] [blame] | 58 | extra_vars['IMAGE_NAME'] = imageName |
azvyagintsev | 4f9239d | 2018-07-18 16:47:15 +0300 | [diff] [blame] | 59 | // Fix some variables |
| 60 | job_env['CLEANUP_AFTER'] = job_env.CLEANUP_AFTER.toBoolean() |
| 61 | job_env['SKIP_UPLOAD'] = job_env.SKIP_UPLOAD.toBoolean() |
| 62 | job_env['CLEANUP_AFTER'] = job_env.CLEANUP_AFTER.toBoolean() |
azvyagintsev | f2c70cc | 2018-07-12 12:19:11 +0300 | [diff] [blame] | 63 | job_env['BUILD_ONLY'] = job_env.BUILD_ONLY.toLowerCase() |
| 64 | job_env['PUBLISH_BACKEND'] = job_env.PUBLISH_BACKEND.toLowerCase() |
Denis Egorenko | c0aba19 | 2019-01-23 18:03:53 +0400 | [diff] [blame] | 65 | publishBackends = job_env['PUBLISH_BACKEND'].split(',') |
azvyagintsev | f2c70cc | 2018-07-12 12:19:11 +0300 | [diff] [blame] | 66 | // |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 67 | defaultGitRef = job_env.get('REFSPEC', 'HEAD') |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 68 | defaultGitUrl = job_env.get('DEFAULT_GIT_URL', null) |
Denis Egorenko | 3d9dfb2 | 2019-03-01 19:18:25 +0400 | [diff] [blame] | 69 | slaveNode = (env.SLAVE_NODE ?: 'virtual') |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 70 | |
| 71 | // Self-check |
| 72 | for (String req_v : ['BUILD_OS', 'BUILD_ONLY','IMAGE_NAME'] ) { |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 73 | if (!job_env.get(req_v, false)) { |
| 74 | throw new Exception("${req_v} not set!") |
| 75 | } |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | def MapToList(input_map) { |
| 79 | /** |
| 80 | * Convert dict in bash-like list |
| 81 | */ |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 82 | def data_list = [] |
| 83 | for (i = 0; i < input_map.size(); i++) { |
| 84 | data = '' |
| 85 | data = input_map.keySet()[i] + "=" + input_map.values()[i] |
| 86 | data_list.add(data) |
| 87 | } |
| 88 | return data_list |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 89 | } |
dtsapikov | 8b0a4fd | 2020-05-18 17:09:40 +0400 | [diff] [blame] | 90 | // Necessary to have ~100G of volume on node for build offline image. Code below chooses huge available slave. |
| 91 | def getHugeSlave() { |
| 92 | slaves = nodesByLabel("virtual") |
| 93 | def targetSlave = '' |
| 94 | for (slave in slaves) { |
| 95 | node(slave){ |
| 96 | outputMsg = sh(returnStdout: true, script: '''#!/bin/bash |
| 97 | df --block-size=G / | awk '{print $4}' | sed -n 2p | sed 's/.$//' |
| 98 | ''') |
| 99 | } |
| 100 | if(outputMsg.toInteger() >= 120){ |
| 101 | targetSlave = slave |
| 102 | break |
| 103 | } |
| 104 | } |
| 105 | return targetSlave |
| 106 | } |
| 107 | |
| 108 | nodeForBuild = getHugeSlave() |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 109 | |
| 110 | timeout(time: 6, unit: 'HOURS') { |
dtsapikov | 8b0a4fd | 2020-05-18 17:09:40 +0400 | [diff] [blame] | 111 | node(nodeForBuild) { |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 112 | def workspace = common.getWorkspace() |
Denis Egorenko | c0aba19 | 2019-01-23 18:03:53 +0400 | [diff] [blame] | 113 | def openstackEnv = "${workspace}/venv" |
| 114 | def rcFile = '' |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 115 | creds = common.getPasswordCredentials(job_env.CREDENTIALS_ID) |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 116 | |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 117 | stage("checkout") { |
| 118 | if (defaultGitRef && defaultGitUrl) { |
| 119 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", job_env.CREDENTIALS_ID) |
| 120 | } else { |
| 121 | throw new Exception("Cannot checkout gerrit patchset: DEFAULT_GIT_URL is null") |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 122 | } |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 123 | } |
| 124 | |
Denis Egorenko | c0aba19 | 2019-01-23 18:03:53 +0400 | [diff] [blame] | 125 | if (job_env.BUILD_ONLY == 'openstack' || 'glance' in publishBackends) { |
| 126 | rcFile = openstack.createOpenstackEnv(workspace, os_openrc['OS_AUTH_URL'], os_openrc['OS_CREDENTIALS_ID'], os_openrc['OS_PROJECT_NAME'], "default", "", "default", "3", "") |
| 127 | } |
| 128 | |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 129 | try { |
| 130 | def _artifact_dir = "${workspace}/artifacts" |
| 131 | def _artifact_list = [] |
| 132 | def ImagesCacheFolder = "${workspace}/../${env.JOB_NAME}_cache/" |
| 133 | stage("Prepare env") { |
| 134 | if (!fileExists("${workspace}/tmp")) { |
| 135 | sh "mkdir -p ${workspace}/tmp" |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 136 | } |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 137 | if (!fileExists(ImagesCacheFolder)) { |
| 138 | sh "mkdir -p ${ImagesCacheFolder}" |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 139 | } |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 140 | if (!fileExists(_artifact_dir)) { |
| 141 | sh "mkdir -p ${_artifact_dir}" |
| 142 | } |
| 143 | if (!fileExists("bin")) { |
| 144 | common.infoMsg("Downloading packer") |
| 145 | sh "mkdir -p bin" |
| 146 | dir("bin") { |
| 147 | def zipname = sh(script: "basename ${job_env.PACKER_URL}", returnStdout: true).trim() |
| 148 | sh(script: "wget --quiet ${job_env.PACKER_URL}", returnStdout: true) |
| 149 | sh "echo \"${job_env.PACKER_ZIP_MD5} ${zipname}\" >> md5sum" |
| 150 | sh(script: "md5sum -c --status md5sum", returnStdout: true) |
| 151 | sh "unzip ${zipname}" |
| 152 | } |
| 153 | } |
| 154 | if (!fileExists("${job_env.BUILD_OS}/images")) { |
| 155 | // clean images dir before building |
| 156 | sh(script: "rm -rf ${job_env.BUILD_OS}/images/*", returnStatus: true) |
| 157 | } |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 158 | } |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 159 | |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 160 | stage("Build Instance") { |
Aleksey Zvyagintsev | 42e7695 | 2018-12-14 13:40:44 +0000 | [diff] [blame] | 161 | def _packer_args = job_env.get('PACKER_ARGS', '') |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 162 | def _packer_log = "${workspace}/packer.log" |
| 163 | // clean old log, for correct status grepping |
| 164 | if (fileExists(_packer_log)) { |
| 165 | sh "rm -v ${_packer_log}" |
| 166 | } |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 167 | |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 168 | dir("${workspace}/${job_env.BUILD_OS}/") { |
| 169 | if (fileExists("config-drive")) { |
| 170 | def model = extra_vars.get('CLUSTER_MODEL', '') |
| 171 | if (model != "") { |
| 172 | checkout([ |
| 173 | $class : 'GitSCM', |
| 174 | branches : [[name: 'FETCH_HEAD']], |
Denis Egorenko | 47e13a6 | 2019-09-05 19:17:30 +0400 | [diff] [blame] | 175 | extensions : [ |
| 176 | [$class: 'RelativeTargetDirectory', relativeTargetDir: 'config-drive/model'], |
| 177 | [$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: true, recursiveSubmodules: true, trackingSubmodules: false], |
| 178 | ], |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 179 | userRemoteConfigs : [[url: model, refspec: extra_vars.get('CLUSTER_MODEL_REF', 'master'), credentialsId: gerritCredentials]] |
| 180 | ]) |
Denis Egorenko | 34d326c | 2019-02-11 20:50:53 +0400 | [diff] [blame] | 181 | |
| 182 | def clusterName = extra_vars.get('CLUSTER_NAME', '') |
| 183 | def mcpVersion = extra_vars.get('MCP_VERSION', '') |
| 184 | if (clusterName == 'mcp-offline' && mcpVersion != '') { |
| 185 | def filePath = 'classes/cluster/mcp-offline/infra/apt01_dummy.yml' |
| 186 | sh "sed -i 's/mcp_version: .*/mcp_version: ${mcpVersion}/' config-drive/model/${filePath}" |
Denis Egorenko | 47e13a6 | 2019-09-05 19:17:30 +0400 | [diff] [blame] | 187 | // speed up build by using internal mirrors in the same region |
| 188 | if (os_openrc.get('CLOUD_REGION', 'US').matches("[Ee][Uu]")) { |
| 189 | sh "sed -i 's/debmirror_mirrors_host: .*/debmirror_mirrors_host: mirror-eu.mcp.mirantis.net/' config-drive/model/${filePath}" |
| 190 | } |
Vladimir Khlyunev | 101e65e | 2023-01-19 14:44:55 +0400 | [diff] [blame^] | 191 | filePath = 'classes/cluster/mcp-offline/infra/mirror/init.yml' |
| 192 | withCredentials([usernamePassword( |
| 193 | credentialsId: gerritCredentialsHttp, |
| 194 | usernameVariable: 'GERRIT_HTTP_USER', |
| 195 | passwordVariable: 'GERRIT_HTTP_PASS') |
| 196 | ]) { |
| 197 | sh "sed -i 's/%STACKLIGHT_PYTEST_GIT_HTTPS_USER%/${GERRIT_HTTP_USER}/' config-drive/model/${filePath}" |
| 198 | sh "sed -i 's/%STACKLIGHT_PYTEST_GIT_HTTPS_PASS%/${GERRIT_HTTP_PASS}/' config-drive/model/${filePath}" |
| 199 | } |
Denis Egorenko | 47e13a6 | 2019-09-05 19:17:30 +0400 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | def reclass_url = extra_vars.get('RECLASS_SYSTEM_URL', '') |
| 203 | def reclass_ref = extra_vars.get('RECLASS_SYSTEM_REF', '') |
| 204 | if (reclass_url && reclass_ref) { |
| 205 | if (fileExists('config-drive/model/classes/system')) { |
| 206 | dir('config-drive/model/classes/system') { |
Denis Egorenko | e5f37ab | 2019-09-16 17:17:40 +0400 | [diff] [blame] | 207 | checkout([ |
| 208 | $class : 'GitSCM', |
| 209 | branches : [[name: 'FETCH_HEAD']], |
| 210 | userRemoteConfigs : [[url: reclass_url, refspec: reclass_ref, credentialsId: gerritCredentials]] |
| 211 | ]) |
Denis Egorenko | 47e13a6 | 2019-09-05 19:17:30 +0400 | [diff] [blame] | 212 | } |
| 213 | } |
Denis Egorenko | 34d326c | 2019-02-11 20:50:53 +0400 | [diff] [blame] | 214 | } |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 215 | } |
| 216 | def scripts = extra_vars.get('GIT_SALT_FORMULAS_SCRIPTS', '') |
| 217 | if (scripts != "") { |
| 218 | checkout([ |
| 219 | $class : 'GitSCM', |
| 220 | branches : [[name: 'FETCH_HEAD']], |
| 221 | extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'config-drive/salt_scripts']], |
| 222 | userRemoteConfigs: [[url: scripts, refspec: extra_vars.get('SCRIPTS_REF', 'master'), credentialsId: gerritCredentials]] |
| 223 | ]) |
| 224 | } |
| 225 | |
| 226 | common.infoMsg("Creating cloud-config drive") |
| 227 | def isoFile = "config-drive/cloudata.iso" |
| 228 | if (fileExists(isoFile)) { |
| 229 | sh "rm -v ${isoFile}" |
| 230 | } |
| 231 | // This is left for backward-compatibility |
| 232 | if (fileExists("config-drive/user-data.yaml")) { |
| 233 | sh "mv config-drive/user-data.yaml config-drive/user-data" |
| 234 | if (!fileExists("config-drive/meta-data")) { |
| 235 | sh "echo 'hostname: ubuntu' > config-drive/meta-data" |
| 236 | } |
| 237 | } |
| 238 | sh "mkisofs -o ${isoFile} -V cidata -r -J --quiet config-drive" |
| 239 | archiveArtifacts artifacts: "${isoFile}" |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (job_env.BUILD_ONLY == "openstack") { |
| 244 | dir("${workspace}/${job_env.BUILD_OS}/") { |
| 245 | extra_vars_list = MapToList(extra_vars) |
| 246 | withEnv(["PATH=${env.PATH}:${workspace}/bin", |
| 247 | "PACKER_LOG_PATH=${_packer_log}", |
| 248 | "PACKER_LOG=1", |
| 249 | "TMPDIR=${workspace}/tmp", |
| 250 | "OS_USERNAME=${creds.username.toString()}", |
| 251 | "OS_PASSWORD=${creds.password.toString()}"] + extra_vars_list) { |
| 252 | |
| 253 | common.infoMsg("Run build with:") |
| 254 | sh(script: 'printenv|sort') |
Vladimir Khlyunev | 0b28723 | 2020-04-29 14:18:28 +0400 | [diff] [blame] | 255 | common.infoMsg("Free disk space by 'df -h' command:") |
| 256 | sh(script: 'df -h') |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 257 | sh(script: "set -xe; packer build -only='openstack' ${_packer_args} -parallel=false template.json") |
| 258 | _os_private = "${workspace}/${job_env.BUILD_OS}/os_${job_env.BUILD_OS}.pem" |
| 259 | if (fileExists(_os_private)) { |
| 260 | common.infoMsg("Packer private key:") |
| 261 | sh "cat ${_os_private}" |
| 262 | } |
| 263 | def packerStatus = sh(script: "grep \"Some builds didn't complete successfully and had errors\" ${_packer_log}", returnStatus: true) |
| 264 | // grep returns 0 if find something |
| 265 | if (packerStatus != 0) { |
| 266 | common.infoMsg("Openstack instance build complete") |
| 267 | } else { |
| 268 | throw new Exception("Openstack Packer build failed") |
| 269 | } |
| 270 | |
| 271 | common.retry(3, 5) { |
| 272 | common.infoMsg("Attempt download openstack image..") |
| 273 | openstack.runOpenstackCommand("openstack image save --file ${_artifact_dir}/${imageName}.qcow2 ${imageName}", rcFile, openstackEnv) |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | } else if (job_env.BUILD_ONLY == 'qemu') { |
| 279 | |
| 280 | dir("${workspace}/${job_env.BUILD_OS}/") { |
| 281 | extra_vars_list = MapToList(extra_vars) |
| 282 | withEnv(["PATH=${env.PATH}:${workspace}/bin", |
| 283 | "PACKER_LOG_PATH=${_packer_log}", |
| 284 | "PACKER_LOG=1", |
| 285 | "TMPDIR=${workspace}/tmp", |
| 286 | "PACKER_IMAGES_CACHE=${ImagesCacheFolder}"] + extra_vars_list) { |
| 287 | common.infoMsg("Run build with:") |
| 288 | sh(script: 'printenv|sort') |
Vladimir Khlyunev | 0b28723 | 2020-04-29 14:18:28 +0400 | [diff] [blame] | 289 | common.infoMsg("Free disk space by 'df -h' command:") |
| 290 | sh(script: 'df -h') |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 291 | sh(script: "set -xe ; packer build -on-error=ask -only='qemu' ${_packer_args} -parallel=false template.json".toString()) |
| 292 | |
| 293 | def packerStatus = sh(script: "grep \"Some builds didn't complete successfully and had errors\" ${PACKER_LOG_PATH}", returnStatus: true) |
| 294 | // grep returns 0 if find something |
| 295 | if (packerStatus != 0) { |
| 296 | common.infoMsg("qemu instance build completed successfully") |
| 297 | } else { |
| 298 | throw new Exception("qemu instance build failed") |
| 299 | } |
| 300 | // collect artifacts |
| 301 | // TODO make it possible, process multiply artifacts by one run. |
| 302 | dir('images/') { |
Aleksey Zvyagintsev | 42e7695 | 2018-12-14 13:40:44 +0000 | [diff] [blame] | 303 | sh(script: 'find .') |
| 304 | def _files = findFiles(glob: "*qemu*/*${imageName}*.qcow2") |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 305 | if (_files.size() > 1) { |
| 306 | common.warningMsg("Multiply artifacts detected!Only first one will be processed!") |
| 307 | } else if (_files.size() == 0) { |
| 308 | throw new Exception("No artifacts detected!BUILD_ONLY=${env.BUILD_ONLY} failed!") |
| 309 | } |
| 310 | for (String x : _files) { |
| 311 | _file = sh(script: "set -x ; readlink -f ${x}", returnStdout: true) |
| 312 | sh(script: "mv -v ${x} ${_artifact_dir}/${imageName}.qcow2") |
| 313 | // Save filename to list |
| 314 | _artifact_list.add("${imageName}.qcow2") |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | } else { |
| 321 | throw new Exception("Unexpected BUILD_ONLY=${env.BUILD_ONLY} target!") |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | stage("Publish artifacts") { |
| 326 | dir(_artifact_dir) { |
Denis Egorenko | c0aba19 | 2019-01-23 18:03:53 +0400 | [diff] [blame] | 327 | def published = false |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 328 | common.infoMsg("Processing md5 for artifacts") |
| 329 | for (String x : _artifact_list) { |
| 330 | _md5 = sh(script: "md5sum ${x} > ${x}.md5; cat ${x}.md5", returnStdout: true).trim() |
| 331 | _size = sh(script: "ls -alh ${x}", returnStdout: true).trim() |
| 332 | common.infoMsg("Artifact file: ${_size}\n${_md5}") |
| 333 | } |
Denis Egorenko | c0aba19 | 2019-01-23 18:03:53 +0400 | [diff] [blame] | 334 | if ('local' in publishBackends) { |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 335 | common.infoMsg("Uploading to: local") |
| 336 | common.infoMsg("For local publish target - nothing to do, all files in: ${_artifact_dir}") |
| 337 | if (job_env.get('CLEANUP_AFTER', false)) { |
| 338 | common.warningMsg("You are trying to use 'local' publish method, along with enabled CLEANUP_AFTER! ") |
| 339 | common.warningMsg("Disabling CLEANUP_AFTER option, to save you'r data ;) ") |
| 340 | job_env.CLEANUP_AFTER = false |
| 341 | } |
Denis Egorenko | c0aba19 | 2019-01-23 18:03:53 +0400 | [diff] [blame] | 342 | published = true |
| 343 | } |
| 344 | if ('glance' in publishBackends) { |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 345 | common.infoMsg("Uploading to: glance-openstack") |
| 346 | if (fileExists("${workspace}/venv")) { |
| 347 | common.infoMsg("cleaning virtualenv at:${workspace}/venv") |
| 348 | sh(script: "rm -rf ${workspace}/venv", returnStatus: true) |
| 349 | } |
Denis Egorenko | c0aba19 | 2019-01-23 18:03:53 +0400 | [diff] [blame] | 350 | openstack.setupOpenstackVirtualenv(openstackEnv) |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 351 | for (String x : findFiles(glob: "*.*")) { |
Denis Egorenko | c0aba19 | 2019-01-23 18:03:53 +0400 | [diff] [blame] | 352 | if (x.getName().endsWith('.qcow2')) { |
| 353 | _md5sum = sh(script: "cat ${x}.md5", returnStdout: true).trim().split()[0] |
Denis Egorenko | f4acb4a | 2019-01-26 13:58:58 +0400 | [diff] [blame] | 354 | _property = "--property data=${dateTime} --property md5sum=${_md5sum} --property build='${env.BUILD_URL}' --container-format bare --disk-format qcow2" |
Denis Egorenko | bae3501 | 2019-03-04 12:56:42 +0400 | [diff] [blame] | 355 | _cmdline = String.format("glance image-create %s --name '%s' --file %s", _property, imageName, x) |
Denis Egorenko | c0aba19 | 2019-01-23 18:03:53 +0400 | [diff] [blame] | 356 | openstack.runOpenstackCommand(_cmdline, rcFile, openstackEnv) |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 357 | } |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 358 | } |
Denis Egorenko | c6dde3a | 2019-01-30 15:54:16 +0400 | [diff] [blame] | 359 | currentBuild.description = "${imageName}.qcow2 uploaded tenant: ${os_openrc['OS_PROJECT_NAME']}" |
Denis Egorenko | c0aba19 | 2019-01-23 18:03:53 +0400 | [diff] [blame] | 360 | published = true |
| 361 | } |
| 362 | if ('http' in publishBackends) { |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 363 | for (String u_file : findFiles(glob: '*.*')) { |
Denis Egorenko | c0aba19 | 2019-01-23 18:03:53 +0400 | [diff] [blame] | 364 | common.infoMsg("Uploading image ${u_file}") |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 365 | def uploadImageStatus = '' |
| 366 | common.retry(3, 5) { |
| 367 | uploadImageStatus = sh(script: "curl -f -T ${u_file} ${job_env.UPLOAD_URL}", returnStatus: true) |
| 368 | if (uploadImageStatus != 0) { |
| 369 | throw new Exception("Uploading file: ${u_file} failed!") |
| 370 | } |
| 371 | } |
| 372 | // Fixme for correct path ? |
| 373 | currentBuild.description = "<a href='http://images.mcp.mirantis.net/${imageName}.qcow2'>${imageName}.qcow2</a>" |
| 374 | } |
Denis Egorenko | c0aba19 | 2019-01-23 18:03:53 +0400 | [diff] [blame] | 375 | published = true |
| 376 | } |
| 377 | if (! published) { |
| 378 | throw new Exception("Unsupported publish backend: ${publishBackends}") |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | } |
| 382 | } |
| 383 | } catch (Throwable e) { |
| 384 | // If there was an error or exception thrown, the build failed |
| 385 | currentBuild.result = "FAILURE" |
| 386 | throw e |
| 387 | } finally { |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 388 | if (job_env.get('CLEANUP_AFTER', false)) { |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 389 | dir(workspace) { |
| 390 | sh "find . -mindepth 1 -delete || true" |
azvyagintsev | f2c70cc | 2018-07-12 12:19:11 +0300 | [diff] [blame] | 391 | } |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 392 | if (job_env.BUILD_ONLY == 'openstack') { |
| 393 | common.warningMsg("openstack Env cleanup not implemented yet!") |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 394 | } |
Ivan Berezovskiy | 23035a2 | 2018-11-14 13:25:51 +0400 | [diff] [blame] | 395 | } else { |
| 396 | common.warningMsg("Env has not been cleaned!Please cleanup it manualy!") |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 397 | } |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 398 | } |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 399 | } |
azvyagintsev | 68786a1 | 2018-07-10 14:42:29 +0300 | [diff] [blame] | 400 | } |