Further tidy-up (+ add warning())

This commit is contained in:
3wc 2020-09-23 11:53:34 +02:00
parent 427ed97678
commit 29343369f3

56
abra
View File

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