blob: 4fb655cf4a50928800d0c6e23a42354ad1c22411 [file] [log] [blame]
Jakub Josef79ecec32017-02-17 14:36:28 +01001package 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 */
16def 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 */
42def uploadPackageStep(file, server, repo, skipExists=false) {
43 return {
44 uploadPackage(
45 file,
46 server,
47 repo,
48 skipExists
49 )
50 }
51}
52
53def 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
63def cleanupSnapshots(server, config='/etc/aptly-publisher.yaml', opts='-d --timeout 600') {
64 sh("aptly-publisher -c ${config} ${opts} --url ${server} cleanup")
65}
66
67def 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
chnyda751ac402017-11-28 17:32:43 +010075def promotePublish(server, source, target, recreate=false, components=null, packages=null, diff=false, opts='-d --timeout 600', dump_publish=false, storage="") {
chnydabe46ffa2017-03-21 13:19:08 +010076 if (components && components != "all" && components != "") {
77 def componentsStr = components.replaceAll(",", " ")
Jakub Josef79ecec32017-02-17 14:36:28 +010078 opts = "${opts} --components ${componentsStr}"
79 }
chnydabe46ffa2017-03-21 13:19:08 +010080 if (packages && packages != "all" && packages != "") {
81 def packagesStr = packages.replaceAll(",", " ")
Jakub Josef79ecec32017-02-17 14:36:28 +010082 opts = "${opts} --packages ${packagesStr}"
83 }
84 if (recreate.toBoolean() == true) {
85 opts = "${opts} --recreate"
86 }
87 if (diff.toBoolean() == true) {
chnyda5de5d432017-04-10 15:35:47 +020088 opts = "${opts} --dry --diff"
Jakub Josef79ecec32017-02-17 14:36:28 +010089 }
chnydabe46ffa2017-03-21 13:19:08 +010090
chnyda751ac402017-11-28 17:32:43 +010091 if (storage && storage != "") {
92 opts = "${opts} --storage ${storage}"
93 }
94
chnyda4906ce02017-08-22 14:45:13 +020095 if (dump_publish) {
96 def now = new Date();
chnydaf2f95792017-12-11 17:09:27 +010097 dumpTarget = target
chnyda4906ce02017-08-22 14:45:13 +020098 def timestamp = now.format("yyyyMMddHHmmss", TimeZone.getTimeZone('UTC'));
chnydaf2f95792017-12-11 17:09:27 +010099
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)
chnyda4906ce02017-08-22 14:45:13 +0200108 }
chnydac78dfa52017-08-22 14:39:43 +0200109
chnydafac3ad52017-12-11 16:19:05 +0100110 sh("aptly-publisher --url ${server} promote --source '${source}' --target '${target}' --force-overwrite ${opts}")
chnydac78dfa52017-08-22 14:39:43 +0200111
Jakub Josef79ecec32017-02-17 14:36:28 +0100112}
113
Filip Pytloun15fbbc92017-11-27 14:33:22 +0100114def publish(server, config='/etc/aptly-publisher.yaml', recreate=false, only_latest=true, force_overwrite=true, opts='-d --timeout 3600') {
Jakub Josef79ecec32017-02-17 14:36:28 +0100115 if (recreate == true) {
116 opts = "${opts} --recreate"
117 }
Filip Pytloun62fe3a42017-11-27 14:30:19 +0100118 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")
chnydad8f51af2017-07-24 10:39:48 +0200125}
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 */
chnyda5fe91ca2017-09-19 09:19:46 +0200136def dumpPublishes(server, prefix, publishes='all', opts='-d --timeout 600') {
chnydaf2f95792017-12-11 17:09:27 +0100137 sh("aptly-publisher dump --url ${server} --save-dir . --prefix ${prefix} -p '${publishes}' ${opts}")
chnyda5fe91ca2017-09-19 09:19:46 +0200138 archiveArtifacts artifacts: "${prefix}*"
chnydad8f51af2017-07-24 10:39:48 +0200139}
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 */
149def 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}