Skip to content

Commit

Permalink
Migrate NTT to Chalice from flask/ec2 (#205)
Browse files Browse the repository at this point in the history
* Migrate NTT to Chalice from flask/ec2

* Fixing last seen data

* Remove initial data

* Default to green

* Fixing font size on safari
  • Loading branch information
devinmatte authored Jun 6, 2024
1 parent c564321 commit 48362a7
Show file tree
Hide file tree
Showing 62 changed files with 2,837 additions and 4,938 deletions.
22 changes: 19 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
.DS_Store
node_modules
.vscode
dist/
venv/
hosts
inventory
*.pem
last_seen.json
traintracker-dev.nginx.conf
__pycache__/
devops/ec2-secrets.py

requirements.txt
__pycache__/
*.pyc
server/**/__pycache__
requirements.txt
server/cfn/*
server/.chalice/deployments/*

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
.envrc
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18
20
6 changes: 6 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
]
}
86 changes: 86 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/bash

set -e

export AWS_PROFILE=transitmatters
export AWS_REGION=us-east-1
export AWS_DEFAULT_REGION=us-east-1
export AWS_PAGER=""

PRODUCTION=false

# Argument parsing
# pass "-p" flag to deploy to production

while getopts "pc" opt; do
case $opt in
p)
PRODUCTION=true
;;
esac
done


$PRODUCTION && ENV_SUFFIX="" || ENV_SUFFIX="-beta"
$PRODUCTION && CHALICE_STAGE="production" || CHALICE_STAGE="beta"
$PRODUCTION && STACK_NAME="ntt" || STACK_NAME="ntt-beta"

$PRODUCTION && FRONTEND_ZONE="traintracker.transitmatters.org" || FRONTEND_ZONE="labs.transitmatters.org"
$PRODUCTION && FRONTEND_CERT_ARN="$TM_NTT_CERT_ARN" || FRONTEND_CERT_ARN="$TM_LABS_WILDCARD_CERT_ARN"
$PRODUCTION && FRONTEND_DOMAIN_PREFIX="" || FRONTEND_DOMAIN_PREFIX="ntt-beta."

BACKEND_ZONE="labs.transitmatters.org"
BACKEND_CERT_ARN="$TM_LABS_WILDCARD_CERT_ARN"
$PRODUCTION && BACKEND_DOMAIN_PREFIX="traintracker-api." || BACKEND_DOMAIN_PREFIX="ntt-api-beta."

BACKEND_BUCKET=ntt-backend$ENV_SUFFIX
FRONTEND_HOSTNAME=$FRONTEND_DOMAIN_PREFIX$FRONTEND_ZONE # Must match in .chalice/config.json!
BACKEND_HOSTNAME=$BACKEND_DOMAIN_PREFIX$BACKEND_ZONE # Must match in .chalice/config.json!


# Ensure required secrets are set
if [[ -z "$DD_API_KEY" ]]; then
echo "Must provide DD_API_KEY in environment to deploy" 1>&2
exit 1
fi

# Identify the version and commit of the current deploy
GIT_VERSION=`git describe --tags --always`
GIT_SHA=`git rev-parse HEAD`
GIT_ABR_VERSION=`git describe --tags --abbrev=0`
echo "Deploying version $GIT_VERSION | $GIT_SHA"

# Adding some datadog tags to get better data
DD_TAGS="git.commit.sha:$GIT_SHA,git.repository_url:github.com/transitmatters/parking-explorer"

npm run build

echo "Deploying Train Tracker CloudFormation stack to $HOSTNAME..."
echo "View stack log here: https://$AWS_REGION.console.aws.amazon.com/cloudformation/home?region=$AWS_REGION"

pushd server/ > /dev/null
poetry export --without-hashes --output requirements.txt
poetry run chalice package --stage $CHALICE_STAGE --merge-template cloudformation.json cfn/
aws cloudformation package --template-file cfn/sam.json --s3-bucket $BACKEND_BUCKET --output-template-file cfn/packaged.yaml
aws cloudformation deploy --stack-name $STACK_NAME \
--template-file cfn/packaged.yaml \
--capabilities CAPABILITY_NAMED_IAM \
--no-fail-on-empty-changeset \
--parameter-overrides \
TMFrontendHostname=$FRONTEND_HOSTNAME \
TMFrontendZone=$FRONTEND_ZONE \
TMFrontendCertArn=$FRONTEND_CERT_ARN \
TMBackendCertArn=$BACKEND_CERT_ARN \
TMBackendHostname=$BACKEND_HOSTNAME \
TMBackendZone=$BACKEND_ZONE \
MbtaV3ApiKey=$MBTA_V3_API_KEY \
DDApiKey=$DD_API_KEY \
GitVersion=$GIT_VERSION \
DDTags=$DD_TAGS

popd > /dev/null
aws s3 sync dist/ s3://$FRONTEND_HOSTNAME

# Grab the cloudfront ID and invalidate its cache
CLOUDFRONT_ID=$(aws cloudfront list-distributions --query "DistributionList.Items[?Aliases.Items!=null] | [?contains(Aliases.Items, '$FRONTEND_HOSTNAME')].Id | [0]" --output text)
aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_ID --paths "/*"
144 changes: 0 additions & 144 deletions devops/cloudformation.json

This file was deleted.

Loading

0 comments on commit 48362a7

Please sign in to comment.