Add debugging options to wrapper

The options on-error and force are needed for debugging the box
creation.
Also replace all hard tabs with soft tabs.
This commit is contained in:
Christian Kotte 2020-07-12 22:41:09 +02:00 committed by Aaron Bull Schaefer
parent 5a93a04295
commit b8ea7c03d4
2 changed files with 260 additions and 176 deletions

View file

@ -102,6 +102,10 @@ code in order to build the final box.
$ wrapacker --country US --dry-run
For debugging purposes, execute:
$ PACKER_LOG=1 ./wrapacker --country=US --provider=virtualbox --on-error=ask --force
See the `--help` flag for additional details.
Known Issues

View file

@ -64,8 +64,8 @@ validate_country() {
# print this script's usage message to stderr
usage() {
cat <<-EOF >&2
usage: wrapacker [-c COUNTRY] [-p PROVIDER] [-t TIMEOUT] [-d] [-h]
EOF
usage: wrapacker [-c COUNTRY] [-p PROVIDER] [-t TIMEOUT] [-o ON-ERROR ACTION] [-f] [-d] [-h]
EOF
}
# print the list of valid countries to stderr
@ -92,6 +92,14 @@ print_valid_time_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
@ -121,6 +129,13 @@ help() {
sets the amount of time packer will wait for trying ssh login;
defaults to 20m
-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
@ -134,7 +149,7 @@ help() {
AUTHOR
Aaron Bull Schaefer <aaron@elasticdog.com>
EOF
EOF
}
@ -148,8 +163,10 @@ done
# reset all variables that might be set
country=''
provider=''
dry_run=''
dry_run=false
timeout=''
on_error=''
force=false
headless=false
# parse command line options
@ -176,6 +193,16 @@ while [[ $1 != '' ]]; do
--timeout=*)
timeout=${1#*=}
;;
-o | --on-error)
on_error=$2
shift
;;
--on-error=*)
on_error=${1#*=}
;;
-f | --force)
force=true
;;
-d| --dry-run)
dry_run=true
;;
@ -184,6 +211,7 @@ while [[ $1 != '' ]]; do
print_valid_countries
print_valid_providers
print_valid_time_units
print_valid_on_error_actions
exit 0
;;
-e| --headless)
@ -260,8 +288,31 @@ else
SSH_TIMEOUT='20m'
fi
if [[ $dry_run ]]; then
cat <<-EOF
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" \\
@ -269,9 +320,25 @@ if [[ $dry_run ]]; then
-var "ssh_timeout=$SSH_TIMEOUT" \\
-var "country=$country" \\
-var "headless=$headless" \\
-on-error=$ON_ERROR \\
-force \\
arch-template.json
EOF
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" \\
-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" \
@ -279,7 +346,20 @@ else
-var "ssh_timeout=$SSH_TIMEOUT" \
-var "country=$country" \
-var "headless=$headless" \
-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" \
-on-error=$ON_ERROR \
arch-template.json
fi
fi
exit $?