update_all_from_skeleton 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #! /bin/bash
  2. #
  3. # Run from an account directory to update all the child directory's standard files
  4. function argparse {
  5. PARAMS=""
  6. while (( "$#" )); do
  7. case "$1" in
  8. -h|--help)
  9. echo Usage: $0 '[-t|--test] [-d|--debug]'
  10. exit 0
  11. ;;
  12. -t|--test)
  13. TESTING="/bin/echo TESTING: "
  14. shift
  15. ;;
  16. -d|--debug)
  17. >&2 echo debug: Enabling debugging..
  18. DEBUG=1
  19. shift
  20. ;;
  21. # -p|--only-path)
  22. # if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
  23. # ONLY_PATH=$2
  24. # shift 2
  25. # else
  26. # echo "Error: Argument for $1 is missing" >&2
  27. # exit 1
  28. # fi
  29. # ;;
  30. -*|--*=) # unsupported flags
  31. echo "Error: Unsupported flag $1" >&2
  32. exit 1
  33. ;;
  34. *) # preserve positional arguments
  35. PARAMS="$PARAMS $1"
  36. shift
  37. ;;
  38. esac
  39. done
  40. # set positional arguments in their proper place
  41. eval set -- "$PARAMS"
  42. }
  43. # Main
  44. argparse $*
  45. SHORT_PWD=$( basename ${PWD} )
  46. PARENT_PWD=$( basename $( cd .. && pwd ) )
  47. [[ $DEBUG == 1 ]] && >&2 echo debug: PWD=$PWD
  48. [[ $DEBUG == 1 ]] && >&2 echo debug: SHORT_PWD=$SHORT_PWD
  49. # Sanity Checking
  50. if [[ $SHORT_PWD == "000-skeleton" ]]; then
  51. >&2 echo Error: Cannot run from skeleton directory. Exiting...
  52. exit 1
  53. fi
  54. if [[ $SHORT_PWD =~ ^[0-9]{3}-.* ]]; then
  55. >&2 echo Error: We appear to be in a module directory. Please run from the account directory you wish to update. Exiting...
  56. exit 2
  57. fi
  58. if [[ ! $PARENT_PWD =~ ^aws ]]; then
  59. >&2 echo Error: We do not appear to be in an account directory. Failing...
  60. exit 3
  61. fi
  62. if [[ $SHORT_PWD =~ legacy ]]; then
  63. >&2 echo Error: This account is appears to be a legacy account. This script will not work.
  64. exit 4
  65. fi
  66. if [[ -f UNUSED.ACCOUNT ]]; then
  67. >&2 echo Error: This account is marked as unused. Failing...
  68. exit 4
  69. fi
  70. for i in `seq -f "../../../000-skeleton/%g*" 0 9`; do
  71. MODULE=$( basename $i )
  72. [[ $TESTING ]] && >&2 echo debug: Processing module $MODULE...
  73. if [[ -d $MODULE ]]; then
  74. [[ $DEBUG ]] && >&2 echo debug: Module exists. Updating carefully.
  75. pushd . > /dev/null
  76. cd $MODULE
  77. update_from_skeleton $*
  78. popd > /dev/null
  79. else
  80. if [[ $MODULE =~ \* ]]; then
  81. continue
  82. fi
  83. # Special case: 020-attach-transit-gateway-to-standard-vpc
  84. if [[ $MODULE == "020-attach-transit-gateway-to-standard-vpc" && $SHORT_PWD =~ -c2$ ]]; then
  85. echo Skipping $MODULE for C2 Accounts
  86. continue
  87. fi
  88. # Special case: 005-iam in common
  89. if [[ $SHORT_PWD =~ common-services && \
  90. ( $MODULE == "005-iam" || \
  91. $MODULE == "010-standard-vpc" || \
  92. $MODULE == "020-attach-transit-gateway-to-standard-vpc" || \
  93. $MODULE == "025-test-instance" \
  94. ) \
  95. ]]; then
  96. # No standard vpc in common services. It doesn't break things, just doesn't seem appropriate at this time.
  97. echo Skipping $MODULE for Common Services Accounts
  98. continue
  99. fi
  100. read -p "$MODULE doesn't presently exist. Create it? [y/N]? " -n 1 -r
  101. echo ""
  102. if [[ $REPLY =~ ^[Yy]$ ]]
  103. then
  104. [[ -d ../../../000-skeleton/$MODULE ]] && ${TESTING} cp -rv ../../../000-skeleton/$MODULE .
  105. else
  106. echo Skipping...
  107. fi
  108. fi
  109. done
  110. echo Finished. Don\'t forget to update account.hcl.
  111. echo '--------------- 000-skeleton/account.hcl ----------------'
  112. cat ../../../000-skeleton/account.hcl
  113. echo '--------------- finished ----------------'