-
Notifications
You must be signed in to change notification settings - Fork 4
/
Justfile
357 lines (276 loc) · 9.48 KB
/
Justfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
version_file := justfile_directory() / "package-versions.json"
pgtap_versions := justfile_directory() / "build/pgtap/versions.json"
pg_prove_version := `jq -r .pg_prove.version < package-versions.json`
pgtap_version := `jq -r .pgtap.version < package-versions.json`
pgtap_hashref := `jq -r '.pgtap.hashref // empty' < package-versions.json`
sqitch_version := `jq -r .sqitch.version < package-versions.json`
alpine_version := `jq -r .alpine.version < package-versions.json`
image_version := `jq -r .VERSION < package-versions.json`
image_date := `jq -r .DATE < package-versions.json`
[private]
@list:
just --list --unsorted
# Show the version as it would be from the built image
version NEW_VERSION="":
#! /usr/bin/env bash
set -euo pipefail
rv() {
jq -r ".$1" < {{ version_file }}
}
show() {
local alpine_version image_date image_version pg_prove_version \
pgtap_version pgtap_hashref sqitch_version
alpine_version="$(rv alpine.version)"
image_date="$(rv DATE)"
image_version="$(rv VERSION)"
pg_prove_version="$(rv pg_prove.version)"
pgtap_hashref="$(rv pgtap.hashref)"
pgtap_version="$(rv pgtap.version)"
sqitch_version="$(rv sqitch.version)"
if [[ -n "${pgtap_hashref}" ]] && [[ "${pgtap_hashref}" != null ]]; then
pgtap_version="${pgtap_version} (${pgtap_hashref})"
fi
cat <<EOS
[gchr.io/]kineticcafe/sqitch-pgtap:${image_version}
alpine ${alpine_version}
sqitch (App::Sqitch) v${sqitch_version}
pgtap ${pgtap_version}
pg_prove ${pg_prove_version}
Last updated ${image_date}
EOS
}
show
if [[ "{{ NEW_VERSION }}" != "" ]]; then
just update_package_versions VERSION {{ NEW_VERSION }}
echo ""
show
fi
# Creates a git tag for the stored image version (only on main branch)
[no-exit-message]
@tag:
if [[ "$(git branch --show-current)" == main ]]; then \
git tag v{{ image_version }}; \
else \
echo "Must be on main branch to create tag v{{ image_version }}."; \
fi
# Set the new sqitch version
sqitch NEW_VERSION: (update_package_versions "sqitch.version" NEW_VERSION)
# Set the new pgtap version
pgtap NEW_VERSION NEW_HASHREF="": (update_package_versions "pgtap.version" NEW_VERSION "pgtap.hashref" NEW_HASHREF)
@git diff --quiet || just update-pgtap
# Set the new pg_prove version
pg_prove NEW_VERSION: (update_package_versions "pg_prove.version" NEW_VERSION)
# Set new alpine version
@alpine NEW_VERSION: (update_package_versions "alpine.version" NEW_VERSION)
jq '.defaults.alpine = "{{ NEW_VERSION }}"' "{{ pgtap_versions }}" | \
sponge "{{ pgtap_versions }}"
git diff --quiet || just update-pgtap
# Add a new PostgreSQL version
postgres VERSION *EXTRA:
#!/usr/bin/env bash
set -euo pipefail
set -- {{ EXTRA }}
while (($#)); do
case "$1" in
using) using="$2" ;;
alpine) alpine="$2" ;;
eol) eol="$2" ;;
*)
echo >&2 "Unknown custom version expecting 'using', 'alpine', or 'eol'."
exit 1
;;
esac
shift 2 || break
done
version="{{ VERSION }}"
case "${version}" in
=*)
jq --arg version "${version:1}" \
'.postgres[] | select(.name == $version)' \
"{{ pgtap_versions }}"
exit 0
;;
esac
jq --arg version "${version}" \
--arg using "${using:-}" \
--arg alpine "${alpine:-}" \
--arg eol "${eol:-}" \
'
{name: $version} as $pg |
(if $using == "" then $pg else $pg + {version: $using} end) as $pg |
(if $alpine == "" then $pg else $pg + {alpine: $alpine} end) as $pg |
(if $eol == "" then $pg else $pg + {eol: $eol} end) as $pg |
.postgres |=
(map(.name) | index($version)) as $ix |
if $ix then .[$ix] = $pg else [$pg] + . end
' "{{ pgtap_versions }}" | sponge "{{ pgtap_versions }}"
git diff --quiet || just update-pgtap
# Build a test version of the image
build:
#!/usr/bin/env bash
set -euo pipefail
docker build \
--build-arg ALPINE_VERSION="{{ alpine_version }}" \
--build-arg PG_PROVE_VERSION="{{ pg_prove_version }}" \
--build-arg PGTAP_VERSION="{{ pgtap_version }}" \
--build-arg SQITCH_VERSION="{{ sqitch_version }}" \
--build-arg __DOCKERFILE_VERSION__="{{ image_version }}" \
--build-arg __DOCKERFILE_DATE__="{{ image_date }}" \
--tag kineticcafe/sqitch-pgtap:latest \
.
# Update pgTAP sources
update-pgtap: download_pgtap generate_pgtap_dockerfile && update-do_pgtap
#!/usr/bin/env bash
set -euo pipefail
docker build \
--build-arg PGTAP_VERSION="{{ pgtap_version }}" \
--tag build-pgtap:latest \
"{{ justfile_directory() }}/build/pgtap"
docker container create --name builder build-pgtap:latest
docker container cp --quiet builder:/opt/pgtap.tar "{{ justfile_directory() }}"
docker container rm builder
docker image rm build-pgtap:latest
tar xf pgtap.tar
rm -rf {{ justfile_directory() }}/build/pgtap/pgtap-"{{ pgtap_version }}" \
{{ justfile_directory() }}/build/pgtap/pgtap-"{{ pgtap_version }}".zip \
{{ justfile_directory() }}/pgtap.tar
git add opt/pgtap
[private]
download_pgtap:
#!/usr/bin/env bash
set -euo pipefail
rm -rf "{{ justfile_directory() }}/build/pgtap/pgtap-{{ pgtap_version }}" \
"{{ justfile_directory() }}/build/pgtap/pgtap-{{ pgtap_version }}.zip" \
"{{ justfile_directory() }}/pgtap.tar"
if [[ -n "{{ pgtap_hashref }}" ]]; then
git clone https://github.com/theory/pgtap.git \
"{{ justfile_directory() }}/build/pgtap/pgtap-{{ pgtap_version }}"
git -C "{{ justfile_directory() }}/build/pgtap/pgtap-{{ pgtap_version }}" \
switch --detach "{{ pgtap_hashref }}"
else
curl -sq -LO \
"https://api.pgxn.org/dist/pgtap/{{ pgtap_version }}/pgtap-{{ pgtap_version }}.zip"
unzip -qq -d "{{ justfile_directory() }}/build/pgtap" "pgtap-{{ pgtap_version }}.zip"
fi
[private]
generate_pgtap_dockerfile:
#!/usr/bin/env ruby
require 'json'
versions = JSON.load_file("{{ pgtap_versions }}")
blocks = versions["postgres"].map { |pg_version|
name = pg_version.fetch("name")
version = pg_version.fetch("version") { name }
alpine = pg_version.fetch("alpine") { versions.dig("defaults", "alpine") }
<<~BUILD_BLOCK
FROM postgres:#{version}-alpine#{alpine} AS build-pgtap-psql-#{name}
ARG PGTAP_VERSION
COPY pgtap-$PGTAP_VERSION /opt/pgtap-$PGTAP_VERSION
RUN <<SETUP
set -eux
apk update
apk add \\
bash \\
build-base \\
make \\
openssl \\
perl \\
perl-dev \\
postgresql-dev \\
wget
wanted_clang=$(
pg_config --configure |
tr ' ' '\\n' |
grep CLANG |
sed "s/'CLANG=\\(.*\\)'/\\1/"
)
if ! command -v "${wanted_clang}" >/dev/null 2>/dev/null; then
version="${wanted_clang/clang-}"
apk add clang"${version}" llvm"${version}"
fi
mkdir -p /opt/pgtap/#{name}
SETUP
RUN <<BUILD
set -eux
cd /opt/pgtap-$PGTAP_VERSION
make
make install
mv sql/pgtap.sql sql/uninstall_pgtap.sql /opt/pgtap/#{name}
BUILD
BUILD_BLOCK
}
copy = versions["postgres"].map { |pg_version|
name = pg_version.fetch("name")
"COPY --from=build-pgtap-psql-#{name} /opt/pgtap/#{name} /opt/pgtap/#{name}"
}
dockerfile = <<~DOCKERFILE
# syntax=docker/dockerfile:1
#{blocks.join("\n\n")}
FROM alpine:{{ alpine_version }} AS package-pgtap
RUN mkdir -p /opt/pgtap
#{copy.join("\n")}
RUN tar cf /opt/pgtap.tar /opt/pgtap
DOCKERFILE
File.write("build/pgtap/Dockerfile", dockerfile)
[private]
update-do_pgtap:
#!/usr/bin/env ruby
require 'json'
versions = JSON.load_file("{{ pgtap_versions }}")
clauses = versions["postgres"].sort_by { _1["name"].to_f }.map { |pg_version|
name = pg_version.fetch("name")
eol = pg_version["eol"]
if eol
<<~CLAUSE
#{name}*)
eol #{name} #{eol}
version=#{name}
;;
CLAUSE
else
"#{name}*) version=#{name} ;;\n"
end
}.join
case_esac = <<~CASE
case "${version}" in
#{clauses.chomp}
*)
echo >&2 "Unknown or unsupported PostgreSQL version '${version}'."
exit 1
;;
esac
CASE
script = File
.read("scripts/do_pgtap")
.sub(/case "\$\{version\}" in.+?esac/m, case_esac.chomp)
File.write("scripts/do_pgtap", script)
[private]
update_package_versions *args:
#! /usr/bin/env bash
set -euo pipefail
set -- {{ args }}
update() {
local key value old
key="$1"
value="${2:-}"
old="$(jq -r ".$1" < {{ version_file }})"
jq -c --arg value "${value}" "
path (.${key}) as \$key |
if \$value == \"\" then
delpaths([\$key])
else
setpath(\$key; \$value)
end
" "{{ version_file }}" | sponge "{{ version_file }}"
tput setaf 1 && echo "- ${key} :: ${old}"
[[ "${value}" == "" ]] || {
tput setaf bold && tput setaf 2 && echo "+ ${key} :: ${value}"
}
tput sgr0
}
while (( $# )); do
update "$1" "${2:-}"
shift 2 || break
done
if ! git diff --quiet; then
update DATE "$(date +"%Y-%m-%d")"
fi