Last active
May 7, 2021 13:48
-
-
Save thapakazi/b5aeec1ed9c3495042789108f0551f02 to your computer and use it in GitHub Desktop.
helper script for jibri on ASG
This file contains hidden or 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
#!/bin/bash | |
meta_url='http://169.254.169.254' | |
AWS_REGION=$(curl --silent $meta_url/latest/dynamic/instance-identity/document | jq -r .region) | |
INSTANCE_ID=$(curl --silent $meta_url/latest/meta-data/instance-id) | |
ASG_NAME=$(aws autoscaling describe-auto-scaling-instances \ | |
--instance-ids $INSTANCE_ID \ | |
--region $AWS_REGION \ | |
| jq -r '.[] | .[] | .AutoScalingGroupName') | |
# modified: jibri exposes metrics on 2222, tested on a docker container | |
JIBRI_STATUS=$(curl --silent "http://localhost:2222/jibri/api/v1.0/health"| jq -j '.status.busyStatus') | |
JIBRI_HEALTH=$(curl --silent "http://localhost:2222/jibri/api/v1.0/health"| jq -j '.status.health.healthStatus') | |
## set/unset instance protection flag for asg | |
## args: no-protected-from-scale-in, protected-from-scale-in | |
protect_instance(){ | |
protection_flag=$1 | |
aws autoscaling set-instance-protection \ | |
--instance-ids $INSTANCE_ID \ | |
--auto-scaling-group-name $ASG_NAME \ | |
--${protection_flag} \ | |
--region $AWS_REGION | |
} | |
## report custom metric to cloudwatch | |
## args: 0|1; 0-BUSY, 1-IDLE | |
report_to_coudwatch(){ | |
CLOUDWATCH_METRIC_NAME="jibri_available" | |
aws cloudwatch put-metric-data \ | |
--metric-name $CLOUDWATCH_METRIC_NAME \ | |
--dimensions app="jibri" \ | |
--namespace "Custom" \ | |
--value $1 \ | |
--region $AWS_REGION | |
} | |
function check_health(){ | |
# if jibri is unhealthy | |
if [ "$JIBRI_HEALTH" = "UNHEALTHY" ] | |
then | |
logger "shutdown_server, jibri is unhealthy, might be dead, recycing server" | |
# systemctl shutdown | |
exit 1; | |
fi | |
} | |
function main(){ | |
# check state | |
if [ "$JIBRI_STATUS" = "IDLE" ] | |
then | |
logger "reporting jibri is idle" | |
report_to_coudwatch 1 | |
protect_instance no-protected-from-scale-in | |
fi | |
if [ "$JIBRI_STATUS" = "BUSY" ] | |
then | |
logger "reporting jibri is BUSY" | |
report_to_coudwatch 0 | |
protect_instance protected-from-scale-in | |
fi | |
} | |
check_health | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment