blob: 3da11fddf97fbdda2ac86f8f5a1b38482a8e4ed3 [file] [log] [blame]
azvyagintsev68786a12018-07-10 14:42:29 +03001/**
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 Berezovskiy23035a22018-11-14 13:25:51 +040024 * CREDENTIALS_ID = Global jenkins cred for clone DEFAULT_GIT_URL
azvyagintsev68786a12018-07-10 14:42:29 +030025 * DEFAULT_GIT_URL
Ivan Berezovskiy23035a22018-11-14 13:25:51 +040026 * REFSPEC
azvyagintsev68786a12018-07-10 14:42:29 +030027 *
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 Egorenkoc0aba192019-01-23 18:03:53 +040033 * PUBLISH_BACKEND = local,glance,http
azvyagintsev68786a12018-07-10 14:42:29 +030034 * 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
38def common = new com.mirantis.mk.Common()
azvyagintsev68786a12018-07-10 14:42:29 +030039def openstack = new com.mirantis.mk.Openstack()
azvyagintsevf2c70cc2018-07-12 12:19:11 +030040def gerrit = new com.mirantis.mk.Gerrit()
azvyagintsev68786a12018-07-10 14:42:29 +030041def date = new Date()
42def dateTime = date.format("ddMMyyyy-HHmmss")
43//
44def job_env = env.getEnvironment().findAll { k, v -> v }
Ivan Berezovskiy23035a22018-11-14 13:25:51 +040045gerritCredentials = env.CREDENTIALS_ID ?: 'gerrit'
azvyagintsev68786a12018-07-10 14:42:29 +030046
azvyagintsev68786a12018-07-10 14:42:29 +030047/////
48extra_vars = readYaml text: job_env.get('EXTRA_VARIABLES_YAML','').trim()
azvyagintsevf2c70cc2018-07-12 12:19:11 +030049// FIXME: os_openrc should be refactored.
50os_openrc = readYaml text: job_env.get('OPENSTACK_OPENRC_YAML','').trim()
azvyagintsev4f9239d2018-07-18 16:47:15 +030051if (job_env.get('TIMESTAMP_INAME', false).toBoolean()) {
Ivan Berezovskiy23035a22018-11-14 13:25:51 +040052 imageName = job_env.IMAGE_NAME + "-" + dateTime
53} else {
54 imageName = job_env.IMAGE_NAME
azvyagintsev68786a12018-07-10 14:42:29 +030055}
56// Overwrite IMAGE_NAME in template.json with expected
azvyagintsevf2c70cc2018-07-12 12:19:11 +030057extra_vars['IMAGE_NAME'] = imageName
azvyagintsev4f9239d2018-07-18 16:47:15 +030058// Fix some variables
59job_env['CLEANUP_AFTER'] = job_env.CLEANUP_AFTER.toBoolean()
60job_env['SKIP_UPLOAD'] = job_env.SKIP_UPLOAD.toBoolean()
61job_env['CLEANUP_AFTER'] = job_env.CLEANUP_AFTER.toBoolean()
azvyagintsevf2c70cc2018-07-12 12:19:11 +030062job_env['BUILD_ONLY'] = job_env.BUILD_ONLY.toLowerCase()
63job_env['PUBLISH_BACKEND'] = job_env.PUBLISH_BACKEND.toLowerCase()
Denis Egorenkoc0aba192019-01-23 18:03:53 +040064publishBackends = job_env['PUBLISH_BACKEND'].split(',')
azvyagintsevf2c70cc2018-07-12 12:19:11 +030065//
Ivan Berezovskiy23035a22018-11-14 13:25:51 +040066defaultGitRef = job_env.get('REFSPEC', 'HEAD')
azvyagintsev68786a12018-07-10 14:42:29 +030067defaultGitUrl = job_env.get('DEFAULT_GIT_URL', null)
azvyagintsev52d4b942019-01-29 19:55:06 +020068slaveNode = (env.SLAVE_NODE ?: 'jsl03.mcp.mirantis.net')
azvyagintsev68786a12018-07-10 14:42:29 +030069
70// Self-check
71for (String req_v : ['BUILD_OS', 'BUILD_ONLY','IMAGE_NAME'] ) {
Ivan Berezovskiy23035a22018-11-14 13:25:51 +040072 if (!job_env.get(req_v, false)) {
73 throw new Exception("${req_v} not set!")
74 }
azvyagintsev68786a12018-07-10 14:42:29 +030075}
76
77def MapToList(input_map) {
78/**
79 * Convert dict in bash-like list
80 */
Ivan Berezovskiy23035a22018-11-14 13:25:51 +040081 def data_list = []
82 for (i = 0; i < input_map.size(); i++) {
83 data = ''
84 data = input_map.keySet()[i] + "=" + input_map.values()[i]
85 data_list.add(data)
86 }
87 return data_list
azvyagintsev68786a12018-07-10 14:42:29 +030088}
89
90timeout(time: 6, unit: 'HOURS') {
Ivan Berezovskiy23035a22018-11-14 13:25:51 +040091 node(slaveNode) {
92 def workspace = common.getWorkspace()
Denis Egorenkoc0aba192019-01-23 18:03:53 +040093 def openstackEnv = "${workspace}/venv"
94 def rcFile = ''
Ivan Berezovskiy23035a22018-11-14 13:25:51 +040095 creds = common.getPasswordCredentials(job_env.CREDENTIALS_ID)
azvyagintsev68786a12018-07-10 14:42:29 +030096
Ivan Berezovskiy23035a22018-11-14 13:25:51 +040097 stage("checkout") {
98 if (defaultGitRef && defaultGitUrl) {
99 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", job_env.CREDENTIALS_ID)
100 } else {
101 throw new Exception("Cannot checkout gerrit patchset: DEFAULT_GIT_URL is null")
azvyagintsev68786a12018-07-10 14:42:29 +0300102 }
azvyagintsev68786a12018-07-10 14:42:29 +0300103 }
104
Denis Egorenkoc0aba192019-01-23 18:03:53 +0400105 if (job_env.BUILD_ONLY == 'openstack' || 'glance' in publishBackends) {
106 rcFile = openstack.createOpenstackEnv(workspace, os_openrc['OS_AUTH_URL'], os_openrc['OS_CREDENTIALS_ID'], os_openrc['OS_PROJECT_NAME'], "default", "", "default", "3", "")
107 }
108
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400109 try {
110 def _artifact_dir = "${workspace}/artifacts"
111 def _artifact_list = []
112 def ImagesCacheFolder = "${workspace}/../${env.JOB_NAME}_cache/"
113 stage("Prepare env") {
114 if (!fileExists("${workspace}/tmp")) {
115 sh "mkdir -p ${workspace}/tmp"
azvyagintsev68786a12018-07-10 14:42:29 +0300116 }
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400117 if (!fileExists(ImagesCacheFolder)) {
118 sh "mkdir -p ${ImagesCacheFolder}"
azvyagintsev68786a12018-07-10 14:42:29 +0300119 }
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400120 if (!fileExists(_artifact_dir)) {
121 sh "mkdir -p ${_artifact_dir}"
122 }
123 if (!fileExists("bin")) {
124 common.infoMsg("Downloading packer")
125 sh "mkdir -p bin"
126 dir("bin") {
127 def zipname = sh(script: "basename ${job_env.PACKER_URL}", returnStdout: true).trim()
128 sh(script: "wget --quiet ${job_env.PACKER_URL}", returnStdout: true)
129 sh "echo \"${job_env.PACKER_ZIP_MD5} ${zipname}\" >> md5sum"
130 sh(script: "md5sum -c --status md5sum", returnStdout: true)
131 sh "unzip ${zipname}"
132 }
133 }
134 if (!fileExists("${job_env.BUILD_OS}/images")) {
135 // clean images dir before building
136 sh(script: "rm -rf ${job_env.BUILD_OS}/images/*", returnStatus: true)
137 }
azvyagintsev68786a12018-07-10 14:42:29 +0300138 }
azvyagintsev68786a12018-07-10 14:42:29 +0300139
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400140 stage("Build Instance") {
Aleksey Zvyagintsev42e76952018-12-14 13:40:44 +0000141 def _packer_args = job_env.get('PACKER_ARGS', '')
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400142 def _packer_log = "${workspace}/packer.log"
143 // clean old log, for correct status grepping
144 if (fileExists(_packer_log)) {
145 sh "rm -v ${_packer_log}"
146 }
azvyagintsev68786a12018-07-10 14:42:29 +0300147
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400148 dir("${workspace}/${job_env.BUILD_OS}/") {
149 if (fileExists("config-drive")) {
150 def model = extra_vars.get('CLUSTER_MODEL', '')
151 if (model != "") {
152 checkout([
153 $class : 'GitSCM',
154 branches : [[name: 'FETCH_HEAD']],
155 recursiveSubmodules: true,
156 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'config-drive/model']],
157 userRemoteConfigs : [[url: model, refspec: extra_vars.get('CLUSTER_MODEL_REF', 'master'), credentialsId: gerritCredentials]]
158 ])
159 }
160 def scripts = extra_vars.get('GIT_SALT_FORMULAS_SCRIPTS', '')
161 if (scripts != "") {
162 checkout([
163 $class : 'GitSCM',
164 branches : [[name: 'FETCH_HEAD']],
165 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'config-drive/salt_scripts']],
166 userRemoteConfigs: [[url: scripts, refspec: extra_vars.get('SCRIPTS_REF', 'master'), credentialsId: gerritCredentials]]
167 ])
168 }
169
170 common.infoMsg("Creating cloud-config drive")
171 def isoFile = "config-drive/cloudata.iso"
172 if (fileExists(isoFile)) {
173 sh "rm -v ${isoFile}"
174 }
175 // This is left for backward-compatibility
176 if (fileExists("config-drive/user-data.yaml")) {
177 sh "mv config-drive/user-data.yaml config-drive/user-data"
178 if (!fileExists("config-drive/meta-data")) {
179 sh "echo 'hostname: ubuntu' > config-drive/meta-data"
180 }
181 }
182 sh "mkisofs -o ${isoFile} -V cidata -r -J --quiet config-drive"
183 archiveArtifacts artifacts: "${isoFile}"
184 }
185 }
186
187 if (job_env.BUILD_ONLY == "openstack") {
188 dir("${workspace}/${job_env.BUILD_OS}/") {
189 extra_vars_list = MapToList(extra_vars)
190 withEnv(["PATH=${env.PATH}:${workspace}/bin",
191 "PACKER_LOG_PATH=${_packer_log}",
192 "PACKER_LOG=1",
193 "TMPDIR=${workspace}/tmp",
194 "OS_USERNAME=${creds.username.toString()}",
195 "OS_PASSWORD=${creds.password.toString()}"] + extra_vars_list) {
196
197 common.infoMsg("Run build with:")
198 sh(script: 'printenv|sort')
199 sh(script: "set -xe; packer build -only='openstack' ${_packer_args} -parallel=false template.json")
200 _os_private = "${workspace}/${job_env.BUILD_OS}/os_${job_env.BUILD_OS}.pem"
201 if (fileExists(_os_private)) {
202 common.infoMsg("Packer private key:")
203 sh "cat ${_os_private}"
204 }
205 def packerStatus = sh(script: "grep \"Some builds didn't complete successfully and had errors\" ${_packer_log}", returnStatus: true)
206 // grep returns 0 if find something
207 if (packerStatus != 0) {
208 common.infoMsg("Openstack instance build complete")
209 } else {
210 throw new Exception("Openstack Packer build failed")
211 }
212
213 common.retry(3, 5) {
214 common.infoMsg("Attempt download openstack image..")
215 openstack.runOpenstackCommand("openstack image save --file ${_artifact_dir}/${imageName}.qcow2 ${imageName}", rcFile, openstackEnv)
216 }
217 }
218 }
219
220 } else if (job_env.BUILD_ONLY == 'qemu') {
221
222 dir("${workspace}/${job_env.BUILD_OS}/") {
223 extra_vars_list = MapToList(extra_vars)
224 withEnv(["PATH=${env.PATH}:${workspace}/bin",
225 "PACKER_LOG_PATH=${_packer_log}",
226 "PACKER_LOG=1",
227 "TMPDIR=${workspace}/tmp",
228 "PACKER_IMAGES_CACHE=${ImagesCacheFolder}"] + extra_vars_list) {
229 common.infoMsg("Run build with:")
230 sh(script: 'printenv|sort')
231 sh(script: "set -xe ; packer build -on-error=ask -only='qemu' ${_packer_args} -parallel=false template.json".toString())
232
233 def packerStatus = sh(script: "grep \"Some builds didn't complete successfully and had errors\" ${PACKER_LOG_PATH}", returnStatus: true)
234 // grep returns 0 if find something
235 if (packerStatus != 0) {
236 common.infoMsg("qemu instance build completed successfully")
237 } else {
238 throw new Exception("qemu instance build failed")
239 }
240 // collect artifacts
241 // TODO make it possible, process multiply artifacts by one run.
242 dir('images/') {
Aleksey Zvyagintsev42e76952018-12-14 13:40:44 +0000243 sh(script: 'find .')
244 def _files = findFiles(glob: "*qemu*/*${imageName}*.qcow2")
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400245 if (_files.size() > 1) {
246 common.warningMsg("Multiply artifacts detected!Only first one will be processed!")
247 } else if (_files.size() == 0) {
248 throw new Exception("No artifacts detected!BUILD_ONLY=${env.BUILD_ONLY} failed!")
249 }
250 for (String x : _files) {
251 _file = sh(script: "set -x ; readlink -f ${x}", returnStdout: true)
252 sh(script: "mv -v ${x} ${_artifact_dir}/${imageName}.qcow2")
253 // Save filename to list
254 _artifact_list.add("${imageName}.qcow2")
255 }
256 }
257 }
258 }
259
260 } else {
261 throw new Exception("Unexpected BUILD_ONLY=${env.BUILD_ONLY} target!")
262 }
263 }
264
265 stage("Publish artifacts") {
266 dir(_artifact_dir) {
Denis Egorenkoc0aba192019-01-23 18:03:53 +0400267 def published = false
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400268 common.infoMsg("Processing md5 for artifacts")
269 for (String x : _artifact_list) {
270 _md5 = sh(script: "md5sum ${x} > ${x}.md5; cat ${x}.md5", returnStdout: true).trim()
271 _size = sh(script: "ls -alh ${x}", returnStdout: true).trim()
272 common.infoMsg("Artifact file: ${_size}\n${_md5}")
273 }
Denis Egorenkoc0aba192019-01-23 18:03:53 +0400274 if ('local' in publishBackends) {
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400275 common.infoMsg("Uploading to: local")
276 common.infoMsg("For local publish target - nothing to do, all files in: ${_artifact_dir}")
277 if (job_env.get('CLEANUP_AFTER', false)) {
278 common.warningMsg("You are trying to use 'local' publish method, along with enabled CLEANUP_AFTER! ")
279 common.warningMsg("Disabling CLEANUP_AFTER option, to save you'r data ;) ")
280 job_env.CLEANUP_AFTER = false
281 }
Denis Egorenkoc0aba192019-01-23 18:03:53 +0400282 published = true
283 }
284 if ('glance' in publishBackends) {
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400285 common.infoMsg("Uploading to: glance-openstack")
286 if (fileExists("${workspace}/venv")) {
287 common.infoMsg("cleaning virtualenv at:${workspace}/venv")
288 sh(script: "rm -rf ${workspace}/venv", returnStatus: true)
289 }
Denis Egorenkoc0aba192019-01-23 18:03:53 +0400290 openstack.setupOpenstackVirtualenv(openstackEnv)
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400291 for (String x : findFiles(glob: "*.*")) {
Denis Egorenkoc0aba192019-01-23 18:03:53 +0400292 if (x.getName().endsWith('.qcow2')) {
293 _md5sum = sh(script: "cat ${x}.md5", returnStdout: true).trim().split()[0]
Denis Egorenkof4acb4a2019-01-26 13:58:58 +0400294 _property = "--property data=${dateTime} --property md5sum=${_md5sum} --property build='${env.BUILD_URL}' --container-format bare --disk-format qcow2"
Denis Egorenkoc0aba192019-01-23 18:03:53 +0400295 _cmdline = String.format("glance image-create --visibility " +
296 "public %s --name '%s' --file %s", _property, imageName, x)
297 openstack.runOpenstackCommand(_cmdline, rcFile, openstackEnv)
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400298 }
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400299 }
Denis Egorenkoc0aba192019-01-23 18:03:53 +0400300 currentBuild.description = "${imageName}.qcow2 uploaded tenant: ${job_env['OS_PROJECT_NAME']}"
301 published = true
302 }
303 if ('http' in publishBackends) {
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400304 for (String u_file : findFiles(glob: '*.*')) {
Denis Egorenkoc0aba192019-01-23 18:03:53 +0400305 common.infoMsg("Uploading image ${u_file}")
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400306 def uploadImageStatus = ''
307 common.retry(3, 5) {
308 uploadImageStatus = sh(script: "curl -f -T ${u_file} ${job_env.UPLOAD_URL}", returnStatus: true)
309 if (uploadImageStatus != 0) {
310 throw new Exception("Uploading file: ${u_file} failed!")
311 }
312 }
313 // Fixme for correct path ?
314 currentBuild.description = "<a href='http://images.mcp.mirantis.net/${imageName}.qcow2'>${imageName}.qcow2</a>"
315 }
Denis Egorenkoc0aba192019-01-23 18:03:53 +0400316 published = true
317 }
318 if (! published) {
319 throw new Exception("Unsupported publish backend: ${publishBackends}")
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400320 }
321
322 }
323 }
324 } catch (Throwable e) {
325 // If there was an error or exception thrown, the build failed
326 currentBuild.result = "FAILURE"
327 throw e
328 } finally {
azvyagintsev68786a12018-07-10 14:42:29 +0300329 if (job_env.get('CLEANUP_AFTER', false)) {
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400330 dir(workspace) {
331 sh "find . -mindepth 1 -delete || true"
azvyagintsevf2c70cc2018-07-12 12:19:11 +0300332 }
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400333 if (job_env.BUILD_ONLY == 'openstack') {
334 common.warningMsg("openstack Env cleanup not implemented yet!")
azvyagintsev68786a12018-07-10 14:42:29 +0300335 }
Ivan Berezovskiy23035a22018-11-14 13:25:51 +0400336 } else {
337 common.warningMsg("Env has not been cleaned!Please cleanup it manualy!")
azvyagintsev68786a12018-07-10 14:42:29 +0300338 }
azvyagintsev68786a12018-07-10 14:42:29 +0300339 }
azvyagintsev68786a12018-07-10 14:42:29 +0300340 }
azvyagintsev68786a12018-07-10 14:42:29 +0300341}