From 2464bd2c31932fb8f8f28db3dd00b4aa98f96fdc Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Mon, 28 Dec 2015 12:21:04 +0900 Subject: [PATCH] Add timeout option to specify ssh_timeout via wrapacker Closes #40 Fixes #8 --- arch-template.json | 6 +++++- wrapacker | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/arch-template.json b/arch-template.json index a95574f..3c9f1f2 100644 --- a/arch-template.json +++ b/arch-template.json @@ -2,7 +2,8 @@ "variables": { "iso_url": "https://mirrors.kernel.org/archlinux/iso/2015.12.01/archlinux-2015.12.01-dual.iso", "iso_checksum": "7de3315b1384e5d2769eca13453e87a8d303050e", - "iso_checksum_type": "sha1" + "iso_checksum_type": "sha1", + "ssh_timeout": "20m" }, "builders": [ { @@ -24,6 +25,7 @@ "hard_drive_interface": "sata", "ssh_username": "vagrant", "ssh_password": "vagrant", + "ssh_timeout": "{{user `ssh_timeout`}}", "shutdown_command": "sudo systemctl start poweroff.timer" }, { @@ -42,6 +44,7 @@ "disk_size": 20480, "ssh_username": "vagrant", "ssh_password": "vagrant", + "ssh_timeout": "{{user `ssh_timeout`}}", "shutdown_command": "sudo systemctl start poweroff.timer" }, { @@ -64,6 +67,7 @@ "disk_size": 20480, "ssh_username": "vagrant", "ssh_password": "vagrant", + "ssh_timeout": "{{user `ssh_timeout`}}", "shutdown_command": "sudo /parallels_tools.sh && sudo rm /parallels_tools.sh && sudo systemctl start poweroff.timer" } ], diff --git a/wrapacker b/wrapacker index 291bf9d..abbdf51 100755 --- a/wrapacker +++ b/wrapacker @@ -24,6 +24,7 @@ else readonly PACKER_BIN='packer' fi +VALID_TIME_UNITS=(ns us ms s m h) ##### Functions @@ -63,7 +64,7 @@ validate_country() { # print this script's usage message to stderr usage() { cat <<-EOF >&2 - usage: wrapacker [-c COUNTRY] [-p PROVIDER] [-h] + usage: wrapacker [-c COUNTRY] [-p PROVIDER] [-t TIMEOUT] [-h] EOF } @@ -83,6 +84,14 @@ print_valid_providers() { 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 this script's help message to stdout help() { cat <<-EOF @@ -111,6 +120,10 @@ help() { -d, --dry-run do not actually perform the build, just show what would run + -t, --timeout + sets the amount of time packer will wait for trying ssh login; + defaults to 20m + -h, --help view this help message @@ -132,6 +145,7 @@ done country='' provider='' dry_run='' +timeout='' # parse command line options while [[ $1 != '' ]]; do @@ -153,10 +167,18 @@ while [[ $1 != '' ]]; do -d| --dry-run) dry_run=true ;; + -t | --timeout) + timeout=$2 + shift + ;; + --timeout=*) + timeout=${1#*=} + ;; -h | --help | -\?) help print_valid_countries print_valid_providers + print_valid_time_units exit 0 ;; --*) @@ -209,18 +231,31 @@ else MIRROR='https://mirrors.kernel.org/archlinux' fi +if [[ $timeout ]]; then + if [[ "$timeout" =~ ^[0-9]+(ns|us|ms|s|m|h)$ ]]; then + TIMEOUT=$timeout + else + warn 'INVALID TIME UNITS SPECIFIED or MISSING UNIT - %s' "$timeout" + usage + print_valid_time_units + exit 1 + fi +fi + SHA1SUMS=$(curl -s "${MIRROR}/iso/latest/sha1sums.txt") ISO_NAME=$(echo "$SHA1SUMS" | awk '/dual.iso/{ print $2 }') ISO_URL="${MIRROR}/iso/latest/${ISO_NAME}" ISO_CHECKSUM=$(echo "$SHA1SUMS" | awk '/dual.iso/{ print $1 }') +TIMEOUT=${TIMEOUT:-"15m"} if [[ $dry_run ]]; then cat <<-EOF $PACKER_BIN build \\ -only=$PACKER_PROVIDER \\ -var "iso_url=$ISO_URL" \\ -var "iso_checksum=$ISO_CHECKSUM" \\ + -var "ssh_timeout=$TIMEOUT" \\ arch-template.json EOF else @@ -228,6 +263,7 @@ else -only=$PACKER_PROVIDER \ -var "iso_url=$ISO_URL" \ -var "iso_checksum=$ISO_CHECKSUM" \ + -var "ssh_timeout=$TIMEOUT" \ arch-template.json fi