#!/usr/bin/env bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/

#########################################################################################
#  SCRIPT: clear-docker-cache.sh
#  Description: Used to cleanup unused docker/podman containers and volumes
######################################################################################
IFS=$'\n\t'
set -euo pipefail

# Prefer docker; fall back to podman when it isn't on PATH. The INFO
# lines surface the fallback in cron mail.
CONTAINER_ENGINE=docker
if ! [ -x "$(command -v docker)" ]; then
  echo -e "INFO: docker not found, checking for podman..."
  if [ -x "$(command -v podman)" ]; then
    echo -e "INFO: podman found, using podman"
    CONTAINER_ENGINE=podman
  else
    # Exit 0 so cron doesn't mail on every run for hosts with no engine.
    echo -e "WARNING: neither docker nor podman found, skipping clear-docker-cache"
    exit 0
  fi
fi

ENGINE_API_VERSION=$("$CONTAINER_ENGINE" version --format '{{.Client.APIVersion}}')
ENGINE_CLIENT_VERSION=$("$CONTAINER_ENGINE" version --format '{{.Client.Version}}')
ENGINE_CLIENT_VERSION="${ENGINE_CLIENT_VERSION%%-*}" # strip -whatever, so 23.0.0-rd becomes 23.0.0
REQUIRED_DOCKER_API_VERSION=1.25
FILTER_FLAG="${FILTER_FLAG:-label=com.gitlab.gitlab-runner.managed=true}"

usage() {
  echo -e "\nUsage: $0 prune-volumes|prune|space|help\n"
  echo -e "\tprune-volumes    Remove all unused containers (both dangling and unreferenced) and volumes"
  echo -e "\tprune            Remove all unused containers (both dangling and unreferenced)"
  echo -e "\tspace            Show docker/podman disk usage"
  echo -e "\thelp             Show usage"
  exit 1
}

# Skip the docker API gate under podman: different version scheme,
# and podman v3+ supports the prune commands below natively.
if [ "$CONTAINER_ENGINE" = "docker" ] && awk "BEGIN {exit !(\"$ENGINE_API_VERSION\" < \"$REQUIRED_DOCKER_API_VERSION\")}"; then
  echo -e "ERROR: Your current API version is lower than ${REQUIRED_DOCKER_API_VERSION}. The client and daemon API must both be at least ${REQUIRED_DOCKER_API_VERSION}+ to run these commands. Kindly upgrade your docker version."
  exit 1
fi

COMMAND="${1:-prune-volumes}"

case "$COMMAND" in

  prune)

    echo -e "\nCheck and remove all unused containers (both dangling and unreferenced)"
    echo -e "-----------------------------------------------------------------------"

    if [ "$CONTAINER_ENGINE" = "docker" ]; then
      if awk "BEGIN {exit !(\"$ENGINE_CLIENT_VERSION\" < \"17.06.1\")}"; then
        # The docker system prune command without pruning volumes does not exist before 17.06.1, so we need to use docker rm
        CONTAINERS=$("$CONTAINER_ENGINE" ps -a -q \
                    --filter=status=exited \
                    --filter=status=dead \
                    --filter="$FILTER_FLAG")

        if [ -n "${CONTAINERS}" ]; then
          "$CONTAINER_ENGINE" rm "${CONTAINERS}"
        fi
      else
        "$CONTAINER_ENGINE" system prune -af --filter "$FILTER_FLAG"
      fi
    else
      # podman supports the modern prune syntax natively
      "$CONTAINER_ENGINE" system prune -af --filter "$FILTER_FLAG"
    fi

    exit 0
    ;;

  space)

    echo -e "\nShow docker/podman disk usage"
    echo -e "-----------------------------"
    "$CONTAINER_ENGINE" system df

    exit 0
    ;;

  help)

    usage
    ;;

  prune-volumes)

    echo -e "\nCheck and remove all unused containers (both dangling and unreferenced) including volumes."
    echo -e "------------------------------------------------------------------------------------------"

    if [ "$CONTAINER_ENGINE" = "docker" ]; then
      if awk "BEGIN {exit !(\"$ENGINE_CLIENT_VERSION\" < \"17.04.0\")}"; then
        # Prior to 17.04, there was no filter flag for `docker system prune`, so we fallback to `docker rm`
        CONTAINERS=$("$CONTAINER_ENGINE" ps -a -q \
                    --filter=status=exited \
                    --filter=status=dead \
                    --filter="$FILTER_FLAG")

        if [ -n "${CONTAINERS}" ]; then
          "$CONTAINER_ENGINE" rm -v "${CONTAINERS}"
        fi

        exit 0
      elif awk "BEGIN {exit !(\"$ENGINE_CLIENT_VERSION\" < \"17.05.0\")}"; then
        # Prior to 17.05, `docker system prune` would also cleanup volumes
        "$CONTAINER_ENGINE" system prune -af --filter "$FILTER_FLAG"
      elif awk "BEGIN {exit !(\"$ENGINE_CLIENT_VERSION\" < \"23.0.0\")}"; then
        # Prior to 23.0, `docker volume prune` didn't support --all
        "$CONTAINER_ENGINE" system prune -af --filter "$FILTER_FLAG"
        "$CONTAINER_ENGINE" volume prune -f --filter "$FILTER_FLAG"
      else
        "$CONTAINER_ENGINE" system prune -af --filter "$FILTER_FLAG"
        "$CONTAINER_ENGINE" volume prune -af --filter "$FILTER_FLAG"
      fi
    else
      # podman supports the modern prune syntax natively
      "$CONTAINER_ENGINE" system prune -af --filter "$FILTER_FLAG"
      "$CONTAINER_ENGINE" volume prune -af --filter "$FILTER_FLAG"
    fi

    exit 0
    ;;

esac
