Advertisement
Guest User

Untitled

a guest
Nov 15th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. ## Default Values for config options
  4. declare -A config
  5.  
  6. options_found=0
  7. boxes_url="boxes.apixio.com"
  8.  
  9. config=(
  10. ["name"]="${name:-apx}"
  11. ["version"]="${version:-c7}"
  12. ["release"]="${release:-$(date +"%Y%m%d-%H%M")}"
  13. ["memory"]="${memory:-512}"
  14. ["size"]="${size:-61440}"
  15. ["packer_user"]="${packer_user:-root}"
  16. ["packer_password"]="${packer_password:-vagrant}"
  17. )
  18.  
  19.  
  20. executor() {
  21. if [[ "$1" == "build" ]]; then
  22. echo "Starting build with options:"
  23.  
  24. for option in "${!config[@]}"
  25. do
  26. echo "$option = ${config[$option]}"
  27. done
  28.  
  29. for option in "${!config[@]}"
  30. do
  31. args+=(-var "$option=${config[$option]}");
  32. done
  33. packer build "${args[@]}" "${config[name]}-${config[version]}.json"
  34. if (( "$upload" == 1 )); then
  35. ping -q -c2 "$boxes_url" &> /dev/null
  36. if (( "$?" == 0 )); then
  37. echo "Beginning upload to: $boxes_url"
  38. else
  39. echo "Host: $boxes_url is not available, skipping upload."
  40. fi
  41. fi
  42.  
  43. fi
  44.  
  45. if [[ "$1" == "show_help" ]]; then
  46. show_help
  47. fi
  48.  
  49. }
  50.  
  51. show_help() {
  52. scriptname="${0##*/}"
  53. cat <<- EOF
  54. | $scriptname
  55. |-------------------------------------------------------------------
  56. | Options: Usage
  57. | -a, --aws Execute the aws builder
  58. | -b, --box Build box <boxname>
  59. | -h, -?, --help Display this help dialogue
  60. | -u, --upload Upload the box to the box host
  61. | -v, --vbox Execute the virtualbox builder
  62.  
  63. EOF
  64. }
  65.  
  66. main() {
  67. while getopts ":abhuvV:" opt;
  68. do
  69. options_found=1
  70. case "$opt" in
  71. a)
  72. break
  73. ;;
  74. b)
  75. executor build
  76. ;;
  77. h)
  78. executor show_help
  79. ;;
  80. u)
  81. upload=1
  82. ;;
  83. v)
  84. virtual_box="true"
  85. ;;
  86. V)
  87. key="${OPTARG##=*}" val="${OPTARG#*=}"
  88. key="${OPTARG%%=*}" val="${OPTARG#*=}"
  89.  
  90. if [[ "${config[$key]+isset}" ]]; then
  91. echo "Valid variable found: $key"
  92. echo "Setting $key to $val"
  93. declare "$key"="$val"
  94. echo "${key} has been set to ${config[$key]}"
  95. else
  96. echo -e "Invalid value specified: $key\n"
  97. echo "Value values are:"
  98. for value in "${!config[@]}"
  99. do
  100. echo "$value"
  101. done
  102. fi
  103. ;;
  104.  
  105. \?)
  106. echo "Invalid option: -$OPTARG" >&2
  107. executor show_help
  108. ;;
  109. :)
  110. echo "Option -$OPTARG requires an argument." >&2
  111. break
  112. esac
  113. done
  114.  
  115. if ((!options_found)); then
  116. echo "No valid options found"
  117. fi
  118. }
  119.  
  120. main "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement