Skip to content

Instantly share code, notes, and snippets.

@veggiemonk
Created October 25, 2018 04:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save veggiemonk/9af975eedd569ee74dffd1bb5560c7e9 to your computer and use it in GitHub Desktop.
Save veggiemonk/9af975eedd569ee74dffd1bb5560c7e9 to your computer and use it in GitHub Desktop.
new-env.sh
#!/bin/bash
set -e
print_usage(){
echo "USAGE:"
echo "./new-env.sh NEW_ENV_NAME REGION [stage|prod|example]"
echo "example: ./new-env.sh dev ap-southeast-2 stage"
echo "REGION is the same for the source and destination environment"
echo "[stage|prod|example] are the source environment that will be copied. "
}
destination=${1:-"1"}
region=${2:-"ap-southeast-2"}
environment=${3:-"stage"}
if [ "$destination" = "1" ]; then
print_usage
exit 1
fi
# useful one-liner which will give you the full directory name of the script no matter where it is being called from
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
main_dir="$( cd "$script_dir/../main" >/dev/null && pwd )"
echo "Main: $main_dir"
src_folder="$main_dir/$region/$environment"
echo "Source: $src_folder"
if [ ! -d "$src_folder" ]; then
echo "error: folder $src_folder does not exist"
print_usage
exit 1
fi
dest_folder="$main_dir/$region/$destination"
echo "Destination: $dest_folder"
if [ -d "$dest_folder" ]; then
echo "error: folder already exist - remove it first"
echo " $ rm -rf $dest_folder"
exit 1
fi
echo "Copying from environment $src_folder"
mkdir -p "$dest_folder"
cd "$src_folder"
tar cf - --exclude=.terragrunt-cache . | (cd "$dest_folder" && tar xvf - )
echo "Environment sucessfully created at $dest_folder"
echo "Configuring..."
sleep 1
find "$dest_folder" -type f -name "terraform.tfvars" -exec sed -i '' "s/$environment/$destination/g" {} \;
sed -i '' "s/$environment/$destination/g" "$dest_folder"/env.tfvars
echo
echo "Configuration done."
echo
echo "To deploy the new environment run:"
echo
echo "$ terragrunt plan-all --terragrunt-non-interactive --terragrunt-working-dir $dest_folder"
echo "$ terragrunt apply-all --terragrunt-non-interactive --terragrunt-working-dir $dest_folder"
echo
echo "The files for the new environment have been created."
echo
# echo " run \"terragrunt apply-all --terragrunt-non-interactive\" "
# read -p "Would you like to deploy the new environment? [y/N] " -n 1 -r;
# echo
# if [[ $REPLY =~ ^[Yy]$ ]]
# then
# echo "Applying changes..."
# else
# echo "No new environment is deployed"
# fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment