Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -ef -o pipefail
  4.  
  5. readonly dir=$(cd "$(dirname "$0")" && pwd)
  6. readonly json="$dir"/tmp.json
  7. dryrun=false
  8. if [[ -f "$1" ]]; then
  9. file=$1
  10. stack_name=$(echo $file | sed s/.py//)
  11. else
  12. echo "Could not find file '$1'"; exit 1
  13. fi
  14.  
  15. function help(){
  16. cat <<- EOF
  17. Usage:
  18. cfn_run.sh [OPTIONS] TEMPLATE_FILE
  19.  
  20. Options:
  21. # -n dry-run
  22. -h help
  23.  
  24. Examples:
  25. run:
  26. run.sh stack.py
  27.  
  28. EOF
  29. exit 0
  30. }
  31.  
  32. function message(){
  33. echo 🍣 "$1"
  34. }
  35.  
  36. function remove_local_json(){
  37. message "removing local temporary file"
  38. rm "$1"
  39. }
  40.  
  41. function confirm_prompt(){
  42. echo
  43. message "execute? (Y/n): "
  44. read answer
  45. case $answer in
  46. y|Y|yes)
  47. echo -e "tyeped yes.\n"
  48. return 0
  49. ;;
  50. n|N|no)
  51. echo -e "tyeped no.\n"
  52. remove_local_json $json
  53. return 1
  54. ;;
  55. *)
  56. echo -e "cannot understand $answer.\n"
  57. confirm_prompt
  58. ;;
  59. esac
  60. }
  61.  
  62. function update_stack(){
  63. message "creating change-set"
  64. readonly change_set=$(
  65. aws cloudformation create-change-set \
  66. --change-set-name "$stack_name"-$(date "+%Y%m%d%H%M%S") \
  67. --stack-name "$stack_name" --template-body file://"$json")
  68. echo "$change_set" | jq
  69. readonly change_set_arn=$(echo $change_set | jq -r '.Id')
  70.  
  71. message "describe change-set"
  72. aws cloudformation describe-change-set --change-set-name "$change_set_arn" | jq
  73.  
  74. confirm_prompt
  75.  
  76. message "execute change-set"
  77.  
  78. remove_local_json $json
  79. }
  80.  
  81. function create_stack(){
  82. message "creation preview"
  83. cat $json | jq
  84. confirm_prompt
  85.  
  86. message "create stack"
  87. aws cloudformation create-stack --stack-name "$stack_name" --template-body file://"$json"
  88.  
  89. remove_local_json $json
  90. }
  91.  
  92. while getopts r:o:u:nh opt; do
  93. case "$opt" in
  94. n) dryrun=true;;
  95. h) help;;
  96. \?) exit 1;;
  97. esac
  98. done
  99. shift $((--OPTIND))
  100.  
  101. message "flake8 linting"
  102. flake8 $file
  103.  
  104. message "converting json"
  105. python $file > "$json"
  106.  
  107. message "checking json syntax"
  108. aws cloudformation validate-template --template-body file://"$json" | jq
  109.  
  110. message "fetch list"
  111. readonly stacks=($(aws cloudformation list-stacks \
  112. --query 'StackSummaries[?StackStatus!=`DELETE_COMPLETE`]' \
  113. | jq -r '.[].StackName'))
  114. echo ${stacks[@]}
  115.  
  116. stack_exists=false
  117. for stack in "${stacks[@]}"; do
  118. [[ "$stack" == "$stack_name" ]] && stack_exists=true
  119. done
  120.  
  121. if "$stack_exists"; then
  122. update_stack
  123. else
  124. create_stack
  125. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement