blob: 651b71a0f186d7f039bcdb43e8291c831e52df1e [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'));
94 dumpPublishes(server, ".", timestamp, target)
95 }
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
101def publish(server, config='/etc/aptly-publisher.yaml', recreate=false, opts='-d --timeout 600') {
102 if (recreate == true) {
103 opts = "${opts} --recreate"
104 }
chnyda8de0fac2017-04-25 13:46:54 +0200105 sh("aptly-publisher --url ${server} -c ${config} ${opts} --force-overwrite publish")
chnydad8f51af2017-07-24 10:39:48 +0200106}
107
108/**
109 * Dump publishes
110 *
111 * @param server Server host
112 * @param save-dir Directory where publishes are to be serialized
113 * @param publishes Publishes to be serialized
114 * @param prefix Prefix of dump files
115 * @param opts Options: debug, timeout, ...
116 */
117def dumpPublishes(server, saveDir, prefix, publishes='all', opts='-d --timeout 600') {
118 sh("aptly-publisher dump --url ${server} --save-dir ${saveDir} --prefix ${prefix} ${opts}")
119 archiveArtifacts artifacts: "${saveDir}/${prefix}*"
120}
121
122/**
123 * Restore publish from YAML file
124 *
125 * @param server Server host
126 * @param recreate Recreate publishes
127 * @param publish Serialized YAML of Publish
128 * @param components Components to restore
129 */
130def restorePublish(server, recreate, publish, components='all') {
131
132 opts = ""
133 if (recreate) {
134 opts << " --recreate"
135 }
136
137 sh("rm tmpFile || true")
138 writeFile(file: "tmpFile", text: publish)
139 sh("aptly-publisher restore --url ${server} --restore-file tmpFile --components ${components} ${opts}")
140}