From 0f3f012fd7323719899b39f165ac581f62ac8375 Mon Sep 17 00:00:00 2001 From: Asif Bacchus Date: Sun, 15 Mar 2020 01:05:59 -0600 Subject: [PATCH] re-add clean operation --- ab-openldap/ab-openldap.sh | 51 +++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/ab-openldap/ab-openldap.sh b/ab-openldap/ab-openldap.sh index ad12ccd..8a37341 100755 --- a/ab-openldap/ab-openldap.sh +++ b/ab-openldap/ab-openldap.sh @@ -45,11 +45,13 @@ cyan=$(tput setaf 6) err=$(tput bold)$(tput setaf 1) magenta=$(tput setaf 5) norm=$(tput sgr0) +red=$(tput setaf 1) yellow=$(tput setaf 3) width=$(tput cols) ### parameter defaults scriptName="$( basename "$0" )" +clean=false restore=false container_name="ab-openldap" volume_data="ab-openldap_data" @@ -91,6 +93,9 @@ scriptHelp () { 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." 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 "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" @@ -136,6 +141,10 @@ while [ $# -gt 0 ]; do # start shell instead of default CMD shell=true ;; + --clean) + # stop if necessary, delete volumes + clean=true + ;; --restore) # restore backup restore=true @@ -191,8 +200,48 @@ done ### 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 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"