From 6a3d78385f14111d3913837580ba63ab7163266f Mon Sep 17 00:00:00 2001 From: Dave North Date: Wed, 25 May 2016 14:52:26 -0400 Subject: [PATCH 1/2] Added docker host support --- docker-clean | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docker-clean b/docker-clean index 06f5987..c5d7514 100755 --- a/docker-clean +++ b/docker-clean @@ -78,9 +78,21 @@ declare -a REMAINING_CONTAINERS # Currently in use declare -a EMPTY_NETWORKS # Currently in use #declare -a VOLUMES_TO_DELETE # Not in use +# @info: Docker host to use (default to empty or localhost). +declare DOCKER_HOST #FUNCTIONS +# @info: Overrides the system docker daemon so we can pass in a host +# @args: Global Arguments $@ +function docker(){ + if [ -z "$DOCKER_HOST" ]; then + command "docker" "$@" + else + command "docker" -H "$DOCKER_HOST" "$@" + fi +} + # @info: Parses and validates the CLI arguments # @args: Global Arguments $@ # TODO: handle use case where just -n or just -l flag is given @@ -101,11 +113,13 @@ function parseCli(){ esac while [[ "$#" -gt 0 ]]; do key="$1" + val="$2" case $key in stop ) STOP_CONTAINERS=true; CLEAN_CONTAINERS=true; CLEAN_IMAGES=true; CLEAN_VOLUMES=true; CLEAN_NETWORKS=true ;; images ) DELETE_TAGGED=true; CLEAN_CONTAINERS=true; CLEAN_IMAGES=true; CLEAN_VOLUMES=true; CLEAN_NETWORKS=true ;; run ) CLEAN_CONTAINERS=true; CLEAN_IMAGES=true; CLEAN_VOLUMES=true; CLEAN_NETWORKS=true ;; all ) STOP_CONTAINERS=true; DELETE_TAGGED=true; CLEAN_CONTAINERS=true; CLEAN_IMAGES=true; CLEAN_VOLUMES=true; CLEAN_NETWORKS=true ;; + -H | --host) DOCKER_HOST=$val; shift;; -s | --stop) STOP_CONTAINERS=true ;; -n | --dry-run) DRY_RUN=true ;; -l | --log) VERBOSE=true ;; @@ -155,6 +169,10 @@ function usage { echo echo "Additional Flag options:" echo + echo "-H or --host Specifies the docker host to run against" + echo " Useful for docker swarm maintenance" + echo " ie: -H 127.0.0.1:4000" + echo echo "-n or --dry-run Adding this additional flag will list items to be" echo " removed without executing any stopping or removing commands" echo From 46d45314f0ab5ba39888ae3b7a6d03b8d28577ab Mon Sep 17 00:00:00 2001 From: Dave North Date: Wed, 25 May 2016 18:07:40 -0400 Subject: [PATCH 2/2] quote the sudo calls to docker otherwise shellcheck thinks they are calling the override function --- docker-clean | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-clean b/docker-clean index c5d7514..0268dd4 100755 --- a/docker-clean +++ b/docker-clean @@ -567,7 +567,7 @@ function restartMachine { # Upstart covers SysV and OpenRC as well. if [[ $init_system =~ upstart ]]; then if [[ $DRY_RUN == false ]]; then - sudo service docker restart + sudo service "docker" restart else echo "Restart command that would be run: sudo service docker restart" fi @@ -579,7 +579,7 @@ function restartMachine { fi elif [[ $init_system =~ rc ]]; then if [[ $DRY_RUN == false ]]; then - sudo launchctl restart docker + sudo launchctl restart "docker" else echo "Restart command that would be run: sudo launchctl restart docker" fi