Last active
October 16, 2017 12:53
-
-
Save chazer/4d7ea89cd568221fad8db3561a1b592e to your computer and use it in GitHub Desktop.
docker-compose project switching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
sed_escape() { sed -e 's/[]\/$*.^|[]/\\&/g'; }; | |
replace_env() { | |
local FILE=$1; shift | |
local ENV=$1; shift | |
local VAL=$1 | |
[ -f "$FILE" ] || ( | |
echo "File not found $FILE" >&2 | |
return 1 | |
) | |
grep -q "^$( echo "$ENV" | sed_escape )=.*$" "$FILE" || echo "$ENV=" >> "$FILE" | |
sed -i "s/^$( echo "$ENV" | sed_escape )=.*/$( echo "$ENV" | sed_escape )=$( echo "$VAL" | sed_escape )/" "$FILE" | |
} | |
__project_current() { | |
[ -f .env ] && ( | |
sed -n 's/^COMPOSE_PROJECT_NAME=//p' .env | |
) | |
} | |
__project_select() { | |
[ -f .env ] || ( | |
touch .env || exit 1 | |
) | |
replace_env ".env" "COMPOSE_PROJECT_NAME" "$1" | |
} | |
__project_others() { | |
[ -f .compose/projects ] && ( | |
cat .compose/projects | grep -v -Fx "$( __project_current )" | |
) | |
} | |
__project_history_add() { | |
[ -d .compose ] || mkdir .compose | |
[ -f .compose/projects ] || touch .compose/projects | |
grep -q -Fx "$1" .compose/projects || echo "$1" >> .compose/projects | |
} | |
__project_history_rm() { | |
[ -f .compose/projects ] || exit 0 | |
sed -i "/$( echo "$1" | sed_escape )/d" .compose/projects | |
} | |
[ -f docker-compose.yml ] || ( | |
echo No file docker-compose.yml >&2 | |
exit 1 | |
) | |
cmd_project() { | |
local COMMAND=$1 | |
local CURR=$( __project_current ) | |
local PROJ=$CUR | |
if [ "$COMMAND" = "select" ]; then | |
shift | |
echo Switch project to $1 >&2 | |
__project_select "$1" || ( | |
echo "Couldn't change project to $1" >&2 | |
return 1 | |
) | |
__project_history_add "$1" | |
return 0 | |
fi | |
if [ "$COMMAND" = "current" ]; then | |
__project_current | |
return $? | |
fi | |
if [ "$COMMAND" = "remove" ]; then | |
shift | |
__project_history_rm "$1" | |
return $? | |
fi | |
if [ "$COMMAND" = "others" ]; then | |
__project_others | |
return $? | |
fi | |
echo "Invalid command" >&2 | |
return 1 | |
} | |
cmd_exec() { | |
local CURR=$( __project_current ) | |
local PROJ="$CURR" | |
local OTHERS= | |
if [ "$1" = "--project" ]; then | |
shift | |
PROJ=$1 | |
shift | |
fi | |
if [ "$1" = "--other-projects" ]; then | |
shift | |
OTHERS=1 | |
fi | |
if [ "$OTHERS" = "1" ]; then | |
local CURR="$( cmd_project current )" | |
cmd_project others | while read -r p; do | |
cmd_project select "$p" | |
"$@" | |
done | |
cmd_project select "$CURR" | |
return 0 | |
fi | |
cmd_project select "$PROJ" || return 1 | |
"$@" | |
local EX=$? | |
cmd_project select "$CURR" | |
return $EX | |
} | |
COMMAND=$1 | |
if [ "$COMMAND" = "project" ]; then | |
shift | |
cmd_project "$@" | |
exit $? | |
fi | |
if [ "$COMMAND" = "exec" ]; then | |
shift | |
cmd_exec "$@" | |
exit $? | |
fi | |
echo "Invalid command" >&2 | |
exit 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# 1. Go to docker-compose project directory | |
# 2. Setup new unique project name | |
# 3. Up project | |
# 4. Down others projects in this direcory | |
ssh <<EOF -p $DEPLOY_PORT $DEPLOY_USER@$DEPLOY_HOST | |
set -x | |
cd "$DEPLOY_REMOTE_PATH" | |
docker-compose-tool project select "$SERVICE-n-$DEPLOY_UNIQ" | |
docker-compose up -d || exit 1 | |
docker-compose-tool exec --other-projects sh -c 'docker-compose rm -s -f -v && docker-compose-tool project remove \$(docker-compose-tool project current)' | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment