terragrunt-local 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #! /bin/bash
  2. findup ()
  3. {
  4. DIR='..'
  5. DIRTEST=$( cd $DIR ; pwd )
  6. while [[ "$DIRTEST" != "/" ]]; do
  7. DIR="$DIR/.."
  8. DIRTEST=$( cd $DIR ; pwd )
  9. if [[ -d "$DIR/$1" ]]; then
  10. echo "$DIR/$1"
  11. return 0
  12. fi
  13. done
  14. # Failed to find
  15. return 1
  16. }
  17. if [ ! -f terragrunt.hcl ]; then
  18. echo "ERROR: No terragrunt.hcl in this directory. Aborting."
  19. exit 1
  20. fi
  21. GITSOURCE=$( cat terragrunt.hcl | egrep -v '^ *#' | egrep 'source *= *"git@' )
  22. if [ "$GITSOURCE" == "" ]; then
  23. echo "ERROR: No source from Git detected. Aborting."
  24. exit 2
  25. fi
  26. # Strip off source = and quotes
  27. GITSOURCE=$( echo "$GITSOURCE" | sed -E 's/.*"([^"]+)".*/\1/' )
  28. # Strip off any ?ref= if it's there
  29. GITSOURCE=$( echo "$GITSOURCE" | sed -E 's/\?.*$//' )
  30. # Thanks to terragrunt needing that magic // construct
  31. GITSOURCE_REPO=$( echo "$GITSOURCE" | sed -E 's#//.*$##' )
  32. GITSOURCE_PATHINREPO=$( echo "$GITSOURCE" | sed -E "s#.*//##" )
  33. BARE_REPONAME=$( basename "$GITSOURCE_REPO" .git )
  34. LOCALPATH=$( findup "$BARE_REPONAME" )
  35. if [[ "$LOCALPATH" == "" ]]; then
  36. echo "ERROR: No $BARE_REPONAME in any parent directory"
  37. exit 1
  38. fi
  39. NEWPATH="$LOCALPATH//$GITSOURCE_PATHINREPO"
  40. echo Substituting \'$GITSOURCE\' with \'$NEWPATH\'
  41. # Provider lockfiles are a pain
  42. #if [ ! -f .terraform.lock.hcl ]; then
  43. # echo "No providers lock (.terraform.lock.hcl) found in this directory. Creating..."
  44. # terragrunt providers lock -platform=darwin_amd64 -platform=linux_amd64 -platform=windows_amd64 -platform=linux_arm64 --terragrunt-source $NEWPATH
  45. # RC=$?
  46. # if [[ $RC != 0 ]]; then
  47. # read -p "ERROR: Failed to generate providers file. Apply anyway? [yN] " -n 1 -r
  48. # echo ""
  49. # if [[ ! $REPLY =~ ^[Yy]$ ]]
  50. # then
  51. # echo Exiting...
  52. # exit 4
  53. # fi
  54. # fi
  55. #fi
  56. # Test locally
  57. # the double // is intentional! Terragrunt uses this to determine the root of the modules repository.
  58. terragrunt $* --terragrunt-source $NEWPATH