Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- ## Default Values for config options
- declare -A config
- options_found=0
- boxes_url="boxes.apixio.com"
- config=(
- ["name"]="${name:-apx}"
- ["version"]="${version:-c7}"
- ["release"]="${release:-$(date +"%Y%m%d-%H%M")}"
- ["memory"]="${memory:-512}"
- ["size"]="${size:-61440}"
- ["packer_user"]="${packer_user:-root}"
- ["packer_password"]="${packer_password:-vagrant}"
- )
- executor() {
- if [[ "$1" == "build" ]]; then
- echo "Starting build with options:"
- for option in "${!config[@]}"
- do
- echo "$option = ${config[$option]}"
- done
- for option in "${!config[@]}"
- do
- args+=(-var "$option=${config[$option]}");
- done
- packer build "${args[@]}" "${config[name]}-${config[version]}.json"
- if (( "$upload" == 1 )); then
- ping -q -c2 "$boxes_url" &> /dev/null
- if (( "$?" == 0 )); then
- echo "Beginning upload to: $boxes_url"
- else
- echo "Host: $boxes_url is not available, skipping upload."
- fi
- fi
- fi
- if [[ "$1" == "show_help" ]]; then
- show_help
- fi
- }
- show_help() {
- scriptname="${0##*/}"
- cat <<- EOF
- | $scriptname
- |-------------------------------------------------------------------
- | Options: Usage
- | -a, --aws Execute the aws builder
- | -b, --box Build box <boxname>
- | -h, -?, --help Display this help dialogue
- | -u, --upload Upload the box to the box host
- | -v, --vbox Execute the virtualbox builder
- EOF
- }
- main() {
- while getopts ":abhuvV:" opt;
- do
- options_found=1
- case "$opt" in
- a)
- break
- ;;
- b)
- executor build
- ;;
- h)
- executor show_help
- ;;
- u)
- upload=1
- ;;
- v)
- virtual_box="true"
- ;;
- V)
- key="${OPTARG##=*}" val="${OPTARG#*=}"
- key="${OPTARG%%=*}" val="${OPTARG#*=}"
- if [[ "${config[$key]+isset}" ]]; then
- echo "Valid variable found: $key"
- echo "Setting $key to $val"
- declare "$key"="$val"
- echo "${key} has been set to ${config[$key]}"
- else
- echo -e "Invalid value specified: $key\n"
- echo "Value values are:"
- for value in "${!config[@]}"
- do
- echo "$value"
- done
- fi
- ;;
- \?)
- echo "Invalid option: -$OPTARG" >&2
- executor show_help
- ;;
- :)
- echo "Option -$OPTARG requires an argument." >&2
- break
- esac
- done
- if ((!options_found)); then
- echo "No valid options found"
- fi
- }
- main "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement