re-add clean operation

This commit is contained in:
Asif Bacchus 2020-03-15 01:05:59 -06:00
parent cee70f7ed8
commit 0f3f012fd7

View File

@ -45,11 +45,13 @@ cyan=$(tput setaf 6)
err=$(tput bold)$(tput setaf 1) err=$(tput bold)$(tput setaf 1)
magenta=$(tput setaf 5) magenta=$(tput setaf 5)
norm=$(tput sgr0) norm=$(tput sgr0)
red=$(tput setaf 1)
yellow=$(tput setaf 3) yellow=$(tput setaf 3)
width=$(tput cols) width=$(tput cols)
### parameter defaults ### parameter defaults
scriptName="$( basename "$0" )" scriptName="$( basename "$0" )"
clean=false
restore=false restore=false
container_name="ab-openldap" container_name="ab-openldap"
volume_data="ab-openldap_data" volume_data="ab-openldap_data"
@ -91,6 +93,9 @@ scriptHelp () {
textblock "${cyan}-s|--shell${norm}" textblock "${cyan}-s|--shell${norm}"
textblock "Switch parameter. Enter the container using an interactive POSIX shell. This happens after startup operations but *before* openLDAP (slapd) is started. This is a great way to test out configuration changes or run custom queries. You can combine this with '--rm' for easy configuration checks or LDIF imports." textblock "Switch parameter. Enter the container using an interactive POSIX shell. This happens after startup operations but *before* openLDAP (slapd) is started. This is a great way to test out configuration changes or run custom queries. You can combine this with '--rm' for easy configuration checks or LDIF imports."
printf "\n" printf "\n"
textblock "${cyan}--clean${norm}"
textblock "Switch parameter. This option will stop and remove ALL running openLDAP containers *AND DESTROY ALL VOLUMES*. This is meant to give you a 'clean start' if you've made configuration changes, etc."
printf "\n"
textblock "${cyan}--restore${norm}" textblock "${cyan}--restore${norm}"
textblock "Switch parameter. Restore a 'slapcat' backup to the data and ldif volumes in preparation for mounting them in a normal container. It is strongly recommended you review your '-t' '--data' and '--ldif' settings before proceeding with this option." textblock "Switch parameter. Restore a 'slapcat' backup to the data and ldif volumes in preparation for mounting them in a normal container. It is strongly recommended you review your '-t' '--data' and '--ldif' settings before proceeding with this option."
printf "\n" printf "\n"
@ -136,6 +141,10 @@ while [ $# -gt 0 ]; do
# start shell instead of default CMD # start shell instead of default CMD
shell=true shell=true
;; ;;
--clean)
# stop if necessary, delete volumes
clean=true
;;
--restore) --restore)
# restore backup # restore backup
restore=true restore=true
@ -191,8 +200,48 @@ done
### process main operations ### process main operations
if [ $clean = true ]; then
# cleanup containers and volumes
# display warning and confirm user's intentions
printf "\nThis will stop and remove all ab-openldap containers %sAND REMOVE ALL PERSISTENT DATA VOLUMES%s. Please ensure you have a backup and understand how to restore your data.\n" \
"$red" "$norm"
printf "%sThis action CANNOT be undone!%s\n\n" \
"$red" "$norm"
prompt_yn
# get all ab-openldap containers
containers=$(docker ps -a --no-trunc --filter "label=org.label-schema.name=ab-openldap" --format "{{ .Names }}")
# check for null value -- no containers to remove
if [ -z "$containers" ]; then
consoleError '0' 'No openldap containers to remove.'
fi
if [ $restore = true ]; then # iterate containers, stop them and remove straggling volumes
set -- dummy $containers
shift
for container; do
printf "\n%sFound %s -- processing:%s\n" \
"$cyan" "$container" "$norm"
# stop container
printf "\t%sStopping container...%s\n" "$red" "$norm"
docker stop ${container} > /dev/null 2>&1
# find volumes
volumes=$(docker inspect --format '{{ range .Mounts }}{{ println .Name }}{{ end }}' ${container})
# remove container
printf "\t%sRemoving container...%s\n" "$red" "$norm"
docker rm -f ${container} > /dev/null 2>&1
# pause to allow write flushing
sleep 3
# iterate volumes
set -- dummy2 $volumes
shift
for volume; do
printf "\t%sRemoving volume '%s'...%s\n" "$red" "$volume" "$norm"
docker volume rm -f ${volume} > /dev/null 2>&1
done
printf "%s...done%s\n" "$cyan" "$norm"
done
elif [ $restore = true ]; then
# automatically restore backups using a temp container to create volumes # automatically restore backups using a temp container to create volumes
printf "%s\n*** Restoring Backup ***\n\n%s" "$magenta" "$norm" printf "%s\n*** Restoring Backup ***\n\n%s" "$magenta" "$norm"
printf "To avoid errors due to existing files, this script will delete any volumes that have the following names (based on --data and --ldif):\n" printf "To avoid errors due to existing files, this script will delete any volumes that have the following names (based on --data and --ldif):\n"