-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4609 from GSA/scale
add scale-web-template
- Loading branch information
Showing
3 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
name: Scale Web Template | ||
|
||
on: # yamllint disable-line rule:truthy | ||
workflow_call: | ||
inputs: | ||
environ: | ||
required: true | ||
type: string | ||
app_names: | ||
required: true | ||
type: string | ||
|
||
secrets: | ||
CF_SERVICE_USER: | ||
required: true | ||
CF_SERVICE_AUTH: | ||
required: true | ||
|
||
jobs: | ||
scale: | ||
name: scale (${{ inputs.environ }}) | ||
environment: ${{ inputs.environ }} | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: ${{ fromJSON(inputs.app_names) }} | ||
steps: | ||
- name: checkout datagov | ||
uses: actions/checkout@v3 | ||
with: | ||
repository: gsa/data.gov | ||
path: './datagov' | ||
- name: scale ${{ matrix.app }} | ||
uses: cloud-gov/cg-cli-tools@main | ||
with: | ||
command: datagov/bin/check-and-renew ${{ matrix.app }} scale | ||
cf_org: gsa-datagov | ||
cf_space: ${{ inputs.environ }} | ||
cf_username: ${{secrets.CF_SERVICE_USER}} | ||
cf_password: ${{secrets.CF_SERVICE_AUTH}} | ||
- name: smoke test | ||
if: ${{ matrix.smoketest }} | ||
run: | | ||
sleep 10 | ||
curl --fail --silent ${{ inputs.app_url }}\ | ||
/api/action/status_show?$(date +%s) | ||
- name: Create Issue if it fails 😢 | ||
if: ${{ failure() && github.ref == 'refs/heads/main' }} | ||
uses: JasonEtco/create-an-issue@v2 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.ADD_TO_PROJECT_PAT }} | ||
GITHUB_JOB: ${{ toJson(github)['job'] }} | ||
GITHUB_ATTEMPTS: ${{ github.run_attempt }} | ||
LAST_COMMIT: ${{ github.sha }} | ||
LAST_RUN_BY: ${{ github.actor }} | ||
RUN_ID: ${{ github.run_id }} | ||
REPO: ${{ github.repository }} | ||
with: | ||
filename: datagov/.github/restart_failure.md | ||
assignees: ${{ github.actor }} | ||
update_existing: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/bin/bash | ||
|
||
MAX_INSTANCES=9 | ||
MIN_INSTANCES=5 | ||
SCALE_STEP=2 | ||
|
||
CPU_BUSY_THRESHOLD=320 | ||
CPU_IDLE_THRESHOLD=250 | ||
|
||
app_status=$(cf app catalog-web) | ||
scale_direction="" | ||
|
||
# This function will check the average CPU usage of all running instances of an app | ||
function set_scale_direction { | ||
# Extract lines containing 'running' | ||
running_lines=$(echo "$app_status" | grep "#[0-9]*[[:space:]]*running") | ||
|
||
cpu_sum=0 | ||
count=0 | ||
|
||
if [ -n "$running_lines" ]; then | ||
while IFS= read -r line | ||
do | ||
# Extract CPU usage and remove '%' character | ||
cpu_usage=$(echo $line | cut -d' ' -f4 | tr -d '%') | ||
cpu_sum=$(echo "$cpu_sum + $cpu_usage" | bc) | ||
((count++)) | ||
done <<< "$running_lines" | ||
fi | ||
|
||
# Calculate average CPU usage | ||
average_cpu=$(echo "scale=2; $cpu_sum / $count" | bc) | ||
|
||
if [ "$(echo "$average_cpu > $CPU_BUSY_THRESHOLD" | bc)" -eq 1 ]; then | ||
echo "Average CPU is $average_cpu. Too High." | ||
scale_direction="up" | ||
elif [ "$(echo "$average_cpu < $CPU_IDLE_THRESHOLD" | bc)" -eq 1 ]; then | ||
echo "Average CPU is $average_cpu. Too Low." | ||
scale_direction="down" | ||
else | ||
echo "Average CPU is $average_cpu. Just Right." | ||
scale_direction="same" | ||
fi | ||
} | ||
|
||
function set_scale_number { | ||
# get the total number of instances | ||
total_instances=$(echo "$app_status" | grep '^instances:' | awk '{print $2}' | cut -d '/' -f 2) | ||
# exit if the total instances is not a number | ||
if ! [[ "$total_instances" =~ ^[0-9]+$ ]]; then | ||
echo "Total instances is not a number. Exiting. Here is the output of the app status:" | ||
echo "$app_status" | ||
exit 1 | ||
fi | ||
echo "Current total instances: $total_instances" | ||
# call the scale direction | ||
set_scale_direction | ||
|
||
# if the direction is up and the total instances is less than the max instances | ||
if [ "$scale_direction" == "up" ] && [ "$total_instances" -lt "$MAX_INSTANCES" ]; then | ||
export scale_to=$((total_instances + SCALE_STEP)) | ||
echo "Scaling up to $scale_to" | ||
elif [ "$scale_direction" == "down" ] && [ "$total_instances" -gt "$MIN_INSTANCES" ]; then | ||
# export the scale_to value but make sure scale_to is large than 1 | ||
export scale_to=$((total_instances - SCALE_STEP < 1 ? 1 : total_instances - SCALE_STEP)) | ||
echo "Scaling down to $scale_to" | ||
else | ||
export scale_to="$total_instances" | ||
echo "Remain at the $scale_direction scale level $scale_to" | ||
fi | ||
} |