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 | |
chnyda | 751ac40 | 2017-11-28 17:32:43 +0100 | [diff] [blame] | 75 | def promotePublish(server, source, target, recreate=false, components=null, packages=null, diff=false, opts='-d --timeout 600', dump_publish=false, storage="") { |
chnyda | be46ffa | 2017-03-21 13:19:08 +0100 | [diff] [blame] | 76 | if (components && components != "all" && components != "") { |
| 77 | def componentsStr = components.replaceAll(",", " ") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 78 | opts = "${opts} --components ${componentsStr}" |
| 79 | } |
chnyda | be46ffa | 2017-03-21 13:19:08 +0100 | [diff] [blame] | 80 | if (packages && packages != "all" && packages != "") { |
| 81 | def packagesStr = packages.replaceAll(",", " ") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 82 | opts = "${opts} --packages ${packagesStr}" |
| 83 | } |
| 84 | if (recreate.toBoolean() == true) { |
| 85 | opts = "${opts} --recreate" |
| 86 | } |
| 87 | if (diff.toBoolean() == true) { |
chnyda | 5de5d43 | 2017-04-10 15:35:47 +0200 | [diff] [blame] | 88 | opts = "${opts} --dry --diff" |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 89 | } |
chnyda | be46ffa | 2017-03-21 13:19:08 +0100 | [diff] [blame] | 90 | |
chnyda | 751ac40 | 2017-11-28 17:32:43 +0100 | [diff] [blame] | 91 | if (storage && storage != "") { |
| 92 | opts = "${opts} --storage ${storage}" |
| 93 | } |
| 94 | |
chnyda | 4906ce0 | 2017-08-22 14:45:13 +0200 | [diff] [blame] | 95 | if (dump_publish) { |
| 96 | def now = new Date(); |
chnyda | f2f9579 | 2017-12-11 17:09:27 +0100 | [diff] [blame^] | 97 | dumpTarget = target |
chnyda | 4906ce0 | 2017-08-22 14:45:13 +0200 | [diff] [blame] | 98 | def timestamp = now.format("yyyyMMddHHmmss", TimeZone.getTimeZone('UTC')); |
chnyda | f2f9579 | 2017-12-11 17:09:27 +0100 | [diff] [blame^] | 99 | |
| 100 | if (target.contains(')') || target.contains('*')) { |
| 101 | sourceTarget = source.split('/') |
| 102 | dumpTarget = target.split('/')[-1] |
| 103 | sourceTarget[-1] = dumpTarget |
| 104 | dumpTarget = sourceTarget.join('/') |
| 105 | } |
| 106 | |
| 107 | dumpPublishes(server, timestamp, dumpTarget) |
chnyda | 4906ce0 | 2017-08-22 14:45:13 +0200 | [diff] [blame] | 108 | } |
chnyda | c78dfa5 | 2017-08-22 14:39:43 +0200 | [diff] [blame] | 109 | |
chnyda | fac3ad5 | 2017-12-11 16:19:05 +0100 | [diff] [blame] | 110 | sh("aptly-publisher --url ${server} promote --source '${source}' --target '${target}' --force-overwrite ${opts}") |
chnyda | c78dfa5 | 2017-08-22 14:39:43 +0200 | [diff] [blame] | 111 | |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Filip Pytloun | 15fbbc9 | 2017-11-27 14:33:22 +0100 | [diff] [blame] | 114 | def publish(server, config='/etc/aptly-publisher.yaml', recreate=false, only_latest=true, force_overwrite=true, opts='-d --timeout 3600') { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 115 | if (recreate == true) { |
| 116 | opts = "${opts} --recreate" |
| 117 | } |
Filip Pytloun | 62fe3a4 | 2017-11-27 14:30:19 +0100 | [diff] [blame] | 118 | if (only_latest == true) { |
| 119 | opts = "${opts} --only-latest" |
| 120 | } |
| 121 | if (force_overwrite == true) { |
| 122 | opts = "${opts} --force-overwrite" |
| 123 | } |
| 124 | sh("aptly-publisher --url ${server} -c ${config} ${opts} publish") |
chnyda | d8f51af | 2017-07-24 10:39:48 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Dump publishes |
| 129 | * |
| 130 | * @param server Server host |
| 131 | * @param save-dir Directory where publishes are to be serialized |
| 132 | * @param publishes Publishes to be serialized |
| 133 | * @param prefix Prefix of dump files |
| 134 | * @param opts Options: debug, timeout, ... |
| 135 | */ |
chnyda | 5fe91ca | 2017-09-19 09:19:46 +0200 | [diff] [blame] | 136 | def dumpPublishes(server, prefix, publishes='all', opts='-d --timeout 600') { |
chnyda | f2f9579 | 2017-12-11 17:09:27 +0100 | [diff] [blame^] | 137 | sh("aptly-publisher dump --url ${server} --save-dir . --prefix ${prefix} -p '${publishes}' ${opts}") |
chnyda | 5fe91ca | 2017-09-19 09:19:46 +0200 | [diff] [blame] | 138 | archiveArtifacts artifacts: "${prefix}*" |
chnyda | d8f51af | 2017-07-24 10:39:48 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Restore publish from YAML file |
| 143 | * |
| 144 | * @param server Server host |
| 145 | * @param recreate Recreate publishes |
| 146 | * @param publish Serialized YAML of Publish |
| 147 | * @param components Components to restore |
| 148 | */ |
| 149 | def restorePublish(server, recreate, publish, components='all') { |
| 150 | |
| 151 | opts = "" |
| 152 | if (recreate) { |
| 153 | opts << " --recreate" |
| 154 | } |
| 155 | |
| 156 | sh("rm tmpFile || true") |
| 157 | writeFile(file: "tmpFile", text: publish) |
| 158 | sh("aptly-publisher restore --url ${server} --restore-file tmpFile --components ${components} ${opts}") |
| 159 | } |