forked from elasticdog/packer-arch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapacker
executable file
·382 lines (339 loc) · 8.78 KB
/
wrapacker
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env bash
#
# wrapacker - https://github.com/elasticdog/packer-arch
#
# A script to automatically ensure the latest ISO download URL and optionally
# use a mirror from the provided country in order to build an Arch Linux box
# using Packer.
#
# Copyright (c) 2015 Aaron Bull Schaefer <[email protected]>
# This source code is provided under the terms of the ICS License
# that can be be found in the LICENSE file.
#
##### Constants
# country codes supported by https://www.archlinux.org/mirrorlist/
readonly VALID_COUNTRIES=(AT AU BD BE BG BR BY CA CH CL CN CO CZ DE DK EC ES FR GB GR HR HU ID IE IL IN IR IS IT JP KR KZ LT LU LV MK NC NL NO NZ PH PL PT RO RS RU SE SG SK TR TW UA US VN ZA)
if command -v packer-io > /dev/null 2>&1; then
# Older arch linux versions called the packer binary packer-io.
readonly PACKER_BIN='packer-io'
else
readonly PACKER_BIN='packer'
fi
VALID_TIME_UNITS=(ns us ms s m h)
##### Functions
# print a message to stderr
warn() {
local fmt="$1"
shift
printf "wrapacker: $fmt\n" "$@" >&2
}
# print a message to stderr and exit with either
# the given status or that of the most recent command
die() {
local st="$?"
if [[ "$1" != *[^0-9]* ]]; then
st="$1"
shift
fi
warn "$@"
exit "$st"
}
# test the VALID_COUNTRIES array for membership of the given value
validate_country() {
local haystack="VALID_COUNTRIES[@]"
local needle=$1
local found=1
for element in "${!haystack}"; do
if [[ $element == "$needle" ]]; then
found=0
break
fi
done
return $found
}
# print this script's usage message to stderr
usage() {
cat <<-EOF >&2
usage: wrapacker [-c COUNTRY] [-p PROVIDER] [-t TIMEOUT] [-w] [-o ON-ERROR ACTION] [-f] [-d] [-h]
EOF
}
# print the list of valid countries to stderr
print_valid_countries() {
printf '\n*** VALID COUNTRY CODES ***\n\n' >&2
for country in "${VALID_COUNTRIES[@]}"; do
printf '%-6s\n' "$country"
done | column >&2
}
# print the list of valid providers to stderr
print_valid_providers() {
printf '\n*** VALID PROVIDERS ***\n\n' >&2
for provider in {parallels,virtualbox,vmware,libvirt}; do
printf '%-6s\n' "$provider"
done | column >&2
}
# print the list of valid time units to stderr
print_valid_time_units() {
printf '\n*** VALID TIME UNITS ***\n\n' >&2
for units in "${VALID_TIME_UNITS[@]}"; do
printf '%-6s\n' "$units"
done | column >&2
}
# print the list of valid on-error actions to stderr
print_valid_on_error_actions() {
printf '\n*** VALID ON-ERROR ACTIONS ***\n\n' >&2
for action in {cleanup,abort,ask}; do
printf '%-6s\n' "$action"
done | column >&2
}
# print this script's help message to stdout
help() {
cat <<-EOF
NAME
wrapacker -- wrap packer to build arch with custom mirror settings
SYNOPSIS
wrapacker [options...]
DESCRIPTION
wrapacker will automatically ensure the latest ISO download URL and
will optionally use a mirror from the provided country in order to
build an Arch Linux box using Packer.
OPTIONS
-c, --country=COUNTRY
the country code to download from;
defaults to the kernel.org US mirror
-p, --provider=PROVIDER
the packer provider to build with;
defaults to virtualbox
-t, --timeout=TIMEOUT
sets the amount of time packer will wait for trying ssh login;
defaults to 20m
-w, --skip-write-zeros
don't inflate the disk to improve virtual disk compaction
-o, --on-error=ACTION
error handling if the build fails;
defaults to cleanup
-f, --force
force a build to continue if artifacts exist; deletes existing artifacts
-d, --dry-run
do not actually perform the build, just show what would run
-h, --help
view this help message
-e, --headless
do not run the build in the GUI
only works with --provider=virtualbox
AUTHOR
Aaron Bull Schaefer <[email protected]>
EOF
}
##### Main
# check for dependencies
for cmd in {awk,curl,sed,tr}; do
command -v $cmd > /dev/null || die "required command \"$cmd\" was not found"
done
# reset all variables that might be set
country=''
provider=''
dry_run=false
timeout=''
skip_write_zeros=false
on_error=''
force=false
headless=false
# parse command line options
while [[ $1 != '' ]]; do
case $1 in
-c | --country)
country=$(echo "$2" | tr '[:lower:]' '[:upper:]')
shift
;;
--country=*)
country=$(echo "${1#*=}" | tr '[:lower:]' '[:upper:]')
;;
-p | --provider)
provider=$2
shift
;;
--provider=*)
provider=${1#*=}
;;
-t | --timeout)
timeout=$2
shift
;;
--timeout=*)
timeout=${1#*=}
;;
-w | --skip-write-zeros)
skip_write_zeros=true
;;
-o | --on-error)
on_error=$2
shift
;;
--on-error=*)
on_error=${1#*=}
;;
-f | --force)
force=true
;;
-d| --dry-run)
dry_run=true
;;
-h | --help | -\?)
help
print_valid_countries
print_valid_providers
print_valid_time_units
print_valid_on_error_actions
exit 0
;;
-e| --headless)
headless=true
;;
--*)
warn "unknown option -- ${1#--}"
usage
exit 1
;;
*)
warn "unknown option -- ${1#-}"
usage
exit 1
;;
esac
shift
done
if [[ $provider ]]; then
case $(echo "$provider" | tr '[:upper:]' '[:lower:]') in
parallels | parallels-iso)
PACKER_PROVIDER='parallels-iso'
;;
virtualbox | virtualbox-iso)
PACKER_PROVIDER='virtualbox-iso'
;;
vmware | vmware-iso)
PACKER_PROVIDER='vmware-iso'
;;
libvirt | qemu)
PACKER_PROVIDER='qemu'
;;
*)
warn "unknown provider -- ${provider}"
usage
print_valid_providers
exit 1
;;
esac
else
PACKER_PROVIDER='virtualbox-iso'
fi
if [[ $country ]]; then
if validate_country "$country"; then
MIRRORLIST="https://archlinux.org/mirrorlist/?country=${country}&protocol=http&protocol=https&ip_version=4&use_mirror_status=on"
MIRROR=$(curl -s "$MIRRORLIST" | awk '/#Server/{ print $3; exit }' | sed 's|/$repo.*||')
else
warn 'INVALID COUNTRY SPECIFIED - %s' "$country"
usage
print_valid_countries
exit 1
fi
else
MIRROR='https://mirrors.kernel.org/archlinux'
country='US'
fi
ISO_CHECKSUM_URL="${MIRROR}/iso/latest/sha256sums.txt"
ISO_NAME=$(curl -sL "$ISO_CHECKSUM_URL" | awk '/-x86_64.iso/{ print $2 }' | tail -n 1)
ISO_URL="${MIRROR}/iso/latest/${ISO_NAME}"
if [[ $timeout ]]; then
if [[ "$timeout" =~ ^[0-9]+(ns|us|ms|s|m|h)$ ]]; then
SSH_TIMEOUT=$timeout
else
warn 'INVALID TIME UNITS SPECIFIED or MISSING UNIT - %s' "$timeout"
usage
print_valid_time_units
exit 1
fi
else
SSH_TIMEOUT='20m'
fi
if [[ $skip_write_zeros = true ]]; then
WRITE_ZEROS="false"
else
WRITE_ZEROS="true"
fi
if [[ $on_error ]]; then
case $(echo "$on_error" | tr '[:upper:]' '[:lower:]') in
cleanup)
ON_ERROR='cleanup'
;;
abort)
ON_ERROR='abort'
;;
ask)
ON_ERROR='ask'
;;
*)
warn "unknown on-error action -- ${on_error}"
usage
print_valid_on_error_actions
exit 1
;;
esac
else
ON_ERROR='cleanup'
fi
if [[ $dry_run = true ]]; then
if [[ $force = true ]]; then
cat <<-EOF
$PACKER_BIN build \\
-only=$PACKER_PROVIDER \\
-var "iso_url=$ISO_URL" \\
-var "iso_checksum_url=$ISO_CHECKSUM_URL" \\
-var "ssh_timeout=$SSH_TIMEOUT" \\
-var "country=$country" \\
-var "headless=$headless" \\
-var "write_zeros=$WRITE_ZEROS" \\
-on-error=$ON_ERROR \\
-force \\
arch-template.json
EOF
else
cat <<-EOF
$PACKER_BIN build \\
-only=$PACKER_PROVIDER \\
-var "iso_url=$ISO_URL" \\
-var "iso_checksum_url=$ISO_CHECKSUM_URL" \\
-var "ssh_timeout=$SSH_TIMEOUT" \\
-var "country=$country" \\
-var "headless=$headless" \\
-var "write_zeros=$WRITE_ZEROS" \\
-on-error=$ON_ERROR \\
arch-template.json
EOF
fi
else
if [[ $force = true ]]; then
$PACKER_BIN build \
-only=$PACKER_PROVIDER \
-var "iso_url=$ISO_URL" \
-var "iso_checksum_url=$ISO_CHECKSUM_URL" \
-var "ssh_timeout=$SSH_TIMEOUT" \
-var "country=$country" \
-var "headless=$headless" \
-var "write_zeros=$WRITE_ZEROS" \
-on-error=$ON_ERROR \
-force \
arch-template.json
else
$PACKER_BIN build \
-only=$PACKER_PROVIDER \
-var "iso_url=$ISO_URL" \
-var "iso_checksum_url=$ISO_CHECKSUM_URL" \
-var "ssh_timeout=$SSH_TIMEOUT" \
-var "country=$country" \
-var "headless=$headless" \
-var "write_zeros=$WRITE_ZEROS" \
-on-error=$ON_ERROR \
arch-template.json
fi
fi
exit $?