Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TTAHUB-3060] Self Service Automation - Database Backup - Phase 2: Publish production url in slack #2279

Merged
merged 60 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
fa6b2a1
phase 1
GarrettEHill Jul 18, 2024
53c325f
next try
GarrettEHill Jul 19, 2024
5e3f32e
a few changes
GarrettEHill Jul 19, 2024
84b1c4c
Update config.yml
GarrettEHill Jul 19, 2024
77cc765
Update config.yml
GarrettEHill Jul 26, 2024
df17f1f
Update config.yml
GarrettEHill Jul 26, 2024
577110a
Update config.yml
GarrettEHill Jul 26, 2024
1fdf64a
try the new stuff
GarrettEHill Jul 27, 2024
cef6b51
Update config.yml
GarrettEHill Jul 29, 2024
d2df7f4
Update config.yml
GarrettEHill Jul 29, 2024
2ce8bf7
Update config.yml
GarrettEHill Jul 29, 2024
bdaea15
Merge branch 'main' into TTAHUB-3060/url-publish
GarrettEHill Jul 29, 2024
be9b8d0
Update yarn-audit-known-issues
GarrettEHill Jul 29, 2024
b43eb10
Update config.yml
GarrettEHill Jul 29, 2024
b5c386c
Update config.yml
GarrettEHill Jul 29, 2024
115b722
more time before timeout
GarrettEHill Jul 29, 2024
eed4226
fix env name
GarrettEHill Jul 29, 2024
39ff07b
Update config.yml
GarrettEHill Jul 30, 2024
2173171
Update cf_lambda.sh
GarrettEHill Jul 30, 2024
453c0db
Update db_backup.sh
GarrettEHill Jul 30, 2024
2c12bcd
Update config.yml
GarrettEHill Jul 30, 2024
542621e
Update config.yml
GarrettEHill Jul 30, 2024
7278aa3
Update config.yml
GarrettEHill Jul 30, 2024
63252a7
debug
GarrettEHill Jul 30, 2024
ba66106
Update db_backup.sh
GarrettEHill Jul 30, 2024
fa8e988
Update db_backup.sh
GarrettEHill Jul 30, 2024
1373597
Update db_backup.sh
GarrettEHill Jul 30, 2024
4c92e0b
Update cf_lambda.sh
GarrettEHill Jul 30, 2024
045d86e
Update config.yml
GarrettEHill Jul 30, 2024
49c080a
debug
GarrettEHill Jul 30, 2024
e99180a
remove adding user
GarrettEHill Jul 30, 2024
7074932
Update config.yml
GarrettEHill Jul 30, 2024
0e2e833
bug fixes
GarrettEHill Jul 30, 2024
3a92a12
fix circleci format error
GarrettEHill Jul 30, 2024
5a9d689
try a different format
GarrettEHill Jul 30, 2024
432e09b
Update config.yml
GarrettEHill Jul 30, 2024
70729ce
Update config.yml
GarrettEHill Jul 30, 2024
0c78b94
Update config.yml
GarrettEHill Jul 30, 2024
b8f4b46
Update config.yml
GarrettEHill Jul 30, 2024
6a585de
Update config.yml
GarrettEHill Jul 30, 2024
286bb08
Update config.yml
GarrettEHill Jul 30, 2024
ee7c116
Update config.yml
GarrettEHill Jul 30, 2024
b8f2be3
Update latest_backup.sh
GarrettEHill Jul 30, 2024
8032edf
Update config.yml
GarrettEHill Jul 30, 2024
2778c0a
add debugging
GarrettEHill Jul 30, 2024
0f9978f
temporarily turn of triggering the backup
GarrettEHill Jul 30, 2024
0448230
more debugging
GarrettEHill Jul 30, 2024
1825913
try a different path using iam roles
GarrettEHill Jul 31, 2024
9290b8d
adding permissions check
GarrettEHill Jul 31, 2024
9d0799b
Remove all the URL implementations
GarrettEHill Jul 31, 2024
eabf7b5
Merge branch 'main' into TTAHUB-3060/url-publish
GarrettEHill Jul 31, 2024
5952b4e
Update bin/latest_backup.sh
GarrettEHill Jul 31, 2024
4a080a1
Update bin/latest_backup.sh
GarrettEHill Jul 31, 2024
a827946
Update bin/latest_backup.sh
GarrettEHill Jul 31, 2024
6b0d8b7
Update bin/latest_backup.sh
GarrettEHill Jul 31, 2024
cf99e5c
Update automation/ci/scripts/cf_lambda.sh
GarrettEHill Jul 31, 2024
65d48b3
Update .circleci/config.yml
GarrettEHill Jul 31, 2024
53d1e80
Update automation/ci/scripts/cf_lambda.sh
GarrettEHill Jul 31, 2024
4c22a0c
Update .circleci/config.yml
GarrettEHill Jul 31, 2024
97dfb27
Merge branch 'main' into TTAHUB-3060/url-publish
GarrettEHill Aug 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 166 additions & 27 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ executors:
- image: cimg/aws:2024.03
commands:
sparse_checkout:
description: "Checkout a sparse directory from a specific branch."
description: "Checkout sparse directories from a specific branch."
parameters:
directory:
directories:
type: string
description: "Directory to checkout sparsely"
description: "Comma-separated list of directories to checkout sparsely"
branch:
type: string
description: "Branch to checkout"
Expand All @@ -45,10 +45,14 @@ commands:
command: |
git clone --no-checkout --filter=blob:none << pipeline.project.git_url >>.git .
- run:
name: Setup Sparse Checkout
name: Sparse Checkout
environment:
DIRECTORIES: "<< parameters.directories >>"
command: |
git config core.sparseCheckout true
echo "<< parameters.directory >>/*" >> .git/info/sparse-checkout
echo $DIRECTORIES | tr ',' '\n' | while read dir; do
echo "$dir" | tee -a .git/info/sparse-checkout
done
- run:
name: Checkout Branch
command: |
Expand Down Expand Up @@ -101,32 +105,131 @@ commands:
}
}"
notify_slack:
description: "Notify slack of a deploy to production"
description: "Notify Slack with message"
parameters:
slack_bot_token:
description: "Slack bot token"
type: string
slack_channel:
description: "Slack channel name to post the message to"
type: string
message_text:
description: "Message text to post to Slack"
type: string
default: ""
message_text_file:
description: "message text_file"
type: string
default: ""
steps:
- run:
name: Notify Slack
command: |
set -x
# Ensure the $BASH_ENV file exists
if [ ! -f $BASH_ENV ]; then
touch $BASH_ENV
fi

