Add undeploy/delete commands

Ref #4
This commit is contained in:
3wc 2020-10-28 01:40:35 +02:00
parent b6b80298c2
commit 8d139d4d28

159
abra
View File

@ -14,6 +14,8 @@ Usage:
abra [options] app new [--server=<server> --domain=<domain>] <app>
abra [options] app (list|ls)
abra [options] app <domain> deploy
abra [options] app <domain> undeploy
abra [options] app <domain> (delete|rm)
abra [options] app <domain> logs [<service>]
abra [options] app <domain> multilogs
abra [options] app <domain> cp <src> <dst>
@ -138,10 +140,10 @@ 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() {
printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() {
[[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() {
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1146}
usage=${DOC:40:838}; digest=3ff34; shorts=(-e -s -v -h '' '' '')
longs=(--env --stack --version --help --server --domain --user)
argcounts=(1 1 0 0 1 1 1); node_0(){ value __env 0; }; node_1(){ value __stack 1
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1227}
usage=${DOC:40:919}; digest=40db2; shorts=(-s -e -v -h '' '' '')
longs=(--stack --env --version --help --server --domain --user)
argcounts=(1 1 0 0 1 1 1); node_0(){ value __stack 0; }; node_1(){ value __env 1
}; node_2(){ switch __version 2; }; node_3(){ switch __help 3; }; node_4(){
value __server 4; }; node_5(){ value __domain 5; }; node_6(){ value __user 6; }
node_7(){ value _app_ a; }; node_8(){ value _domain_ a; }; node_9(){
@ -151,47 +153,51 @@ value _version_ a; }; node_15(){ value _cmd_ a; }; node_16(){ value _data_ a; }
node_17(){ value _host_ a; }; node_18(){ value _user_ a; }; node_19(){
value _port_ a; }; node_20(){ value _command_ a; }; node_21(){ _command app; }
node_22(){ _command new; }; node_23(){ _command list; }; node_24(){ _command ls
}; node_25(){ _command deploy; }; node_26(){ _command logs; }; node_27(){
_command multilogs; }; node_28(){ _command cp; }; node_29(){ _command run; }
node_30(){ _command secret; }; node_31(){ _command auto; }; node_32(){
_command generate; }; node_33(){ _command insert; }; node_34(){ _command server
}; node_35(){ _command add; }; node_36(){ _command rm; }; node_37(){
_command use; }; node_38(){ _command init; }; node_39(){ _command upgrade; }
node_40(){ optional 0 1 2 3; }; node_41(){ optional 40; }; node_42(){
optional 4 5; }; node_43(){ required 41 21 22 42 7; }; node_44(){ either 23 24
}; node_45(){ required 44; }; node_46(){ required 41 21 45; }; node_47(){
required 41 21 8 25; }; node_48(){ optional 9; }; node_49(){
required 41 21 8 26 48; }; node_50(){ required 41 21 8 27; }; node_51(){
required 41 21 8 28 10 11; }; node_52(){ optional 6; }; node_53(){ oneormore 12
}; node_54(){ required 41 21 8 29 52 9 53; }; node_55(){
required 41 21 8 29 9 53; }; node_56(){ required 41 21 8 30 31; }; node_57(){
optional 15; }; node_58(){ required 41 21 8 30 32 13 14 57; }; node_59(){
required 41 21 8 30 33 13 14 16; }; node_60(){ optional 18; }; node_61(){
optional 19; }; node_62(){ required 41 34 35 17 60 61; }; node_63(){
required 41 34 45; }; node_64(){ required 41 34 36 17; }; node_65(){
required 41 34 37 17; }; node_66(){ required 41 34 38 17; }; node_67(){
required 41 39; }; node_68(){ required 41 20 53; }; node_69(){
either 43 46 47 49 50 51 54 55 56 58 59 62 63 64 65 66 67 68; }; node_70(){
required 69; }; cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2
printf "%s\n" "${DOC:40:838}" >&2; exit 1; }'; unset var___env var___stack \
var___version var___help var___server var___domain var___user var__app_ \
var__domain_ var__service_ var__src_ var__dst_ var__args_ var__secret_ \
var__version_ var__cmd_ var__data_ var__host_ var__user_ var__port_ \
var__command_ var_app var_new var_list var_ls var_deploy var_logs \
var_multilogs var_cp var_run var_secret var_auto var_generate var_insert \
var_server var_add var_rm var_use var_init var_upgrade; parse 70 "$@"
local prefix=${DOCOPT_PREFIX:-''}; unset "${prefix}__env" "${prefix}__stack" \
"${prefix}__version" "${prefix}__help" "${prefix}__server" "${prefix}__domain" \
"${prefix}__user" "${prefix}_app_" "${prefix}_domain_" "${prefix}_service_" \
"${prefix}_src_" "${prefix}_dst_" "${prefix}_args_" "${prefix}_secret_" \
"${prefix}_version_" "${prefix}_cmd_" "${prefix}_data_" "${prefix}_host_" \
"${prefix}_user_" "${prefix}_port_" "${prefix}_command_" "${prefix}app" \
"${prefix}new" "${prefix}list" "${prefix}ls" "${prefix}deploy" "${prefix}logs" \
"${prefix}multilogs" "${prefix}cp" "${prefix}run" "${prefix}secret" \
"${prefix}auto" "${prefix}generate" "${prefix}insert" "${prefix}server" \
"${prefix}add" "${prefix}rm" "${prefix}use" "${prefix}init" "${prefix}upgrade"
eval "${prefix}"'__env=${var___env:-}'
}; node_25(){ _command deploy; }; node_26(){ _command undeploy; }; node_27(){
_command delete; }; node_28(){ _command rm; }; node_29(){ _command logs; }
node_30(){ _command multilogs; }; node_31(){ _command cp; }; node_32(){
_command run; }; node_33(){ _command secret; }; node_34(){ _command auto; }
node_35(){ _command generate; }; node_36(){ _command insert; }; node_37(){
_command server; }; node_38(){ _command add; }; node_39(){ _command use; }
node_40(){ _command init; }; node_41(){ _command upgrade; }; node_42(){
optional 0 1 2 3; }; node_43(){ optional 42; }; node_44(){ optional 4 5; }
node_45(){ required 43 21 22 44 7; }; node_46(){ either 23 24; }; node_47(){
required 46; }; node_48(){ required 43 21 47; }; node_49(){ required 43 21 8 25
}; node_50(){ required 43 21 8 26; }; node_51(){ either 27 28; }; node_52(){
required 51; }; node_53(){ required 43 21 8 52; }; node_54(){ optional 9; }
node_55(){ required 43 21 8 29 54; }; node_56(){ required 43 21 8 30; }
node_57(){ required 43 21 8 31 10 11; }; node_58(){ optional 6; }; node_59(){
oneormore 12; }; node_60(){ required 43 21 8 32 58 9 59; }; node_61(){
required 43 21 8 32 9 59; }; node_62(){ required 43 21 8 33 34; }; node_63(){
optional 15; }; node_64(){ required 43 21 8 33 35 13 14 63; }; node_65(){
required 43 21 8 33 36 13 14 16; }; node_66(){ optional 18; }; node_67(){
optional 19; }; node_68(){ required 43 37 38 17 66 67; }; node_69(){
required 43 37 47; }; node_70(){ required 43 37 28 17; }; node_71(){
required 43 37 39 17; }; node_72(){ required 43 37 40 17; }; node_73(){
required 43 41; }; node_74(){ required 43 20 59; }; node_75(){
either 45 48 49 50 53 55 56 57 60 61 62 64 65 68 69 70 71 72 73 74; }
node_76(){ required 75; }; cat <<<' docopt_exit() {
[[ -n $1 ]] && printf "%s\n" "$1" >&2; printf "%s\n" "${DOC:40:919}" >&2; exit 1
}'; unset var___stack var___env var___version var___help var___server \
var___domain var___user var__app_ var__domain_ var__service_ var__src_ \
var__dst_ var__args_ var__secret_ var__version_ var__cmd_ var__data_ \
var__host_ var__user_ var__port_ var__command_ var_app var_new var_list var_ls \
var_deploy var_undeploy var_delete var_rm var_logs var_multilogs var_cp \
var_run var_secret var_auto var_generate var_insert var_server var_add var_use \
var_init var_upgrade; parse 76 "$@"; local prefix=${DOCOPT_PREFIX:-''}
unset "${prefix}__stack" "${prefix}__env" "${prefix}__version" \
"${prefix}__help" "${prefix}__server" "${prefix}__domain" "${prefix}__user" \
"${prefix}_app_" "${prefix}_domain_" "${prefix}_service_" "${prefix}_src_" \
"${prefix}_dst_" "${prefix}_args_" "${prefix}_secret_" "${prefix}_version_" \
"${prefix}_cmd_" "${prefix}_data_" "${prefix}_host_" "${prefix}_user_" \
"${prefix}_port_" "${prefix}_command_" "${prefix}app" "${prefix}new" \
"${prefix}list" "${prefix}ls" "${prefix}deploy" "${prefix}undeploy" \
"${prefix}delete" "${prefix}rm" "${prefix}logs" "${prefix}multilogs" \
"${prefix}cp" "${prefix}run" "${prefix}secret" "${prefix}auto" \
"${prefix}generate" "${prefix}insert" "${prefix}server" "${prefix}add" \
"${prefix}use" "${prefix}init" "${prefix}upgrade"
eval "${prefix}"'__stack=${var___stack:-}'
eval "${prefix}"'__env=${var___env:-}'
eval "${prefix}"'__version=${var___version:-false}'
eval "${prefix}"'__help=${var___help:-false}'
eval "${prefix}"'__server=${var___server:-}'
@ -212,7 +218,9 @@ eval "${prefix}"'_command_=${var__command_:-}'
eval "${prefix}"'app=${var_app:-false}'; eval "${prefix}"'new=${var_new:-false}'
eval "${prefix}"'list=${var_list:-false}'; eval "${prefix}"'ls=${var_ls:-false}'
eval "${prefix}"'deploy=${var_deploy:-false}'
eval "${prefix}"'logs=${var_logs:-false}'
eval "${prefix}"'undeploy=${var_undeploy:-false}'
eval "${prefix}"'delete=${var_delete:-false}'
eval "${prefix}"'rm=${var_rm:-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}'
@ -220,22 +228,21 @@ eval "${prefix}"'auto=${var_auto:-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}"'add=${var_add:-false}'; eval "${prefix}"'use=${var_use:-false}'
eval "${prefix}"'init=${var_init:-false}'
eval "${prefix}"'upgrade=${var_upgrade:-false}'; local docopt_i=1
[[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2; for ((;docopt_i>0;docopt_i--)); do
declare -p "${prefix}__env" "${prefix}__stack" "${prefix}__version" \
declare -p "${prefix}__stack" "${prefix}__env" "${prefix}__version" \
"${prefix}__help" "${prefix}__server" "${prefix}__domain" "${prefix}__user" \
"${prefix}_app_" "${prefix}_domain_" "${prefix}_service_" "${prefix}_src_" \
"${prefix}_dst_" "${prefix}_args_" "${prefix}_secret_" "${prefix}_version_" \
"${prefix}_cmd_" "${prefix}_data_" "${prefix}_host_" "${prefix}_user_" \
"${prefix}_port_" "${prefix}_command_" "${prefix}app" "${prefix}new" \
"${prefix}list" "${prefix}ls" "${prefix}deploy" "${prefix}logs" \
"${prefix}multilogs" "${prefix}cp" "${prefix}run" "${prefix}secret" \
"${prefix}auto" "${prefix}generate" "${prefix}insert" "${prefix}server" \
"${prefix}add" "${prefix}rm" "${prefix}use" "${prefix}init" "${prefix}upgrade"
done; }
"${prefix}list" "${prefix}ls" "${prefix}deploy" "${prefix}undeploy" \
"${prefix}delete" "${prefix}rm" "${prefix}logs" "${prefix}multilogs" \
"${prefix}cp" "${prefix}run" "${prefix}secret" "${prefix}auto" \
"${prefix}generate" "${prefix}insert" "${prefix}server" "${prefix}add" \
"${prefix}use" "${prefix}init" "${prefix}upgrade"; done; }
# docopt parser above, complete command for generating this parser is `docopt.sh abra`
PROGRAM_NAME=$(basename "$0")
@ -311,14 +318,22 @@ get_servers() {
}
get_app_secrets() {
# FIXME 3wc: requires bash 4, use for loop instead
mapfile -t SECRETS < <(grep PASSWORD "$ENV_FILE" | cut -d' ' -f2)
}
load_instance() {
DOMAIN="$abra__domain_"
ENV_FILE=$(find ~/.abra -name "$DOMAIN.env")
if [ "$(echo "$ENV_FILE" | wc -l)" -gt 1 ]; then
error "Found $DOMAIN in multiple servers: $ENV_FILE"
# FIXME 3wc: requires bash 4, use for loop instead
mapfile -t ENV_FILES < <(find -L ~/.abra -name "$DOMAIN.env")
case "${#ENV_FILES[@]}" in
1 ) ;;
0 ) error "Can't find app '$DOMAIN'"; return;;
* ) error "Found $DOMAIN in multiple servers: ${ENV_FILES[*]}"; return;;
esac
ENV_FILE="${ENV_FILES[0]}"
if [ ! -f "$ENV_FILE" ]; then
error "Can't open ENV_FILE '$ENV_FILE'"
fi
IFS='/' read -r -a PARTS <<< "$ENV_FILE"
SERVER="${PARTS[-2]}"
@ -449,7 +464,7 @@ sub_app_new (){
echo " $(tput setaf 3)Customise the configuration:"
echo " $ENV_FILE$(tput sgr0)"
echo " $(tput setaf 2)Deploy it:"
echo " abra deploy $DOMAIN$(tput sgr0)"
echo " abra app deploy $DOMAIN$(tput sgr0)"
}
###### .. app <domain> deploy
@ -497,6 +512,32 @@ sub_app_deploy (){
)
}
###### .. app <domain> undeploy
sub_app_undeploy (){
load_instance
load_instance_env
warning "Un-deploy $DOMAIN from $SERVER?"
read -rp "Enter to continue, Ctrl+C to quit"
docker stack rm "$STACK_NAME"
}
###### .. app <domain> delete
sub_app_rm (){
sub_app_delete
}
sub_app_delete (){
load_instance
load_instance_env
warning "Delete $ENV_FILE?"
read -rp "Enter to continue, Ctrl+C to quit"
rm "$ENV_FILE"
}
###### .. app <domain> secret insert
sub_app_secret_insert() {
load_instance
@ -676,6 +717,8 @@ sub_server_init() {
}
sub_server_add() {
require_abra_dir
HOST="$abra__host_"
USERNAME="$abra__user_"
PORT="$abra__port_"
@ -690,6 +733,8 @@ sub_server_add() {
docker context create "$HOST" \
--docker "host=ssh://$USERNAME$HOST$PORT"
mkdir -p "$ABRA_DIR/$HOST"
}
sub_server_rm() {