| Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 1 | package com.mirantis.mk | 
|  | 2 |  | 
|  | 3 | /** | 
|  | 4 | * | 
|  | 5 | * Aptly functions | 
|  | 6 | * | 
|  | 7 | */ | 
|  | 8 |  | 
|  | 9 | /** | 
|  | 10 | * Upload package into local repo | 
|  | 11 | * | 
|  | 12 | * @param file          File path | 
|  | 13 | * @param server        Server host | 
|  | 14 | * @param repo          Repository name | 
|  | 15 | */ | 
|  | 16 | def uploadPackage(file, server, repo, skipExists=false) { | 
|  | 17 | def pkg = file.split('/')[-1].split('_')[0] | 
|  | 18 | def jobName = currentBuild.build().environment.JOB_NAME | 
|  | 19 |  | 
|  | 20 | sh("curl -v -f -F file=@${file} ${server}/api/files/${pkg}") | 
|  | 21 | sh("curl -v -o curl_out_${pkg}.log -f -X POST ${server}/api/repos/${repo}/file/${pkg}") | 
|  | 22 |  | 
|  | 23 | try { | 
|  | 24 | sh("cat curl_out_${pkg}.log | json_pp | grep 'Unable to add package to repo' && exit 1 || exit 0") | 
|  | 25 | } catch (err) { | 
|  | 26 | sh("curl -s -f -X DELETE ${server}/api/files/${pkg}") | 
|  | 27 | if (skipExists == true) { | 
|  | 28 | println "[WARN] Package ${pkg} already exists in repo so skipping it's upload as requested" | 
|  | 29 | } else { | 
|  | 30 | error("Package ${pkg} already exists in repo, did you forget to add changelog entry and raise version?") | 
|  | 31 | } | 
|  | 32 | } | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | /** | 
|  | 36 | * Build step to upload package. For use with eg. parallel | 
|  | 37 | * | 
|  | 38 | * @param file          File path | 
|  | 39 | * @param server        Server host | 
|  | 40 | * @param repo          Repository name | 
|  | 41 | */ | 
|  | 42 | def uploadPackageStep(file, server, repo, skipExists=false) { | 
|  | 43 | return { | 
|  | 44 | uploadPackage( | 
|  | 45 | file, | 
|  | 46 | server, | 
|  | 47 | repo, | 
|  | 48 | skipExists | 
|  | 49 | ) | 
|  | 50 | } | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | def snapshotRepo(server, repo, timestamp = null) { | 
|  | 54 | // XXX: timestamp parameter is obsoleted, time of snapshot creation is | 
|  | 55 | // what we should always use, not what calling pipeline provides | 
|  | 56 | def now = new Date(); | 
|  | 57 | def ts = now.format("yyyyMMddHHmmss", TimeZone.getTimeZone('UTC')); | 
|  | 58 |  | 
|  | 59 | def snapshot = "${repo}-${ts}" | 
|  | 60 | sh("curl -f -X POST -H 'Content-Type: application/json' --data '{\"Name\":\"$snapshot\"}' ${server}/api/repos/${repo}/snapshots") | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | def cleanupSnapshots(server, config='/etc/aptly-publisher.yaml', opts='-d --timeout 600') { | 
|  | 64 | sh("aptly-publisher -c ${config} ${opts} --url ${server} cleanup") | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | def diffPublish(server, source, target, components=null, opts='--timeout 600') { | 
|  | 68 | if (components) { | 
|  | 69 | def componentsStr = components.join(' ') | 
|  | 70 | opts = "${opts} --components ${componentsStr}" | 
|  | 71 | } | 
|  | 72 | sh("aptly-publisher --dry --url ${server} promote --source ${source} --target ${target} --diff ${opts}") | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | def promotePublish(server, source, target, recreate=false, components=null, packages=null, diff=false, opts='-d --timeout 600') { | 
|  | 76 | if (components) { | 
|  | 77 | def componentsStr = components.join(' ') | 
|  | 78 | opts = "${opts} --components ${componentsStr}" | 
|  | 79 | } | 
|  | 80 | if (packages) { | 
|  | 81 | def packagesStr = packages.join(' ') | 
|  | 82 | opts = "${opts} --packages ${packagesStr}" | 
|  | 83 | } | 
|  | 84 | if (recreate.toBoolean() == true) { | 
|  | 85 | opts = "${opts} --recreate" | 
|  | 86 | } | 
|  | 87 | if (diff.toBoolean() == true) { | 
|  | 88 | opts = "--dry --diff" | 
|  | 89 | } | 
|  | 90 | sh("aptly-publisher --url ${server} promote --source ${source} --target ${target} ${opts}") | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | def publish(server, config='/etc/aptly-publisher.yaml', recreate=false, opts='-d --timeout 600') { | 
|  | 94 | if (recreate == true) { | 
|  | 95 | opts = "${opts} --recreate" | 
|  | 96 | } | 
|  | 97 | sh("aptly-publisher --url ${server} -c ${config} ${opts} publish") | 
|  | 98 | } |