Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Consul watch handler for the type service
#! /bin/bash
FLINT_HOSTNAME="flint-hostname"
FLINT_USERNAME="flint-username"
FLINT_PASSWORD="flint-password"
FLINTBIT="flintbit"
FLINTBIT_PARAMS="flintbit_params"
consul_http_protocol="http"
consul_hostname="localhost"
consul_http_port="8500"
URL_trigger_flintbit="/v1/bit/run/"
URL_get_key_value_store="/v1/kv/?recurse"
agent_hostname="agent-hostname"
leader_address="leader-address"
node_address="node-address"
HEADER_content_type="Content-Type: application/json"
HEADER_cache_control="Cache-Control: no-cache"
#To validate flint configuration parameters like hostname,username,password and flintbit-name
ValidateFlintConfigParam() {
echo "validating flint hostname..."
if [ ! -z $FLINT_HOSTNAME ]; then
echo "validating flint username..."
if [ ! -z $FLINT_USERNAME ]; then
echo "validating flint password..."
if [ ! -z $FLINT_PASSWORD ]; then
echo "validating flint flintbit name..."
if [ ! -z $FLINTBIT ]; then
echo "flint configuration parameters are valid!!"
echo "flint-username: $FLINT_USERNAME,flint-password: $FLINT_PASSWORD,flint-hostname: $FLINT_HOSTNAME,flintbit-name: $FLINTBIT"
return 0
else
echo "Error: flint flintbit name not found!!"
return 1
fi;
else
echo "Error: flint password not found!!"
return 1
fi;
else
echo "Error: flint username not found!!"
return 1
fi;
else
echo "Error: flint hostname not found!!"
return 1
fi;
}
GetFlintConfigParam(){
FLINT_HOSTNAME=`echo "$1" | jq '.[] | select(.Key=="flint/hostname")' | jq '.Value' | tr -d '"' | base64 -d`
FLINT_USERNAME=`echo "$1" | jq '.[] | select(.Key=="flint/username")' | jq '.Value' | tr -d '"' | base64 -d`
FLINT_PASSWORD=`echo "$1" | jq '.[] | select(.Key=="flint/password")' | jq '.Value' | tr -d '"' | base64 -d`
FLINTBIT=`echo "$1" | jq '.[] | select(.Key=="flint/services/'$2'")' | jq '.Value' | tr -d '"' | base64 -d`
}
GetFlintbitParameters(){
FLINTBIT_PARAMS=`echo "$1" | jq '.[] | select(.Key=="flint/services/'$2'/flintbit/parameters")' | jq '.Value' | tr -d '"' | base64 -d`
}
service_info=`cat $STDIN`
echo "change in service state detected..."
echo "$service_info"
service_info_array_length=`echo "$service_info" | jq 'length'`
if [ $service_info_array_length -eq 0 ]; then
#statements
echo "Error: service definition not found!!"
else
if [ $service_info_array_length -eq 1 ];then
#statements
echo "found service definition..."
service_id=`echo "$service_info" | jq '.[].Service.ID' | tr -d '"'`
checks_array=`echo "$service_info" | jq '.[].Checks'`
checks_array_length=`echo "$checks_array" | jq 'length'`
if [ $checks_array_length -ne 0 ]; then
#statements
check_id=`echo "$service_info" | jq '.[].Checks[].CheckID' | tr -d '"'`
echo "synced service with id: $service_id"
echo "check-id for $service_id are: $check_id"
checks_info=`echo "$checks_array" | jq '.[] | select(.ServiceID=="apache2")'`
status=`echo $checks_info | jq '.Status' | tr -d '"'`
if [ "$status" = "critical" ]; then
#statements
node_address=`echo "$service_info" | jq '.[].Node.Address' | tr -d '"'`
url="$consul_http_protocol""://""$consul_hostname"":""$consul_http_port""$URL_get_key_value_store"
all_keys_info=`curl -sS -X GET -H "$HEADER_cache_control" "$url"`
if [ $? -eq 0 ]; then
if [ ! -z "$all_keys_info" ]; then
GetFlintConfigParam $all_keys_info $service_id
GetFlintbitParameters $all_keys_info $service_id
ValidateFlintConfigParam
if [ $? -eq 0 ]; then
echo "now, going to trigger flintbit..."
echo "triggering flintbit: $FLINTBIT"
flint_URL="$FLINT_HOSTNAME""$URL_trigger_flintbit""$FLINTBIT""/sync"
apache2_hostname=`echo "$service_info" | jq '.[].Service.Address' | tr -d '"'`
apache2_hostname_username=`echo "$FLINTBIT_PARAMS" | jq '.username' | tr -d '"'`
apache2_hostname_password=`echo "$FLINTBIT_PARAMS" | jq '.password' | tr -d '"'`
flintbit_params=`jq -n --arg apache2_hostname "$apache2_hostname" --arg username "$apache2_hostname_username" --arg password "$apache2_hostname_password" '{"target": $apache2_hostname ,"username":$username,"password":$password}'`
flintbitresponse=`curl -sS -X POST -H "x-flint-username:$FLINT_USERNAME" -H "x-flint-password:$FLINT_PASSWORD" -H "$HEADER_content_type" -H "$HEADER_cache_control" -d "$flintbit_params" "$flint_URL" | jq '.'`
echo "flint response: $flintbitresponse"
else
echo "Error: flint configuration parameters are invalid!!"
exit 1
fi;
else
echo "Error: Unable to retrieve key-value store"
exit 1
fi;
else
echo "Error: Unable to retrieve key-value store"
exit 1
fi;
else
echo "dont worry!! service $service_id is currently $status.."
echo "so, not triggering flint.."
fi;
else
echo "no checks found.."
echo "register checks to the local agent"
exit 1
fi;
else
echo "waiting for change in service state"
fi;
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment