This repository has been archived on 2021-07-03. You can view files and clone it, but cannot push or open issues or pull requests.
abra/abra

431 lines
8.9 KiB
Plaintext
Raw Normal View History

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
###### Utility functions
2020-09-13 22:16:46 +00:00
yml_pattern_exists() {
PATTERN=$1
if [ -f "$ABRA_CONFIG" ]; then
RESULT=$(yq read "$ABRA_CONFIG" "$PATTERN")
2020-09-13 22:16:46 +00:00
if [ "$RESULT" != 0 ]; then
return 0
fi
fi
return 1
}
2020-09-07 21:29:29 +00:00
parse_subcommand() {
SUBCOMMAND="$1"
PREFIX=$2
if [ -n "$PREFIX" ]; then
PPREFIX="_$2"
SPREFIX="$2 "
SSPREFIX=" $2"
fi
case $SUBCOMMAND in
"" | "-h" | "--help")
"sub${PPREFIX}_help"
;;
*)
shift 2
"sub${PPREFIX}_${SUBCOMMAND}" "$@"
if [ $? = 127 ]; then
echo "Error: '$SPREFIX$SUBCOMMAND' is not a known subcommand." >&2
echo " Run '$PROGRAM_NAME$SSPREFIX --help' for a list of known subcommands." >&2
exit 1
fi
;;
esac
}
error() {
echo "$(tput setaf 1)ERROR: $*$(tput sgr0)"
2020-09-23 09:53:34 +00:00
exit 1
}
warning() {
echo "$(tput setaf 3)WARNING: $*$(tput sgr0)"
}
success() {
echo "$(tput setaf 2)$*$(tput sgr0)"
}
###### Top-level arguments
ABRA_CONFIG=abra.yml
if [ "$1" == "-c" ]; then
ABRA_CONFIG=$2
shift 2
fi
if [ "$1" == "-e" ]; then
ABRA_ENV=$2
shift 2
fi
2020-09-13 07:31:43 +00:00
if [ "$1" == "-a" ]; then
STACK_NAME=$2
shift 2
fi
###### Load config
if [ -f "$ABRA_CONFIG" ]; then
2020-09-24 22:36:23 +00:00
require_yq
2020-09-13 22:16:46 +00:00
if yml_pattern_exists stack_name; then
STACK_NAME=$(yq read "$ABRA_CONFIG" stack_name)
2020-09-13 22:16:46 +00:00
fi
# FIXME load other variables somehow
fi
if [ -n "$ABRA_ENV" ]; then
# shellcheck disable=SC1090
source "$ABRA_ENV" || error "Unable to load env from '$ABRA_ENV'"
fi
###### Default settings
if [ -z "$COMPOSE_FILE" ]; then
COMPOSE_FILE="compose.yml"
fi
if [ -z "$ABRA_STACK_DIR" ]; then
ABRA_STACK_DIR="stacks/$SERVICE"
2020-09-13 22:16:46 +00:00
fi
2020-09-23 09:53:34 +00:00
load_context() {
if [ -z "$DOCKER_CONTEXT" ]; then
warning "\$DOCKER_CONTEXT not set, (slowly) looking it up"
# shellcheck disable=SC2063
2020-09-23 09:53:34 +00:00
DOCKER_CONTEXT=$(docker context ls | grep '*' | cut -d' ' -f1)
# FIXME 3wc: make sure grep doesn't parse this, we're want a literal '*'
2020-09-23 09:53:34 +00:00
fi
}
###### Safety checks
2020-09-24 22:36:23 +00:00
require_yq() {
if ! type yq > /dev/null 2>&1; then
error "yq program is not installed"
fi
}
require_multitail() {
if ! type multitail > /dev/null 2>&1; then
error "multitail program is not installed"
fi
}
2020-09-23 09:53:34 +00:00
require_stack() {
if [ -z "$STACK_NAME" ]; then
error "no stack_name, export \$STACK_NAME=my_cool_app or add it to abra.yml"
fi
}
require_stack_dir() {
if [ -z "$ABRA_STACK_DIR" ] || [ ! -d "$ABRA_STACK_DIR" ]; then
error "can't find \$ABRA_STACK_DIR '$ABRA_STACK_DIR'"
fi
}
2020-09-11 10:58:11 +00:00
if [ -z "$ABRA_ENV" ] && [ -f .envrc ] && type direnv > /dev/null 2>&1 && ! direnv status | grep -q 'Found RC allowed true'; then
error "direnv is blocked, run direnv allow"
2020-09-07 21:29:29 +00:00
fi
2020-09-08 06:49:27 +00:00
###### Custom commands
if [ -f abra-commands.sh ]; then
2020-09-13 07:31:43 +00:00
# shellcheck disable=SC1091
source abra-commands.sh
fi
###### Global help
sub_help() {
echo "Usage: $PROGRAM_NAME [-a STACK_NAME] [-c CONFIG] [-e ENV_FILE] <subcommand> [options]"
2020-09-07 21:29:29 +00:00
echo ""
echo "Subcommands:"
echo " context [--help] [SUBCOMMAND] manage remote swarm contexts"
echo " cp SRC_PATH SERVICE:DEST_PATH copy files to a container"
echo " deploy let 'em rip"
echo " logs SERVICE [ARGS] tail logs from a deployed service"
2020-09-24 22:36:23 +00:00
echo " multilogs tail logs from a whole stackk"
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"
echo " secret [--help] [SUBCOMMAND] manage secrets"
2020-09-22 12:17:12 +00:00
echo " upgrade upgrade to the latest version"
echo " ... (custom commands)"
2020-09-07 21:29:29 +00:00
echo ""
echo "Make sure \$STACK_NAME is set using direnv, -a, -e or -c"
echo ""
echo "Runs compose.yml by default, set e.g. COMPOSE_FILE=\"compose.yml:compose2.yml\" to override"
}
###### Subcommand `secret`
sub_secret_help() {
2020-09-24 19:19:40 +00:00
echo "Usage: $PROGRAM_NAME [global opts] secret <subcommand> [sub opts]"
echo ""
echo "Subcommands:"
2020-09-24 19:19:40 +00:00
echo " generate SECRET VERSION [PWGEN] generate & store secret"
echo " insert SECRET VERSION PW save PW in docker and pass"
2020-09-07 21:29:29 +00:00
}
sub_secret_insert() {
2020-09-23 09:53:34 +00:00
require_stack
load_context
2020-09-07 21:29:29 +00:00
SECRET=$1
VERSION=$2
PW=$3
2020-09-07 21:29:29 +00:00
if [ -z "$SECRET" ] || [ -z "$VERSION" ] || [ -z "$PW" ]; then
echo "Usage: $PROGRAM_NAME secret insert SECRET VERSION PW"
2020-09-07 21:29:29 +00:00
exit
fi
echo "$PW" | docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" - > /dev/null
echo "$PW" | pass insert "hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}" -m > /dev/null
}
sub_secret_generate(){
SECRET=$1
VERSION=$2
PWGEN=${3:-pwqgen}
PW=$($PWGEN)
success "Password: $PW"
sub_secret_insert "$SECRET" "$VERSION" "$PW"
2020-09-07 21:29:29 +00:00
}
sub_secret() {
SUBCOMMAND=$1
shift
# shellcheck disable=SC2068
parse_subcommand "$SUBCOMMAND" "secret" $@
}
###### Subcommand `run`
sub_run_args(){
2020-09-23 09:53:34 +00:00
require_stack
2020-09-07 21:29:29 +00:00
SERVICE=$1
2020-09-12 12:00:28 +00:00
DOCKER_ARGS=$2
shift 2
2020-09-08 06:49:27 +00:00
if [ -z "$SERVICE" ]; then
echo "Usage: $PROGRAM_NAME run SERVICE DOCKER_ARGS [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-23 09:53:34 +00:00
error "Can't find a container for ${STACK_NAME}_${SERVICE}"
2020-09-07 21:29:29 +00:00
exit
fi
# shellcheck disable=SC2086
docker exec $DOCKER_ARGS -it "$CONTAINER" "$@"
return
2020-09-07 21:29:29 +00:00
}
sub_run(){
SERVICE=$1
shift
sub_run_args "$SERVICE" "" "$@"
}
###### Subcommand `deploy`
2020-09-07 21:29:29 +00:00
sub_deploy (){
2020-09-23 09:53:34 +00:00
require_stack
require_stack_dir
load_context
2020-09-07 21:29:29 +00:00
echo "About to deploy:"
2020-09-23 09:53:34 +00:00
echo " Context: $(tput setaf 4)${DOCKER_CONTEXT}$(tput sgr0)"
echo " Compose: $(tput setaf 3)${ABRA_STACK_DIR}/${COMPOSE_FILE}$(tput sgr0)"
if [ -n "$DOMAIN" ]; then
echo " Domain: $(tput setaf 2)${DOMAIN}$(tput sgr0)"
fi
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
(
cd "$ABRA_STACK_DIR" || error "\$ABRA_STACK_DIR '$ABRA_STACK_DIR' not found"
# shellcheck disable=SC2086
if docker stack deploy -c ${COMPOSE_FILE/:/ -c } "$STACK_NAME"; then
if [ -n "$DOMAIN" ]; then
success "Yay! App should be available at https://${DOMAIN}"
else
success "Yay! That worked. No \$DOMAIN defined, check logs."
fi
else
error "Oh no! Something went wrong 😕 Check errors above"
fi
)
2020-09-07 21:29:29 +00:00
}
###### Subcommand `logs`
2020-09-24 22:48:46 +00:00
# Inspired by https://github.com/moby/moby/issues/31458#issuecomment-475411564
2020-09-24 22:36:23 +00:00
sub_multilogs() {
require_stack
require_multitail
# Get a list of the service names
SERVICES=$(docker stack services --format "{{.Name}}" "${STACK_NAME}")
# Sort the service names
SERVICES=$(echo "${SERVICES}" | sort)
# Create the command to run
COMMAND='multitail --mergeall'
for SERVICE in ${SERVICES}; do
COMMAND="${COMMAND} -L 'docker service logs --tail 20 -f ${SERVICE}'"
done
# Run the command
bash -c "${COMMAND}"
}
2020-09-08 07:10:37 +00:00
sub_logs (){
2020-09-23 09:53:34 +00:00
require_stack
2020-09-08 07:10:37 +00:00
SERVICE=$1
2020-09-24 22:36:23 +00:00
if [ -z "$SERVICE" ]; then
warning "No \$SERVICE provided, running multilogs"
sub_multilogs
fi
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
LOGS_ARGS=$@
2020-09-12 12:00:28 +00:00
fi
# shellcheck disable=SC2086
docker service logs "${STACK_NAME}_${SERVICE}" $LOGS_ARGS
2020-09-08 07:10:37 +00:00
}
###### Subcommand `cp`
sub_cp() {
2020-09-23 09:53:34 +00:00
require_stack
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-19 10:02:55 +00:00
if [ -z "$SERVICE" ]; then
echo "Usage: $PROGRAM_NAME cp SERVICE:SRC_PATH DEST_PATH"
echo " $PROGRAM_NAME cp SRC_PATH SERVICE:DEST_PATH"
echo ""
error "Can't find SERVICE in either SRC or DEST"
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-23 09:53:34 +00:00
error "Can't find a container for ${STACK_NAME}_${SERVICE}"
exit
fi
CP_ARGS=$(echo "$SOURCE $DEST" | sed "s/$SERVICE/$CONTAINER/")
# shellcheck disable=SC2086
docker cp $CP_ARGS
}
###### Subcommand `context`
sub_context_help() {
2020-09-24 19:19:40 +00:00
echo "Usage: $PROGRAM_NAME [global opts] context <subcommand> [sub opts]"
echo ""
echo "Subcommands:"
echo " create HOST [USER] [PORT] set up remote Docker context"
echo " use HOST activate remote Docker context"
echo " init HOST activate swarm mode"
}
sub_context_init() {
load_context
docker swarm init || true
docker network create --driver=overlay proxy --scope swarm || true
}
sub_context_create() {
HOST="$1"
USERNAME="$2"
PORT="$3"
if [ -n "$PORT" ]; then
PORT=":$PORT"
fi
if [ -n "$USERNAME" ]; then
USERNAME="$USERNAME@"
fi
docker context create "$HOST" \
--docker "host=ssh://$USERNAME$HOST$PORT"
}
sub_context_use() {
docker context use "$1"
}
sub_context() {
SUBCOMMAND2=$1
shift
# shellcheck disable=SC2068
parse_subcommand "$SUBCOMMAND2" "context" $@
}
2020-09-23 09:53:34 +00:00
###### Subcommand `upgrade`
sub_upgrade() {
curl -fsSL https://install.abra.autonomic.zone | bash
}
###### Main
SUBCOMMAND=$1
shift
# shellcheck disable=SC2086,SC2068
parse_subcommand $SUBCOMMAND "" $@