# Architecture & Internal Design This document describes DAM's internal architecture, execution flow, and design patterns. --- ## Overview DAM is a single Bash script (`dam.sh`, version 1.3.2). It requires Bash 4.4+, Docker with a reachable daemon, a Compose implementation, `column`, and standard Linux command-line utilities. It uses: - **Bash associative arrays** for caching - **`find`** for directory discovery - **`pushd`/`popd`** for working directory management - **Docker CLI** and **Docker Compose CLI** for all container operations --- ## Execution Flow ``` ┌─────────────────────────────────────────────────────────┐ │ User invocation │ │ e.g., "dkr start " or "start " or "./dam.sh ..." │ └──────────────────────────┬──────────────────────────────┘ │ ▼ ┌──────────────────────────────────────────────────────────┐ │ 1. Script Initialization │ │ • set -uo pipefail │ │ • Bash version check (≥ 4.4) │ │ • Load config from /etc/docker-app-manager.conf │ │ (after root ownership and permission checks, or use │ │ fallback defaults) │ │ • Build FIND_PRUNE_ARGS from EXCLUDE_DIRS │ │ • Initialize APP_DIR_CACHE │ │ • Initialize MAX_SEARCH_DEPTH │ └──────────────────────────┬──────────────────────────────┘ │ ▼ ┌──────────────────────────────────────────────────────────┐ │ 2. Multicall Detection │ │ • Check if script was called as "start", "stop", etc. │ │ via basename "$0" │ │ • If so, prepend the action to "$@" │ └──────────────────────────┬──────────────────────────────┘ │ ▼ ┌──────────────────────────────────────────────────────────┐ │ 3. Administrative Command Check │ │ • If $1 is "install", "config", "uninstall", │ │ or "self-update": │ │ → Verify EUID == 0 (require sudo) │ │ → Execute administrative handler │ │ → Exit │ └──────────────────────────┬──────────────────────────────┘ │ (not admin) ▼ ┌──────────────────────────────────────────────────────────┐ │ 4. Header & Help Check │ │ • Print header (unless "get" command) │ │ • Check for --help / -h / help / no-args → usage() │ │ • Extract ACTION from $1 │ │ • Normalize "frec" → "force-recreate" │ └──────────────────────────┬──────────────────────────────┘ │ ▼ ┌──────────────────────────────────────────────────────────┐ │ 5. Docker Readiness Check │ │ • docker_ready(): │ │ 1. command -v docker │ │ 2. docker info │ │ 3. docker compose version OR docker-compose version │ │ 4. column utility │ └──────────────────────────┬──────────────────────────────┘ │ ▼ ┌──────────────────────────────────────────────────────────┐ │ 6. Command Dispatch │ │ │ │ case "$ACTION" in │ │ list (no args) → inventory_apps_table() │ │ cleanup → cleanup_dangling_resources() │ │ everything else → parse_target_apps() then: │ │ list → print_app_level_details_table() │ │ get → get resource handler │ │ status → status handler │ │ others → per-app processing loop │ │ esac │ └──────────────────────────┬──────────────────────────────┘ │ ▼ ┌──────────────────────────────────────────────────────────┐ │ 7. App Selection & Parsing │ │ parse_target_apps(): │ │ • Parse -y/--yes, "with" modifiers, log/get args │ │ • Handle "all" / "all except" / specific apps │ │ • Validate app existence for "except" targets │ │ • Confirmation prompt for "delete all" │ │ • Populate APPS_TO_PROCESS array │ └──────────────────────────┬──────────────────────────────┘ │ ▼ ┌──────────────────────────────────────────────────────────┐ │ 8. Per-App Processing Loop │ │ for app in APPS_TO_PROCESS: │ │ process_app(ACTION, app): │ │ • find_app_dir(app) → resolve directory │ │ • Check excluded │ │ • Dispatch to handler: │ │ - update → run_update_action_for_app() │ │ - logs → run_logs_action_for_app() │ │ - debug → run_debug_action_for_app() │ │ - others → run_compose_action_for_app() │ │ • Track SUCCESS/FAILED/SKIPPED counters │ └──────────────────────────┬──────────────────────────────┘ │ ▼ ┌──────────────────────────────────────────────────────────┐ │ 9. Summary & Cleanup │ │ • print_summary(ACTION) │ │ • If delete/update: cleanup_dangling_resources() │ │ • Exit with appropriate code │ └─────────────────────────────────────────────────────────┘ ``` --- ## Key Functions ### Configuration & Setup | Function / block | Purpose | | :--------------- | :------ | | Top-level config loading | Validates ownership and permissions, then loads config or sets fallback defaults | ### App Discovery | Function | Purpose | | :------- | :------ | | `find_app_dir()` | Resolves directory basenames and effective Compose project names to a directory, using a cache and reporting ambiguity. | | `has_compose_files()` | Checks if a directory contains a supported Compose filename. | | `get_app_compose_file()` | Returns the highest-priority Compose filename, or an update script for an update-only app. | | `resolve_compose_files()` | Resolves `using` files into `-f` arguments; rejects an all-missing file set for `stop`, `kill`, `recreate`, `force-recreate`, and `delete`. | | `has_update_files()` | Checks if a directory has Compose files or update scripts. | | `get_all_apps()` | Returns discovered app names for the `all` selector. | | `is_excluded()` | Checks an exact directory-basename match against `EXCLUDE_DIRS`. | ### Docker Abstraction | Function | Purpose | | :------- | :------ | | `dc()` | Wrapper: tries `docker compose`, falls back to `docker-compose`. | | `docker_ready()` | Validates Docker, daemon, Compose, and the `column` utility. | ### Command Handlers | Function | Purpose | | :------- | :------ | | `run_compose_action_for_app()` | Handles lifecycle actions and service-specific targets. | | `run_update_action_for_app()` | Handles custom update scripts or the Compose pull/build/up flow. | | `run_logs_action_for_app()` | Handles log viewing, service selection, and keyword translation. | | `run_debug_action_for_app()` | Placeholder; does not run diagnostics. | | `process_app()` | Per-app entry point; dispatches to the appropriate handler. | | `cleanup_dangling_resources()` | Handles requested cleanup operations (images, networks, volumes, build cache). | ### Argument Parsing | Function | Purpose | | :------- | :------ | | `parse_target_apps()` | Parses app selection, modifiers, logs/get arguments, and populates `APPS_TO_PROCESS`. | ### Display & Output | Function | Purpose | | :------- | :------ | | `print_header()` | Prints the DAM banner. | | `print_section()` | Prints a per-app section header. | | `print_summary()` | Prints the final summary with counters. | | `usage()` | Prints help/usage information. | | `print_apps_in_columns()` | Formats app names in a three-column layout. | | `list_available_apps()` | Lists apps organized by search directory. | | `inventory_apps_table()` | Prints a global inventory table with status. | | `print_app_level_details_table()` | Prints details for specifically selected apps. | ### Administrative | Function/Block | Purpose | | :------------- | :------ | | Install handler | Interactive wizard and installation file operations. | | Self-update handler | Downloads, validates, and installs an update. | | Uninstall handler | Removes the installed script, managed links, and config file. | --- ## Error Handling ### Script-Level - `set -uo pipefail` is set at the top of the script: - `-u`: undefined variables cause an error - `-o pipefail`: pipeline failures propagate - Note: `set -e` (exit on error) is **not** used - errors are handled explicitly ### Per-App Error Handling - Each app operation is wrapped in a success/failure check - Failed apps are counted (`FAILED++`) and listed in the summary - Processing **continues** to the next app even if one fails - The script exits with code 1 if any app failed ### Docker Errors - Docker/Compose errors are passed through directly - DAM adds `[FAIL]` messages but does not suppress Docker's error output - The `dc` wrapper checks for both `docker compose` and `docker-compose` availability --- ## Working Directory Management DAM uses `pushd`/`popd` to manage working directories: ```bash pushd "$dir" > /dev/null || return 1 # ... run docker compose commands ... popd > /dev/null || true ``` Each app operation changes to the app's directory before running Compose commands, then returns. This ensures Compose picks up the correct `compose.yaml` file. The `|| true` on `popd` ensures the script doesn't fail if `popd` has an issue (defensive programming). --- ## Multicall Binary Pattern DAM supports being invoked as different command names (multicall binary pattern): ```bash COMMAND_NAME="$(basename "$0")" SUPPORTED_ACTIONS=("start" "stop" "kill" "restart" ...) if [[ " ${SUPPORTED_ACTIONS[*]} " =~ \ ${COMMAND_NAME}\ ]]; then set -- "$COMMAND_NAME" "$@" fi ``` When installed with raw commands (no prefix), the symlink `start` → `docker-app-manager` means: - `basename "$0"` returns `start` - DAM prepends `start` to the argument list - The rest of the script processes it as if the user typed `dam start ...` --- ## Output Design DAM uses ANSI color codes for structured, visually clear output: | Color | Used For | | :---- | :------- | | Red (`\033[0;31m`) | Errors, warnings, failed items | | Green (`\033[0;32m`) | Success, completed items | | Yellow (`\033[1;33m`) | Warnings, skipped items, confirmations | | Blue (`\033[0;34m`) | Step indicators (`[1/2]`, `[2/3]`, etc.) | | Cyan (`\033[0;36m`) | Informational messages, app names | | Bold (`\033[1m`) | Headers, section titles | Each app operation is visually separated with a section header: ``` ┌───────────────────────────────────────────────────── │ start: └───────────────────────────────────────────────────── ``` The final summary shows: ``` ══════════════════════ SUMMARY ══════════════════════ Action performed : start Total apps processed : 3 ✔ Successful : 2 ✘ Failed : 1 Successful apps: Failed apps: broken-app ═════════════════════════════════════════════════════ ``` # Core Conceptual Model Understanding how DAM (Docker App Manager) interacts with your system and Docker requires understanding a few core concepts. DAM bridges the gap between your host filesystem's organization and Docker's internal resource management. --- ## The Mapping: Human-Facing to Docker DAM's fundamental philosophy is that **directories equal projects**. 1. **Human-Facing Application Name**: The name you type in the CLI (e.g., ``). 2. **Application Directory**: DAM searches its configured `SEARCH_DIRS` for a folder exactly matching this name. (If you explicitly overridden the name via `.env` or `name:`, DAM will match that custom name instead). This directory is treated as the application's home. 3. **Compose File**: DAM looks inside the discovered directory for a standard Docker Compose file (e.g., `docker-compose.yml`, `compose.yaml`). 4. **Docker Compose Project**: When executing commands, DAM sets the application directory as the working directory (`PWD`). Docker Compose then uses the directory's basename as the **Project Name** by default. ```text User Input: "" ↓ DAM discovers: /opt/stacks// ↓ DAM finds: /opt/stacks//docker-compose.yml ↓ DAM executes: docker compose up -d (with /opt/stacks// as PWD) ↓ Docker Compose creates resources labeled with project "" ``` --- ## Key Distinctions ### Host Filesystem vs Docker-Managed Resources - **Host Filesystem**: The directories (e.g., `/opt/stacks/`) where your Compose files and local bind mounts live. DAM's discovery process relies entirely on this. - **Docker-Managed Resources**: Volumes, networks, images, and containers stored internally by the Docker Daemon. DAM operates on these indirectly through Docker Compose. When you delete an application directory from your filesystem, the Docker-managed resources are **not** automatically deleted unless you run a command like `dam delete ` first. ### Container Name vs Service Name vs Project Name DAM abstracts this distinction, but understanding it is critical when using commands like `get` or `logs`: | Concept | What It Is | Example | | :------ | :--------- | :------ | | **Project Name** | The name of the entire application stack. Derived from the directory name (by default). | `` | | **Service Name** | A logical component of your project defined in `compose.yaml` under `services:`. | ``, `postgres`, `redis` | | **Container Name** | The actual running instance of a service, named by Compose. | `--1`, `-postgres-1` | When you use DAM, you always target the **Project Name**. You never need to target individual containers. ### Docker Daemon vs Docker Client vs Docker Compose - **Docker Daemon**: The background service running on your host that actually builds, runs, and manages your containers. DAM requires the daemon to be running and accessible. - **Docker Client (`docker`)**: The CLI tool used to interact with the daemon. DAM uses this internally for things like `status`, `get`, and `cleanup`. - **Docker Compose (`docker compose`)**: A higher-level tool that parses `compose.yaml` files to manage multi-container applications (projects). DAM uses this for all lifecycle commands (`start`, `stop`, `kill`, `recreate`, `update`). ## Further Reading - [Application Discovery Detailed Flow](application-discovery.md) - [DAM vs Docker Commands](docker-vs-dam.md)