This repository has been archived on 2024-10-23. You can view files and clone it, but cannot push or open issues or pull requests.
abra-bash/abra
3wc 8da3e7dfca
Some checks failed
continuous-integration/drone/push Build is failing
Move -a processing, another shellcheck fix
2020-09-11 16:04:48 +02:00

116 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
PROGRAM_NAME=""$(basename "$0")
if [ $1 == "-a" ]; then
STACK_NAME=$2
shift 2
fi
if [ -z "$STACK_NAME" ]; then
echo "$(tput setaf 1)ERROR: \$STACK_NAME must be set (e.g. export STACK_NAME=my_cool_app)$(tput sgr0 )"
exit
fi
if type direnv > /dev/null 2>&1 && ! direnv status | grep -q 'Found RC allowed true'; then
echo "$(tput setaf 1)ERROR: direnv is blocked, run direnv allow$(tput sgr0)"
exit
fi
sub_help(){
echo "Usage: $PROGRAM_NAME [-a STACK_NAME] <subcommand> [options]"
echo ""
echo "Subcommands:"
echo " run SERVICE [CMD] run a command in the specified service's container"
echo " secret_generate SECRET VERSION [CMD] generate a secret, store it in pass & as a Docker secret"
echo " deploy [COMPOSE_FILE] let 'em rip"
echo " logs SERVICE tail logs from a deployed service"
echo ""
echo "Make sure \$STACK_NAME is set (probably using direnv)"
}
sub_secret_generate(){
SECRET=$1
VERSION=$2
PW=${3:-pwqgen}
if [ -z "$SECRET" ] || [ -z "$VERSION" ]; then
echo "Usage: $PROGRAM_NAME secret_generate SECRET VERSION"
exit
fi
$PW | tee \
>(docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" -) \
>(pass insert "hosts/autonomic-swarm/${STACK_NAME}/${SECRET}" -m)
}
sub_run(){
SERVICE=$1
SH=${2:-/bin/sh}
if [ -z "$SERVICE" ]; then
echo "Usage: $PROGRAM_NAME run SERVICE [CMD]"
exit
fi
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
| grep "${STACK_NAME}_${SERVICE}" | cut -d',' -f1)
if [ -z "$CONTAINER" ]; then
echo "Container not found! 🚨"
exit
fi
docker exec -it "$CONTAINER" "$SH"
return
}
sub_deploy (){
COMPOSE=${1:-compose.yml}
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)"
read -rp "Continue? (y/[n])? " choice
case "$choice" in
y|Y ) ;;
n|N ) return;;
* ) return;;
esac
if docker stack deploy -c "$COMPOSE" "$STACK_NAME"; then
echo "$(tput setaf 2)Yay! App should be available at https://${DOMAIN}$(tput sgr0)"
else
echo "$(tput setaf 1)Oh no! Something went wrong 😕 Check errors above$(tput sgr0)"
fi
}
sub_logs (){
SERVICE=$1
docker service logs "${STACK_NAME}_${SERVICE}" \
--follow \
--no-trunc \
--details \
--timestamps
}
subcommand=$1
case $subcommand in
"" | "-h" | "--help")
sub_help
;;
*)
shift
"sub_${subcommand}" "$@"
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