Commit c6394795 authored by Dhanushraj Murugan's avatar Dhanushraj Murugan
Browse files

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

Related to #243.
parent a6991b6e
Loading
Loading
Loading
Loading
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