forked from toolshed/abra
Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
92c91ddbb0 | |||
cff9b13f60 | |||
0444991636 | |||
28ba33b18e | |||
77eb83b128 | |||
ff7fcf2201 | |||
b0d525a980 | |||
aa1ffd5d8a | |||
5627e67bf7 | |||
29343369f3 | |||
427ed97678 | |||
b01fee3c86 | |||
949246821f | |||
60f2892acd | |||
0268685cfa | |||
dd4f31d9a1 | |||
8a19bb059c | |||
3a1f4e7bf6 | |||
a065f5f2a6 | |||
3d47cf97c0 | |||
e052aa2b27 | |||
9660f32b84 | |||
32cef2af68 |
@ -7,7 +7,7 @@ steps:
|
||||
commands:
|
||||
- apt update
|
||||
- apt install -y shellcheck
|
||||
- shellcheck abra
|
||||
- shellcheck abra script.d/*
|
||||
trigger:
|
||||
branch:
|
||||
- main
|
||||
|
13
CHANGELOG.md
13
CHANGELOG.md
@ -1,3 +1,16 @@
|
||||
# abra 0.3.0 (2020-09-27)
|
||||
|
||||
- Add multilogs stack logs implementation ([#8](https://git.autonomic.zone/compose-stacks/abra/issues/8)
|
||||
- Add beginnings of "monorepo" functionality
|
||||
|
||||
# abra 0.2.0 (2020-09-24)
|
||||
|
||||
- Prepare for swarm install script using script.d ([#12](https://git.autonomic.zone/compose-stacks/planning/issues/12))
|
||||
|
||||
# abra 0.1.2 (2020-09-22)
|
||||
|
||||
- Add upgrade command ([#10](https://git.autonomic.zone/autonomic-cooperative/abra/issues/10))
|
||||
|
||||
# abra 0.1.1 (2020-09-22)
|
||||
|
||||
- Add installer script ([#9](https://git.autonomic.zone/autonomic-cooperative/abra/issues/9))
|
||||
|
29
README.md
29
README.md
@ -7,7 +7,7 @@ Docker stack magic 🎩🐇
|
||||
## Install
|
||||
|
||||
```sh
|
||||
curl https://install.abra.autonomic.zone | bash
|
||||
curl -fsSL https://install.abra.autonomic.zone | bash
|
||||
```
|
||||
|
||||
Specific releases are available via the project [release page](https://git.autonomic.zone/autonomic-cooperative/abra/releases).
|
||||
@ -24,6 +24,33 @@ cd abra
|
||||
make dev_install
|
||||
```
|
||||
|
||||
See [autonomic-cooperative/installer-scripts](https://git.autonomic.zone/autonomic-cooperative/installer-scripts) for the installer script deployment. To make a release, just add an entry to [CHANGELOG.md](./CHANGELOG.md) (following [semver](https://semver.org/) please) and then `git tag x.x.x && git push origin main --tags`. If you want the [installer-scripts](https://git.autonomic.zone/autonomic-cooperative/installer-scripts) deployment to pick that up, you'll need to change the version number in the [Makefile](https://git.autonomic.zone/autonomic-cooperative/installer-scripts/src/branch/main/Makefile) and run `make` in that repository and push the changes.
|
||||
|
||||
## Specify what to deploy where
|
||||
|
||||
You can use `abra` in one of 2 ways:
|
||||
|
||||
1. Clone a `compose-stack`, create an `.envrc` in it, and run `abra` in that
|
||||
directory. Be sure to set `ABRA_STACK_DIR=.`
|
||||
2. "Monorepo mode": keep all your `compose-stack`s in one directory and all your `env` files in
|
||||
another, e.g.:
|
||||
```
|
||||
$ tree
|
||||
.
|
||||
├── apps
|
||||
│ ├── mediawiki.demo.autonomic.zone.env
|
||||
│ ├── wordpress.demo.autonomic.zone.env
|
||||
└── stacks
|
||||
├── gitea
|
||||
├── matrix-synapse
|
||||
├── mediawiki
|
||||
├── nextcloud
|
||||
├── swarmpit
|
||||
├── traefik
|
||||
└── wordpress
|
||||
$ abra -e apps/mediawiki.demo.autonomic.zone.env deploy
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
- `abra run mariadb mysqldump gitea -p'GdIbMeS09SURRktBnm3jcTufsL5z0MPd' | gzip > ../git.autonomic.zone_mariadb_`date +%F`.sql.gz`
|
||||
|
291
abra
291
abra
@ -1,22 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
PROGRAM_NAME=$(basename "$0")
|
||||
ABRA_CONFIG=abra.yml
|
||||
|
||||
if [ -z "$COMPOSE_FILE" ]; then
|
||||
COMPOSE_FILE="compose.yml"
|
||||
fi
|
||||
###### Utility functions
|
||||
|
||||
yml_pattern_exists() {
|
||||
PATTERN=$1
|
||||
|
||||
if ! type yq > /dev/null 2>&1; then
|
||||
echo "$(tput setaf 1)ERROR: yq program is not installed$(tput sgr0)"
|
||||
exit
|
||||
fi
|
||||
|
||||
if [ -f $ABRA_CONFIG ]; then
|
||||
RESULT=$(yq read $ABRA_CONFIG "$PATTERN")
|
||||
if [ -f "$ABRA_CONFIG" ]; then
|
||||
RESULT=$(yq read "$ABRA_CONFIG" "$PATTERN")
|
||||
|
||||
if [ "$RESULT" != 0 ]; then
|
||||
return 0
|
||||
@ -26,69 +18,196 @@ yml_pattern_exists() {
|
||||
return 1
|
||||
}
|
||||
|
||||
parse_subcommand() {
|
||||
SUBCOMMAND="$1"
|
||||
PREFIX=$2
|
||||
|
||||
if [ -n "$PREFIX" ]; then
|
||||
PPREFIX="_$2"
|
||||
SPREFIX="$2 "
|
||||
SSPREFIX=" $2"
|
||||
fi
|
||||
|
||||
case $SUBCOMMAND in
|
||||
"" | "-h" | "--help")
|
||||
"sub${PPREFIX}_help"
|
||||
;;
|
||||
*)
|
||||
shift 2
|
||||
"sub${PPREFIX}_${SUBCOMMAND}" "$@"
|
||||
if [ $? = 127 ]; then
|
||||
echo "Error: '$SPREFIX$SUBCOMMAND' is not a known subcommand." >&2
|
||||
echo " Run '$PROGRAM_NAME$SSPREFIX --help' for a list of known subcommands." >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
error() {
|
||||
echo "$(tput setaf 1)ERROR: $*$(tput sgr0)"
|
||||
exit 1
|
||||
}
|
||||
|
||||
warning() {
|
||||
echo "$(tput setaf 3)WARNING: $*$(tput sgr0)"
|
||||
}
|
||||
|
||||
success() {
|
||||
echo "$(tput setaf 2)$*$(tput sgr0)"
|
||||
}
|
||||
|
||||
###### Top-level arguments
|
||||
|
||||
ABRA_CONFIG=abra.yml
|
||||
if [ "$1" == "-c" ]; then
|
||||
ABRA_CONFIG=$2
|
||||
shift 2
|
||||
fi
|
||||
|
||||
if [ "$1" == "-e" ]; then
|
||||
ABRA_ENV=$2
|
||||
shift 2
|
||||
fi
|
||||
|
||||
if [ "$1" == "-a" ]; then
|
||||
STACK_NAME=$2
|
||||
shift 2
|
||||
fi
|
||||
|
||||
if [ -f abra.yml ]; then
|
||||
###### Load config
|
||||
|
||||
if [ -f "$ABRA_CONFIG" ]; then
|
||||
require_yq
|
||||
|
||||
if yml_pattern_exists stack_name; then
|
||||
STACK_NAME=$(yq read abra.yml stack_name)
|
||||
STACK_NAME=$(yq read "$ABRA_CONFIG" stack_name)
|
||||
fi
|
||||
# FIXME load other variables somehow
|
||||
fi
|
||||
|
||||
if [ -z "$STACK_NAME" ]; then
|
||||
echo "$(tput setaf 1)ERROR: \$STACK_NAME must be set (e.g. export STACK_NAME=my_cool_app)$(tput sgr0 )"
|
||||
exit
|
||||
if [ -n "$ABRA_ENV" ]; then
|
||||
# shellcheck disable=SC1090
|
||||
source "$ABRA_ENV" || error "Unable to load env from '$ABRA_ENV'"
|
||||
fi
|
||||
|
||||
if type direnv > /dev/null 2>&1 && ! direnv status | grep -q 'Found RC allowed true'; then
|
||||
echo "$(tput setaf 1)ERROR: direnv is blocked, run direnv allow$(tput sgr0)"
|
||||
exit
|
||||
###### Default settings
|
||||
|
||||
if [ -z "$COMPOSE_FILE" ]; then
|
||||
COMPOSE_FILE="compose.yml"
|
||||
fi
|
||||
|
||||
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"
|
||||
# shellcheck disable=SC2063
|
||||
DOCKER_CONTEXT=$(docker context ls | grep '*' | cut -d' ' -f1)
|
||||
# FIXME 3wc: make sure grep doesn't parse this, we're want a literal '*'
|
||||
fi
|
||||
}
|
||||
|
||||
###### Safety checks
|
||||
|
||||
require_yq() {
|
||||
if ! type yq > /dev/null 2>&1; then
|
||||
error "yq program is not installed"
|
||||
fi
|
||||
}
|
||||
|
||||
require_multitail() {
|
||||
if ! type multitail > /dev/null 2>&1; then
|
||||
error "multitail program is not installed"
|
||||
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"
|
||||
fi
|
||||
|
||||
###### Custom commands
|
||||
|
||||
if [ -f abra-commands.sh ]; then
|
||||
# shellcheck disable=SC1091
|
||||
. abra-commands.sh
|
||||
source abra-commands.sh
|
||||
fi
|
||||
|
||||
###### Global help
|
||||
|
||||
sub_help() {
|
||||
echo "Usage: $PROGRAM_NAME [-a STACK_NAME] <subcommand> [options]"
|
||||
echo "Usage: $PROGRAM_NAME [-a STACK_NAME] [-c CONFIG] [-e ENV_FILE] <subcommand> [options]"
|
||||
echo ""
|
||||
echo "Subcommands:"
|
||||
echo " context [--help] [SUBCOMMAND] manage remote swarm contexts"
|
||||
echo " cp SRC_PATH SERVICE:DEST_PATH copy files to a container"
|
||||
echo " deploy let 'em rip"
|
||||
echo " logs SERVICE [ARGS] tail logs from a deployed service"
|
||||
echo " multilogs tail logs from a whole stackk"
|
||||
echo " run SERVICE CMD run a command in the specified service's container"
|
||||
echo " run_args SERVICE ARGS CMD run, passing extra args to docker exec"
|
||||
echo " secret_generate SECRET VERSION [CMD] generate a secret, store it in pass & as a Docker secret"
|
||||
echo " secret [--help] [SUBCOMMAND] manage secrets"
|
||||
echo " upgrade upgrade to the latest version"
|
||||
echo " ... (custom commands)"
|
||||
echo ""
|
||||
echo "Make sure \$STACK_NAME is set using direnv or -a"
|
||||
echo "Make sure \$STACK_NAME is set using direnv, -a, -e or -c"
|
||||
echo ""
|
||||
echo "Runs compose.yml by default, set e.g. COMPOSE_FILE=\"compose.yml:compose2.yml\" to override"
|
||||
}
|
||||
|
||||
###### Subcommand `secret`
|
||||
|
||||
sub_secret_help() {
|
||||
echo "Usage: $PROGRAM_NAME [-a STACK_NAME] secret <subcommand> [options]"
|
||||
echo "Usage: $PROGRAM_NAME [global opts] secret <subcommand> [sub opts]"
|
||||
echo ""
|
||||
echo "Subcommands:"
|
||||
echo " generate [PW] generate & store secret"
|
||||
echo " generate SECRET VERSION [PWGEN] generate & store secret"
|
||||
echo " insert SECRET VERSION PW save PW in docker and pass"
|
||||
}
|
||||
|
||||
sub_secret_insert() {
|
||||
require_stack
|
||||
load_context
|
||||
|
||||
SECRET=$1
|
||||
VERSION=$2
|
||||
PW=$3
|
||||
|
||||
if [ -z "$SECRET" ] || [ -z "$VERSION" ] || [ -z "$PW" ]; then
|
||||
echo "Usage: $PROGRAM_NAME secret insert SECRET VERSION PW"
|
||||
exit
|
||||
fi
|
||||
|
||||
echo "$PW" | docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" - > /dev/null
|
||||
echo "$PW" | pass insert "hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}" -m > /dev/null
|
||||
}
|
||||
|
||||
sub_secret_generate(){
|
||||
SECRET=$1
|
||||
VERSION=$2
|
||||
PW=${3:-pwqgen}
|
||||
PWGEN=${3:-pwqgen}
|
||||
|
||||
if [ -z "$SECRET" ] || [ -z "$VERSION" ]; then
|
||||
echo "Usage: $PROGRAM_NAME secret_generate SECRET VERSION"
|
||||
exit
|
||||
fi
|
||||
PW=$($PWGEN)
|
||||
|
||||
$PW | tee \
|
||||
>(docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" -) \
|
||||
>(pass insert "hosts/autonomic-swarm/${STACK_NAME}/${SECRET}" -m)
|
||||
success "Password: $PW"
|
||||
|
||||
echo "sub_secret_insert \"$SECRET\" \"$VERSION\" \"$PW\""
|
||||
exit
|
||||
sub_secret_insert "$SECRET" "$VERSION" "$PW"
|
||||
}
|
||||
|
||||
sub_secret() {
|
||||
@ -98,14 +217,18 @@ sub_secret() {
|
||||
parse_subcommand "$SUBCOMMAND" "secret" $@
|
||||
}
|
||||
|
||||
###### Subcommand `run`
|
||||
|
||||
sub_run_args(){
|
||||
require_stack
|
||||
|
||||
SERVICE=$1
|
||||
DOCKER_ARGS=$2
|
||||
|
||||
shift 2
|
||||
|
||||
if [ -z "$SERVICE" ]; then
|
||||
echo "Usage: $PROGRAM_NAME run SERVICE [CMD]"
|
||||
echo "Usage: $PROGRAM_NAME run SERVICE DOCKER_ARGS [CMD]"
|
||||
exit
|
||||
fi
|
||||
|
||||
@ -113,7 +236,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
|
||||
|
||||
@ -131,9 +254,16 @@ sub_run(){
|
||||
sub_run_args "$SERVICE" "" "$@"
|
||||
}
|
||||
|
||||
###### Subcommand `deploy`
|
||||
|
||||
sub_deploy (){
|
||||
require_stack
|
||||
require_stack_dir
|
||||
load_context
|
||||
|
||||
echo "About to deploy:"
|
||||
echo " Compose: $(tput setaf 3)${PWD}/${COMPOSE_FILE}$(tput sgr0)"
|
||||
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)"
|
||||
fi
|
||||
@ -147,21 +277,51 @@ sub_deploy (){
|
||||
* ) return;;
|
||||
esac
|
||||
|
||||
# shellcheck disable=SC2086
|
||||
if docker stack deploy -c ${COMPOSE_FILE/:/ -c } "$STACK_NAME"; then
|
||||
if [ -n "$DOMAIN" ]; then
|
||||
echo "$(tput setaf 2)Yay! App should be available at https://${DOMAIN}$(tput sgr0)"
|
||||
(
|
||||
cd "$ABRA_STACK_DIR" || error "\$ABRA_STACK_DIR '$ABRA_STACK_DIR' not found"
|
||||
# shellcheck disable=SC2086
|
||||
if docker stack deploy -c ${COMPOSE_FILE/:/ -c } "$STACK_NAME"; then
|
||||
if [ -n "$DOMAIN" ]; then
|
||||
success "Yay! App should be available at https://${DOMAIN}"
|
||||
else
|
||||
success "Yay! That worked. No \$DOMAIN defined, check logs."
|
||||
fi
|
||||
else
|
||||
echo "$(tput setaf 2)Yay! That worked. No \$DOMAIN defined, check logs.(tput sgr0)"
|
||||
error "Oh no! Something went wrong 😕 Check errors above"
|
||||
fi
|
||||
else
|
||||
echo "$(tput setaf 1)Oh no! Something went wrong 😕 Check errors above$(tput sgr0)"
|
||||
fi
|
||||
)
|
||||
}
|
||||
|
||||
###### Subcommand `logs`
|
||||
|
||||
# Inspired by https://github.com/moby/moby/issues/31458#issuecomment-475411564
|
||||
sub_multilogs() {
|
||||
require_stack
|
||||
require_multitail
|
||||
|
||||
# Get a list of the service names
|
||||
SERVICES=$(docker stack services --format "{{.Name}}" "${STACK_NAME}")
|
||||
# Sort the service names
|
||||
SERVICES=$(echo "${SERVICES}" | sort)
|
||||
# Create the command to run
|
||||
COMMAND='multitail --mergeall'
|
||||
for SERVICE in ${SERVICES}; do
|
||||
COMMAND="${COMMAND} -L 'docker service logs --tail 20 -f ${SERVICE}'"
|
||||
done
|
||||
# Run the command
|
||||
bash -c "${COMMAND}"
|
||||
}
|
||||
|
||||
sub_logs (){
|
||||
require_stack
|
||||
|
||||
SERVICE=$1
|
||||
|
||||
if [ -z "$SERVICE" ]; then
|
||||
warning "No \$SERVICE provided, running multilogs"
|
||||
sub_multilogs
|
||||
fi
|
||||
|
||||
shift
|
||||
|
||||
if [ $# -eq 0 ]; then
|
||||
@ -179,7 +339,11 @@ sub_logs (){
|
||||
docker service logs "${STACK_NAME}_${SERVICE}" $LOGS_ARGS
|
||||
}
|
||||
|
||||
###### Subcommand `cp`
|
||||
|
||||
sub_cp() {
|
||||
require_stack
|
||||
|
||||
SOURCE=$1
|
||||
DEST=$2
|
||||
|
||||
@ -187,18 +351,17 @@ sub_cp() {
|
||||
SERVICE=$(echo "$SERVICE" | tr -d ':')
|
||||
|
||||
if [ -z "$SERVICE" ]; then
|
||||
echo "$(tput setaf 1)ERROR: Can't find SERVICE in either SRC or DEST$(tput sgr0)"
|
||||
echo ""
|
||||
echo "Usage: $PROGRAM_NAME cp SERVICE:SRC_PATH DEST_PATH"
|
||||
echo " $PROGRAM_NAME cp SRC_PATH SERVICE:DEST_PATH"
|
||||
exit
|
||||
echo ""
|
||||
error "Can't find SERVICE in either SRC or DEST"
|
||||
fi
|
||||
|
||||
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
|
||||
| grep "${STACK_NAME}_${SERVICE}" | cut -d',' -f1)
|
||||
|
||||
if [ -z "$CONTAINER" ]; then
|
||||
echo "$(tput setaf 1)ERROR: Can't find a ${STACK_NAME}_${SERVICE}$(tput sgr0)"
|
||||
error "Can't find a container for ${STACK_NAME}_${SERVICE}"
|
||||
exit
|
||||
fi
|
||||
|
||||
@ -208,8 +371,10 @@ sub_cp() {
|
||||
docker cp $CP_ARGS
|
||||
}
|
||||
|
||||
###### Subcommand `context`
|
||||
|
||||
sub_context_help() {
|
||||
echo "Usage: $PROGRAM_NAME [-a STACK_NAME] context <subcommand> [options]"
|
||||
echo "Usage: $PROGRAM_NAME [global opts] context <subcommand> [sub opts]"
|
||||
echo ""
|
||||
echo "Subcommands:"
|
||||
echo " init HOST [USER] [PORT] set up remote Docker context"
|
||||
@ -244,34 +409,16 @@ sub_context() {
|
||||
parse_subcommand "$SUBCOMMAND2" "context" $@
|
||||
}
|
||||
|
||||
parse_subcommand() {
|
||||
SUBCOMMAND="$1"
|
||||
PREFIX=$2
|
||||
|
||||
if [ -n "$PREFIX" ]; then
|
||||
PPREFIX="_$2"
|
||||
SPREFIX="$2 "
|
||||
SSPREFIX=" $2"
|
||||
fi
|
||||
###### Subcommand `upgrade`
|
||||
|
||||
case $SUBCOMMAND in
|
||||
"" | "-h" | "--help")
|
||||
"sub${PPREFIX}_help"
|
||||
;;
|
||||
*)
|
||||
shift 2
|
||||
"sub${PPREFIX}_${SUBCOMMAND}" "$@"
|
||||
if [ $? = 127 ]; then
|
||||
echo "Error: '$SPREFIX$SUBCOMMAND' is not a known subcommand." >&2
|
||||
echo " Run '$PROGRAM_NAME$SSPREFIX --help' for a list of known subcommands." >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
sub_upgrade() {
|
||||
curl -fsSL https://install.abra.autonomic.zone | bash
|
||||
}
|
||||
|
||||
###### Main
|
||||
|
||||
SUBCOMMAND=$1
|
||||
shift
|
||||
|
||||
# shellcheck disable=SC2086,SC2068
|
||||
parse_subcommand $SUBCOMMAND "" $@
|
||||
|
@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
ABRA_VERSION="0.1.0"
|
||||
ABRA_VERSION="0.1.2"
|
||||
ABRA_SRC="https://git.autonomic.zone/autonomic-cooperative/abra/raw/tag/$ABRA_VERSION/abra"
|
||||
|
||||
function install_abra {
|
16
script.d/swarm-installer
Executable file
16
script.d/swarm-installer
Executable file
@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
|
||||
install_docker() {
|
||||
echo "install_docker: TODO"
|
||||
}
|
||||
init_swarm() {
|
||||
echo "init_swarm: TODO"
|
||||
}
|
||||
|
||||
run_installation() {
|
||||
install_docker
|
||||
init_swarm
|
||||
}
|
||||
|
||||
run_installation
|
||||
exit 0
|
Reference in New Issue
Block a user