parent
b49b510c43
commit
c50b1d8760
648
abra
648
abra
@ -5,8 +5,21 @@
|
|||||||
DOC="
|
DOC="
|
||||||
The cooperative cloud utility belt 🎩🐇
|
The cooperative cloud utility belt 🎩🐇
|
||||||
|
|
||||||
Usage: abra [-c|--config=<path>] [-e|--env=<path>] [-h|--help]
|
Usage:
|
||||||
[-s|--stack=<path>] [-v|--version] <command> [<args>...]
|
abra [options] deploy
|
||||||
|
abra [options] upgrade
|
||||||
|
abra [options] logs [<app>]
|
||||||
|
abra [options] multilogs [<app>]
|
||||||
|
abra [options] cp (<app>:<path> <path>|<path> <app>:<path>)
|
||||||
|
abra [options] run [--user=<user>] <app> <args>...
|
||||||
|
abra [options] run <app> <args>...
|
||||||
|
abra [options] secret generate <secret> <version> [<cmd>]
|
||||||
|
abra [options] secret insert <secret> <version> <data>
|
||||||
|
abra [options] server add <host> [<user>] [<port>]
|
||||||
|
abra [options] server rm <host>
|
||||||
|
abra [options] server use <host>
|
||||||
|
abra [options] server init <host>
|
||||||
|
abra [options] server ls
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-c, --config=<path> Stack configuration to use
|
-c, --config=<path> Stack configuration to use
|
||||||
@ -15,26 +28,6 @@ Options:
|
|||||||
-s, --stack=<path> Name of the target stack
|
-s, --stack=<path> Name of the target stack
|
||||||
-v, --version Show program version
|
-v, --version Show program version
|
||||||
|
|
||||||
These are common Abra commands used in various situations:
|
|
||||||
|
|
||||||
Manage an application lifecycle:
|
|
||||||
deploy let 'em rip
|
|
||||||
upgrade upgrade to the latest version
|
|
||||||
|
|
||||||
View logs from running applications:
|
|
||||||
logs tail logs from a deployed service
|
|
||||||
multilogs tail logs from a whole stack
|
|
||||||
|
|
||||||
Interact with running containers:
|
|
||||||
cp <src> <dest> copy files to a container
|
|
||||||
run <service> <cmd> run <cmd> in <service>'s container
|
|
||||||
|
|
||||||
Manage servers:
|
|
||||||
server add <host> [user] [port] add docker context
|
|
||||||
server rm <host> remove docker context
|
|
||||||
server use <host> activate docker context
|
|
||||||
server init <host> activate docker swarm mode
|
|
||||||
|
|
||||||
Manage secrets:
|
Manage secrets:
|
||||||
secret generate <secret> <version> [pwgen] generate & store secret
|
secret generate <secret> <version> [pwgen] generate & store secret
|
||||||
secret insert <secret> <version> <password> save password in docker & pass
|
secret insert <secret> <version> <password> save password in docker & pass
|
||||||
@ -120,8 +113,14 @@ left=("${initial_left[@]}"); return 1; }; optional() { local node_idx
|
|||||||
for node_idx in "$@"; do "node_$node_idx"; done; return 0; }; oneormore() {
|
for node_idx in "$@"; do "node_$node_idx"; done; return 0; }; oneormore() {
|
||||||
local i=0; local prev=${#left[@]}; while "node_$1"; do ((i++)) || true
|
local i=0; local prev=${#left[@]}; while "node_$1"; do ((i++)) || true
|
||||||
[[ $prev -eq ${#left[@]} ]] && break; prev=${#left[@]}; done
|
[[ $prev -eq ${#left[@]} ]] && break; prev=${#left[@]}; done
|
||||||
if [[ $i -ge 1 ]]; then return 0; fi; return 1; }; switch() { local i
|
if [[ $i -ge 1 ]]; then return 0; fi; return 1; }; _command() { local i
|
||||||
for i in "${!left[@]}"; do local l=${left[$i]}
|
local name=${2:-$1}; for i in "${!left[@]}"; do local l=${left[$i]}
|
||||||
|
if [[ ${parsed_params[$l]} = 'a' ]]; then
|
||||||
|
if [[ ${parsed_values[$l]} != "$name" ]]; then return 1; fi
|
||||||
|
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
|
||||||
|
[[ $testdepth -gt 0 ]] && return 0; if [[ $3 = true ]]; then
|
||||||
|
eval "((var_$1++)) || true"; else eval "var_$1=true"; fi; return 0; fi; done
|
||||||
|
return 1; }; switch() { local i; for i in "${!left[@]}"; do local l=${left[$i]}
|
||||||
if [[ ${parsed_params[$l]} = "$2" ]]; then
|
if [[ ${parsed_params[$l]} = "$2" ]]; then
|
||||||
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
|
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
|
||||||
[[ $testdepth -gt 0 ]] && return 0; if [[ $3 = true ]]; then
|
[[ $testdepth -gt 0 ]] && return 0; if [[ $3 = true ]]; then
|
||||||
@ -135,37 +134,88 @@ eval "var_$1+=($value)"; else eval "var_$1=$value"; fi; return 0; fi; done
|
|||||||
return 1; }; stdout() { printf -- "cat <<'EOM'\n%s\nEOM\n" "$1"; }; stderr() {
|
return 1; }; stdout() { printf -- "cat <<'EOM'\n%s\nEOM\n" "$1"; }; stderr() {
|
||||||
printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() {
|
printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() {
|
||||||
[[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() {
|
[[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() {
|
||||||
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1353}
|
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1102}
|
||||||
usage=${DOC:40:131}; digest=0d574; shorts=(-c -e -h -s -v)
|
usage=${DOC:40:574}; digest=fab9b; shorts=(-h -v -e -c -s '')
|
||||||
longs=(--config --env --help --stack --version); argcounts=(1 1 0 1 0)
|
longs=(--help --version --env --config --stack --user); argcounts=(0 0 1 1 1 1)
|
||||||
node_0(){ value __config 0 true; }; node_1(){ value __env 1 true; }; node_2(){
|
node_0(){ switch __help 0; }; node_1(){ switch __version 1; }; node_2(){
|
||||||
switch __help 2; }; node_3(){ value __stack 3 true; }; node_4(){
|
value __env 2; }; node_3(){ value __config 3; }; node_4(){ value __stack 4; }
|
||||||
switch __version 4; }; node_5(){ value _command_ a; }; node_6(){
|
node_5(){ value __user 5; }; node_6(){ value _app_ a; }; node_7(){
|
||||||
value _args_ a true; }; node_7(){ optional 0 0; }; node_8(){ optional 1 1; }
|
value _app___path_ a; }; node_8(){ value _path_ a; }; node_9(){
|
||||||
node_9(){ either 2 2; }; node_10(){ optional 9; }; node_11(){ optional 3 3; }
|
value _args_ a true; }; node_10(){ value _secret_ a; }; node_11(){
|
||||||
node_12(){ either 4 4; }; node_13(){ optional 12; }; node_14(){ oneormore 6; }
|
value _version_ a; }; node_12(){ value _cmd_ a; }; node_13(){ value _data_ a; }
|
||||||
node_15(){ optional 14; }; node_16(){ required 7 8 10 11 13 5 15; }; node_17(){
|
node_14(){ value _host_ a; }; node_15(){ value _user_ a; }; node_16(){
|
||||||
required 16; }; cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2
|
value _port_ a; }; node_17(){ _command deploy; }; node_18(){ _command upgrade; }
|
||||||
printf "%s\n" "${DOC:40:131}" >&2; exit 1; }'; unset var___config var___env \
|
node_19(){ _command logs; }; node_20(){ _command multilogs; }; node_21(){
|
||||||
var___help var___stack var___version var__command_ var__args_; parse 17 "$@"
|
_command cp; }; node_22(){ _command run; }; node_23(){ _command secret; }
|
||||||
local prefix=${DOCOPT_PREFIX:-''}; unset "${prefix}__config" "${prefix}__env" \
|
node_24(){ _command generate; }; node_25(){ _command insert; }; node_26(){
|
||||||
"${prefix}__help" "${prefix}__stack" "${prefix}__version" "${prefix}_command_" \
|
_command server; }; node_27(){ _command add; }; node_28(){ _command rm; }
|
||||||
"${prefix}_args_"; if declare -p var___config >/dev/null 2>&1; then
|
node_29(){ _command use; }; node_30(){ _command init; }; node_31(){ _command ls
|
||||||
eval "${prefix}"'__config=("${var___config[@]}")'; else
|
}; node_32(){ optional 0 1 2 3 4; }; node_33(){ optional 32; }; node_34(){
|
||||||
eval "${prefix}"'__config=()'; fi; if declare -p var___env >/dev/null 2>&1; then
|
required 33 17; }; node_35(){ required 33 18; }; node_36(){ optional 6; }
|
||||||
eval "${prefix}"'__env=("${var___env[@]}")'; else eval "${prefix}"'__env=()'; fi
|
node_37(){ required 33 19 36; }; node_38(){ required 33 20 36; }; node_39(){
|
||||||
eval "${prefix}"'__help=${var___help:-false}'
|
required 7 8; }; node_40(){ required 8 7; }; node_41(){ either 39 40; }
|
||||||
if declare -p var___stack >/dev/null 2>&1; then
|
node_42(){ required 41; }; node_43(){ required 33 21 42; }; node_44(){
|
||||||
eval "${prefix}"'__stack=("${var___stack[@]}")'; else
|
optional 5; }; node_45(){ oneormore 9; }; node_46(){ required 33 22 44 6 45; }
|
||||||
eval "${prefix}"'__stack=()'; fi
|
node_47(){ required 33 22 6 45; }; node_48(){ optional 12; }; node_49(){
|
||||||
|
required 33 23 24 10 11 48; }; node_50(){ required 33 23 25 10 11 13; }
|
||||||
|
node_51(){ optional 15; }; node_52(){ optional 16; }; node_53(){
|
||||||
|
required 33 26 27 14 51 52; }; node_54(){ required 33 26 28 14; }; node_55(){
|
||||||
|
required 33 26 29 14; }; node_56(){ required 33 26 30 14; }; node_57(){
|
||||||
|
required 33 26 31; }; node_58(){
|
||||||
|
either 34 35 37 38 43 46 47 49 50 53 54 55 56 57; }; node_59(){ required 58; }
|
||||||
|
cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2
|
||||||
|
printf "%s\n" "${DOC:40:574}" >&2; exit 1; }'; unset var___help var___version \
|
||||||
|
var___env var___config var___stack var___user var__app_ var__app___path_ \
|
||||||
|
var__path_ var__args_ var__secret_ var__version_ var__cmd_ var__data_ \
|
||||||
|
var__host_ var__user_ var__port_ var_deploy var_upgrade var_logs var_multilogs \
|
||||||
|
var_cp var_run var_secret var_generate var_insert var_server var_add var_rm \
|
||||||
|
var_use var_init var_ls; parse 59 "$@"; local prefix=${DOCOPT_PREFIX:-''}
|
||||||
|
unset "${prefix}__help" "${prefix}__version" "${prefix}__env" \
|
||||||
|
"${prefix}__config" "${prefix}__stack" "${prefix}__user" "${prefix}_app_" \
|
||||||
|
"${prefix}_app___path_" "${prefix}_path_" "${prefix}_args_" \
|
||||||
|
"${prefix}_secret_" "${prefix}_version_" "${prefix}_cmd_" "${prefix}_data_" \
|
||||||
|
"${prefix}_host_" "${prefix}_user_" "${prefix}_port_" "${prefix}deploy" \
|
||||||
|
"${prefix}upgrade" "${prefix}logs" "${prefix}multilogs" "${prefix}cp" \
|
||||||
|
"${prefix}run" "${prefix}secret" "${prefix}generate" "${prefix}insert" \
|
||||||
|
"${prefix}server" "${prefix}add" "${prefix}rm" "${prefix}use" "${prefix}init" \
|
||||||
|
"${prefix}ls"; eval "${prefix}"'__help=${var___help:-false}'
|
||||||
eval "${prefix}"'__version=${var___version:-false}'
|
eval "${prefix}"'__version=${var___version:-false}'
|
||||||
eval "${prefix}"'_command_=${var__command_:-}'
|
eval "${prefix}"'__env=${var___env:-}'
|
||||||
|
eval "${prefix}"'__config=${var___config:-}'
|
||||||
|
eval "${prefix}"'__stack=${var___stack:-}'
|
||||||
|
eval "${prefix}"'__user=${var___user:-}'; eval "${prefix}"'_app_=${var__app_:-}'
|
||||||
|
eval "${prefix}"'_app___path_=${var__app___path_:-}'
|
||||||
|
eval "${prefix}"'_path_=${var__path_:-}'
|
||||||
if declare -p var__args_ >/dev/null 2>&1; then
|
if declare -p var__args_ >/dev/null 2>&1; then
|
||||||
eval "${prefix}"'_args_=("${var__args_[@]}")'; else eval "${prefix}"'_args_=()'
|
eval "${prefix}"'_args_=("${var__args_[@]}")'; else eval "${prefix}"'_args_=()'
|
||||||
fi; local docopt_i=1; [[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2
|
fi; eval "${prefix}"'_secret_=${var__secret_:-}'
|
||||||
for ((;docopt_i>0;docopt_i--)); do declare -p "${prefix}__config" \
|
eval "${prefix}"'_version_=${var__version_:-}'
|
||||||
"${prefix}__env" "${prefix}__help" "${prefix}__stack" "${prefix}__version" \
|
eval "${prefix}"'_cmd_=${var__cmd_:-}'; eval "${prefix}"'_data_=${var__data_:-}'
|
||||||
"${prefix}_command_" "${prefix}_args_"; done; }
|
eval "${prefix}"'_host_=${var__host_:-}'
|
||||||
|
eval "${prefix}"'_user_=${var__user_:-}'
|
||||||
|
eval "${prefix}"'_port_=${var__port_:-}'
|
||||||
|
eval "${prefix}"'deploy=${var_deploy:-false}'
|
||||||
|
eval "${prefix}"'upgrade=${var_upgrade:-false}'
|
||||||
|
eval "${prefix}"'logs=${var_logs:-false}'
|
||||||
|
eval "${prefix}"'multilogs=${var_multilogs:-false}'
|
||||||
|
eval "${prefix}"'cp=${var_cp:-false}'; eval "${prefix}"'run=${var_run:-false}'
|
||||||
|
eval "${prefix}"'secret=${var_secret:-false}'
|
||||||
|
eval "${prefix}"'generate=${var_generate:-false}'
|
||||||
|
eval "${prefix}"'insert=${var_insert:-false}'
|
||||||
|
eval "${prefix}"'server=${var_server:-false}'
|
||||||
|
eval "${prefix}"'add=${var_add:-false}'; eval "${prefix}"'rm=${var_rm:-false}'
|
||||||
|
eval "${prefix}"'use=${var_use:-false}'
|
||||||
|
eval "${prefix}"'init=${var_init:-false}'; eval "${prefix}"'ls=${var_ls:-false}'
|
||||||
|
local docopt_i=1; [[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2
|
||||||
|
for ((;docopt_i>0;docopt_i--)); do declare -p "${prefix}__help" \
|
||||||
|
"${prefix}__version" "${prefix}__env" "${prefix}__config" "${prefix}__stack" \
|
||||||
|
"${prefix}__user" "${prefix}_app_" "${prefix}_app___path_" "${prefix}_path_" \
|
||||||
|
"${prefix}_args_" "${prefix}_secret_" "${prefix}_version_" "${prefix}_cmd_" \
|
||||||
|
"${prefix}_data_" "${prefix}_host_" "${prefix}_user_" "${prefix}_port_" \
|
||||||
|
"${prefix}deploy" "${prefix}upgrade" "${prefix}logs" "${prefix}multilogs" \
|
||||||
|
"${prefix}cp" "${prefix}run" "${prefix}secret" "${prefix}generate" \
|
||||||
|
"${prefix}insert" "${prefix}server" "${prefix}add" "${prefix}rm" \
|
||||||
|
"${prefix}use" "${prefix}init" "${prefix}ls"; done; }
|
||||||
# docopt parser above, complete command for generating this parser is `docopt.sh abra`
|
# docopt parser above, complete command for generating this parser is `docopt.sh abra`
|
||||||
|
|
||||||
PROGRAM_NAME=$(basename "$0")
|
PROGRAM_NAME=$(basename "$0")
|
||||||
@ -173,423 +223,397 @@ PROGRAM_NAME=$(basename "$0")
|
|||||||
###### Utility functions
|
###### Utility functions
|
||||||
|
|
||||||
yml_pattern_exists() {
|
yml_pattern_exists() {
|
||||||
PATTERN=$1
|
PATTERN=$1
|
||||||
|
|
||||||
if [ -f "$ABRA_CONFIG" ]; then
|
if [ -f "$ABRA_CONFIG" ]; then
|
||||||
RESULT=$(yq read "$ABRA_CONFIG" "$PATTERN")
|
RESULT=$(yq read "$ABRA_CONFIG" "$PATTERN")
|
||||||
|
|
||||||
if [ "$RESULT" != 0 ]; then
|
if [ "$RESULT" != 0 ]; then
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
|
|
||||||
parse_subcommand() {
|
|
||||||
SUBCOMMAND="$1"
|
|
||||||
PREFIX=$2
|
|
||||||
|
|
||||||
if [ -n "$PREFIX" ]; then
|
|
||||||
PPREFIX="_$2"
|
|
||||||
SPREFIX="$2 "
|
|
||||||
fi
|
|
||||||
|
|
||||||
case $SUBCOMMAND in
|
|
||||||
"" | "-h" | "--help")
|
|
||||||
"sub${PPREFIX}_help"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
shift 2
|
|
||||||
"sub${PPREFIX}_${SUBCOMMAND}" "$@"
|
|
||||||
if [ $? = 127 ]; then
|
|
||||||
error "Error: '$SPREFIX$SUBCOMMAND' is not a known subcommand."
|
|
||||||
fi
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
}
|
}
|
||||||
|
|
||||||
error() {
|
error() {
|
||||||
echo "$(tput setaf 1)ERROR: $*$(tput sgr0)"
|
echo "$(tput setaf 1)ERROR: $*$(tput sgr0)"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
warning() {
|
warning() {
|
||||||
echo "$(tput setaf 3)WARNING: $*$(tput sgr0)"
|
echo "$(tput setaf 3)WARNING: $*$(tput sgr0)"
|
||||||
}
|
}
|
||||||
|
|
||||||
success() {
|
success() {
|
||||||
echo "$(tput setaf 2)$*$(tput sgr0)"
|
echo "$(tput setaf 2)$*$(tput sgr0)"
|
||||||
}
|
}
|
||||||
|
|
||||||
###### Default settings
|
###### Default settings
|
||||||
|
|
||||||
if [ -z "$COMPOSE_FILE" ]; then
|
if [ -z "$COMPOSE_FILE" ]; then
|
||||||
COMPOSE_FILE="compose.yml"
|
COMPOSE_FILE="compose.yml"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "$ABRA_STACK_DIR" ]; then
|
if [ -z "$ABRA_STACK_DIR" ]; then
|
||||||
ABRA_STACK_DIR="stacks/$SERVICE"
|
ABRA_STACK_DIR="stacks/$SERVICE"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
###### Safety checks
|
###### Safety checks
|
||||||
|
|
||||||
require_yq() {
|
require_yq() {
|
||||||
if ! type yq > /dev/null 2>&1; then
|
if ! type yq > /dev/null 2>&1; then
|
||||||
error "yq program is not installed"
|
error "yq program is not installed"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
require_multitail() {
|
require_multitail() {
|
||||||
if ! type multitail > /dev/null 2>&1; then
|
if ! type multitail > /dev/null 2>&1; then
|
||||||
error "multitail program is not installed"
|
error "multitail program is not installed"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
require_stack() {
|
require_stack() {
|
||||||
if [ -z "$STACK_NAME" ]; then
|
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() {
|
require_stack_dir() {
|
||||||
if [ -z "$ABRA_STACK_DIR" ] || [ ! -d "$ABRA_STACK_DIR" ]; then
|
if [ -z "$ABRA_STACK_DIR" ] || [ ! -d "$ABRA_STACK_DIR" ]; then
|
||||||
error "can't find \$ABRA_STACK_DIR '$ABRA_STACK_DIR'"
|
error "can't find \$ABRA_STACK_DIR '$ABRA_STACK_DIR'"
|
||||||
fi
|
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"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
###### Run-time loading
|
###### Run-time loading
|
||||||
|
|
||||||
load_custom_commands() {
|
load_custom_commands() {
|
||||||
if [ -f abra-commands.sh ]; then
|
if [ -f abra-commands.sh ]; then
|
||||||
# shellcheck disable=SC1091
|
# shellcheck disable=SC1091
|
||||||
source abra-commands.sh
|
source abra-commands.sh
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f "$ABRA_STACK_DIR/abra-commands.sh" ]; then
|
if [ -f "$ABRA_STACK_DIR/abra-commands.sh" ]; then
|
||||||
# shellcheck disable=SC1090
|
# shellcheck disable=SC1090
|
||||||
source "$ABRA_STACK_DIR/abra-commands.sh"
|
source "$ABRA_STACK_DIR/abra-commands.sh"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
load_context() {
|
load_context() {
|
||||||
# Load current context from env or Docker
|
# Load current context from env or Docker
|
||||||
if [ -z "$DOCKER_CONTEXT" ]; then
|
if [ -z "$DOCKER_CONTEXT" ]; then
|
||||||
warning "\$DOCKER_CONTEXT not set, (slowly) looking it up"
|
warning "\$DOCKER_CONTEXT not set, (slowly) looking it up"
|
||||||
# shellcheck disable=SC2063
|
# shellcheck disable=SC2063
|
||||||
DOCKER_CONTEXT=$(docker context ls | grep '*' | cut -d' ' -f1)
|
DOCKER_CONTEXT=$(docker context ls | grep '*' | cut -d' ' -f1)
|
||||||
# make sure grep doesn't parse this, we want a literal '*'
|
# make sure grep doesn't parse this, we want a literal '*'
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
###### Subcommand `secret`
|
###### Subcommand `secret`
|
||||||
|
|
||||||
sub_secret_insert() {
|
sub_secret_insert() {
|
||||||
require_stack
|
require_stack
|
||||||
load_context
|
load_context
|
||||||
|
|
||||||
SECRET=$1
|
SECRET="$abra__secret_"
|
||||||
VERSION=$2
|
VERSION="$abra__version_"
|
||||||
PW=$3
|
PW="$abra__data_"
|
||||||
|
|
||||||
if [ -z "$SECRET" ] || [ -z "$VERSION" ] || [ -z "$PW" ]; then
|
if [ -z "$SECRET" ] || [ -z "$VERSION" ] || [ -z "$PW" ]; then
|
||||||
error "Required arguments missing"
|
error "Required arguments missing"
|
||||||
echo
|
fi
|
||||||
echo "Usage: $PROGRAM_NAME secret insert SECRET VERSION PW"
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "$PW" | docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" - > /dev/null
|
echo "$PW" | docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" - > /dev/null
|
||||||
echo "$PW" | pass insert "hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}" -m > /dev/null
|
if type pass > /dev/null 2>&1; then
|
||||||
|
echo "$PW" | pass insert "hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}" -m > /dev/null
|
||||||
|
success "pass: hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
sub_secret_generate(){
|
sub_secret_generate(){
|
||||||
SECRET=$1
|
SECRET="$abra__secret_"
|
||||||
VERSION=$2
|
VERSION="$abra__version_"
|
||||||
PWGEN=${3:-pwqgen}
|
PWGEN=${abra__cmd_:-pwqgen}
|
||||||
|
|
||||||
if [ -z "$SECRET" ] || [ -z "$VERSION" ]; then
|
if [ -z "$SECRET" ] || [ -z "$VERSION" ]; then
|
||||||
error "Required arguments missing"
|
error "Required arguments missing"
|
||||||
echo
|
echo
|
||||||
echo "Usage: $PROGRAM_NAME secret generate SECRET VERSION [PW]"
|
echo "Usage: $PROGRAM_NAME secret generate SECRET VERSION [PW]"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
PW=$($PWGEN)
|
PW=$($PWGEN)
|
||||||
|
|
||||||
success "Password: $PW"
|
success "Password: $PW"
|
||||||
|
|
||||||
sub_secret_insert "$SECRET" "$VERSION" "$PW"
|
# FIXME 3wc: this is a little janky, might be better to make a
|
||||||
}
|
# util_secret_insert function which this and sub_secret_insert can call
|
||||||
|
abra__data_="$PW"
|
||||||
|
|
||||||
sub_secret() {
|
sub_secret_insert
|
||||||
SUBCOMMAND=$1
|
|
||||||
shift
|
|
||||||
# shellcheck disable=SC2068
|
|
||||||
parse_subcommand "$SUBCOMMAND" "secret" $@
|
|
||||||
}
|
}
|
||||||
|
|
||||||
###### Subcommand `run`
|
###### Subcommand `run`
|
||||||
|
|
||||||
sub_run(){
|
sub_run(){
|
||||||
require_stack
|
require_stack
|
||||||
|
|
||||||
if [ "$1" == "-u" ]; then
|
if [ -n "$abra___user" ]; then
|
||||||
RUN_USER="-u $2"
|
RUN_USER="-u $abra___user"
|
||||||
shift 2
|
fi
|
||||||
fi
|
|
||||||
|
|
||||||
SERVICE=$1
|
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
|
||||||
|
| grep "${STACK_NAME}_${abra__app_}" | head -n1 | cut -d',' -f1)
|
||||||
|
|
||||||
shift
|
if [ -z "$CONTAINER" ]; then
|
||||||
|
error "Can't find a container for ${STACK_NAME}_${abra__app_}"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
if [ -z "$SERVICE" ]; then
|
# shellcheck disable=SC2086
|
||||||
echo "Usage: $PROGRAM_NAME run [-u USER] SERVICE [CMD]"
|
docker exec $RUN_USER -it "$CONTAINER" "$@"
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
|
return
|
||||||
| grep "${STACK_NAME}_${SERVICE}" | head -n1 | cut -d',' -f1)
|
|
||||||
|
|
||||||
if [ -z "$CONTAINER" ]; then
|
|
||||||
error "Can't find a container for ${STACK_NAME}_${SERVICE}"
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
# shellcheck disable=SC2086
|
|
||||||
docker exec $RUN_USER -it "$CONTAINER" "$@"
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
###### Subcommand `deploy`
|
###### Subcommand `deploy`
|
||||||
|
|
||||||
sub_deploy (){
|
sub_deploy (){
|
||||||
require_stack
|
require_stack
|
||||||
require_stack_dir
|
require_stack_dir
|
||||||
load_context
|
load_context
|
||||||
|
|
||||||
echo "About to deploy:"
|
echo "About to deploy:"
|
||||||
echo " Context: $(tput setaf 4)${DOCKER_CONTEXT}$(tput sgr0)"
|
echo " Context: $(tput setaf 4)${DOCKER_CONTEXT}$(tput sgr0)"
|
||||||
if [ "${COMPOSE_FILE/:/}" == "${COMPOSE_FILE}" ]; then
|
if [ "${COMPOSE_FILE/:/}" == "${COMPOSE_FILE}" ]; then
|
||||||
echo " Compose: $(tput setaf 3)${ABRA_STACK_DIR}/${COMPOSE_FILE}$(tput sgr0)"
|
echo " Compose: $(tput setaf 3)${ABRA_STACK_DIR}/${COMPOSE_FILE}$(tput sgr0)"
|
||||||
else
|
else
|
||||||
echo " Compose: $(tput setaf 3)${ABRA_STACK_DIR}/"
|
echo " Compose: $(tput setaf 3)${ABRA_STACK_DIR}/"
|
||||||
IFS=':' read -ra COMPOSE_FILES <<< "$COMPOSE_FILE"
|
IFS=':' read -ra COMPOSE_FILES <<< "$COMPOSE_FILE"
|
||||||
for COMPOSE in "${COMPOSE_FILES[@]}"; do
|
for COMPOSE in "${COMPOSE_FILES[@]}"; do
|
||||||
echo " - ${COMPOSE}"
|
echo " - ${COMPOSE}"
|
||||||
done
|
done
|
||||||
tput sgr0
|
tput sgr0
|
||||||
fi
|
fi
|
||||||
if [ -n "$DOMAIN" ]; then
|
if [ -n "$DOMAIN" ]; then
|
||||||
echo " Domain: $(tput setaf 2)${DOMAIN}$(tput sgr0)"
|
echo " Domain: $(tput setaf 2)${DOMAIN}$(tput sgr0)"
|
||||||
fi
|
fi
|
||||||
echo " Stack: $(tput setaf 1)${STACK_NAME}$(tput sgr0)"
|
echo " Stack: $(tput setaf 1)${STACK_NAME}$(tput sgr0)"
|
||||||
|
|
||||||
read -rp "Continue? (y/[n])? " choice
|
read -rp "Continue? (y/[n])? " choice
|
||||||
|
|
||||||
case "$choice" in
|
case "$choice" in
|
||||||
y|Y ) ;;
|
y|Y ) ;;
|
||||||
n|N ) return;;
|
n|N ) return;;
|
||||||
* ) return;;
|
* ) return;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
(
|
(
|
||||||
cd "$ABRA_STACK_DIR" || error "\$ABRA_STACK_DIR '$ABRA_STACK_DIR' not found"
|
cd "$ABRA_STACK_DIR" || error "\$ABRA_STACK_DIR '$ABRA_STACK_DIR' not found"
|
||||||
# shellcheck disable=SC2086
|
# shellcheck disable=SC2086
|
||||||
if docker stack deploy -c ${COMPOSE_FILE/:/ -c } "$STACK_NAME"; then
|
if docker stack deploy -c ${COMPOSE_FILE/:/ -c } "$STACK_NAME"; then
|
||||||
if [ -n "$DOMAIN" ]; then
|
if [ -n "$DOMAIN" ]; then
|
||||||
success "Yay! App should be available at https://${DOMAIN}"
|
success "Yay! App should be available at https://${DOMAIN}"
|
||||||
else
|
else
|
||||||
success "Yay! That worked. No \$DOMAIN defined, check logs."
|
success "Yay! That worked. No \$DOMAIN defined, check logs."
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
error "Oh no! Something went wrong 😕 Check errors above"
|
error "Oh no! Something went wrong 😕 Check errors above"
|
||||||
fi
|
fi
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
###### Subcommand `logs`
|
###### Subcommand `logs`
|
||||||
|
|
||||||
# Inspired by https://github.com/moby/moby/issues/31458#issuecomment-475411564
|
# Inspired by https://github.com/moby/moby/issues/31458#issuecomment-475411564
|
||||||
sub_multilogs() {
|
sub_multilogs() {
|
||||||
require_stack
|
require_stack
|
||||||
require_multitail
|
require_multitail
|
||||||
|
|
||||||
# Get a list of the service names
|
# Get a list of the service names
|
||||||
SERVICES=$(docker stack services --format "{{.Name}}" "${STACK_NAME}")
|
SERVICES=$(docker stack services --format "{{.Name}}" "${STACK_NAME}")
|
||||||
# Sort the service names
|
# Sort the service names
|
||||||
SERVICES=$(echo "${SERVICES}" | sort)
|
SERVICES=$(echo "${SERVICES}" | sort)
|
||||||
# Create the command to run
|
# Create the command to run
|
||||||
COMMAND='multitail --mergeall'
|
COMMAND='multitail --mergeall'
|
||||||
for SERVICE in ${SERVICES}; do
|
for SERVICE in ${SERVICES}; do
|
||||||
COMMAND="${COMMAND} -L 'docker service logs --tail 20 -f ${SERVICE}'"
|
COMMAND="${COMMAND} -L 'docker service logs --tail 20 -f ${SERVICE}'"
|
||||||
done
|
done
|
||||||
# Run the command
|
# Run the command
|
||||||
bash -c "${COMMAND}"
|
bash -c "${COMMAND}"
|
||||||
}
|
}
|
||||||
|
|
||||||
sub_logs (){
|
sub_logs (){
|
||||||
require_stack
|
require_stack
|
||||||
|
|
||||||
SERVICE=$1
|
SERVICE=$1
|
||||||
|
|
||||||
if [ -z "$SERVICE" ]; then
|
if [ -z "$SERVICE" ]; then
|
||||||
warning "No \$SERVICE provided, running multilogs"
|
warning "No \$SERVICE provided, running multilogs"
|
||||||
sub_multilogs
|
sub_multilogs
|
||||||
fi
|
fi
|
||||||
|
|
||||||
shift
|
shift
|
||||||
|
|
||||||
if [ $# -eq 0 ]; then
|
if [ $# -eq 0 ]; then
|
||||||
LOGS_ARGS="\
|
LOGS_ARGS="\
|
||||||
--follow \
|
--follow \
|
||||||
--tail 20 \
|
--tail 20 \
|
||||||
--no-trunc \
|
--no-trunc \
|
||||||
--details \
|
--details \
|
||||||
--timestamps"
|
--timestamps"
|
||||||
else
|
else
|
||||||
# shellcheck disable=SC2124
|
# shellcheck disable=SC2124
|
||||||
LOGS_ARGS=$@
|
LOGS_ARGS=$@
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# shellcheck disable=SC2086
|
# shellcheck disable=SC2086
|
||||||
docker service logs "${STACK_NAME}_${SERVICE}" $LOGS_ARGS
|
docker service logs "${STACK_NAME}_${SERVICE}" $LOGS_ARGS
|
||||||
}
|
}
|
||||||
|
|
||||||
###### Subcommand `cp`
|
###### Subcommand `cp`
|
||||||
|
|
||||||
sub_cp() {
|
sub_cp() {
|
||||||
require_stack
|
require_stack
|
||||||
|
|
||||||
SOURCE=$1
|
SOURCE=$1
|
||||||
DEST=$2
|
DEST=$2
|
||||||
|
|
||||||
SERVICE=$(echo "$SOURCE" | grep -o '^[^:]\+:' || echo "$DEST" | grep -o '^[^:]\+:')
|
SERVICE=$(echo "$SOURCE" | grep -o '^[^:]\+:' || echo "$DEST" | grep -o '^[^:]\+:')
|
||||||
SERVICE=$(echo "$SERVICE" | tr -d ':')
|
SERVICE=$(echo "$SERVICE" | tr -d ':')
|
||||||
|
|
||||||
if [ -z "$SERVICE" ]; then
|
if [ -z "$SERVICE" ]; then
|
||||||
echo "Usage: $PROGRAM_NAME cp SERVICE:SRC_PATH DEST_PATH"
|
echo "Usage: $PROGRAM_NAME cp SERVICE:SRC_PATH DEST_PATH"
|
||||||
echo " $PROGRAM_NAME cp SRC_PATH SERVICE:DEST_PATH"
|
echo " $PROGRAM_NAME cp SRC_PATH SERVICE:DEST_PATH"
|
||||||
echo ""
|
echo ""
|
||||||
error "Can't find SERVICE in either SRC or DEST"
|
error "Can't find SERVICE in either SRC or DEST"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
|
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
|
||||||
| 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 container for ${STACK_NAME}_${SERVICE}"
|
error "Can't find a container for ${STACK_NAME}_${SERVICE}"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
CP_ARGS=$(echo "$SOURCE $DEST" | sed "s/$SERVICE:/$CONTAINER:/")
|
CP_ARGS=$(echo "$SOURCE $DEST" | sed "s/$SERVICE:/$CONTAINER:/")
|
||||||
|
|
||||||
# shellcheck disable=SC2086
|
# shellcheck disable=SC2086
|
||||||
docker cp ${CP_ARGS}
|
docker cp ${CP_ARGS}
|
||||||
}
|
}
|
||||||
|
|
||||||
###### Subcommand `context`
|
###### Subcommand `context`
|
||||||
|
|
||||||
sub_server_init() {
|
sub_server_init() {
|
||||||
export DOCKER_CONTEXT="$1"
|
export DOCKER_CONTEXT="$1"
|
||||||
|
|
||||||
if [ -n "$DOCKER_CONTEXT" ]; then
|
if [ -n "$DOCKER_CONTEXT" ]; then
|
||||||
load_context
|
load_context
|
||||||
fi
|
fi
|
||||||
|
|
||||||
docker swarm init || true
|
docker swarm init || true
|
||||||
docker network create --driver=overlay proxy --scope swarm || true
|
docker network create --driver=overlay proxy --scope swarm || true
|
||||||
}
|
}
|
||||||
|
|
||||||
sub_server_add() {
|
sub_server_add() {
|
||||||
HOST="$1"
|
HOST="$abra__host_"
|
||||||
USERNAME="$2"
|
USERNAME="$abra__user_"
|
||||||
PORT="$3"
|
PORT="$abra__port_"
|
||||||
|
|
||||||
if [ -n "$PORT" ]; then
|
if [ -n "$PORT" ]; then
|
||||||
PORT=":$PORT"
|
PORT=":$PORT"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -n "$USERNAME" ]; then
|
if [ -n "$USERNAME" ]; then
|
||||||
USERNAME="$USERNAME@"
|
USERNAME="$USERNAME@"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
docker context create "$HOST" \
|
docker context create "$HOST" \
|
||||||
--docker "host=ssh://$USERNAME$HOST$PORT"
|
--docker "host=ssh://$USERNAME$HOST$PORT"
|
||||||
}
|
}
|
||||||
|
|
||||||
sub_server_rm() {
|
sub_server_rm() {
|
||||||
HOST="$1"
|
docker context rm "$abra__host_"
|
||||||
docker context rm "$HOST"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub_server_use() {
|
sub_server_use() {
|
||||||
docker context use "$1"
|
docker context use "$abra__host_"
|
||||||
}
|
}
|
||||||
|
|
||||||
sub_server_ls() {
|
sub_server_ls() {
|
||||||
docker context ls
|
docker context ls
|
||||||
}
|
}
|
||||||
|
|
||||||
sub_server() {
|
|
||||||
SUBCOMMAND2=$1
|
|
||||||
shift
|
|
||||||
# shellcheck disable=SC2068
|
|
||||||
parse_subcommand "$SUBCOMMAND2" "server" $@
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
###### Subcommand `upgrade`
|
###### Subcommand `upgrade`
|
||||||
|
|
||||||
sub_upgrade() {
|
sub_upgrade() {
|
||||||
curl -fsSL https://install.abra.autonomic.zone | bash
|
curl -fsSL https://install.abra.autonomic.zone | bash
|
||||||
}
|
}
|
||||||
|
|
||||||
###### Docker alias subcommands
|
###### Docker alias subcommands
|
||||||
|
|
||||||
sub_stack() {
|
sub_stack() {
|
||||||
# shellcheck disable=SC2068
|
# shellcheck disable=SC2068
|
||||||
docker stack $@
|
docker stack $@
|
||||||
}
|
}
|
||||||
|
|
||||||
sub_volume() {
|
sub_volume() {
|
||||||
# shellcheck disable=SC2068
|
# shellcheck disable=SC2068
|
||||||
docker volume $@
|
docker volume $@
|
||||||
}
|
}
|
||||||
|
|
||||||
###### Main
|
###### Main
|
||||||
|
|
||||||
abra() {
|
abra() {
|
||||||
declare abra___stack abra___env abra___config abra__command_ abra__args_
|
# TODO (3wc): we either need to do this, or add s'hellcheck disable' all over
|
||||||
|
# the place to handle the dynamically-defined vars
|
||||||
|
declare abra___stack abra___env abra___config abra__command_ abra__args_ \
|
||||||
|
abra__secret_ abra__version_ abra__data_ abra___user abra__host_ \
|
||||||
|
abra__app_ abra__port_ abra__user_
|
||||||
|
|
||||||
DOCOPT_PREFIX=abra_
|
DOCOPT_PREFIX=abra_
|
||||||
eval "$(docopt "$@")"
|
eval "$(docopt "$@")"
|
||||||
|
|
||||||
STACK_NAME=$abra___stack
|
STACK_NAME=$abra___stack
|
||||||
|
|
||||||
if [ -n "$abra___env" ]; then
|
if [ -n "$abra___env" ]; then
|
||||||
# shellcheck disable=SC1090
|
# shellcheck disable=SC1090
|
||||||
source "$abra___env" || error "Unable to load env from '$abra___env'"
|
source "$abra___env" || error "Unable to load env from '$abra___env'"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -n "$abra___config" ]; then
|
if [ -n "$abra___config" ]; then
|
||||||
require_yq
|
require_yq
|
||||||
|
|
||||||
if yml_pattern_exists stack_name; then
|
if yml_pattern_exists stack_name; then
|
||||||
STACK_NAME=$(yq read "$ABRA_CONFIG" stack_name)
|
STACK_NAME=$(yq read "$ABRA_CONFIG" stack_name)
|
||||||
fi
|
fi
|
||||||
# FIXME load other variables somehow
|
# FIXME load other variables somehow
|
||||||
fi
|
fi
|
||||||
|
|
||||||
load_custom_commands
|
load_custom_commands
|
||||||
|
|
||||||
# shellcheck disable=SC2086
|
# Search for sub_* functions, and check if any of them matches enabled
|
||||||
"sub_${abra__command_}" ${abra__args_[*]}
|
# arguments (i.e. is a command and is specified)
|
||||||
|
for SUBCOMMAND in $(declare -Ff | grep 'sub_' | cut -d' ' -f3 | sort -r); do
|
||||||
|
IFS='_' read -r -a PARTS <<< "$SUBCOMMAND"
|
||||||
|
for PART in "${PARTS[@]:1}"; do
|
||||||
|
VAR=$(eval "echo \$abra_$PART")
|
||||||
|
if [ ! "$VAR" == "true" ]; then
|
||||||
|
continue 2
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
abra__command_=$(IFS="_"; echo "${PARTS[*]:1}")
|
||||||
|
break
|
||||||
|
done
|
||||||
|
|
||||||
|
# Fallback for non-docopt-defined options
|
||||||
|
# shellcheck disable=SC2086
|
||||||
|
"sub_${abra__command_}" ${abra__args_[*]}
|
||||||
}
|
}
|
||||||
|
|
||||||
abra "$@"
|
abra "$@"
|
||||||
|
Reference in New Issue
Block a user