Clean script: remove only the stack volumes and ask for confirmation

Closes #243 (closed)

Description

clean_capif_docker_services.sh ended with docker volume prune --all --force, which deletes every unused Docker volume on the host, not only OpenCAPIF's. It now runs docker compose down -v for each selected stack, so only the volumes attached to those stacks' containers are removed, and the host-wide prune is gone.

New option -y (and --yes) skips the confirmation. Without it, the script lists the stacks and asks Continue? [y/N], but only when run in an interactive terminal; without a terminal (CI, cron) it behaves as before.

Checked

  • bash -n syntax check.
  • Run with a stub docker command (nothing real executed): -c -y, --yes -a, no terminal, and the prompt answered n (aborts, nothing removed).
  • On a v5 deployment, the 7 anonymous volumes belong to the mongo, mongo_register, redis and vault containers. A real docker compose down -v on a sample project removed only its own volume.

Known limitation

Anonymous volumes whose containers were already removed are no longer deleted (the old prune removed them). They can be removed manually with docker volume prune.

Merge request reports

Loading
+35 −4
Changes for services/clean_capif_docker_services.sh: 35 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -24,10 +24,22 @@ help() {
  echo "       -s : Clean Robot Mock service"
  echo "       -a : Clean all services"
  echo "       -z : Clean images generated by docker-compose. Boolean. Default false"
  echo "       -y : Do not ask for confirmation before deleting (long form: --yes)"
  echo "       -h : show this help"
  exit 1
}

# getopts only understands short options: translate the long option --yes into -y
ARGS=()
for arg in "$@"; do
  if [[ "$arg" == "--yes" ]]; then
    ARGS+=("-y")
  else
    ARGS+=("$arg")
  fi
done
set -- "${ARGS[@]}"

if [[ $# -lt 1 ]]
then
  echo "You must specify an option before run script."
@@ -36,9 +48,10 @@ fi

FILES=()
echo "${FILES[@]}"
ASSUME_YES="false"

# Read params
while getopts "cvrahmsz:" opt; do
while getopts "cvrahmsyz:" opt; do
  case $opt in
    c)
      echo "Remove Capif services"
@@ -64,6 +77,9 @@ while getopts "cvrahmsz:" opt; do
      echo "Remove all services"
      FILES=("$SERVICES_DIR/docker-compose-capif.yml" "$SERVICES_DIR/docker-compose-vault.yml" "$SERVICES_DIR/docker-compose-register.yml" "$SERVICES_DIR/docker-compose-mock-server.yml" "$SERVICES_DIR//monitoring/docker-compose.yml")
      ;;
    y)
      ASSUME_YES="true"
      ;;
    z)
      echo "Remove images generated by docker-compose"
      REMOVE_IMAGES=$OPTARG
@@ -95,10 +111,24 @@ else
  REMOVE_IMAGES=""
fi

# Ask for confirmation before deleting data. The question is skipped with -y/--yes,
# and when the script runs without a terminal (CI, cron), so automated use is unchanged.
if [[ ${#FILES[@]} -gt 0 && "$ASSUME_YES" != "true" && -t 0 ]]; then
  echo ""
  echo "This will stop and remove the containers and the VOLUMES (stored data) of:"
  for FILE in "${FILES[@]}"; do
    echo "  - $FILE"
  done
  read -r -p "Continue? [y/N] " answer
  if [[ ! "$answer" =~ ^[Yy]$ ]]; then
    echo "Aborted. Nothing was removed."
    exit 1
  fi
fi

for FILE in "${FILES[@]}"; do
  echo "Executing 'docker compose down' for file $FILE"
  REGISTRY_BASE_URL=$REGISTRY_BASE_URL SERVICES_DIR=$SERVICES_DIR OCF_VERSION=$OCF_VERSION CAPIF_PRIV_KEY=$CAPIF_PRIV_KEY_BASE_64 DUID=$DUID DGID=$DGID MONITORING=$MONITORING_STATE LOG_LEVEL=$LOG_LEVEL docker compose -f "$FILE" down $REMOVE_IMAGES
  echo "Executing 'docker compose down -v' for file $FILE"
  REGISTRY_BASE_URL=$REGISTRY_BASE_URL SERVICES_DIR=$SERVICES_DIR OCF_VERSION=$OCF_VERSION CAPIF_PRIV_KEY=$CAPIF_PRIV_KEY_BASE_64 DUID=$DUID DGID=$DGID MONITORING=$MONITORING_STATE LOG_LEVEL=$LOG_LEVEL docker compose -f "$FILE" down -v $REMOVE_IMAGES
  status=$?
    if [ $status -eq 0 ]; then
        echo "*** Removed Service from $FILE ***"
@@ -120,6 +150,7 @@ done

docker network rm ${CAPIF_NETWORK_NAME}

docker volume prune --all --force
# Volumes are removed per stack by 'docker compose down -v' above, so volumes that
# belong to other projects on the same host are left untouched.

echo "Clean complete."
 No newline at end of file
Loading