source $BASH_ENV
cat $BASH_ENV

# Evaluate message_text_script if provided
if [ -n "<< parameters.message_text_file >>" ]; then
MESSAGE_TEXT=$(cat "<< parameters.message_text_file >>")
else
MESSAGE_TEXT="<< parameters.message_text >>"
fi

echo $MESSAGE_TEXT

# Ensure all parameters are provided
if [ -z "<< parameters.slack_bot_token >>" ] || [ -z "<< parameters.slack_channel >>" ] || [ -z "$MESSAGE_TEXT" ]; then
echo "Missing required parameters. Notification will not be sent."
exit 1
fi

response=$(curl -s -X POST \
-H "Authorization: Bearer << parameters.slack_bot_token >>" \
-H 'Content-type: application/json;charset=utf-8' \
--data "{
\"channel\": \"<< parameters.slack_channel >>\",
\"text\": \"$MESSAGE_TEXT\"
}" \
https://slack.com/api/chat.postMessage)

ok=$(echo $response | jq -r '.ok')
error=$(echo $response | jq -r '.error')

if [ "$ok" != "true" ]; then
echo "Slack notification failed: $error"
exit 1
else
echo "Slack notification sent successfully"
fi

notify_slack_deploy:
parameters:
slack_bot_token:
description: "Slack bot token"
type: string
slack_channel:
description: "Slack channel name to post the message to"
type: string
environment_name:
description: "Name of environment"
type: string
steps:
- run:
name: Notify Slack of Deployment
command: |
# Check if the CIRCLE_PULL_REQUEST variable is set and extract the PR number from it
if [ ! -z "$CIRCLE_PULL_REQUEST" ]; then
PR_NUMBER=${CIRCLE_PULL_REQUEST##*/}
MESSAGE_TEXT=":rocket: Deployment of PR <$CIRCLE_PULL_REQUEST|$PR_NUMBER> to production was successful!"
else
MESSAGE_TEXT=":rocket: Deployment to production was successful!"
fi
- run:
name: Generate Message
command: |
# Determine the environment URL
case "<< parameters.environment_name >>" in
sandbox)
ENV_URL="https://tta-smarthub-sandbox.app.cloud.gov/"
;;
dev)
ENV_URL="https://tta-smarthub-dev.app.cloud.gov/"
;;
staging)
ENV_URL="https://tta-smarthub-staging.app.cloud.gov/"
;;
production)
ENV_URL="https://ttahub.ohs.acf.hhs.gov"
;;
*)
ENV_URL="#"
;;
esac

env_name="<< parameters.environment_name >>"

