Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
Mapwithai cloudformation (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zack LaVergne authored Dec 17, 2021
1 parent 1d349be commit 28f262b
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 283 deletions.
102 changes: 52 additions & 50 deletions .circleci/config.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ jobs:
command: |
sudo apt-get update
sudo apt-get install -y libgeos-dev # Required for shapely
sudo yarn global add @mapbox/cfn-config @mapbox/cloudfriend
# sudo yarn global add @mapbox/cfn-config @mapbox/cloudfriend
sudo npm install -g @mapbox/cfn-config @mapbox/cloudfriend
sudo pip3 install awscli --upgrade
- run:
name: Set folder permissions
Expand Down Expand Up @@ -79,7 +80,7 @@ jobs:
gitsha:
description: "The 40 char hash of the git commit"
type: string
working_directory: /home/circleci/tasking-manager
working_directory: /home/circleci/mwai-tasking-manager
docker:
- image: circleci/python:3.7-buster-node
steps:
Expand All @@ -90,36 +91,37 @@ jobs:
command: |
sudo apt-get update
sudo apt-get install -y libgeos-dev jq # Required for shapely
sudo yarn global add @mapbox/cfn-config @mapbox/cloudfriend
# sudo yarn global add @mapbox/cfn-config @mapbox/cloudfriend
sudo npm install -g @mapbox/cfn-config @mapbox/cloudfriend
sudo pip3 install awscli --upgrade
- run:
name: Configure AWS Access Key ID
command: |
aws configure set aws_access_key_id \
$AWS_ACCESS_KEY_ID \
--profile default
- run:
name: Configure AWS Secret Access Key
command: |
aws configure set aws_secret_access_key \
$AWS_SECRET_ACCESS_KEY \
--profile default
- run:
name: Configure AWS default region
command: |
aws configure set region $AWS_REGION \
--profile default
# - run:
# name: Configure AWS Access Key ID
# command: |
# aws configure set aws_access_key_id \
# $AWS_ACCESS_KEY_ID \
# --profile default
# - run:
# name: Configure AWS Secret Access Key
# command: |
# aws configure set aws_secret_access_key \
# $AWS_SECRET_ACCESS_KEY \
# --profile default
# - run:
# name: Configure AWS default region
# command: |
# aws configure set region $AWS_REGION \
# --profile default
- run:
name: Get RDS Instance ID
command: |
chmod +x .circleci/rdsid.sh
RDS_ID=$(./.circleci/rdsid.sh '{"aws:cloudformation:stack-name": "tasking-manager-<< parameters.stack_name >>"}')
RDS_ID=$(./.circleci/rdsid.sh '{"aws:cloudformation:stack-name": "mwai-tasking-manager-<< parameters.stack_name >>"}')
echo "export RDS_ID=$RDS_ID" >> $BASH_ENV
- run:
name: Remove last snapshot and backup database
no_output_timeout: 15m
command: |
DESCRIBE_SNAPSHOT=$(aws rds describe-db-snapshots --db-snapshot-identifier tm3-<< parameters.stack_name >>-$RDS_ID-latest --db-instance-identifier $RDS_ID --output text)
DESCRIBE_SNAPSHOT=$(aws rds describe-db-snapshots --db-snapshot-identifier mwai-tasking-manager-<< parameters.stack_name >>-$RDS_ID-latest --db-instance-identifier $RDS_ID --output text)
NONCE=$(openssl rand -hex 4)
echo "export NONCE=$NONCE" >> $BASH_ENV
# Copy old snapshot to temporary
Expand All @@ -128,17 +130,17 @@ jobs:
echo "Snapshot does not exist, creating one now."
else
aws rds copy-db-snapshot \
--source-db-snapshot tm3-<< parameters.stack_name >>-${RDS_ID}-latest \
--target-db-snapshot tm3-<< parameters.stack_name >>-${RDS_ID}-${NONCE}
--source-db-snapshot mwai-tasking-manager-<< parameters.stack_name >>-${RDS_ID}-latest \
--target-db-snapshot mwai-tasking-manager-<< parameters.stack_name >>-${RDS_ID}-${NONCE}
aws rds delete-db-snapshot \
--db-snapshot-identifier tm3-<< parameters.stack_name >>-${RDS_ID}-latest
--db-snapshot-identifier mwai-tasking-manager-<< parameters.stack_name >>-${RDS_ID}-latest
fi
# create new aws rds snapshot
aws rds create-db-snapshot \
--db-snapshot-identifier tm3-<< parameters.stack_name >>-${RDS_ID}-latest \
--db-snapshot-identifier mwai-tasking-manager-<< parameters.stack_name >>-${RDS_ID}-latest \
--db-instance-identifier ${RDS_ID}
aws rds wait db-snapshot-completed \
--db-snapshot-identifier tm3-<< parameters.stack_name >>-${RDS_ID}-latest \
--db-snapshot-identifier mwai-tasking-manager-<< parameters.stack_name >>-${RDS_ID}-latest \
--db-instance-identifier ${RDS_ID}
if [[ $? -eq 255 ]]; then
echo "Production snapshot creation failed. Exiting with exit-code 125"
Expand All @@ -148,14 +150,14 @@ jobs:
name: Download config file
command: |
export GITSHA=<< parameters.gitsha >>
aws s3 cp s3://hot-cfn-config/tasking-manager/tasking-manager-<< parameters.stack_name >>-${AWS_REGION}.cfn.json - | jq -c --arg GITSHA "$GITSHA" '.GitSha = $GITSHA' > $CIRCLE_WORKING_DIRECTORY/cfn-config-<< parameters.stack_name >>.json
aws s3 cp s3://mapwithai-cfn-config/mwai-tasking-manager/mwai-tasking-manager-<< parameters.stack_name >>-${AWS_REGION}.cfn.json - | jq -c --arg GITSHA "$GITSHA" '.GitSha = $GITSHA' > $CIRCLE_WORKING_DIRECTORY/cfn-config-<< parameters.stack_name >>.json
- deploy:
name: Deploy to << parameters.stack_name >>
command: |
export NODE_PATH=/usr/local/share/.config/yarn/global/node_modules/
validate-template $CIRCLE_WORKING_DIRECTORY/scripts/aws/cloudformation/tasking-manager.template.js
validate-template $CIRCLE_WORKING_DIRECTORY/scripts/aws/cloudformation/tasking-manager.template.js -r us-west-1
export JSON_CONFIG="$(cat $CIRCLE_WORKING_DIRECTORY/cfn-config-<< parameters.stack_name >>.json)"
cfn-config update << parameters.stack_name >> $CIRCLE_WORKING_DIRECTORY/scripts/aws/cloudformation/tasking-manager.template.js -f -c hot-cfn-config -t hot-cfn-config -r $AWS_REGION -p "$JSON_CONFIG"
cfn-config update << parameters.stack_name >> $CIRCLE_WORKING_DIRECTORY/scripts/aws/cloudformation/tasking-manager.template.js -f -c mapwithai-cfn-config -t mapwithai-cfn-config -r $AWS_REGION -p "$JSON_CONFIG" -n mwai-tasking-manager
- run:
name: Cleanup
when: always
Expand All @@ -169,7 +171,7 @@ jobs:
aws rds delete-db-snapshot --db-snapshot-identifier tm3-<< parameters.stack_name >>-${RDS_ID}-${NONCE}
fi
# Delete manual snapshot if database ID changed
RDS_ID_NEW=$(./.circleci/rdsid.sh '{"aws:cloudformation:stack-name": "tasking-manager-<< parameters.stack_name >>"}')
RDS_ID_NEW=$(./.circleci/rdsid.sh '{"aws:cloudformation:stack-name": "mwai-tasking-manager-<< parameters.stack_name >>"}')
if [ "$RDS_ID" != "$RDS_ID_NEW" ]
then
aws rds delete-db-snapshot --db-snapshot-identifier tm3-<< parameters.stack_name >>-${RDS_ID}-latest
Expand All @@ -189,32 +191,32 @@ jobs:
name: Install modules
command: |
sudo pip3 install awscli --upgrade
- run:
name: Configure AWS Access Key ID
command: |
aws configure set aws_access_key_id \
$AWS_ACCESS_KEY_ID \
--profile default
- run:
name: Configure AWS Secret Access Key
command: |
aws configure set aws_secret_access_key \
$AWS_SECRET_ACCESS_KEY \
--profile default
- run:
name: Configure AWS default region
command: |
aws configure set region $AWS_REGION \
--profile default
# - run:
# name: Configure AWS Access Key ID
# command: |
# aws configure set aws_access_key_id \
# $AWS_ACCESS_KEY_ID \
# --profile default
# - run:
# name: Configure AWS Secret Access Key
# command: |
# aws configure set aws_secret_access_key \
# $AWS_SECRET_ACCESS_KEY \
# --profile default
# - run:
# name: Configure AWS default region
# command: |
# aws configure set region $AWS_REGION \
# --profile default
- run:
name: Deploy Frontend to S3
command: |
cd ${CIRCLE_WORKING_DIRECTORY}/frontend/
export TM_ENVIRONMENT=<< parameters.stack_name >>
yarn
CI=true GENERATE_SOURCEMAP=false yarn build
aws s3 sync build/ s3://tasking-manager-<< parameters.stack_name >>-react-app --delete
export DISTRIBUTION_ID=`aws cloudformation list-exports --output=text --query "Exports[?Name=='tasking-manager-<< parameters.stack_name >>-cloudfront-id-${AWS_REGION}'].Value"`
aws s3 sync build/ s3://mwai-tasking-manager-<< parameters.stack_name >>-react-app --delete
export DISTRIBUTION_ID=`aws cloudformation list-exports --output=text --query "Exports[?Name=='mwai-tasking-manager-<< parameters.stack_name >>-cloudfront-id-${AWS_REGION}'].Value"`
aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths "/*"
workflows:
version: 2
Expand Down
6 changes: 3 additions & 3 deletions .circleci/rdsid.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/bash
ARNS=$(aws rds describe-db-instances --query "DBInstances[].DBInstanceArn" --output text --region us-east-1)
ARNS=$(aws rds describe-db-instances --query "DBInstances[].DBInstanceArn" --output text --region us-west-1)
for line in $ARNS; do
TAGS=$(aws rds list-tags-for-resource --resource-name "$line" --query "TagList[]" --region us-east-1)
TAGS=$(aws rds list-tags-for-resource --resource-name "$line" --query "TagList[]" --region us-west-1)
MATCHES=$(echo $TAGS | python -c "import sys, json; tags = json.loads('$1'); remote = {t['Key']: t['Value'] for t in json.load(sys.stdin)}; print('$line'.split(':')[-1]) if len({'$line' for k, v in tags.items() if k in remote and v == remote[k]}) > 0 else ''")
if [[ ! -z $MATCHES ]]; then
echo $MATCHES
fi
done
done
4 changes: 3 additions & 1 deletion backend/services/messaging/smtp_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def _send_message(
msg.attach(part1)

current_app.logger.debug(f"Sending email via SMTP {to_address}")
if current_app.config["LOG_LEVEL"] == "DEBUG":
if current_app.config["SMTP_SETTINGS"]["host"] is None:
current_app.logger.debug(msg.as_string())
else:
sender = SMTPService._init_smtp_client()
Expand All @@ -132,6 +132,8 @@ def _init_smtp_client():
""" Initialise SMTP client from app settings """
smtp_settings = current_app.config["SMTP_SETTINGS"]
sender = smtplib.SMTP(smtp_settings["host"], port=smtp_settings["smtp_port"])
if current_app.config["LOG_LEVEL"] == "DEBUG":
sender.set_debuglevel(1)
sender.starttls()
if smtp_settings["smtp_user"] and smtp_settings["smtp_password"]:
sender.login(smtp_settings["smtp_user"], smtp_settings["smtp_password"])
Expand Down
88 changes: 88 additions & 0 deletions frontend/src/assets/img/main-logo-hot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 28f262b

Please sign in to comment.