Skip to content

Commit

Permalink
added example commands
Browse files Browse the repository at this point in the history
  • Loading branch information
schmunk42 committed Nov 20, 2016
1 parent a554643 commit 2b1bb4d
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
10 changes: 10 additions & 0 deletions data/example/bin/roj-cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

# Command for removing unused networks, volumes and images

docker network ls -q | xargs docker network rm

docker volume ls -q | xargs docker volume rm

# Filter broken on swarm, see https://github.com/docker/swarm/issues/1700 and https://github.com/docker/swarm/pull/2514
docker images -a | grep "none" | awk '{print $3}' | xargs docker rmi
15 changes: 15 additions & 0 deletions data/example/bin/roj-cmd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

# Command to recursively execute a command when a docker-compose.yml is found in a directory

echo $1

find $1 -type f -iname "docker-compose.yml" -print0 | while IFS= read -r -d $'\0' line; do
echo ""
#echo "$line"
#ls -l "$line"
pushd $(dirname $line)
echo "======================================================="
$2
popd
done
119 changes: 119 additions & 0 deletions data/example/bin/roj-redeploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env bash

# Redeployment script for docker-compose
#
# Requirements: docker-compose.yml in ${STACK_DIR} (defaults to currenty working directory)
#
# Loop testing: (while true; roj-redeploy.sh; done;) >> /roj/_log/loop.log 2>&1

set -e

# Check directory
STACK_DIR=${STACK_DIR:-$(pwd)}
if [[ ! $(find ${STACK_DIR} -maxdepth 1 -name docker-compose.yml) ]]; then
echo "Error: docker-compose.yml not found"
exit 1
fi

cd ${STACK_DIR}

# Import or auto-detect and sanitize compose project name for usage directly with docker client
export $(cat .env | grep -v ^# | xargs)
COMPOSE_PROJECT_NAME=${COMPOSE_PROJECT_NAME:-$(basename $(pwd))}
COMPOSE_PROJECT_NAME_SANITIZED=$(echo $COMPOSE_PROJECT_NAME | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]')
export GREEN_STACK_NAME=${COMPOSE_PROJECT_NAME_SANITIZED}
export BLUE_STACK_NAME=${COMPOSE_PROJECT_NAME_SANITIZED}blue
_green="$(echo -e "\033[32m")"
_blue="$(echo -e "\033[34m")"
_warning="$(echo -e "\033[43m\033[30m")"
_default="$(echo -e "\033[39m\033[49m")"
# Start redeployment
echo ""
echo ""
echo -e "${_default}=> Redeploy\e[21m"
echo "Project name: ${COMPOSE_PROJECT_NAME_SANITIZED}"
if [[ ! ${STACK_UP_ONLY} ]]; then
echo ""
echo "Pulling stack images..."
docker-compose pull
if [[ ! $(docker-compose ps | grep "Exit") && $(docker-compose ps | grep "Up") ]]; then
n=0
until [ $n -ge 20 ]
do
n=$[$n+1]
set +e
export COMPOSE_PROJECT_NAME=${BLUE_STACK_NAME}
echo ""
echo "${_blue}-> Cleaning up stale 'blue' stacks..."
docker-compose down -v --remove-orphans
(docker network ls | grep ${BLUE_STACK_NAME}_default | awk '{print $1}' | xargs -r docker network rm)
docker ps | grep "${BLUE_STACK_NAME} "
echo ""
echo -e "${_blue}-> Setting up temporary 'blue' stack..."
export COMPOSE_PROJECT_NAME=${BLUE_STACK_NAME}
(docker ps | grep ${BLUE_STACK_NAME}) || echo "Blue clean."
(docker-compose up -d && docker-compose ps) && break
echo -e "${_warning}--> Retry $n in 10 seconds${_default}"
sleep 10
done
else
echo -e "${_warning}[!] Exited containers in 'green' stack found, skipped cleanup & deployment of 'blue' stack${_default}"
fi
fi
# TODO catch 20 retries
set -e
echo ""
echo -e "${_green}-> Redeploying main stack..."
export COMPOSE_PROJECT_NAME=${GREEN_STACK_NAME}
n=0
until [ $n -ge 20 ]
do
n=$[$n+1]
set +e
echo -e "${_green}--> Cleanup stale networks (ignore active endpoints error)..."
docker network ls | grep ${GREEN_STACK_NAME}_default | awk '{print $1}' | xargs -r docker network rm
if [[ ! ${STACK_UP_ONLY} ]]; then
echo -e "${_green}--> down"
docker-compose down -v --remove-orphans
fi
echo -e "${_green}--> up"
docker-compose up -d && break
set -e
echo -e "${_warning}--> Retry $n in 10 seconds${_default}"
sleep 10
done
# TODO catch 20 retries
docker-compose ps
echo ""
echo "${_blue}-> Removing 'blue' stack..."
export COMPOSE_PROJECT_NAME=${BLUE_STACK_NAME}
docker-compose down -v --remove-orphans
docker network ls | grep ${BLUE_STACK_NAME}_default | awk '{print $1}' | xargs -r docker network rm
echo ""
echo -e "${_default}"
echo "Done."
sleep 3
12 changes: 12 additions & 0 deletions data/example/bin/roj-reload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

# Stack reload script

[ -z "$STACK_DIR" ] && echo "Error: STACK_DIR variable is not set" && exit 1

cd ${STACK_DIR}

docker-compose down -v --remove-orphans
docker-compose up -d
docker-compose ps

0 comments on commit 2b1bb4d

Please sign in to comment.