2020-09-07 21:29:29 +00:00
|
|
|
#!/bin/bash
|
2020-09-08 06:58:49 +00:00
|
|
|
PROGRAM_NAME=""$(basename "$0")
|
2020-09-07 21:29:29 +00:00
|
|
|
|
2020-09-08 06:58:49 +00:00
|
|
|
if [ -z "$STACK_NAME" ] || [ -z "$STACK_NAME" ]; then
|
2020-09-07 21:29:29 +00:00
|
|
|
echo "ERROR: STACK_NAME must be set (e.g. export STACK_NAME=my_cool_app)"
|
|
|
|
exit
|
|
|
|
fi
|
2020-09-08 06:49:27 +00:00
|
|
|
|
2020-09-07 21:29:29 +00:00
|
|
|
sub_help(){
|
|
|
|
echo "Usage: $PROGRAM_NAME <subcommand> [options]"
|
|
|
|
echo ""
|
|
|
|
echo "Subcommands:"
|
|
|
|
echo " service_run SERVICE [CMD] run a command in the specified service's container"
|
|
|
|
echo " secret_generate SECRET VERSION generate a secret, store it in pass & as a Docker secret"
|
|
|
|
echo " deploy [COMPOSE_FILE] let 'em rip"
|
2020-09-08 07:10:37 +00:00
|
|
|
echo " logs SERVICE tail logs from a deployed service"
|
2020-09-07 21:29:29 +00:00
|
|
|
echo ""
|
|
|
|
echo "Make sure \$STACK_NAME is set (probably using direnv)"
|
|
|
|
}
|
|
|
|
|
|
|
|
sub_secret_generate(){
|
|
|
|
SECRET=$1
|
|
|
|
VERSION=$2
|
|
|
|
|
2020-09-08 06:58:49 +00:00
|
|
|
if [ -z "$SECRET" ] || [ -z "$VERSION" ]; then
|
2020-09-07 21:29:29 +00:00
|
|
|
echo "Usage: $PROGRAM_NAME secret_generate SECRET VERSION"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
pwqgen | tee \
|
|
|
|
>(docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" -) \
|
2020-09-08 06:58:49 +00:00
|
|
|
>(pass insert "hosts/autonomic-swarm/${STACK_NAME}/${SECRET}" -m)
|
2020-09-07 21:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub_service_run(){
|
|
|
|
SERVICE=$1
|
|
|
|
SH=${2:-/bin/sh}
|
2020-09-08 06:49:27 +00:00
|
|
|
|
2020-09-07 21:29:29 +00:00
|
|
|
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
|
2020-09-08 06:58:49 +00:00
|
|
|
| grep "${STACK_NAME}_${SERVICE}" | cut -d',' -f1)
|
2020-09-07 21:29:29 +00:00
|
|
|
|
2020-09-08 06:58:49 +00:00
|
|
|
if [ -z "$CONTAINER" ]; then
|
2020-09-07 21:29:29 +00:00
|
|
|
echo "Container not found! 🚨"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2020-09-08 06:58:49 +00:00
|
|
|
docker exec -it "$CONTAINER" "$SH"
|
2020-09-07 21:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub_deploy (){
|
|
|
|
COMPOSE=${1:-compose.yml}
|
2020-09-08 06:49:27 +00:00
|
|
|
|
2020-09-07 21:29:29 +00:00
|
|
|
echo "About to deploy:"
|
|
|
|
echo "Compose: $(tput setaf 3)${PWD}/${COMPOSE}$(tput sgr0)"
|
|
|
|
echo "Domain: $(tput setaf 2)${DOMAIN}$(tput sgr0)"
|
|
|
|
echo "Stack: $(tput setaf 1)${STACK_NAME}$(tput sgr0)"
|
|
|
|
|
2020-09-08 06:58:49 +00:00
|
|
|
read -rp "Continue? (y/[n])? " choice
|
2020-09-07 21:29:29 +00:00
|
|
|
|
|
|
|
case "$choice" in
|
|
|
|
y|Y ) ;;
|
|
|
|
n|N ) return;;
|
|
|
|
* ) return;;
|
|
|
|
esac
|
|
|
|
|
2020-09-08 06:58:49 +00:00
|
|
|
docker stack deploy -c "$COMPOSE" "$STACK_NAME"
|
2020-09-07 21:29:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 07:10:37 +00:00
|
|
|
sub_logs (){
|
|
|
|
SERVICE=$1
|
|
|
|
|
|
|
|
docker service logs "${STACK_NAME}_${SERVICE}" \
|
|
|
|
--follow \
|
|
|
|
--no-trunc \
|
|
|
|
--details \
|
|
|
|
--timestamps
|
|
|
|
}
|
|
|
|
|
2020-09-07 21:29:29 +00:00
|
|
|
subcommand=$1
|
|
|
|
case $subcommand in
|
|
|
|
"" | "-h" | "--help")
|
|
|
|
sub_help
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
shift
|
2020-09-08 06:58:49 +00:00
|
|
|
"sub_${subcommand}" "$@"
|
2020-09-07 21:29:29 +00:00
|
|
|
if [ $? = 127 ]; then
|
|
|
|
echo "Error: '$subcommand' is not a known subcommand." >&2
|
|
|
|
echo " Run '$PROGRAM_NAME --help' for a list of known subcommands." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|