commit a8a83f9e06785972eed3fe87be4321bc6ec0f2ef Author: 3wc <3wc.git@doesthisthing.work> Date: Mon Sep 7 23:29:29 2020 +0200 Initial import diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e47d857 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +dev_install: + ln -s $(PWD)/abra ~/.local/bin + +install: + install abra $(HOME)/.local/bin/abra diff --git a/README b/README new file mode 100644 index 0000000..5c88a71 --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +abra +=== + +Docker stack magic 🎩🐇 diff --git a/abra b/abra new file mode 100755 index 0000000..f8c5654 --- /dev/null +++ b/abra @@ -0,0 +1,82 @@ +#!/bin/bash +PROGRAM_NAME=$(basename $0) + +if [ -z $STACK_NAME ] || [ -z $STACK_NAME ]; then + echo "ERROR: STACK_NAME must be set (e.g. export STACK_NAME=my_cool_app)" + exit +fi + +sub_help(){ + echo "Usage: $PROGRAM_NAME [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" + echo "" + echo "Make sure \$STACK_NAME is set (probably using direnv)" +} + +sub_secret_generate(){ + SECRET=$1 + VERSION=$2 + + if [ -z $SECRET ] || [ -z $VERSION ]; then + echo "Usage: $PROGRAM_NAME secret_generate SECRET VERSION" + exit + fi + + pwqgen | tee \ + >(docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" -) \ + >(pass insert hosts/autonomic-swarm/${STACK_NAME}/${SECRET} -m) +} + +sub_service_run(){ + SERVICE=$1 + SH=${2:-/bin/sh} + + 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 +} + +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 -p "Continue? (y/[n])? " choice + + case "$choice" in + y|Y ) ;; + n|N ) return;; + * ) return;; + esac + + docker stack deploy -c $COMPOSE $STACK_NAME +} + +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