diff --git a/.env.local b/.env.local new file mode 100644 index 00000000..381a4e4f --- /dev/null +++ b/.env.local @@ -0,0 +1,7 @@ +ENVIRON=local +# Database credentials for the local development DB +TILEGARDEN_DB_NAME=postgres +TILEGARDEN_DB_HOST=database.internal.tilegarden +TILEGARDEN_DB_PORT=5432 +TILEGARDEN_DB_USER=postgres +TILEGARDEN_DB_PASSWORD=postgres diff --git a/.env.template b/.env.template index cc0f2e18..d1a99c44 100644 --- a/.env.template +++ b/.env.template @@ -1,17 +1,20 @@ +# Name of this deployment environment (e.g. 'dev', 'staging', 'production') +ENVIRON=dev + # Import values from local environment # If you want to set them specifically, change them to assignments USER AWS_PROFILE -# $USER will be appended to the project name specified here +# $USER and environment will be appended to the project name specified here PROJECT_NAME= # Database credentials # These get set on the Lambda function and templated into your map-config.xml file at runtime. -#TILEGARDEN_DB_NAME= -#TILEGARDEN_DB_HOST= -#TILEGARDEN_DB_PORT= -#TILEGARDEN_DB_USER= -#TILEGARDEN_DB_PASSWORD= +TILEGARDEN_DB_NAME= +TILEGARDEN_DB_HOST= +TILEGARDEN_DB_PORT= +TILEGARDEN_DB_USER= +TILEGARDEN_DB_PASSWORD= # Function config information ## REQUIRED ## diff --git a/.gitignore b/.gitignore index 22c3120c..446340af 100644 --- a/.gitignore +++ b/.gitignore @@ -55,7 +55,8 @@ typings/ .yarn-integrity # dotenv environment variables file -.env +.env* +!.env.local # next.js build output .next diff --git a/docker-compose.yml b/docker-compose.yml index 0a7f7e58..860b4c7b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,7 +18,23 @@ services: ports: - "3000:3000" - "9229:9229" - env_file: .env + environment: + # Variables to pass through from environment-specific .env files + - ENVIRON + - USER + - AWS_PROFILE + - PROJECT_NAME + - TILEGARDEN_DB_NAME + - TILEGARDEN_DB_HOST + - TILEGARDEN_DB_PORT + - TILEGARDEN_DB_USER + - TILEGARDEN_DB_PASSWORD + - LAMBDA_REGION + - LAMBDA_ROLE + - LAMBDA_TIMEOUT + - LAMBDA_MEMORY + - LAMBDA_SUBNETS + - LAMBDA_SECURITY_GROUPS links: - database:database.internal.tilegarden volumes: @@ -29,10 +45,12 @@ services: terraform: image: hashicorp/terraform:0.11.14 - env_file: .env environment: - - TF_VAR_region=${LAMBDA_REGION} - - TF_VAR_source_name=${PROJECT_NAME}-${USER} + - AWS_PROFILE + # Providing placeholder defaults here stops docker-compose from warning about missing + # variables every time you run it. + - TF_VAR_region=${LAMBDA_REGION:-us-east-1} + - TF_VAR_source_name=${PROJECT_NAME:-tilegarden}-${USER}-${ENVIRON:-unknown} working_dir: /home/terraform volumes: - ./src/terraform:/home/terraform diff --git a/scripts/deploy b/scripts/deploy index a6f3023c..d29dc4e2 100755 --- a/scripts/deploy +++ b/scripts/deploy @@ -4,6 +4,7 @@ set -e DEPLOY_NEW="" NO_CACHE="" +ENVIRON="dev" function usage() { echo -n "Usage: $(basename "${0}") [OPTION] @@ -17,17 +18,19 @@ Options: } function main() { - if [[ $DEPLOY_NEW != "--new" && ! -f ./src/tiler/claudia.json ]]; then - echo "No existing deployment found to update! (did you mean to use '--new'?)" + if [[ $DEPLOY_NEW != "--new" && ! -f "./src/tiler/claudia/claudia-${ENVIRON}.json" ]]; then + echo "No existing '$ENVIRON' deployment found to update! (did you mean to use '--new'?)" exit 1 fi docker-compose build --pull terraform + echo "Building tiler for environment '$ENVIRON'" docker-compose -f docker-compose.yml \ + --env-file ".env.${ENVIRON}" \ build --pull ${NO_CACHE:+$NO_CACHE} tiler - ./scripts/publish ${DEPLOY_NEW:+$DEPLOY_NEW} + ./scripts/publish ${DEPLOY_NEW:+$DEPLOY_NEW} "${ENVIRON}" } @@ -45,12 +48,14 @@ then NO_CACHE='--no-cache' shift ;; - -h| --help| *) + -h| --help) usage ;; + *) + ENVIRON=$1 + shift esac done - main + main $ENVIRON fi - diff --git a/scripts/destroy b/scripts/destroy index 0cc2e60d..3f8c80e2 100755 --- a/scripts/destroy +++ b/scripts/destroy @@ -3,32 +3,36 @@ set -e function usage() { - echo -n "Usage: $(basename "${0}") -Removes all published resources from AWS + echo -n "Usage: $(basename "${0}") ENVIRON +Removes all published resources from AWS for the specified deployment. Options: --help Display this help text " } function main() { - echo "WARNING: you are about to destroy all your deployed resources!" + ENVIRON=${1} + echo "WARNING: you are about to destroy all your deployed resources for '${ENVIRON}'!" printf "That includes:\n - Associated AWS Lambda functions\n - Associated API Gateways\n - Associated IAM roles (if automatically generated)\n - Associated CloudFront distributions\n" read -p "If this is what you want, type 'yes' to continue: " -r echo if [[ $REPLY =~ ^[yY][eE][sS]$ ]] then - # Destroy deployed resources # If any part of this fails, the parts that didn't fail # remain destroyed, so it's best to just carry on and # follow through. - docker-compose run --no-deps tiler yarn destroy || true - docker-compose run -e TF_VAR_source_id=$( .api-id", + "parse-id": "jq -r '.api.id' claudia/claudia-${ENVIRON}.json > .api-id-${ENVIRON}", "test": "eslint src && jest --coverage" }, "devDependencies": { diff --git a/src/tiler/scripts/deploy b/src/tiler/scripts/deploy index f8c45638..6f9681ab 100755 --- a/src/tiler/scripts/deploy +++ b/src/tiler/scripts/deploy @@ -1,6 +1,13 @@ #!/bin/bash -yarn claudia update --no-optional-dependencies \ +if [ -z "${ENVIRON}" ]; then + echo 'Required $ENVIRON variable not set.' + exit 1 +fi + +yarn claudia update \ + --config claudia/claudia-${ENVIRON}.json \ + --no-optional-dependencies \ ${LAMBDA_TIMEOUT:+--timeout ${LAMBDA_TIMEOUT}} \ ${LAMBDA_MEMORY:+--memory ${LAMBDA_MEMORY}} \ ${LAMBDA_SECURITY_GROUPS:+--security-group-ids ${LAMBDA_SECURITY_GROUPS}} \ diff --git a/src/tiler/scripts/deploy-new b/src/tiler/scripts/deploy-new index 334451ce..a4cf15f1 100755 --- a/src/tiler/scripts/deploy-new +++ b/src/tiler/scripts/deploy-new @@ -1,11 +1,21 @@ #!/bin/bash -yarn claudia create --no-optional-dependencies --api-module src/api \ - --name ${PROJECT_NAME} --region ${LAMBDA_REGION} \ +if [ -z "${ENVIRON}" ]; then + echo 'Required $ENVIRON variable not set.' + exit 1 +elif [ -z "${PROJECT_NAME}" ]; then + echo 'Required $PROJECT_NAME variable not set.' + exit 1 +fi + +yarn claudia create \ + --config claudia/claudia-${ENVIRON}.json \ + --no-optional-dependencies --api-module src/api \ + --name "${PROJECT_NAME}-${USER}-${ENVIRON}" --region ${LAMBDA_REGION} \ ${LAMBDA_ROLE:+--role ${LAMBDA_ROLE}} \ ${LAMBDA_TIMEOUT:+--timeout ${LAMBDA_TIMEOUT}} \ ${LAMBDA_MEMORY:+--memory ${LAMBDA_MEMORY}} \ ${LAMBDA_SECURITY_GROUPS:+--security-group-ids ${LAMBDA_SECURITY_GROUPS}} \ ${LAMBDA_SUBNETS:+--subnet-ids ${LAMBDA_SUBNETS}} \ --set-env TILEGARDEN_DB_HOST=${TILEGARDEN_DB_HOST},TILEGARDEN_DB_NAME=${TILEGARDEN_DB_NAME},TILEGARDEN_DB_PASSWORD=${TILEGARDEN_DB_PASSWORD},TILEGARDEN_DB_PORT=${TILEGARDEN_DB_PORT},TILEGARDEN_DB_USER=${TILEGARDEN_DB_USER} \ -&& yarn parse-id +&& jq -r '.api.id' claudia/claudia-${ENVIRON}.json > .api-id-${ENVIRON}