Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another bash script to power up|down aiven services using service tags #71

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions solutions/scripts/avn-power-services.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/bin/bash

# jq needs to be installed https://stedolan.github.io/jq/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this (or a smaller version of it?) be useful in the USAGE output? For those folks like me who just download and run things :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea! A bit like Git that says "no no but you might have wanted to write this instead?" ;)

# The PROJECT services needs to have at least these 2 tags
# { "env": ENVIRONMENT,
# "start-seq": SEQUENCE_NUMBER }
# ENVIRONMENT could be anything like "test1", "PROD", "dev", etc
# SEQUENCE_NUMBER should be an integer like 10, 50, 130, etc
# avn cli needs to be authenticated
# https://console.aiven.io/profile/auth "Authentication tokens" -> [Generate token]
# avn user login --token [email]
#
# This file follows these bash/sh shell scripts rules https://github.com/koalaman/shellcheck

USAGE="usage: --project PROJECT --tag-env ENVIRONMENT [--on | --off]"
if [ $# -ne 5 ]
then
echo "$USAGE"
exit 1
fi

while [ $# -gt 0 ]; do
case $1 in
--project)
shift && PROJECT="$1"
;;
--tag-env)
shift && TAG_ENV="$1"
;;
--on)
shift && POWER="ON"
;;
--off)
shift && POWER="OFF"
;;
*)
echo "unknown argument $1"
exit 1
;;
esac
shift
done

echo "Turning [$POWER] services for project [$PROJECT] tagged [$TAG_ENV]"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are services that either have complete data loss (e.g. Kafka) or run the risk of partial data loss (e.g. Redis) when being powered off. Is a confirmation dialog that acknowledges this risk important here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right! Why not. Let's go for a confirmation dialog then!

function service_list() {
local services
services=$(avn service list --project "$PROJECT" --json | jq -c '[.[] | {service_name, state, tags }]')
echo "$services"
}

function service_update_power() {
local service_name=$1
local power_arg
if [ "$POWER" == "ON" ]; then
power_arg="--power-on"
else
power_arg="--power-off"
fi
avn service update --project "$PROJECT" "$power_arg" "$service_name"
}

function filter_services() {
local services=$1
local filtered
filtered=$(jq -c --arg env "$TAG_ENV" '[.[] | select(.tags.env == $env)]' <<< "$services")
echo "$filtered"
}

function sort_services() {
local services=$1
local sorted
if [ "$POWER" == "ON" ]; then
sorted=$(jq -c 'sort_by(.tags."start-seq")' <<< "$services")
else
sorted=$(jq -c 'sort_by(.tags."start-seq") | reverse' <<< "$services")
fi
echo "$sorted"
}

function power() {
local services=$1
local service_name
local state
jq -c '.[]' <<< "$services" | while read -r service; do
service_name=$(jq -r '.service_name' <<< "$service")
state=$(jq -r '.state' <<< "$service")
if { [ "$state" == "POWEROFF" ] && [ "$POWER" == "ON" ]; } ||
{ [ "$state" == "RUNNING" ] && [ "$POWER" == "OFF" ]; } then
echo "Powering $POWER $service_name ..."
service_update_power "$service_name"
else
echo "$service_name is [$state]"
fi
done
}

services=$(service_list)
filtered=$(filter_services "$services")
sorted=$(sort_services "$filtered")
power "$sorted"