automa/automa
m1ngsama e5542de818 polish: CLI UX overhaul and rich .env.example metadata
CLI improvements:
- Unicode status indicators (✔ ✘ ▶ ● ○ ⚠) and braille spinners
- Animated spinner for docker pull/up operations
- Project metadata parsed from .env.example (@name, @desc, @url, @port, @note)
- Descriptions shown in list, deploy selection, and status views
- Auto-generate passwords for secret fields (PASSWORD/TOKEN/AUTHKEY)
- Confirmation prompt before deploy with project summary
- Post-deploy access URL hint based on @port metadata
- Divider lines for visual section separation
- Helpful error messages with suggested commands
- Command aliases: ls, st, ps, down, log, configure
- Bash 3.2 compatible (no associative arrays)

.env.example enrichment:
- All projects now have @name, @desc, @url, @port metadata headers
- Inline field descriptions shown as context during interactive config
- Tailscale: @note hints for profile-based DERP deployment
- Structured comments group related settings visually

Installer:
- Prerequisite check with per-tool status (✔/✘)
- Quieter git operations
- Cleaner post-install instructions
2026-04-15 10:15:43 +08:00

664 lines
18 KiB
Bash
Executable file

#!/usr/bin/env bash
# automa - interactive Docker Compose project deployer
#
# Quick start:
# curl -fsSL https://raw.githubusercontent.com/m1ngsama/automa/main/install.sh | bash
# cd ~/automa && ./automa deploy
set -euo pipefail
AUTOMA_VERSION="1.0.0"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# ============================================================================
# Terminal
# ============================================================================
if [[ -t 1 ]]; then
RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m'
BLUE='\033[0;34m' CYAN='\033[0;36m'
BOLD='\033[1m' DIM='\033[2m' NC='\033[0m'
COLS=$(tput cols 2>/dev/null || echo 80)
else
RED='' GREEN='' YELLOW='' BLUE='' CYAN=''
BOLD='' DIM='' NC=''
COLS=80
fi
# ============================================================================
# Output helpers
# ============================================================================
info() { echo -e " ${GREEN}\xe2\x9c\x94${NC} $*"; }
warn() { echo -e " ${YELLOW}\xe2\x9a\xa0${NC} $*"; }
error() { echo -e " ${RED}\xe2\x9c\x98${NC} $*" >&2; }
step() { echo -e " ${CYAN}\xe2\x96\xb6${NC} ${BOLD}$*${NC}"; }
dim() { echo -e " ${DIM}$*${NC}"; }
divider() {
printf " ${DIM}"
printf '%.0s\xe2\x94\x80' $(seq 1 $(( (COLS - 4) / 3 + 1 )) )
printf "${NC}\n"
}
# Spinner for long operations
spinner() {
local pid=$1 msg="${2:-Working...}"
local frames=('⠋' '⠙' '⠹' '⠸' '⠼' '⠴' '⠦' '⠧' '⠇' '⠏')
local i=0
while kill -0 "$pid" 2>/dev/null; do
printf "\r ${CYAN}%s${NC} %s" "${frames[$i]}" "$msg"
i=$(( (i + 1) % ${#frames[@]} ))
sleep 0.1
done
wait "$pid"
local rc=$?
printf "\r\033[2K" # clear line
return $rc
}
run_with_spinner() {
local msg="$1"; shift
"$@" &>/dev/null &
local pid=$!
if spinner "$pid" "$msg"; then
info "$msg"
return 0
else
error "$msg"
return 1
fi
}
# ============================================================================
# Prerequisites
# ============================================================================
check_docker() {
if ! command -v docker &>/dev/null; then
echo ""
error "Docker is not installed"
echo ""
dim "Install Docker:"
dim " curl -fsSL https://get.docker.com | sh"
dim ""
dim "Or visit: https://docs.docker.com/engine/install/"
echo ""
exit 1
fi
if ! docker compose version &>/dev/null 2>&1; then
echo ""
error "Docker Compose plugin is not installed"
dim "Install: https://docs.docker.com/compose/install/"
echo ""
exit 1
fi
}
# ============================================================================
# Project helpers
# ============================================================================
PROJECTS=()
discover_projects() {
PROJECTS=()
for dir in "$SCRIPT_DIR"/*/; do
[[ -f "$dir/compose.yaml" ]] && PROJECTS+=("$(basename "$dir")")
done
}
project_exists() { [[ -f "$SCRIPT_DIR/$1/compose.yaml" ]]; }
# Parse @key from .env.example header
project_meta() {
local slug="$1" key="$2"
local env_example="$SCRIPT_DIR/$slug/.env.example"
[[ -f "$env_example" ]] || return
while IFS= read -r line; do
if [[ "$line" =~ ^#\ @${key}\ (.+) ]]; then
echo "${BASH_REMATCH[1]}"
return
fi
[[ ! "$line" =~ ^# ]] && return # stop at first non-comment
done < "$env_example"
}
# Collect all @note lines
project_notes() {
local slug="$1"
local env_example="$SCRIPT_DIR/$slug/.env.example"
[[ -f "$env_example" ]] || return
while IFS= read -r line; do
[[ "$line" =~ ^#\ @note\ (.+) ]] && echo "${BASH_REMATCH[1]}"
[[ ! "$line" =~ ^# ]] && return
done < "$env_example"
}
project_status() {
local slug="$1"
if [[ ! -f "$SCRIPT_DIR/$slug/.env" ]]; then
echo "not_configured"
elif compose "$slug" ps --status running 2>/dev/null | grep -q .; then
echo "running"
else
echo "stopped"
fi
}
status_badge() {
case "$1" in
running) echo -e "${GREEN}\xe2\x97\x8f running${NC}" ;;
stopped) echo -e "${YELLOW}\xe2\x97\x8f stopped${NC}" ;;
not_configured) echo -e "${DIM}\xe2\x97\x8b not configured${NC}" ;;
esac
}
# compose wrapper
compose() {
local slug="$1"; shift
local dir="$SCRIPT_DIR/$slug"
local args=(-f "$dir/compose.yaml")
[[ -f "$dir/.env" ]] && args+=(--env-file "$dir/.env")
docker compose "${args[@]}" "$@"
}
# Get access URL after deploy
access_hint() {
local slug="$1"
local port_var
port_var=$(project_meta "$slug" "port")
[[ -z "$port_var" ]] && return
local env_file="$SCRIPT_DIR/$slug/.env"
[[ -f "$env_file" ]] || return
local port_val
port_val=$(grep "^${port_var}=" "$env_file" 2>/dev/null | cut -d= -f2)
[[ -z "$port_val" ]] && return
if [[ "$port_val" == *:* ]]; then
echo "http://${port_val}"
elif [[ "$port_val" == */* ]]; then
echo "$port_val"
else
echo "http://localhost:${port_val}"
fi
}
# ============================================================================
# Interactive .env configuration
# ============================================================================
generate_password() {
LC_ALL=C tr -dc 'A-Za-z0-9' </dev/urandom 2>/dev/null | head -c 24 || openssl rand -hex 12
}
configure_env() {
local slug="$1"
local dir="$SCRIPT_DIR/$slug"
local env_example="$dir/.env.example"
local env_file="$dir/.env"
if [[ ! -f "$env_example" ]]; then
warn "No .env.example found, skipping"
return 0
fi
local name
name=$(project_meta "$slug" "name")
name="${name:-$slug}"
# Handle existing .env
if [[ -f "$env_file" ]]; then
echo ""
dim ".env already exists for ${BOLD}${name}${NC}"
echo ""
echo -e " ${BOLD}k${NC} Keep current configuration"
echo -e " ${BOLD}r${NC} Reconfigure from scratch"
echo -e " ${BOLD}v${NC} View current values"
echo ""
while true; do
read -rp " Choose [k]: " choice
case "${choice:-k}" in
k) info "Keeping existing .env"; return 0 ;;
r) break ;;
v)
echo ""
divider
while IFS= read -r line; do
echo -e " ${DIM}${line}${NC}"
done < "$env_file"
divider
echo ""
;;
*) dim "Enter k, r, or v" ;;
esac
done
fi
echo ""
step "Configure ${name}"
local desc
desc=$(project_meta "$slug" "desc")
local url
url=$(project_meta "$slug" "url")
[[ -n "$desc" ]] && dim "$desc"
[[ -n "$url" ]] && dim "Docs: ${url}"
# Show notes
local has_notes=0
while IFS= read -r note; do
[[ -z "$note" ]] && continue
[[ $has_notes -eq 0 ]] && echo ""
echo -e " ${YELLOW}!${NC} ${DIM}${note}${NC}"
has_notes=1
done < <(project_notes "$slug")
echo ""
divider
dim "Press ${BOLD}Enter${NC}${DIM} to accept [default] values${NC}"
echo ""
local tmp_env
tmp_env="$(mktemp)"
while IFS= read -r line; do
# Blank line
[[ -z "$line" ]] && continue
# Skip @metadata
[[ "$line" =~ ^#\ @(name|desc|url|port|note) ]] && continue
# Comment → show as hint
if [[ "$line" =~ ^#.* ]]; then
echo -e " ${DIM}${line#\# }${NC}"
continue
fi
local key="${line%%=*}"
local default="${line#*=}"
if [[ -n "$default" ]]; then
read -rp " ${BOLD}${key}${NC} [${DIM}${default}${NC}]: " val
echo "${key}=${val:-$default}" >> "$tmp_env"
else
# Required — check if it's a secret
if [[ "$key" =~ PASSWORD|SECRET|TOKEN|AUTHKEY ]]; then
echo -e " ${DIM}Leave blank to auto-generate${NC}"
read -rp " ${BOLD}${key}${NC}: " val
if [[ -z "$val" ]]; then
val=$(generate_password)
echo -e " ${DIM}Generated: ${val}${NC}"
fi
else
while true; do
read -rp " ${BOLD}${key}${NC} ${RED}(required)${NC}: " val
[[ -n "$val" ]] && break
echo -e " ${RED}This field cannot be empty${NC}"
done
fi
echo "${key}=${val}" >> "$tmp_env"
fi
done < "$env_example"
echo ""
divider
mv "$tmp_env" "$env_file"
chmod 600 "$env_file"
info "Configuration saved"
}
# ============================================================================
# Commands
# ============================================================================
cmd_list() {
banner
check_docker
discover_projects
if [[ ${#PROJECTS[@]} -eq 0 ]]; then
warn "No projects found"
return 1
fi
local i=1
for slug in "${PROJECTS[@]}"; do
local st
st=$(project_status "$slug")
local badge
badge=$(status_badge "$st")
local desc
desc=$(project_meta "$slug" "desc")
printf " ${BOLD}%2d${NC} %-20s %b\n" "$i" "$slug" "$badge"
[[ -n "$desc" ]] && echo -e " ${DIM}${desc}${NC}"
((i++))
done
echo ""
}
cmd_deploy() {
banner
check_docker
discover_projects
if [[ ${#PROJECTS[@]} -eq 0 ]]; then
error "No projects found"
return 1
fi
# Direct deploy
if [[ $# -gt 0 ]]; then
local ok=0 fail=0
for name in "$@"; do
echo ""
if deploy_project "$name"; then ((ok++)); else ((fail++)); fi
done
deploy_summary $ok $fail
return
fi
# Interactive
step "Select projects to deploy"
echo ""
local i=1
for slug in "${PROJECTS[@]}"; do
local st
st=$(project_status "$slug")
local badge
badge=$(status_badge "$st")
local desc
desc=$(project_meta "$slug" "desc")
printf " ${BOLD}%2d${NC} %-20s %b\n" "$i" "$slug" "$badge"
[[ -n "$desc" ]] && echo -e " ${DIM}${desc}${NC}"
((i++))
done
echo ""
dim "Enter numbers separated by spaces, e.g. ${BOLD}1 3 5${NC}"
dim "Type ${BOLD}all${NC} to deploy everything, or ${BOLD}q${NC} to quit"
echo ""
read -rp " > " selection
[[ -z "$selection" || "$selection" == "q" ]] && return 0
local selected=()
if [[ "$selection" == "all" ]]; then
selected=("${PROJECTS[@]}")
else
for num in $selection; do
if [[ "$num" =~ ^[0-9]+$ ]] && ((num > 0 && num <= ${#PROJECTS[@]})); then
selected+=("${PROJECTS[$((num-1))]}")
else
warn "Skipping invalid: $num"
fi
done
fi
[[ ${#selected[@]} -eq 0 ]] && return 0
# Confirmation
echo ""
divider
step "Will deploy:"
for s in "${selected[@]}"; do
local desc
desc=$(project_meta "$s" "desc")
echo -e " ${CYAN}\xe2\x96\xb6${NC} ${s} ${DIM}${desc:-}${NC}"
done
echo ""
read -rp " Proceed? [Y/n] " confirm
[[ "${confirm:-y}" =~ ^[Nn] ]] && { echo ""; dim "Cancelled."; return 0; }
divider
local ok=0 fail=0
for name in "${selected[@]}"; do
echo ""
if deploy_project "$name"; then ((ok++)); else ((fail++)); fi
done
deploy_summary $ok $fail
}
deploy_project() {
local slug="$1"
if ! project_exists "$slug"; then
error "Project not found: ${slug}"
dim "Run ${BOLD}automa list${NC}${DIM} to see available projects${NC}"
return 1
fi
local name
name=$(project_meta "$slug" "name")
name="${name:-$slug}"
local desc
desc=$(project_meta "$slug" "desc")
step "${name}"
[[ -n "$desc" ]] && dim "${desc}"
configure_env "$slug"
if [[ ! -f "$SCRIPT_DIR/$slug/.env" ]]; then
error "No .env — run: ${BOLD}automa config $slug${NC}"
return 1
fi
echo ""
if run_with_spinner "Pulling images..." compose "$slug" pull; then
if run_with_spinner "Starting containers..." compose "$slug" up -d; then
local url
url=$(access_hint "$slug")
[[ -n "$url" ]] && dim "Access: ${BOLD}${url}${NC}"
return 0
fi
fi
return 1
}
deploy_summary() {
local ok=$1 fail=$2
echo ""
divider
echo ""
if [[ $fail -eq 0 ]]; then
info "${BOLD}All done!${NC} ${ok} project(s) deployed"
else
warn "${BOLD}Done.${NC} ${GREEN}${ok} deployed${NC}, ${RED}${fail} failed${NC}"
fi
echo ""
dim "Useful commands:"
dim " ${BOLD}automa status${NC}${DIM} — check running state${NC}"
dim " ${BOLD}automa logs${NC}${DIM} <project> — view logs${NC}"
dim " ${BOLD}automa update${NC}${DIM} <project> — pull & restart${NC}"
echo ""
}
cmd_stop() {
local slug="${1:-}"
if [[ -z "$slug" ]]; then
error "Usage: ${BOLD}automa stop <project>${NC}"
dim "Run ${BOLD}automa list${NC}${DIM} to see available projects${NC}"
return 1
fi
if ! project_exists "$slug"; then
error "Project not found: ${slug}"
return 1
fi
run_with_spinner "Stopping ${slug}..." compose "$slug" down
}
cmd_logs() {
local slug="${1:-}"
if [[ -z "$slug" ]]; then
error "Usage: ${BOLD}automa logs <project>${NC}"
return 1
fi
shift
if ! project_exists "$slug"; then
error "Project not found: ${slug}"
return 1
fi
compose "$slug" logs -f "$@"
}
cmd_status() {
banner
check_docker
discover_projects
if [[ ${#PROJECTS[@]} -eq 0 ]]; then
warn "No projects found"
return 1
fi
for slug in "${PROJECTS[@]}"; do
local st name
st=$(project_status "$slug")
name=$(project_meta "$slug" "name")
name="${name:-$slug}"
local badge
badge=$(status_badge "$st")
echo -e " ${BOLD}${name}${NC} ${badge}"
if [[ "$st" == "running" ]]; then
compose "$slug" ps --format "table {{.Name}}\t{{.Status}}" 2>/dev/null \
| tail -n +2 \
| while IFS= read -r line; do
echo -e " ${DIM}${line}${NC}"
done
fi
echo ""
done
}
cmd_restart() {
local slug="${1:-}"
if [[ -z "$slug" ]]; then
error "Usage: ${BOLD}automa restart <project>${NC}"
return 1
fi
if ! project_exists "$slug"; then
error "Project not found: ${slug}"
return 1
fi
run_with_spinner "Restarting ${slug}..." compose "$slug" restart
}
cmd_config() {
local slug="${1:-}"
if [[ -z "$slug" ]]; then
error "Usage: ${BOLD}automa config <project>${NC}"
return 1
fi
if ! project_exists "$slug"; then
error "Project not found: ${slug}"
return 1
fi
configure_env "$slug"
}
cmd_update() {
local slug="${1:-}"
if [[ -z "$slug" ]]; then
error "Usage: ${BOLD}automa update <project>${NC}"
return 1
fi
if ! project_exists "$slug"; then
error "Project not found: ${slug}"
return 1
fi
local name
name=$(project_meta "$slug" "name")
name="${name:-$slug}"
echo ""
step "Updating ${name}"
run_with_spinner "Pulling latest images..." compose "$slug" pull
run_with_spinner "Recreating containers..." compose "$slug" up -d
echo ""
info "Update complete"
echo ""
}
banner() {
echo ""
echo -e " ${BOLD}${CYAN}automa${NC} ${DIM}v${AUTOMA_VERSION}${NC}"
dim "Self-hosted Docker Compose deployer"
echo ""
}
cmd_help() {
banner
cat <<EOF
${BOLD}Usage${NC}
automa <command> [options]
${BOLD}Commands${NC}
${BOLD}deploy${NC} [project...] Deploy projects interactively or by name
${BOLD}list${NC} List all projects and their status
${BOLD}status${NC} Show running containers
${BOLD}config${NC} <project> Configure environment variables
${BOLD}stop${NC} <project> Stop a running project
${BOLD}restart${NC} <project> Restart a project
${BOLD}update${NC} <project> Pull latest images and recreate
${BOLD}logs${NC} <project> Follow container logs
${BOLD}help${NC} Show this help message
${BOLD}Examples${NC}
${DIM}\$${NC} automa deploy ${DIM}# interactive selection${NC}
${DIM}\$${NC} automa deploy forgejo nextcloud ${DIM}# deploy by name${NC}
${DIM}\$${NC} automa status ${DIM}# overview dashboard${NC}
${DIM}\$${NC} automa logs minecraft ${DIM}# follow logs${NC}
${BOLD}Quick start${NC}
${DIM}\$${NC} curl -fsSL https://raw.githubusercontent.com/m1ngsama/automa/main/install.sh | bash
${DIM}\$${NC} cd ~/automa && ./automa deploy
EOF
}
# ============================================================================
# Main
# ============================================================================
main() {
local cmd="${1:-}"
[[ -z "$cmd" ]] && { cmd_help; return; }
shift
case "$cmd" in
deploy) cmd_deploy "$@" ;;
list|ls) cmd_list ;;
status|st|ps) cmd_status ;;
stop|down) cmd_stop "$@" ;;
restart) cmd_restart "$@" ;;
logs|log) cmd_logs "$@" ;;
config|configure) cmd_config "$@" ;;
update|upgrade) cmd_update "$@" ;;
help|-h|--help) cmd_help ;;
version|-v|--version) echo "automa v${AUTOMA_VERSION}" ;;
*)
error "Unknown command: ${cmd}"
dim "Run ${BOLD}automa help${NC}${DIM} for usage${NC}"
exit 1
;;
esac
}
main "$@"