if [ ! -z "${CIRCLE_PULL_REQUEST}" ]; then
PR_NUMBER=${CIRCLE_PULL_REQUEST##*/}

PR_TITLE=$(curl -s "${CIRCLE_PULL_REQUEST}" | sed -e :a -e "N; s/\n/ /g; ta" | grep -oP "<bdi class=\"js-issue-title markdown-title\">[^<]+</bdi>" | sed -re "s~<[^>]+>~~g")

if [ ! -z "${PR_TITLE}" ]; then
JIRA_URLS=$(curl -s "${CIRCLE_PULL_REQUEST}" | sed -e :a -e "N; s/\n/ /g; ta" | grep -oP "Issue[(]s[)]</h2>.*Checklists</h2>" | grep -oP "\"https[^\"]+\"" | sed -e "s~\"~~g" | grep -o "https://jira.acf.gov/browse/[A-Z0-9-]*")

MESSAGE_TEXT=":rocket: Deployment of PR <${CIRCLE_PULL_REQUEST}|${PR_NUMBER}> (${PR_TITLE}) to <${ENV_URL}|${env_name}> was successful!"
if [ ! -z "${JIRA_URLS}" ]; then
MESSAGE_TEXT="${MESSAGE_TEXT}\nJIRA URLs in the PR:\n${JIRA_URLS}"
fi
else
MESSAGE_TEXT=":rocket: Deployment of PR <${CIRCLE_PULL_REQUEST}|${PR_NUMBER}> to <${ENV_URL}|${env_name}> was successful!"
fi
else
MESSAGE_TEXT=":rocket: Deployment to <${ENV_URL}|${env_name}> was successful!"
fi

echo -e "${MESSAGE_TEXT}" > /tmp/message_file

- notify_slack:
slack_bot_token: << parameters.slack_bot_token >>
slack_channel: << parameters.slack_channel >>
message_text_file: "/tmp/message_file"

curl -X POST -H "Authorization: Bearer << parameters.slack_bot_token >>" \
-H 'Content-type: application/json;charset=utf-8' \
--data "{
\"channel\": \"<< parameters.slack_channel >>\",
\"text\": \"$MESSAGE_TEXT\"
}" https://slack.com/api/chat.postMessage

cf_deploy:
description: "Login to cloud foundry space with service account credentials
Expand Down Expand Up @@ -308,12 +411,13 @@ commands:
sudo apt-get update
# Install uuid-runtime to have access to uuidgen
# Install pv wget
sudo apt-get install pv uuid-runtime wget
sudo apt-get install -y pv uuid-runtime wget coreutils jq

# Install Cloud Foundry CLI
wget -q -O - https://packages.cloudfoundry.org/debian/cli.cloudfoundry.org.key | sudo apt-key add -
echo "deb https://packages.cloudfoundry.org/debian stable main" | sudo tee /etc/apt/sources.list.d/cloudfoundry-cli.list
sudo apt-get update
sudo apt-get install cf8-cli
sudo apt-get install -y cf8-cli
# Install plugin needed for connect-to-service
cf install-plugin -f https://github.com/cloud-gov/cf-service-connect/releases/download/v1.1.3/cf-service-connect_linux_amd64

