blob: 8cac735643b6bf9b2904c092b7c8dd92a1584a33 [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
chnyda4906ce02017-08-22 14:45:13 +020075def promotePublish(server, source, target, recreate=false, components=null, packages=null, diff=false, opts='-d --timeout 600', dump_publish=false) {
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
chnyda4906ce02017-08-22 14:45:13 +020091 if (dump_publish) {
92 def now = new Date();
93 def timestamp = now.format("yyyyMMddHHmmss", TimeZone.getTimeZone('UTC'));
chnyda5fe91ca2017-09-19 09:19:46 +020094 dumpPublishes(server, timestamp, target)
chnyda4906ce02017-08-22 14:45:13 +020095 }
chnydac78dfa52017-08-22 14:39:43 +020096
97 sh("aptly-publisher --url ${server} promote --source ${source} --target ${target} --force-overwrite ${opts}")
98
Jakub Josef79ecec32017-02-17 14:36:28 +010099}
100
Filip Pytloun62fe3a42017-11-27 14:30:19 +0100101def publish(server, config='/etc/aptly-publisher.yaml', recreate=false, only_latest=true, force_overwrite=true, opts='-d --timeout 600') {
Jakub Josef79ecec32017-02-17 14:36:28 +0100102 if (recreate == true) {
103 opts = "${opts} --recreate"
104 }
Filip Pytloun62fe3a42017-11-27 14:30:19 +0100105 if (only_latest == true) {
106 opts = "${opts} --only-latest"
107 }
108 if (force_overwrite == true) {
109 opts = "${opts} --force-overwrite"
110 }
111 sh("aptly-publisher --url ${server} -c ${config} ${opts} publish")
chnydad8f51af2017-07-24 10:39:48 +0200112}
113
114/**
115 * Dump publishes
116 *
117 * @param server Server host
118 * @param save-dir Directory where publishes are to be serialized
119 * @param publishes Publishes to be serialized
120 * @param prefix Prefix of dump files
121 * @param opts Options: debug, timeout, ...
122 */
chnyda5fe91ca2017-09-19 09:19:46 +0200123def dumpPublishes(server, prefix, publishes='all', opts='-d --timeout 600') {
chnydaf36d7042017-09-19 09:25:32 +0200124 sh("aptly-publisher dump --url ${server} --save-dir . --prefix ${prefix} -p ${publishes} ${opts}")
chnyda5fe91ca2017-09-19 09:19:46 +0200125 archiveArtifacts artifacts: "${prefix}*"
chnydad8f51af2017-07-24 10:39:48 +0200126}
127
128/**
129 * Restore publish from YAML file
130 *
131 * @param server Server host
132 * @param recreate Recreate publishes
133 * @param publish Serialized YAML of Publish
134 * @param components Components to restore
135 */
136def restorePublish(server, recreate, publish, components='all') {
137
138 opts = ""
139 if (recreate) {
140 opts << " --recreate"
141 }
142
143 sh("rm tmpFile || true")
144 writeFile(file: "tmpFile", text: publish)
145 sh("aptly-publisher restore --url ${server} --restore-file tmpFile --components ${components} ${opts}")
146}