Skip to content

Commit

Permalink
added dev-env-usage and slack message code
Browse files Browse the repository at this point in the history
  • Loading branch information
jackjoy-gds committed May 12, 2023
1 parent 16c45fc commit 2203507
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 32 deletions.
36 changes: 9 additions & 27 deletions concourse/pipelines/fast-startup-and-shutdown-cf-env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ resources:
icon: table
expose_build_created_by: true

# - name: dev-env-usage-state
# type: s3-iam
# source:
# bucket: ((state_bucket))
# region_name: ((aws_region))
# versioned_file: dev-env-usage-state
# initial_version: "-"
# initial_content: ""

- name: startup-trigger
type: semver-iam
source:
Expand Down Expand Up @@ -133,30 +124,16 @@ jobs:
- name: kick-off-shutdown
serial: true
plan:
- put: build-created-by-keyval
- put: shutdown-trigger
params: {bump: patch}

- name: fast-startup-cf-env
serial: true
plan:
- get: build-created-by-keyval
trigger: true
passed: ['kick-off-startup']
- task: print
config:
platform: linux
image_resource:
type: registry-image
source:
repository: busybox
inputs:
- name: build-created-by-keyval
run:
path: /bin/sh
args:
- -c
- |
cat build-created-by-keyval/version.json
trigger: true
- get: startup-trigger
passed: ['kick-off-startup']
trigger: true
Expand All @@ -172,6 +149,7 @@ jobs:
- name: bosh-vars-store
- name: paas-cf
- name: bosh-ca-crt
- name: build-created-by-keyval
params:
BOSH_ENVIRONMENT: ((bosh_fqdn))
BOSH_CA_CERT: ((bosh-ca-cert))
Expand All @@ -190,11 +168,14 @@ jobs:
- -e
- -c
- |
bash ./paas-cf/concourse/scripts/fast-startup-and-shutdown-cf-env.sh "((deploy_env))" wake
bash ./paas-cf/concourse/scripts/fast-startup-and-shutdown-cf-env.sh "((deploy_env))" wake "((dev_env_usage_slack_webhook_url))"
- name: fast-shutdown-cf-env
serial: true
plan:
- get: build-created-by-keyval
passed: ['kick-off-startup']
trigger: true
- get: bosh-vars-store
- get: paas-cf
- get: bosh-ca-crt
Expand All @@ -211,6 +192,7 @@ jobs:
- name: bosh-vars-store
- name: paas-cf
- name: bosh-ca-crt
- name: build-created-by-keyval
params:
BOSH_ENVIRONMENT: ((bosh_fqdn))
BOSH_CA_CERT: ((bosh-ca-cert))
Expand All @@ -229,4 +211,4 @@ jobs:
- -e
- -c
- |
bash ./paas-cf/concourse/scripts/fast-startup-and-shutdown-cf-env.sh "((deploy_env))" sleep
bash ./paas-cf/concourse/scripts/fast-startup-and-shutdown-cf-env.sh "((deploy_env))" sleep "((dev_env_usage_slack_webhook_url))"
1 change: 0 additions & 1 deletion concourse/scripts/dev-env-usage-update.sh

This file was deleted.

32 changes: 31 additions & 1 deletion concourse/scripts/fast-startup-and-shutdown-cf-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ main() {
manage_db_instances_state
fi
echo "${env_arg} is now ${past_tense}"
update_usage_and_slack
echo "done."
echo
}
Expand Down Expand Up @@ -199,12 +200,16 @@ parse_args() {
target_db_state='available'
past_tense='awake'
fi
webhook_arg="${3:-}"
if [[ "${webhook_arg}" = '' ]]; then
print_error 'expecting valid slack webhook url in arg3'
fi
}

print_header() {
echo '
┌─────────────────────────────┐
Fast Startup and Shutdown CF Env
Fast Startup and Shutdown CF Env
└─────────────────────────────┘
'
echo " env: ${env_arg}"
Expand Down Expand Up @@ -372,4 +377,29 @@ toggle_bosh_resurrector() {
echo "bosh resurrector toggled ${action}"
}

# update dev env usage and send slack update message
update_usage_and_slack() {
local usage_msg=""
echo "updating dev-env-usage..."
local build_created_by
build_created_by=$(jq -r '.build_created_by' < build-created-by-keyval/version.json)
local update_usage="${build_created_by} | ${past_tense}"
local s3_dev_envs
s3_dev_envs=$(aws s3api list-buckets | jq -r '.Buckets[].Name|match("gds-paas-(dev[0-9][0-9])-state").captures[].string')
local s3_dev_env
for s3_dev_env in ${s3_dev_envs}; do
local existing_usage
existing_usage=$(aws s3 cp "s3://gds-paas-${s3_dev_env}-state/dev-env-usage-file" -)
if [[ "${env_arg}" = "${s3_dev_env}" ]]; then
if [[ "${existing_usage}" != "${update_usage}" ]]; then
existing_usage="${update_usage}"
echo "${existing_usage}" | aws s3 cp - "s3://gds-paas-${s3_dev_env}-state/dev-env-usage-file"
fi
fi
usage_msg="${usage_msg}\n${s3_dev_env} | ${existing_usage}"
done
echo -e "dev-env-usage summary${usage_msg}" | sed 's/^/ /'
curl -s -H 'Content-type: application/json' -d "{\"text\":\"*dev-env-usage summary*\n\`\`\`${usage_msg}\`\`\`\"}" "${webhook_arg}" >/dev/null 2>&1
}

main "$@"
15 changes: 12 additions & 3 deletions scripts/upload-secrets/upload-slack-secrets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@

deploy_env = ENV.fetch("DEPLOY_ENV")

credhub_namespaces = [
cyber_credhub_namespaces = [
"/concourse/main/create-cloudfoundry",
"/#{deploy_env}/#{deploy_env}",
]

cyber_slack_webhook_url = ENV["SLACK_WEBHOOK_URL"] || get_secret("gds.slack.com/cyber_slack_webhook_url")
dev_env_usage_slack_webhook_url = ENV["DEV_ENV_USAGE_SLACK_WEBHOOK_URL"] || get_secret("gds.slack.com/dev-env-usage-webhook")

upload_secrets(
credhub_namespaces,
cyber_credhub_namespaces,
"cyber_slack_webhook_url" => cyber_slack_webhook_url,
)

dev_env_usage_credhub_namespaces = [
"/concourse/main/fast-startup-and-shutdown-cf-env",
]

dev_env_usage_slack_webhook_url = ENV["DEV_ENV_USAGE_SLACK_WEBHOOK_URL"] || get_secret("gds.slack.com/dev_env_usage_webhook")

upload_secrets(
dev_env_usage_credhub_namespaces,
"dev_env_usage_slack_webhook_url" => dev_env_usage_slack_webhook_url,
)

0 comments on commit 2203507

Please sign in to comment.