Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 90 additions & 16 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ _online_hosts := shell('python3 "$1"', justfile_directory() / "scripts/online_ho
# Defined once so a change to how the fleet addresses itself is a one-line
# edit instead of eight.
_play := "ansible-playbook --connection=local -l " + machine + " -e target=" + machine + " -e ansible_connection=local -e ansible_host=127.0.0.1"
_secret_tag_pattern := "secrets|bitwarden|ssh|ssh_keys|github|tailscale|kube|forgejo|registry|atuin|ai|omp"

# Apply all config to this machine (unlocks BW interactively if needed)
apply *args:
Expand All @@ -38,21 +39,57 @@ apply *args:
SKIP_TAGS="secrets"
fi
rc=0
unit_args=()
if [ -n "$SKIP_TAGS" ]; then
unit_args+=(--no-secrets)
{{ _play }} site.yml --skip-tags "$SKIP_TAGS" $BECOME_ARGS {{ args }} || rc=$?
else
{{ _play }} -e "bw_session=${BW_SESSION:-}" site.yml $BECOME_ARGS {{ args }} || rc=$?
{{ _play }} site.yml $BECOME_ARGS {{ args }} || rc=$?
fi
urc=0; scripts/run-units.sh "${unit_args[@]}" || urc=$?
[ "$rc" -ne 0 ] || rc=$urc
scripts/record-apply.py "$rc" apply "$SKIP_TAGS"
exit $rc

# Apply only specific tags (e.g. just apply-tags kube,shell)
apply-tags tags:
#!/usr/bin/env bash
set -uo pipefail
cd {{ dotfiles_dir }}
cd {{ dotfiles_dir }} || exit 1
rc=0
{{ _play }} -e "bw_session=${BW_SESSION:-}" site.yml --tags {{ tags }} || rc=$?
ansible_tags="{{ tags }}"
unit_args=(--tags "{{ tags }}")
skip_args=()
needs_secrets=0
IFS=',' read -ra tag_list <<< "{{ tags }}"
for tag in "${tag_list[@]}"; do
case "$tag" in
{{ _secret_tag_pattern }}) needs_secrets=1 ;;
esac
done
if [ "$needs_secrets" -eq 1 ]; then
# bw-unlock accepts an environment session as-is; reject stale ones first.
if [ -n "${BW_SESSION:-}" ] && ! bw unlock --check >/dev/null 2>&1; then
unset BW_SESSION
fi
if bw_env=$(scripts/bw-resolve.sh local) && eval "$bw_env" &&
[ -n "${BW_SESSION:-}" ] && bw unlock --check >/dev/null 2>&1 &&
timeout 15 bw sync >/dev/null; then
# Secret roles selected by their own tag still need bw_unlocked set.
ansible_tags="$ansible_tags,bitwarden"
else
echo "Bitwarden unavailable or sync failed; skipping secrets." >&2
unset BW_SESSION
skip_args=(--skip-tags secrets)
unit_args+=(--no-secrets)
rc=1
fi
fi
arc=0
{{ _play }} site.yml --tags "$ansible_tags" "${skip_args[@]}" || arc=$?
[ "$rc" -ne 0 ] || rc=$arc
urc=0; scripts/run-units.sh "${unit_args[@]}" || urc=$?
[ "$rc" -ne 0 ] || rc=$urc
scripts/record-apply.py "$rc" apply-tags "{{ tags }}"
exit $rc

Expand All @@ -64,19 +101,31 @@ apply-nosecrets *args:
git pull --ff-only || true
rc=0
{{ _play }} site.yml --skip-tags secrets {{ args }} || rc=$?
urc=0; scripts/run-units.sh --no-secrets || urc=$?
[ "$rc" -ne 0 ] || rc=$urc
scripts/record-apply.py "$rc" apply-nosecrets secrets
exit $rc

