Skip to content

Commit

Permalink
Change gcp_deploy script in preparation for production
Browse files Browse the repository at this point in the history
  • Loading branch information
Flip Sasser committed Jun 5, 2020
1 parent 8340d1e commit 074ec93
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ RUN bundle config --global frozen 1 \
&& bundle config set without "${bundle_without}" \
&& bundle config build.google-protobuf --with-cflags=-D__va_copy=va_copy \
&& bundle config build.nokogiri --use-system-libraries \
&& bundle install --jobs=20 \
&& bundle install --jobs=$(getconf _NPROCESSORS_ONLN) \
&& rm -rf /usr/local/bundle/cache/*.gem \
&& find /usr/local/bundle/gems/ -name "*.c" -delete \
&& find /usr/local/bundle/gems/ -name "*.o" -delete
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ GEM
hashdiff (>= 0.4.0, < 2.0.0)
websocket-driver (0.7.2)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.4)
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
yard (0.9.25)
Expand Down
14 changes: 9 additions & 5 deletions bin/docker_entrypoint
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#!/bin/sh

until nc -z -v -w30 $DATABASE_HOST 5432
do
sleep 1
done
bundle check || bundle install --jobs=$(getconf _NPROCESSORS_ONLN)

sleep 2 # Well give it a second, jeez
if [ -n "${DATABASE_URL}" ]; then
database_host=$(echo ${DATABASE_URL} | cut -d "/" -f3 | cut -d "@" -f2 | cut -d ":" -f1)
database_port=$(echo ${DATABASE_URL} | cut -d "/" -f3 | cut -d "@" -f2 | cut -d ":" -f2)
until nc -z -v -w30 ${database_host} ${database_port}; do
echo "Waiting for ${database_host}:${database_port}..."
sleep 1
done
fi

echo "Starting with '$@'"
exec $@
44 changes: 22 additions & 22 deletions bin/gcp_setup
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
#!/usr/bin/env bash
if [ $# -lt 1 ]
then
if [ $# -lt 1 ]; then
echo "Usage: gcp_setup ENVIRONMENT [REGION] (where ENVIRONMENT is either 'production' or 'staging')"
exit
fi

env=$1
env_name="staging" && [[ "${env}" == "production" ]] && env_name="live"
project="staging-coyote"
branch_name="master" && [[ "${env}" == "production" ]] && branch_name="production"
project="${env_name}-coyote"

region=$2 && [[ -z "$2" ] && "us-east1"
region=$2 && [[ -z "$2" ]] && region="us-east1"
alias gcloud="gcloud --project=${project}"
alias gsutil="gsutil -p ${project}"

# Step 1: enable the APIs
# gcloud services enable cloudbuild.googleapis.com secretmanager.googleapis.com run.googleapis.com

# Step 2: configure the SQL instance
db_instance="coyote-${region}"
existing_instance=`gcloud sql instances list | grep ${db_instance}`
db_instance="coyote-${branch_name}-${region}"
existing_instance=$(gcloud sql instances list | grep ${db_instance})
if [[ -z "${existing_instance}" ]]; then
echo "Creating Cloud SQL instance '${db_instance}'"
gcloud sql instances create ${db_instance} --database-version=POSTGRES_11 --tier db-g1-small --region=${region}
gcloud sql instances create ${db_instance} --database-version=POSTGRES_11 --tier db-f1-micro --region=${region}
else
echo "Using existing Cloud SQL instance '${db_instance}'"
fi

db_user="coyote_${env}"
if [[ -z "${DB_PASSWORD}" ]]; then
db_password=`bundle exec rails runner "puts SecureRandom.hex(20)"`
db_password=$(bundle exec rails runner "puts SecureRandom.hex(20)")
echo "Updating password for PostgreSQL user '${db_user}': '${db_password}'"
gcloud sql users set-password ${db_user} --instance=${db_instance} --password="${db_password}"
else
echo "Using database password '${DB_PASSWORD}'"
fi

db_host=`gcloud sql instances describe ${db_instance} | sed -rn 's/connectionName: (.+)/\1/p'`
db_host=$(gcloud sql instances describe ${db_instance} | pcregrep -o1 "connectionName: (.+)")
db_host="/cloudsql/${db_host}"
db_host=`rails runner "puts CGI.escape('${db_host}')"`
db_host=$(rails runner "puts CGI.escape('${db_host}')")
db_url="postgresql://${db_user}:${db_password}@${db_host}/coyote_${env}"

# Step 3: copy various credentials to the secrets manager
secrets=`gcloud secrets list`
secrets=$(gcloud secrets list)
add_secret() {
echo "Updating secret ${name}..."
name=$1
value=$2
has_secret=`echo -n ${secrets} |grep "\b${name}\b"`
has_secret=$(echo -n ${secrets} | grep "\b${name}\b")
if [[ -z "${value}" ]]; then
echo " Skipping (blank value)"
return
Expand All @@ -62,24 +62,24 @@ add_secret() {
}

add_secret "DATABASE_URL" "${db_url}"
add_secret "RAILS_BASE_KEY" `cat config/master.key`
add_secret "RAILS_MASTER_KEY" `cat config/credentials/production.key`
add_secret "MAILER_PASSWORD" `rails credentials:show --environment ${env} |grep mailer -A10 | sed -rn 's/\s*password: "(.+)"/\1/p'`
add_secret "RAILS_BASE_KEY" $(cat config/master.key)
add_secret "RAILS_MASTER_KEY" $(cat config/credentials/production.key)
add_secret "MAILER_PASSWORD" $(rails credentials:show --environment ${env} | grep mailer -A10 | pcregrep -o1 'password: "(.+)"')
if [[ "${env}" == "staging" ]]; then
add_secret "RAILS_STAGING_KEY" `cat config/credentials/staging.key`
add_secret "RAILS_STAGING_KEY" $(cat config/credentials/staging.key)
fi

# Step 4: set up the storage bucket
bucket="teamcoyote-uploads-${env}"
has_bucket=`gsutil ls | grep "\b${bucket}\b"`
# [[ -z "${has_bucket}" ]] && gsutil mb -l ${region} gs://${bucket}/
has_bucket=$(gsutil ls | grep "\b${bucket}\b")
[[ -z "${has_bucket}" ]] && gsutil mb -l ${region} gs://${bucket}/

# Step 5: set up a cloud build trigger

# Step 5: create the Cloudtasker queue
queue_command= "RAILS_ENV=production rails cloudtasker:setup_queue"
queue_command= "rails cloudtasker:setup_queue RAILS_ENV=production"
if [[ "${env}" == "staging" ]]; then
STAGING=1 ${queue_command}
${queue_command} STAGING=1
else
${queue_command}
fi

# Step 6:
3 changes: 2 additions & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
timeout: 1200s
steps:

# Prepare to import secrets
Expand Down Expand Up @@ -58,4 +59,4 @@ steps:
entrypoint: bash
args:
- -c
- gcloud run deploy coyote --image=gcr.io/$PROJECT_ID/coyote:latest --platform=managed --region=us-east1 --allow-unauthenticated --add-cloudsql-instances=$PROJECT_ID:us-east1:coyote-us-east1 --update-env-vars MAILER_PASSWORD=`cat tmp/mailer_password.txt`
- gcloud run deploy coyote --image=gcr.io/$PROJECT_ID/coyote:latest --platform=managed --region=us-east1 --allow-unauthenticated --add-cloudsql-instances=$PROJECT_ID:us-east1:coyote-$BRANCH_NAME-us-east1 --update-env-vars MAILER_PASSWORD=`cat tmp/mailer_password.txt`
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ services:
environment:
BOOTSNAP_CACHE_DIR: /bundle/bootsnap
DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL: "true"
DATABASE_HOST: db
NODE_ENV: ${NODE_ENV:-development}
RAILS_ENV: ${RAILS_ENV:-development}
WEBPACKER_DEV_SERVER_HOST: webpacker
Expand All @@ -60,7 +59,6 @@ services:
command: bundle exec rails db:test:prepare spec
environment:
DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL: "true"
DATABASE_HOST: db
DATABASE_URL: postgres://postgres:postgres@db:5432/coyote_test
NODE_ENV: test
PORT: 3030
Expand Down

0 comments on commit 074ec93

Please sign in to comment.