Skip to content

Commit

Permalink
add and document maintenance mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dcmcand committed Mar 9, 2021
1 parent 64bf3cd commit d501eff
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,32 @@ You can run psql commands directly against a deployed database by following thes
On success, your terminal prompt will change to match the `db_name` from the database instance credentials.
This indicates you are in an open psql session, the command-line interface to PostgreSQL.

**Using Maintenance Mode**

if you need to put the application into maintenance mode, you can run the maintenance script located at `bin/maintenance`.

This script require that you have [Cloud Foundry's CLI v7](https://github.com/cloudfoundry/cli/wiki/V7-CLI-Installation-Guide) installed to run.
The script takes two flags
- \-m | \-\-maintenance\-mode controls whether the script takes the app into maintenance mode or out of it.
- Options are "on" or "off
- Default is "off"
- \-e | \-\-environment controls which environment you are targeting.
- Options are "sandbox", "dev", "staging", and "prod"
- Default is "prod"
Ex.
```
# Puts the dev environment into maintenance mode
./bin/maintenance -e dev -m on
# Takes prod out of maintenance mode
./bin/maintenance
```
If you are not logged into the cf cli, it will ask you for an sso temporary password. You can get a temporary password at https://login.fr.cloud.gov/passcode.
<!-- Links -->
[adhoc-main]: https://github.com/adhocteam/Head-Start-TTADP/tree/main
Expand Down
115 changes: 115 additions & 0 deletions bin/maintenance
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#! /bin/bash

maintenance=false
environment=prod

## On entering maintenance mode, this script will unmap the environments primary route and map it to that environments maintenance page.
## It will then map the maintenance route to the app so we still have access to it.
## On leaving maintenance mode, it will return the primary route to the app and delete the maintenance route

# Sandbox routes
sandboxmaintenanceroute=tta-smarthub-sandbox-maintenance
sandboxroute=tta-smarthub-sandbox

# Dev routes
devmaintenanceroute=tta-smarthub-dev-maintenance
devroute=tta-smarthub-dev

# Staging routes
stagingmaintenanceroute=tta-smarthub-staging-maintenance
stagingroute=tta-smarthub-staging

# Prod routes
prodmaintenanceroute=tta-smarthub-prod
prodroute=ttahub

# display usage information
help() {
echo "Usage: maintenance [options...]"
echo " -m, --maintenance-mode boolean value that controls whether maintenance mode is turned on or off. Default is off"
echo " -e, --environment specifies which environment the script is run on. Options are prod, staging, dev, or sandbox. Default is prod"
echo " -h, --help show this help text"
}

# Check feature flags and validate input
while [ "$1" != "" ]; do
case $1 in
--maintenance-mode | -m) shift
if [[ "$1" != "on" && "$1" != "off" ]]; then
echo "$1 is not a valid value"
help
exit 1
fi
maintenance=$1
;;
--environment | -e) shift
if [[ "$1" != "prod" && "$1" != "staging" && "$1" != "dev" && "$1" != "sandbox" ]]; then
echo "$1 is not a valid value"
help
exit 1
fi
environment=$1
;;
--help | -h) help
exit 0
;;
*) help
exit 1
;;
esac
shift
done

# Check the cf-cli v7 is available
version=$(cf version | cut -d " " -f 3 | cut -d "." -f 1)
if [[ $version != "7" ]]; then
echo "Cloud Foundry CLI v7 not found. Please install it to continue"
echo "https://github.com/cloudfoundry/cli/wiki/V7-CLI-Installation-Guide"
exit 1
fi

route=
maintenanceroute=
domain=app.cloud.gov

case $environment in
sandbox) route=$sandboxroute
maintenanceroute=$sandboxmaintenanceroute
;;
dev) route=$devroute
maintenanceroute=$devmaintenanceroute
;;
staging) route=$stagingroute
maintenanceroute=$stagingmaintenanceroute
;;
prod) route=$prodroute
maintenanceroute=$prodmaintenanceroute
domain=ohs.acf.hhs.gov
;;
esac

# try targeting the correct space and capture the exit status
cf target -o hhs-acf-ohs-tta -s ttahub-$environment > /dev/null

status=$?
# if cf target failed then login

if [[ $status != 0 ]]; then
cf login -a api.fr.cloud.gov -o hhs-acf-ohs-tta -s ttahub-$environment --sso
fi

# Put site into maintenance mode
if [[ $maintenance == "on" ]]; then
cf map-route tta-smarthub-$environment app.cloud.gov -n $maintenanceroute
cf map-route tta-smarthub-maintenance-page-$environment $domain -n $route
cf unmap-route tta-smarthub-$environment $domain -n $route
echo "The $environment environment is in maintenance mode and can be reached at https://$maintenanceroute.app.cloud.gov"
fi

# Bring site out of maintenance mode
if [[ $maintenance == "off" ]]; then
cf unmap-route tta-smarthub-$environment app.cloud.gov -n $maintenanceroute
cf map-route tta-smarthub-$environment $domain -n $route
cf unmap-route tta-smarthub-maintenance-page-$environment $domain -n $route
echo "The $environment environment is out of maintenance mode and can be reached at https://$route.$domain"
fi

0 comments on commit d501eff

Please sign in to comment.