update_all_from_skeleton 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. if [[ -f UNMANAGED.ACCOUNT ]]; then
  71. >&2 echo Error: This account is marked as unmanaged. Failing...
  72. exit 4
  73. fi
  74. for i in `seq -f "../../../000-skeleton/%g*" 0 9`; do
  75. MODULE=$( basename $i )
  76. [[ $TESTING ]] && >&2 echo debug: Processing module $MODULE...
  77. if [[ -d $MODULE ]]; then
  78. [[ $DEBUG ]] && >&2 echo debug: Module exists. Updating carefully.
  79. pushd . > /dev/null
  80. cd $MODULE
  81. update_from_skeleton $*
  82. popd > /dev/null
  83. else
  84. if [[ $MODULE =~ \* ]]; then
  85. continue
  86. fi
  87. # Special case: 020-attach-transit-gateway-to-standard-vpc
  88. if [[ $MODULE == "020-attach-transit-gateway-to-standard-vpc" && $SHORT_PWD =~ -c2$ ]]; then
  89. echo Skipping $MODULE for C2 Accounts
  90. continue
  91. fi
  92. # Special case: 005-iam in common
  93. if [[ $SHORT_PWD =~ common-services && \
  94. ( $MODULE == "005-iam" || \
  95. $MODULE == "010-standard-vpc" || \
  96. $MODULE == "020-attach-transit-gateway-to-standard-vpc" || \
  97. $MODULE == "025-test-instance" \
  98. ) \
  99. ]]; then
  100. # No standard vpc in common services. It doesn't break things, just doesn't seem appropriate at this time.
  101. echo Skipping $MODULE for Common Services Accounts
  102. continue
  103. fi
  104. read -p "$MODULE doesn't presently exist. Create it? [y/N]? " -n 1 -r
  105. echo ""
  106. if [[ $REPLY =~ ^[Yy]$ ]]
  107. then
  108. [[ -d ../../../000-skeleton/$MODULE ]] && ${TESTING} cp -rv ../../../000-skeleton/$MODULE .
  109. else
  110. echo Skipping...
  111. fi
  112. fi
  113. done
  114. echo Finished. Don\'t forget to update account.hcl.
  115. echo '--------------- 000-skeleton/account.hcl ----------------'
  116. cat ../../../000-skeleton/account.hcl
  117. echo '--------------- finished ----------------'