diff --git a/boxes/common/download.sh b/boxes/common/download.sh index 21cda38..9f9dcf0 100755 --- a/boxes/common/download.sh +++ b/boxes/common/download.sh @@ -1,5 +1,32 @@ #!/bin/bash set -e -echo " Will check if the '${CONTAINER}' exists and error if doesnt" -echo " Will create '${CONTAINER}'" +source common/ui.sh + +container_exists=$(lxc-ls | grep -q ${CONTAINER}) +# If container exists, check if want to continue +if $container_exists; then + if ! $(confirm "The '${CONTAINER}' container already exists, do you want to continue building the box?" 'n'); then + log 'Aborting...' + exit 1 + fi +fi + +# If container exists and wants to continue building the box +if $container_exists; then + if $(confirm "Do you want to rebuild the '${CONTAINER}' container?" 'n'); then + log "Destroying container ${CONTAINER}..." + lxc-stop -n ${CONTAINER} &>/dev/null || true + lxc-destroy -n ${CONTAINER} + else + log "Reusing existing container..." + exit 0 + fi +fi + +# If we got to this point, we need to create the container +log "Creating container..." +lxc-create -n ${CONTAINER} -t download -- \ + --dist ${DISTRIBUTION} \ + --release ${RELEASE} \ + --arch ${ARCH} diff --git a/boxes/common/ui.sh b/boxes/common/ui.sh new file mode 100644 index 0000000..2f84194 --- /dev/null +++ b/boxes/common/ui.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +log() { + echo " ${1}" >&2 +} + +debug() { + [ ! $DEBUG ] || echo " [DEBUG] ${1}" >&2 +} + +confirm() { + question=${1} + default=${2} + default_prompt= + + if [ $default = 'n' ]; then + default_prompt="y/N" + default='No' + else + default_prompt="Y/n" + default='Yes' + fi + + echo -n " ${question} [${default_prompt}] " >&2 + read answer + + if [ -z $answer ]; then + debug "Answer not provided, assuming '${default}'" + answer=${default} + fi + + if $(echo ${answer} | grep -q -i '^y'); then + return 0 + else + return 1 + fi +}