WIP: mass version checking

This commit is contained in:
3wc 2021-07-10 00:47:55 +02:00
parent 9f835318d2
commit 5b2e37a7b2
1 changed files with 47 additions and 39 deletions

86
abra
View File

@ -1058,84 +1058,92 @@ POWERED BY (for --status)
}
sub_app_list (){
SERVER="$abra___server"
if [ -z "$SERVER" ]; then
SERVER='*'
local server="$abra___server"
if [ -z "$server" ]; then
server='*'
fi
shopt -s nullglob dotglob
# shellcheck disable=SC2206
ENV_FILES=($ABRA_DIR/servers/$SERVER/*.env)
env_files=($ABRA_DIR/servers/$server/*.env)
shopt -u nullglob dotglob
STATUS="$( [[ $abra___status == "true" ]] && echo "Y" )"
status="$( [[ $abra___status == "true" ]] && echo "Y" )"
if [ -n "$STATUS" ]; then
if [ "$SERVER" = "*" ]; then
if [ -n "$status" ]; then
if [ "$server" = "*" ]; then
get_servers
else
SERVERS=( "$SERVER" )
servers=( "$SERVER" )
fi
local -a DEPLOYED_APPS # array
local -a CHECKED_SERVERS # array
local -a deployed_apps # array
local -a checked_servers # array
warning "Loading status from ${#SERVERS[@]} server(s), patience advised.."
warning "Loading status from ${#servers[@]} server(s), patience advised.."
if [ "$abra___versions" == "true" ]; then
warning "Also looking up container versions. Maybe go for a walk."
fi
for SERVER in "${SERVERS[@]}"; do
SERVER="${SERVER##*/}" # basename
mapfile -t SERVER_APPS < <(DOCKER_CONTEXT="$SERVER" docker stack ls --format '{{ .Name }}' 2>/dev/null)
# add $SERVER~ to the start of each DEPLOYED_APPS
DEPLOYED_APPS+=("${SERVER_APPS[@]/#/$SERVER~}")
for server in "${servers[@]}"; do
server="${server##*/}" # basename
mapfile -t server_apps < <(DOCKER_CONTEXT="$SERVER" docker stack ls --format '{{ .Name }}' 2>/dev/null)
#if [ "$abra___versions" == "true" ]; then
# filter="{{index .Spec.Labels \"coop-cloud.*.$SERVICE.version\" }}"
# labels=$(docker service inspect -f "$filter" "" 2>/dev/null)
#else
# add $server~ to the start of each DEPLOYED_APPS
deployed_apps+=("${server_apps[@]/#/$server~}")
#fi
done
fi
# FIXME 3wc: doesn't take into account --type filtering
printf "%s lovely apps:\n\n" "${#ENV_FILES[@]}"
printf "%s lovely apps:\n\n" "${#env_files[@]}"
for i in "${!ENV_FILES[@]}"; do
for i in "${!env_files[@]}"; do
# Output header inside the loop, so it's included in the pipe to `column`
if [ "$i" == 0 ]; then
printf " DOMAIN\tTYPE\tSERVER%s%s\n" "${STATUS:+ }" "${STATUS:+STATUS}"
printf " --\t--\t--%s\n" "${STATUS:+ --}"
printf " DOMAIN\tTYPE\tserver%s%s\n" "${status:+ }" "${status:+status}"
printf " --\t--\t--%s\n" "${status:+ --}"
fi
local ENV_FILE="${ENV_FILES[$i]}" APP_STACK_NAME
local env_file="${env_files[$i]}" app_stack_name
IFS='/' read -r -a PARTS <<< "$ENV_FILE"
IFS='/' read -r -a parts <<< "$env_file"
FILE="${PARTS[-1]}"
SERVER="${PARTS[-2]}"
DOMAIN="${FILE%.env}"
env_file="${parts[-1]}"
server="${parts[-2]}"
domain="${env_file%.env}"
set -a
# shellcheck disable=SC1090
TYPE="$(source "$ENV_FILE" && echo "$TYPE")"
app_type="$(source "$env_file" && echo "$TYPE")"
# shellcheck disable=SC1090
APP_STACK_NAME="$(source "$ENV_FILE" && echo "$STACK_NAME")"
app_stack_name="$(source "$env_file" && echo "$STACK_NAME")"
set +a
if [ "$abra___type" != "" ] && [ "$abra___type" != "$TYPE" ]; then
if [ "$abra___type" != "" ] && [ "$abra___type" != "$app_type" ]; then
continue
fi
if [ -z "$APP_STACK_NAME" ]; then
APP_STACK_NAME="${DOMAIN//./_}"
if [ -z "$app_stack_name" ]; then
app_stack_name="${domain//./_}"
fi
if [ -n "$STATUS" ]; then
APP_STATUS=$( printf '%s\n' "${DEPLOYED_APPS[@]}" | grep -qP "^${SERVER}~${APP_STACK_NAME}$" && echo "deployed" || echo "inactive")
if [[ "$APP_STATUS" == "inactive" ]] ; then
if [[ ${CHECKED_SERVERS[*]} =~ ${SERVER} ]]; then
APP_STATUS="unknown"
if [ -n "$status" ]; then
app_status=$( printf '%s\n' "${deployed_apps[@]}" | grep -qP "^${server}~${app_stack_name}$" && echo "deployed" || echo "inactive")
if [[ "$app_status" == "inactive" ]] ; then
if [[ ${checked_servers[*]} =~ ${server} ]]; then
app_status="unknown"
else
if ! docker context inspect "$SERVER" > /dev/null 2>&1; then
APP_STATUS="unknown"
if ! docker context inspect "$server" > /dev/null 2>&1; then
app_status="unknown"
fi
CHECKED_SERVERS+=("$SERVER")
checked_servers+=("$server")
fi
fi
fi
printf " %s\t%s\t%s%s\n" "$DOMAIN" "$TYPE" "$SERVER" "${STATUS:+ }${APP_STATUS}"
printf " %s\t%s\t%s%s\n" "$domain" "$app_type" "$server" "${status:+ }${app_status}"
done | column -s' ' -t
# Align table `-t` based on tab characters -s`^V<Tab>`
}