Skip to content

Commit

Permalink
feat(upgrade-backup): database backup
Browse files Browse the repository at this point in the history
- backup the database before running upgrades on the database

[ Fixes #159519407 ]
  • Loading branch information
WinstonKamau committed Jan 2, 2019
1 parent ca3372d commit 3570d31
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 40 deletions.
44 changes: 7 additions & 37 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ jobs:
when: on_success
command: |
if [ "$CIRCLE_NODE_INDEX" == 0 ]; then
bash scripts/slack_notification.sh "Staging database was upgraded successfully" "good"
bash scripts/slack_notification.sh "good"
fi
- run:
name: Build Failed
when: on_fail
command: |
if [ "$CIRCLE_NODE_INDEX" == 0 ]; then
bash scripts/slack_notification.sh "Staging database upgrade failed!!!" "danger"
bash scripts/slack_notification.sh "danger"
fi
deploy-staging:
deploy:
<<: *defaults
steps:
- checkout
Expand All @@ -61,39 +61,14 @@ jobs:
when: on_success
command: |
if [ "$CIRCLE_NODE_INDEX" == 0 ]; then
bash scripts/slack_notification.sh "The develop branch has been deployed to the staging environment" "good"
bash scripts/slack_notification.sh "good"
fi
- run:
name: Build Failed
when: on_fail
command: |
if [ "$CIRCLE_NODE_INDEX" == 0 ]; then
bash scripts/slack_notification.sh "Deployment to staging failed!!!" "danger"
fi
deploy-master:
<<: *defaults
steps:
- checkout
- setup_remote_docker:
version: 17.11.0-ce
- run:
name: deploy application
command: |
bash scripts/deploy.sh
- run:
name: Build Success
when: on_success
command: |
if [ "$CIRCLE_NODE_INDEX" == 0 ]; then
bash scripts/slack_notification.sh "The master branch has been deployed to the production environment" "good"
fi
- run:
name: Build Failed
when: on_fail
command: |
if [ "$CIRCLE_NODE_INDEX" == 0 ]; then
bash scripts/slack_notification.sh "Deployment to production failed!!!" "danger"
bash scripts/slack_notification.sh "danger"
fi
workflows:
Expand All @@ -108,17 +83,12 @@ workflows:
branches:
only:
- develop
- deploy-staging:
- master
- deploy:
requires:
- upgrade-database
filters:
branches:
only:
- develop
- deploy-master:
requires:
- test
filters:
branches:
only:
- master
44 changes: 41 additions & 3 deletions scripts/slack_notification.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,55 @@ set -o errexit
set -o pipefail


deploy_env_variables() {
MESSAGE_COLOR="$1"

if [ "$MESSAGE_COLOR" == "good" ]; then
MESSAGE_TEXT="The $CIRCLE_BRANCH branch has been deployed to the $ENVIRONMENT environment"
elif [ "$MESSAGE_COLOR" == "danger" ]; then
MESSAGE_TEXT="Deployment to $ENVIRONMENT failed!!!"
else
echo "Warning!: $MESSAGE_COLOR is not a color that was expected to be provided"
fi

}


upgrade_env_variables() {
MESSAGE_COLOR="$1"

if [ "$MESSAGE_COLOR" == "good" ]; then
MESSAGE_TEXT="$ENVIRONMENT database was upgraded successfully"
elif [ "$MESSAGE_COLOR" == "danger" ]; then
MESSAGE_TEXT="$ENVIRONMENT database upgrade failed!!!"
else
echo "Warning!: $MESSAGE_COLOR is not a color that was expected to be provided"
fi
}


declare_env_variables() {

# Declaring environment variables
#
# Some environment variables assigned externally are:
# SLACK_CHANNEL_HOOK : This is the webhook for the Slack App where notifications will be sent from
# DEPLOYMENT_CHANNEL : This is the channel on which the Slack notifications will be posted
# MESSAGE_TEXT: The text to be sent to the slack channel
# Some template for the Slack message
MESSAGE_TEXT="$1"
MESSAGE_COLOR="$2"

if [ "$CIRCLE_BRANCH" == "master" ]; then
ENVIRONMENT="Production"
else
ENVIRONMENT="Staging"
fi

if [ "$CIRCLE_JOB" == "upgrade-database" ]; then
upgrade_env_variables "$@"
fi

if [ "$CIRCLE_JOB" == "deploy" ]; then
deploy_env_variables "$@"
fi

COMMIT_LINK="https://github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/commit/${CIRCLE_SHA1}"
IMG_TAG="$(git rev-parse --short HEAD)"
Expand Down
34 changes: 34 additions & 0 deletions scripts/upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,39 @@ set_variables(){
APP_SETTINGS="Production"
CLOUDSQL_CONNECTION_NAME=${PRODUCTION_CLOUD_SQL_CONNECTION_NAME}
DATABASE_URL=${PRODUCTION_DATABASE_URL}
INSTANCE_NAME=${PRODUCTION_INSTANCE_NAME}
DATABASE_NAME=${PRODUCTION_DATABASE_NAME}
else
APP_SETTINGS="Staging"
CLOUDSQL_CONNECTION_NAME=${STAGING_CLOUD_SQL_CONNECTION_NAME}
DATABASE_URL=${STAGING_DATABASE_URL}
INSTANCE_NAME=${STAGING_INSTANCE_NAME}
DATABASE_NAME=${STAGING_DATABASE_NAME}
fi
}

install_google_cloud_sdk(){
echo "====> Installing google cloud sdk"
echo "deb http://packages.cloud.google.com/apt cloud-sdk-jessie main" | sudo tee /etc/apt/sources.list.d/google-cloud-sdk.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install kubectl google-cloud-sdk
}

authenticate_google_cloud() {
echo "====> Store Sand authenticate with service account"
echo $GCLOUD_SERVICE_KEY | base64 --decode > ${HOME}/gcloud-service-key.json
echo "Configuring Google Cloud Sdk"
gcloud auth activate-service-account --key-file=${HOME}/gcloud-service-key.json
gcloud --quiet config set project ${GOOGLE_PROJECT_ID}
}

export_data() {
DUMP_NAME=$(echo "${APP_SETTINGS}" | tr '[:upper:]' '[:lower:]')-sqldumpfile-$(date '+%Y-%m-%d-%H-%M-%S').gz
gcloud sql export sql ${INSTANCE_NAME} gs://${SOCIETIES_GCP_BUCKET}/${DUMP_NAME} \
--database=${DATABASE_NAME}
}


authorize_docker() {
echo "====> Store Sand authenticate with service account"
echo $GCLOUD_SERVICE_KEY | base64 --decode > ${HOME}/gcloud-service-key.json
Expand All @@ -24,10 +50,18 @@ authorize_docker() {
docker login -u _json_key -p "$(cat ${HOME}/gcloud-service-key.json)" https://gcr.io
}

logout_docker_google_cloud() {
gcloud auth revoke --all
}

main() {
set_variables
install_google_cloud_sdk
authenticate_google_cloud
export_data
authorize_docker
make upgrade APP_SETTINGS="${APP_SETTINGS}" CLOUDSQL_CONNECTION_NAME="${CLOUDSQL_CONNECTION_NAME}" DATABASE_URL="${DATABASE_URL}"
logout_docker_google_cloud
}

main "$@"

0 comments on commit 3570d31

Please sign in to comment.