Expand Down Expand Up @@ -386,6 +490,19 @@ commands:
environment:
CF_RDS_SERVICE_NAME: ttahub-prod
CF_S3_SERVICE_NAME: ttahub-db-backups
- run:
name: Generate Message
command: |
if [ ! -z "$CIRCLE_PULL_REQUEST" ]; then
PR_NUMBER=${CIRCLE_PULL_REQUEST##*/}
echo ":download::database: Production backup before PR <$CIRCLE_PULL_REQUEST|$PR_NUMBER> successful!" > /tmp/message_file
else
echo ":download::database: Production backup successful!" > /tmp/message_file
fi
- notify_slack:
slack_bot_token: $SLACK_BOT_TOKEN
slack_channel: "acf-head-start-eng"
message_text_file: "/tmp/message_file"
- run:
name: Logout of service account
command: |
Expand Down Expand Up @@ -437,7 +554,7 @@ parameters:
default: "al-ttahub-2939-add-fei-root-cause-to-goal-card"
type: string
sandbox_git_branch: # change to feature branch to test deployment
default: "gh/cfignore-keep-sql-drop-tests"
default: "TTAHUB-3060/url-publish"
type: string
prod_new_relic_app_id:
default: "877570491"
Expand Down Expand Up @@ -664,6 +781,11 @@ jobs:
- run:
name: Wait for server to start
command: ./bin/ping-server 3000
- run:
name: Monitor database
command: |
docker attach $(docker ps | grep postgres | awk '{print $1}')
background: true
- run:
name: Install playwright dependencies
command: |
Expand Down Expand Up @@ -874,6 +996,11 @@ jobs:
env_name: sandbox
new_relic_app_id: << pipeline.parameters.sandbox_new_relic_app_id >>
new_relic_api_key: $NEW_RELIC_REST_API_KEY
- notify_slack_deploy:
slack_bot_token: $SLACK_BOT_TOKEN
slack_channel: "acf-head-start-github"
environment_name: "sandbox"

- when: # dev
condition:
and:
Expand Down Expand Up @@ -928,6 +1055,11 @@ jobs:
env_name: dev
new_relic_app_id: << pipeline.parameters.dev_new_relic_app_id >>
new_relic_api_key: $NEW_RELIC_REST_API_KEY
- notify_slack_deploy:
slack_bot_token: $SLACK_BOT_TOKEN
slack_channel: "acf-head-start-github"
environment_name: "dev"

- when: # staging
condition:
and:
Expand Down Expand Up @@ -980,6 +1112,11 @@ jobs:
env_name: staging
new_relic_app_id: << pipeline.parameters.staging_new_relic_app_id >>
new_relic_api_key: $NEW_RELIC_REST_API_KEY
- notify_slack_deploy:
slack_bot_token: $SLACK_BOT_TOKEN
slack_channel: "acf-head-start-github"
environment_name: "staging"

- when: # prod
condition:
and:
Expand Down Expand Up @@ -1032,16 +1169,18 @@ jobs:
env_name: prod
new_relic_app_id: << pipeline.parameters.prod_new_relic_app_id >>
new_relic_api_key: $NEW_RELIC_REST_API_KEY
- notify_slack:
- notify_slack_deploy:
slack_bot_token: $SLACK_BOT_TOKEN
slack_channel: "acf-ohs-ttahub--contractor-customer-team"
environment_name: "production"

resource_class: large
backup_upload_production:
docker:
- image: cimg/base:2024.05
steps:
- sparse_checkout:
directory: 'automation'
directories: 'automation'
branch: << pipeline.git.branch >>
- cf_backup:
auth_client_secret: PROD_AUTH_CLIENT_SECRET
Expand Down
5 changes: 4 additions & 1 deletion automation/ci/scripts/cf_lambda.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ set -o pipefail
set -o noglob
set -o noclobber

# Source the environment file to get the URLs
source /etc/environment

# -----------------------------------------------------------------------------
# Generic helper functions
# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -423,7 +426,7 @@ function run_task {
function monitor_task {
local app_name=$1
local task_name=$2
local timeout=${3:-200} # Default timeout in seconds
local timeout=${3:-300} # Default timeout in seconds
validate_parameters "$app_name"
validate_parameters "$task_name"
local start_time
Expand Down
3 changes: 3 additions & 0 deletions automation/db-backup/scripts/db_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ perform_backup_and_upload() {
fi
set -e
}

# -----------------------------------------------------------------------------

# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -776,11 +777,13 @@ function main() {
local backup_filename_prefix=$1
local rds_server=$2
local aws_s3_server=$3
local duration=${4-86400} # Default duration to 24 hours

log "INFO" "Validate parameters and exports"
parameters_validate "${backup_filename_prefix}"
parameters_validate "${rds_server}"
parameters_validate "${aws_s3_server}"
parameters_validate "${duration}"

export_validate "VCAP_SERVICES"

Expand Down
5 changes: 2 additions & 3 deletions bin/latest_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ fetch_latest_backup_info_and_cleanup() {

check_cf_version

# Main execution block
while [[ "$#" -gt 0 ]]; do
case $1 in
-n|--service-name) cf_s3_service_name="$2"; shift ;;
Expand All @@ -392,15 +391,15 @@ while [[ "$#" -gt 0 ]]; do
-e|--erase-file) erase_file="$2"; shift ;;
-k|--delete-old-keys) delete_old_keys="yes" ;;
-h|--help) echo "Usage: $0 [-n | --service-name <CF_S3_SERVICE_NAME>] [-s | --s3-folder <s3_folder>] [-a | --allow-deletion] [-l | --list-zip-files] [-f | --specific-file <file_name>] [-d | --download-and-verify] [-e | --erase-file <zip_file>] [-k | --delete-old-keys]"; exit 0 ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
*) echo "Unknown parameter passed: $1"; exit 12 ;;
esac
shift
done

# Check for required dependencies (cf CLI and AWS CLI)
if ! type cf >/dev/null 2>&1 || ! type aws >/dev/null 2>&1; then
echo "Error: Make sure both Cloud Foundry CLI and AWS CLI are installed."
exit 1
exit 12
fi

# Fetch the latest backup information, generate URLs, and clean up the service key
Expand Down