Further tidy-up (+ add warning())

This commit is contained in:
3wc 2020-09-23 11:53:34 +02:00
parent 3d3d6b9b67
commit 66013f6ddb
1 changed files with 47 additions and 11 deletions

58
abra
View File

@ -50,7 +50,11 @@ parse_subcommand() {
error() {
echo "$(tput setaf 1)ERROR: $*$(tput sgr0)"
exit
exit 1
}
warning() {
echo "$(tput setaf 3)WARNING: $*$(tput sgr0)"
}
###### Top-level arguments
@ -95,11 +99,26 @@ if [ -z "$ABRA_STACK_DIR" ]; then
ABRA_STACK_DIR="stacks/$SERVICE"
fi
load_context() {
if [ -z "$DOCKER_CONTEXT" ]; then
warning "\$DOCKER_CONTEXT not set, (slowly) looking it up"
DOCKER_CONTEXT=$(docker context ls | grep '*' | cut -d' ' -f1)
fi
}
###### Safety checks
if [ -z "$STACK_NAME" ]; then
error "no stack_name, export \$STACK_NAME=my_cool_app or add it to abra.yml"
fi
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
}
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"
@ -143,6 +162,9 @@ sub_secret_help() {
}
sub_secret_generate(){
require_stack
load_context
SECRET=$1
VERSION=$2
PW=${3:-pwqgen}
@ -154,7 +176,7 @@ sub_secret_generate(){
$PW | tee \
>(docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" -) \
>(pass insert "hosts/autonomic-swarm/${STACK_NAME}/${SECRET}" -m)
>(pass insert "hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}" -m)
}
sub_secret() {
@ -167,6 +189,8 @@ sub_secret() {
###### Subcommand `run`
sub_run_args(){
require_stack
SERVICE=$1
DOCKER_ARGS=$2
@ -181,7 +205,7 @@ sub_run_args(){
| grep "${STACK_NAME}_${SERVICE}" | cut -d',' -f1)
if [ -z "$CONTAINER" ]; then
echo "Container not found! 🚨"
error "Can't find a container for ${STACK_NAME}_${SERVICE}"
exit
fi
@ -202,7 +226,12 @@ sub_run(){
###### Subcommand `deploy`
sub_deploy (){
require_stack
require_stack_dir
load_context
echo "About to deploy:"
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)"
@ -235,6 +264,8 @@ sub_deploy (){
###### Subcommand `logs`
sub_logs (){
require_stack
SERVICE=$1
shift
@ -257,6 +288,8 @@ sub_logs (){
###### Subcommand `cp`
sub_cp() {
require_stack
SOURCE=$1
DEST=$2
@ -274,7 +307,7 @@ sub_cp() {
| grep "${STACK_NAME}_${SERVICE}" | cut -d',' -f1)
if [ -z "$CONTAINER" ]; then
error "Can't find a ${STACK_NAME}_${SERVICE}"
error "Can't find a container for ${STACK_NAME}_${SERVICE}"
exit
fi
@ -315,10 +348,6 @@ sub_context_use() {
docker context use "$1"
}
sub_upgrade() {
curl -fsSL https://install.abra.autonomic.zone | bash
}
sub_context() {
SUBCOMMAND2=$1
shift
@ -326,6 +355,13 @@ sub_context() {
parse_subcommand "$SUBCOMMAND2" "context" $@
}
###### Subcommand `upgrade`
sub_upgrade() {
curl -fsSL https://install.abra.autonomic.zone | bash
}
###### Main
SUBCOMMAND=$1