2020-09-07 21:29:29 +00:00
|
|
|
#!/bin/bash
|
2020-09-13 22:16:46 +00:00
|
|
|
|
2020-09-13 21:33:55 +00:00
|
|
|
PROGRAM_NAME=$(basename "$0")
|
2020-09-13 22:16:46 +00:00
|
|
|
ABRA_CONFIG=abra.yml
|
|
|
|
|
2020-09-18 21:42:01 +00:00
|
|
|
if [ -z "$COMPOSE" ]; then
|
|
|
|
COMPOSE="compose.yml"
|
|
|
|
fi
|
|
|
|
|
2020-09-13 22:16:46 +00:00
|
|
|
yml_pattern_exists() {
|
|
|
|
PATTERN=$1
|
|
|
|
|
|
|
|
if ! type yq > /dev/null 2>&1; then
|
|
|
|
echo "$(tput setaf 1)ERROR: yq program is not installed$(tput sgr0)"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -f $ABRA_CONFIG ]; then
|
|
|
|
RESULT=$(yq read $ABRA_CONFIG "$PATTERN")
|
|
|
|
|
|
|
|
if [ "$RESULT" != 0 ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
2020-09-07 21:29:29 +00:00
|
|
|
|
2020-09-13 07:31:43 +00:00
|
|
|
if [ "$1" == "-a" ]; then
|
2020-09-11 14:04:48 +00:00
|
|
|
STACK_NAME=$2
|
|
|
|
shift 2
|
|
|
|
fi
|
|
|
|
|
2020-09-13 22:16:46 +00:00
|
|
|
if [ -f abra.yml ]; then
|
|
|
|
if yml_pattern_exists stack_name; then
|
|
|
|
STACK_NAME=$(yq read abra.yml stack_name)
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-09-11 14:04:48 +00:00
|
|
|
if [ -z "$STACK_NAME" ]; then
|
2020-09-11 11:07:01 +00:00
|
|
|
echo "$(tput setaf 1)ERROR: \$STACK_NAME must be set (e.g. export STACK_NAME=my_cool_app)$(tput sgr0 )"
|
2020-09-11 10:58:11 +00:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2020-09-11 11:07:01 +00:00
|
|
|
if type direnv > /dev/null 2>&1 && ! direnv status | grep -q 'Found RC allowed true'; then
|
2020-09-11 10:58:11 +00:00
|
|
|
echo "$(tput setaf 1)ERROR: direnv is blocked, run direnv allow$(tput sgr0)"
|
2020-09-07 21:29:29 +00:00
|
|
|
exit
|
|
|
|
fi
|
2020-09-08 06:49:27 +00:00
|
|
|
|
2020-09-11 16:04:47 +00:00
|
|
|
if [ -f abra-commands.sh ]; then
|
2020-09-13 07:31:43 +00:00
|
|
|
# shellcheck disable=SC1091
|
2020-09-11 16:04:47 +00:00
|
|
|
. abra-commands.sh
|
|
|
|
fi
|
|
|
|
|
|
|
|
sub_help() {
|
2020-09-11 10:58:59 +00:00
|
|
|
echo "Usage: $PROGRAM_NAME [-a STACK_NAME] <subcommand> [options]"
|
2020-09-07 21:29:29 +00:00
|
|
|
echo ""
|
|
|
|
echo "Subcommands:"
|
2020-09-14 17:16:34 +00:00
|
|
|
echo " cp SRC_PATH SERVICE:DEST_PATH copy files to a container"
|
2020-09-18 21:42:01 +00:00
|
|
|
echo " deploy let 'em rip"
|
2020-09-14 17:16:34 +00:00
|
|
|
echo " logs SERVICE [ARGS] tail logs from a deployed service"
|
2020-09-12 12:00:28 +00:00
|
|
|
echo " run SERVICE CMD run a command in the specified service's container"
|
|
|
|
echo " run_args SERVICE ARGS CMD run, passing extra args to docker exec"
|
2020-09-08 18:10:37 +00:00
|
|
|
echo " secret_generate SECRET VERSION [CMD] generate a secret, store it in pass & as a Docker secret"
|
2020-09-11 16:04:47 +00:00
|
|
|
echo " ... (custom commands)"
|
2020-09-07 21:29:29 +00:00
|
|
|
echo ""
|
2020-09-11 16:04:47 +00:00
|
|
|
echo "Make sure \$STACK_NAME is set using direnv or -a"
|
2020-09-18 21:42:01 +00:00
|
|
|
echo ""
|
|
|
|
echo "Runs compose.yml by default, set e.g. COMPOSE=\"compose.yml compose2.yml\" to override"
|
2020-09-07 21:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub_secret_generate(){
|
|
|
|
SECRET=$1
|
|
|
|
VERSION=$2
|
2020-09-08 18:10:37 +00:00
|
|
|
PW=${3:-pwqgen}
|
2020-09-07 21:29:29 +00:00
|
|
|
|
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
|
|
|
|
|
2020-09-08 18:10:37 +00:00
|
|
|
$PW | tee \
|
2020-09-07 21:29:29 +00:00
|
|
|
>(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
|
|
|
}
|
|
|
|
|
2020-09-11 16:04:47 +00:00
|
|
|
sub_run_args(){
|
2020-09-07 21:29:29 +00:00
|
|
|
SERVICE=$1
|
2020-09-12 12:00:28 +00:00
|
|
|
DOCKER_ARGS=$2
|
2020-09-11 16:04:47 +00:00
|
|
|
|
|
|
|
shift 2
|
2020-09-08 06:49:27 +00:00
|
|
|
|
2020-09-08 23:54:27 +00:00
|
|
|
if [ -z "$SERVICE" ]; then
|
|
|
|
echo "Usage: $PROGRAM_NAME run SERVICE [CMD]"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
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-13 21:36:35 +00:00
|
|
|
# shellcheck disable=SC2086
|
|
|
|
docker exec $DOCKER_ARGS -it "$CONTAINER" "$@"
|
2020-09-08 23:54:27 +00:00
|
|
|
|
|
|
|
return
|
2020-09-07 21:29:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 16:04:47 +00:00
|
|
|
sub_run(){
|
|
|
|
SERVICE=$1
|
|
|
|
|
|
|
|
shift
|
|
|
|
|
|
|
|
sub_run_args "$SERVICE" "" "$@"
|
|
|
|
}
|
|
|
|
|
2020-09-07 21:29:29 +00:00
|
|
|
sub_deploy (){
|
|
|
|
echo "About to deploy:"
|
2020-09-08 23:54:27 +00:00
|
|
|
echo " Compose: $(tput setaf 3)${PWD}/${COMPOSE}$(tput sgr0)"
|
2020-09-13 15:42:03 +00:00
|
|
|
if [ -n "$DOMAIN" ]; then
|
|
|
|
echo " Domain: $(tput setaf 2)${DOMAIN}$(tput sgr0)"
|
|
|
|
fi
|
2020-09-08 23:54:27 +00:00
|
|
|
echo " Stack: $(tput setaf 1)${STACK_NAME}$(tput sgr0)"
|
2020-09-07 21:29:29 +00:00
|
|
|
|
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-18 21:42:01 +00:00
|
|
|
# shellcheck disable=SC2086
|
|
|
|
if docker stack deploy -c ${COMPOSE/ / -c } "$STACK_NAME"; then
|
2020-09-13 15:42:03 +00:00
|
|
|
if [ -n "$DOMAIN" ]; then
|
|
|
|
echo "$(tput setaf 2)Yay! App should be available at https://${DOMAIN}$(tput sgr0)"
|
|
|
|
else
|
|
|
|
echo "$(tput setaf 2)Yay! That worked. No \$DOMAIN defined, check logs.(tput sgr0)"
|
|
|
|
fi
|
2020-09-08 23:54:27 +00:00
|
|
|
else
|
|
|
|
echo "$(tput setaf 1)Oh no! Something went wrong 😕 Check errors above$(tput sgr0)"
|
|
|
|
fi
|
2020-09-07 21:29:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 07:10:37 +00:00
|
|
|
sub_logs (){
|
|
|
|
SERVICE=$1
|
|
|
|
|
2020-09-12 12:00:28 +00:00
|
|
|
shift
|
2020-09-13 07:31:43 +00:00
|
|
|
|
2020-09-12 12:00:28 +00:00
|
|
|
if [ $# -eq 0 ]; then
|
|
|
|
LOGS_ARGS="\
|
2020-09-08 07:10:37 +00:00
|
|
|
--follow \
|
|
|
|
--no-trunc \
|
|
|
|
--details \
|
2020-09-12 12:00:28 +00:00
|
|
|
--timestamps"
|
|
|
|
else
|
2020-09-13 07:31:43 +00:00
|
|
|
# shellcheck disable=SC2124
|
2020-09-13 21:36:35 +00:00
|
|
|
LOGS_ARGS=$@
|
2020-09-12 12:00:28 +00:00
|
|
|
fi
|
|
|
|
|
2020-09-13 21:36:35 +00:00
|
|
|
# shellcheck disable=SC2086
|
|
|
|
docker service logs "${STACK_NAME}_${SERVICE}" $LOGS_ARGS
|
2020-09-08 07:10:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 17:16:34 +00:00
|
|
|
sub_cp() {
|
|
|
|
SOURCE=$1
|
|
|
|
DEST=$2
|
|
|
|
|
|
|
|
SERVICE=$(echo "$SOURCE" | grep -o '^[^:]\+:' || echo "$DEST" | grep -o '^[^:]\+:')
|
2020-09-19 10:02:55 +00:00
|
|
|
SERVICE=$(echo "$SERVICE" | tr -d ':')
|
2020-09-14 17:16:34 +00:00
|
|
|
|
2020-09-19 10:02:55 +00:00
|
|
|
if [ -z "$SERVICE" ]; then
|
2020-09-14 17:16:34 +00:00
|
|
|
echo "$(tput setaf 1)ERROR: Can't find SERVICE in either SRC or DEST$(tput sgr0)"
|
|
|
|
echo ""
|
|
|
|
echo "Usage: $PROGRAM_NAME cp SERVICE:SRC_PATH DEST_PATH"
|
|
|
|
echo " $PROGRAM_NAME cp SRC_PATH SERVICE:DEST_PATH"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
|
|
|
|
| grep "${STACK_NAME}_${SERVICE}" | cut -d',' -f1)
|
|
|
|
|
2020-09-19 10:02:55 +00:00
|
|
|
if [ -z "$CONTAINER" ]; then
|
2020-09-14 17:16:34 +00:00
|
|
|
echo "$(tput setaf 1)ERROR: Can't find a ${STACK_NAME}_${SERVICE}$(tput sgr0)"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
CP_ARGS=$(echo "$SOURCE $DEST" | sed "s/$SERVICE/$CONTAINER/")
|
|
|
|
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
docker cp $CP_ARGS
|
|
|
|
}
|
|
|
|
|
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
|