# Apply to a remote machine with specific tags (e.g. just apply-remote-tags bihar kube,shell)
apply-remote-tags name tags:
#!/usr/bin/env bash
set -euo pipefail
# A BW session is only valid on the machine that unlocked it, so resolve
# one on the remote rather than forwarding ours.
REMOTE_BW_SESSION=$({{ dotfiles_dir }}/scripts/bw-resolve.sh remote {{ name }} || true)
ssh -t {{ name }} "
# Resolve on the target only for explicitly secret-bearing tags.
REMOTE_BW_SESSION=""
IFS=',' read -ra tag_list <<< "{{ tags }}"
for tag in "${tag_list[@]}"; do
case "$tag" in
{{ _secret_tag_pattern }})
REMOTE_BW_SESSION=$({{ dotfiles_dir }}/scripts/bw-resolve.sh remote {{ name }} || true)
break ;;
esac
done
EXPECT_SESSION=0; [ -z "$REMOTE_BW_SESSION" ] || EXPECT_SESSION=1
BW_SESSION="$REMOTE_BW_SESSION" ssh -o SendEnv=BW_SESSION -t {{ name }} "
export PATH=\"\$HOME/.local/bin:/home/linuxbrew/.linuxbrew/bin:\$PATH\"
export BW_SESSION='${REMOTE_BW_SESSION}'
if [ '$EXPECT_SESSION' = 1 ] && [ -z \"\${BW_SESSION:-}\" ]; then
echo 'SSH server did not accept BW_SESSION; remote apply aborted.' >&2; exit 1
fi
cd ~/.local/share/dotfiles && git pull --ff-only && just apply-tags {{ tags }}
"

Expand All @@ -86,7 +135,18 @@ mesh-check:

# Apply only dotfile configs (shell, git, tmux, etc.)
dotfiles:
cd {{ dotfiles_dir }} && {{ _play }} site.yml --tags dotfiles
#!/usr/bin/env bash
set -uo pipefail
cd {{ dotfiles_dir }}
rc=0
{{ _play }} site.yml --tags dotfiles || rc=$?
urc=0; scripts/run-units.sh --tags dotfiles || urc=$?
[ "$rc" -ne 0 ] || rc=$urc
exit $rc

# Run the bash units (units/*.sh) in parallel via mise, e.g. `just units --tags omp`
units *args:
cd {{ dotfiles_dir }} && scripts/run-units.sh {{ args }}

# Apply only packages (Homebrew + Flatpak)
packages:
Expand All @@ -103,10 +163,13 @@ apply-remote name *args:
echo "Bitwarden unlocked on {{ name }} (session: ${#APPLY_BW_SESSION} chars)"
fi
echo "Applying to {{ name }} with forwarded BW session..."
ssh -t {{ name }} "
EXPECT_SESSION=0; [ -z "$APPLY_BW_SESSION" ] || EXPECT_SESSION=1
BW_SESSION="$APPLY_BW_SESSION" ssh -o SendEnv=BW_SESSION -t {{ name }} "
export PATH=\"\$HOME/.local/bin:/home/linuxbrew/.linuxbrew/bin:\$PATH\"
export BW_SESSION='${APPLY_BW_SESSION}'
[ -n \"\$BW_SESSION\" ] && (umask 077 && printf '%s' \"\$BW_SESSION\" > /tmp/bw_session)
if [ '$EXPECT_SESSION' = 1 ] && [ -z \"\${BW_SESSION:-}\" ]; then
echo 'SSH server did not accept BW_SESSION; remote apply aborted.' >&2; exit 1
fi
[ -n \"\${BW_SESSION:-}\" ] && (umask 077 && printf '%s' \"\$BW_SESSION\" > /tmp/bw_session)
cd ~/.local/share/dotfiles && git pull --ff-only && just apply {{ args }}
"
just push-terminfo {{ name }}
Expand Down Expand Up @@ -282,8 +345,15 @@ apply-online-tags tags:
[ -z "$ONLINE_HOSTS" ] && echo "No remote hosts online." && exit 0

declare -A SESSIONS
for host in $ONLINE_HOSTS; do
SESSIONS[$host]=$(scripts/bw-resolve.sh remote "$host" || true)
IFS=',' read -ra tag_list <<< "{{ tags }}"
for tag in "${tag_list[@]}"; do
case "$tag" in
{{ _secret_tag_pattern }})
for host in $ONLINE_HOSTS; do
SESSIONS[$host]=$(scripts/bw-resolve.sh remote "$host" || true)
done
break ;;
esac
done

PIDS=()
Expand All @@ -296,7 +366,7 @@ apply-online-tags tags:
echo "Applying tags '{{ tags }}' to $host (background)..."
ssh "$host" "
export PATH=\"\$HOME/.local/bin:/home/linuxbrew/.linuxbrew/bin:\$PATH\"
export BW_SESSION='${SESSIONS[$host]}'
export BW_SESSION='${SESSIONS[$host]:-}'
cd ~/.local/share/dotfiles && git pull --ff-only && just apply-tags {{ tags }}
" > "/tmp/apply_${host}.log" 2>&1 &
PIDS+=("$!:$host")
Expand Down Expand Up @@ -443,7 +513,7 @@ doctor:
echo "→ Machine identity"
if [ -f /etc/dotfiles-machine ]; then pass "machine: $(cat /etc/dotfiles-machine)"; else warn "/etc/dotfiles-machine missing — run bootstrap"; fi
echo "→ Core binaries"
for bin in brew bw gh tailscale ansible-playbook just; do
for bin in brew bw gh tailscale ansible-playbook just mise; do
if command -v "$bin" >/dev/null 2>&1; then pass "$bin"; else warn "$bin missing"; fi
done
echo "→ Tailscale"
Expand Down Expand Up @@ -516,6 +586,10 @@ docs-build:
seed-kube:
@{{ dotfiles_dir }}/scripts/bw-seed-kube.sh

# Seed Bitwarden with the omp auth-broker token via SSH to spindle
seed-omp-broker:
@{{ dotfiles_dir }}/scripts/bw-seed-omp-broker.sh

# Show all online fleet machines' doctor status in parallel
doctor-fleet:
#!/usr/bin/env bash
Expand Down
85 changes: 58 additions & 27 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ run_phase1() {
echo "═══════════════════════════════════════════════════"
echo ""

cd "$DOTFILES_DIR"
cd "$DOTFILES_DIR" || return 1
local rc=0 urc=0
ansible-playbook \
--connection=local \
-l "$MACHINE_NAME" \
Expand All @@ -331,7 +332,10 @@ run_phase1() {
-e ansible_host=127.0.0.1 \
-e "is_desktop=$is_desktop" \
--skip-tags secrets \
site.yml
site.yml || rc=$?
scripts/run-units.sh --no-secrets || urc=$?
[ "$rc" -ne 0 ] || rc=$urc
return "$rc"
}

# ── Bitwarden login ───────────────────────────────────────────────
Expand All @@ -346,6 +350,17 @@ setup_bitwarden() {
fi

local bw_status
# Prefer a usable session, including the cached session from a previous run.
if [ -n "${BW_SESSION:-}" ] && ! bw unlock --check >/dev/null 2>&1; then
unset BW_SESSION
fi
if [ -z "${BW_SESSION:-}" ] && [ -s /tmp/bw_session ]; then
local cached_session
cached_session=$(cat /tmp/bw_session)
if BW_SESSION="$cached_session" bw unlock --check >/dev/null 2>&1; then
export BW_SESSION="$cached_session"
fi
fi
bw_status=$(bw status 2>/dev/null | python3 -c "import json,sys; print(json.load(sys.stdin)['status'])" 2>/dev/null || echo "unauthenticated")

echo ""
Expand All @@ -356,24 +371,24 @@ setup_bitwarden() {
echo "═══════════════════════════════════════════════════"
echo ""

case "$bw_status" in
unlocked)
BW_SESSION=$(bw unlock --raw 2>/dev/null || true)
;;
locked)
echo "Unlocking Bitwarden..."
BW_SESSION=$(bw unlock --raw)
;;
unauthenticated)
if [ -z "${BW_SESSION:-}" ]; then
if [ "$bw_status" = "unauthenticated" ]; then
echo "Log in to Bitwarden to complete setup:"
bw login
BW_SESSION=$(bw unlock --raw)
;;
esac

export BW_SESSION
rm -f /tmp/bw_session
(umask 077 && printf '%s' "$BW_SESSION" > /tmp/bw_session)
bw login || return 1
fi
echo "Unlocking Bitwarden..."
BW_SESSION=$(bw unlock --raw) || return 1
export BW_SESSION
fi
if [ -z "$BW_SESSION" ] || ! bw unlock --check >/dev/null 2>&1; then
echo "Bitwarden session is invalid; secrets setup skipped." >&2
return 1
fi
if ! timeout 15 bw sync >/dev/null; then
echo "Bitwarden sync failed; secrets setup skipped." >&2
return 1
fi
(umask 077 && printf '%s' "$BW_SESSION" > /tmp/bw_session) || return 1
return 0
}

Expand All @@ -382,17 +397,20 @@ run_phase2() {
local is_desktop="false"
[ "$MACHINE_TYPE" = "desktop" ] && is_desktop="true"

cd "$DOTFILES_DIR"
cd "$DOTFILES_DIR" || return 1
local rc=0 urc=0
ansible-playbook \
--connection=local \
-l "$MACHINE_NAME" \
-e "target=$MACHINE_NAME" \
-e ansible_connection=local \
-e ansible_host=127.0.0.1 \
-e "is_desktop=$is_desktop" \
-e "bw_session=${BW_SESSION:-}" \
--tags secrets \
site.yml
site.yml || rc=$?
scripts/run-units.sh --tags omp,secrets || urc=$?
[ "$rc" -ne 0 ] || rc=$urc
return "$rc"
}

# ── Push inventory changes (after GitHub auth is set up) ─────────
Expand Down Expand Up @@ -434,12 +452,23 @@ main() {
ensure_sudo
ensure_tailscale
install_collections
run_phase1
local rc=0 phase_rc=0
run_phase1 || rc=$?

# Phase 2 runs if Bitwarden CLI is now available (installed by Homebrew in Phase 1)
# Phase 2 still runs after a Phase 1 failure, if the vault is available.
if setup_bitwarden; then
run_phase2
push_inventory_changes
run_phase2 || phase_rc=$?
[ "$rc" -ne 0 ] || rc=$phase_rc
if [ "$phase_rc" -eq 0 ]; then
push_inventory_changes || { phase_rc=$?; [ "$rc" -ne 0 ] || rc=$phase_rc; }
fi
else
echo "Bitwarden setup failed; Phase 2 was not run." >&2
[ "$rc" -ne 0 ] || rc=1
fi
if [ "$rc" -ne 0 ]; then
echo "Bootstrap failed (exit $rc); address the errors and re-run." >&2
return "$rc"
fi

echo ""
Expand All @@ -453,4 +482,6 @@ main() {
echo " To re-apply anytime: just apply"
}

main
if [[ -z "${BASH_SOURCE[0]:-}" || "${BASH_SOURCE[0]}" == "$0" ]]; then
main
fi
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Core Concepts

- [Architecture](architecture.md)
- [Units](units.md)
- [Inventory & Groups](inventory.md)
- [Secrets with Bitwarden](bitwarden.md)
- [Onboarding a New Machine](onboarding.md)
Expand Down
10 changes: 5 additions & 5 deletions docs/src/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ SSHes to the target, forwards the Bitwarden session token, pulls the latest dotf
just apply-tags kube,shell
```

Runs only roles tagged with the given tags. Useful for deploying specific configs without running the full playbook. `--skip-tags` works the same way in reverse — `just apply-nosecrets` is `--skip-tags secrets` under the hood (see [Bitwarden](bitwarden.md)).
Runs roles and units selected by the given tags. Explicit secret-bearing tags first resolve and sync Bitwarden; unrelated tags do not prompt. `just apply-nosecrets` passes `--skip-tags secrets` to Ansible and `--no-secrets` to units (see [Bitwarden](bitwarden.md)).

#### Available Tags

Expand All @@ -64,7 +64,7 @@ Every role in `site.yml` carries one or more tags. Tags fall into two kinds: **p
| System + packages | `bitwarden` | `secrets`, `bitwarden` |
| System + packages | `shell_fonts` | `dotfiles`, `shell`, `fonts` |
| System + packages | `shell_dotfiles` | `dotfiles`, `shell`, `dotfiles` |
| System + packages | `omp` | `dotfiles`, `omp` |
| System + packages | `omp` unit | `dotfiles`, `omp` |
| System + packages | `hive_ops` | `hive`, `hive_ops` |
| System + packages | `git` | `dotfiles`, `git` |
| System + packages | `neovim` | `dotfiles`, `neovim`, `editor` |
Expand Down Expand Up @@ -96,7 +96,7 @@ Common phase-wide selections:
- `--tags secrets` — every BW-backed role only (`bitwarden`, `shell_atuin`, `shell_ai`, `ssh_keys`, `github`, `tailscale`, `kube`, `forgejo_registry`)
- `--tags desktop` — every desktop-app role (`flatpak`, `bluefin_common`, `gnome`, `zen_browser`, `pipewire_audio`)
- `--tags services` — every long-running service role (`syncthing`, `systemd`, `bst_dashboard`, `kirocrew`, `proxy`, `tailscale_cert`)
- `--skip-tags secrets` — everything except BW-backed roles. This is `just apply-nosecrets`, which the daily `dotfiles-update.timer` and the `dots` alias run; `bootstrap.sh` and `phone-apply.service` pass `--skip-tags secrets` directly
- `--skip-tags secrets` — everything except BW-backed roles. `just apply-nosecrets`, bootstrap's first phase and `phone-apply.service` also pass `--no-secrets` to units.

Tags do not replace the [`skip_*` host_vars flags](#skip-flags) or inventory-group gating described below — a role can be tag-selected and still no-op if its `when:` condition (group membership, `skip_*`, `machine_profile`) evaluates false.

Expand Down Expand Up @@ -144,13 +144,13 @@ Every flag `site.yml` reads, all defaulting to `false` (`| default(false)`). Set
| `skip_proxy` | `proxy` | not `vps`, not `vm-test` | `mumbai`, `punjab`, `termux` |
| `skip_tailscale_cert` | `tailscale_cert` | not `vps`, not `vm-test` | `mumbai`, `termux` |

"desktop" is `is_desktop` (membership of the `desktop` group unless overridden); "not `vm-test`" is `machine_profile != 'vm-test'`. Roles without a flag either always run or are gated another way: `termux_packages` (`termux_hosts` group), `omp` (not Termux), `kirocrew` (`kirocrew_enabled: true`), `server_hardening` (`server`/`vps` groups), `retired_services` (not `vps`, not Termux, systemd hosts).
"desktop" is `is_desktop` (membership of the `desktop` group unless overridden); "not `vm-test`" is `machine_profile != 'vm-test'`. Roles without a flag either always run or are gated another way: `termux_packages` (`termux_hosts` group), `kirocrew` (`kirocrew_enabled: true`), `server_hardening` (`server`/`vps` groups), `retired_services` (not `vps`, not Termux, systemd hosts). The `omp` unit skips Termux.

## Secrets Flow

1. Ansible requests a Bitwarden session (env var, cached file, or interactive unlock)
2. The `bitwarden` role resolves `BW_SESSION` and caches it
3. Secrets-tagged roles consume the session token for API calls
4. For remote applies, `scripts/bw-resolve.sh remote <host>` unlocks the vault on the target and the recipe exports that session inline over `ssh -t` (no `SendEnv`) — see [Remote Apply](bitwarden.md#remote-apply)
4. For remote applies, `scripts/bw-resolve.sh remote <host>` unlocks the vault on the target and SSH forwards that session using `SendEnv=BW_SESSION`; the target must accept it — see [Remote Apply](bitwarden.md#remote-apply).

No secrets are stored in git — the repo is fully public.
Loading
Loading