diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml new file mode 100644 index 0000000..795d541 --- /dev/null +++ b/.github/workflows/docs.yml @@ -0,0 +1,48 @@ +--- +name: Docs +on: + pull_request: # builds it, to catch what breaks it + branches: [main] + paths: ["docs/**", ".github/workflows/docs.yml"] + push: # builds and publishes it + branches: [main] + paths: ["docs/**", ".github/workflows/docs.yml"] +permissions: + contents: read +concurrency: # one publish at a time; a new push supersedes the previous one's + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true +jobs: + build: + name: Build the website + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@v5 + + - name: Install Nix + uses: cachix/install-nix-action@v31 + + - name: Build + run: nix develop .#ci --command just docs::build + + - name: Upload + uses: actions/upload-pages-artifact@v5 + with: + path: .output/docs + + publish: + name: Publish to GitHub Pages + if: github.event_name == 'push' + needs: build + runs-on: ubuntu-latest + permissions: + pages: write # to publish + id-token: write # to prove it's this workflow + environment: + name: github-pages + url: ${{ steps.publish.outputs.page_url }} + steps: + - name: Publish + id: publish + uses: actions/deploy-pages@v5 diff --git a/README.md b/README.md index 642c7ce..a8eacb9 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,19 @@

- +

sshwarm

k9s for ssh. Your Linux machines, their services, containers and logs, in one terminal.

-

+

+ checks + release + docs + license +

+ +

Tired of ssh-ing into box after box to find out what's deployed where, which service died overnight and which container is crash-looping? **sshwarm** shows the whole swarm at a @@ -23,84 +30,19 @@ glance, with the logs of whatever is red a keypress away. go install github.com/cmdoret/sshwarm@latest ``` -Or run directly from the flake using nix: -```sh -nix run github:cmdoret/sshwarm -- --hosts hosts.toml -``` - -Or download a binary from the [releases](https://github.com/cmdoret/sshwarm/releases). - -## Requirements - -Linux machines with systemd and bash, reachable over ssh without a password prompt -(keys or agent), where the ssh user: - -- is in the `systemd-journal` group. -- can run docker without password (either rootless or with passwordless sudo). - -To start/stop services, or reboot machines, the user must also be root, or able to -run `systemctl` with passwordless sudo. +Or `nix run github:cmdoret/sshwarm`, a [release](https://github.com/cmdoret/sshwarm/releases) +binary, or the [container image](docs/quickstart.md#container). ## Use ```sh -sshwarm --hosts hosts.toml +sshwarm --hosts hosts.toml # one [[host]] per machine, with its name = "web-1" ``` -Machines behave like namespaces in k9s: - -* `:` goes to a table: `:ctr web` lists the containers of the machine `web`. -`:ctr all` (or `:ctr` from the machines) those of every machine, same with `:svc` and `:st`. - -### Hosts file +Then `?` lists the keys. -Only `name` is required. ssh looks up `host`, or else `name`, in `~/.ssh/config` for -anything not set in the file, like ports or jump hosts. Save it as -`~/.config/sshwarm/hosts.toml`, or set `SSHWARM_HOSTS` or `SSHWARM_FLAKE`, to skip -the flag. See `sshwarm --help` for more. - -```toml -# hosts.toml: one [[host]] per machine -[[host]] -name = "db" # ssh db: user, key, port... from "Host db" in ~/.ssh/config - -[[host]] -name = "web-1" -host = "web-1.example.org" # ssh -i ~/.ssh/web deploy@web-1.example.org -user = "deploy" -key = "~/.ssh/web" -``` - -### Nix flake - -Instead of a hosts file, you may provide a Nix flake exposing `deploy.nodes` in its outputs. -See [deploy-rs](https://github.com/serokell/deploy-rs#overall-usage) for more information. - -```sh -sshwarm --flake ./nix -``` - -## How it works - -```mermaid -flowchart LR - src["hosts.toml or flake.nix"] --> sw[sshwarm] - sw -- "ssh, in parallel,
with ~/.ssh/config" --> m["each machine
runs probe.sh"] - m -- "OS, reboot, usage,
units, stacks" --> sw - sw --> ui["browser:
machines, services,
stacks, containers"] - ui -. "logs, describe, shell,
on demand" .-> m -``` - -## Docker images - -Container images are also available, but they suffer from a few usability limitations: - -* nix is not included in the image, so you can only use `hosts.toml` -* `hosts.toml` must fully define the hosts, as ssh refuses a mounted `~/.ssh/config`. - -```sh -docker run --rm -it -v ./hosts.toml:/hosts.toml:ro \ - -v ~/.ssh/id_ed25519:/root/.ssh/id_ed25519:ro -v ~/.ssh/known_hosts:/root/.ssh/known_hosts:ro \ - ghcr.io/cmdoret/sshwarm --hosts /hosts.toml -``` +## Docs +[Quickstart](docs/quickstart.md) · [Configuration](docs/configuration.md) · +[Architecture](docs/architecture.md) · [Contributing](docs/CONTRIBUTING.md), also at +[cmdoret.net/sshwarm](https://cmdoret.net/sshwarm). diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md new file mode 100644 index 0000000..86fb4f9 --- /dev/null +++ b/docs/CONTRIBUTING.md @@ -0,0 +1,50 @@ +# Contributing + +## Setup + +With [Nix](https://nixos.org/download) and [direnv](https://direnv.net): + +```sh +git clone https://github.com/cmdoret/sshwarm && cd sshwarm +direnv allow # or: nix develop +``` + +The dev shell has Go and all the tooling to work in the repo. + +## Tasks + +| | | +|---|---| +| `just test` | tests | +| `just lint` | golangci-lint, shellcheck, `nix flake check` | +| `just format` | format everything | +| `just build` | binary and Nix package, into `.output/` | +| `just update` | Go dependencies, flake lock, vendor hash | +| `just docs::serve` | this documentation, live | +| `just` | every recipe | + +The git hooks format, lint, and update the Nix vendor hash when `go.mod` changes: +the commit then fails; stage the new hash and commit again. + +## Tests + +- No real ssh: `testutil.FakeBin` puts a fake `ssh` first on `PATH`. +- The probe fixtures in `internal/swarm/testdata` come from real machines: + `sshwarm probe `. +- The browser's tests type keys and read the screen: `internal/ui/tui_test.go`. + +## Code + +See [architecture](architecture.md). +Style: [Google's Go style guide](https://google.github.io/styleguide/go/). +Comments say why, not what. + +## Release + +```sh +just release::bump minor # or patch, major, x.y.z: commits the version, tags it +git push --follow-tags +``` + +Then publish a GitHub release of the tag. CI uploads the binaries and pushes the +images: `x.y.z`, and `latest` unless it's a pre-release. diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md new file mode 100644 index 0000000..78db0cf --- /dev/null +++ b/docs/SUMMARY.md @@ -0,0 +1,7 @@ +# Summary + +[Quickstart](quickstart.md) + +- [Configuration](configuration.md) +- [Architecture](architecture.md) +- [Contributing](CONTRIBUTING.md) diff --git a/docs/architecture.md b/docs/architecture.md new file mode 100644 index 0000000..881a570 --- /dev/null +++ b/docs/architecture.md @@ -0,0 +1,72 @@ +# Architecture + +```mermaid +flowchart LR + src["hosts.toml
or flake"] --> sw[sshwarm] + sw -- "ssh, every --refresh:
probe.sh on stdin" --> m["each machine"] + m -- "### sections:
OS, usage, units, stacks" --> sw + sw --> ui["browser,
or status"] + ui -. "logs, describe,
shell, actions" .-> m +``` + +1. sshwarm reads the machines from a hosts file or a flake. +2. It pipes [`probe.sh`](https://github.com/cmdoret/sshwarm/blob/main/internal/swarm/probe.sh) + to every machine over ssh, in parallel, and waits for all. The browser does it + again `--refresh` after the last round ended. +3. The probe only reads: OS, uptime, usage, pending reboot, `systemctl show` of every + service, `docker ps` and `docker inspect` of compose containers. It prints + `### ` sections; a command that fails prints `### error `. +4. sshwarm parses each machine's output into a report, and rates what's in it: idle, + fine, warning or broken. A machine's problems are what's broken, what restarted on + its own in the last 10 minutes, a disk 90% full, and the probe's errors. + `status` exits 1 on any. +5. Logs, descriptions, shells and actions each run their own ssh command, on demand. + +## In time + +A refresh, a unit's logs, then a container's restart: + +```mermaid +sequenceDiagram + actor You + participant UI as browser + participant SW as swarm + participant M as each machine + + loop every --refresh, from the end of the last round + UI->>SW: probe + SW->>M: ssh: bash, probe.sh on stdin, to all at once + M-->>SW: OS, usage, units, containers, in sections + SW-->>UI: a report per machine, rated + end + + You->>UI: l, on a unit + UI->>M: ssh: journalctl -u unit -n 300 + M-->>UI: its last lines + Note over UI,M: s follows them: journalctl -f, with a terminal + + You->>UI: R, on a container + UI-->>You: restart container web-1 on app-1? y/N + You->>UI: y + UI->>M: ssh: docker restart web-1 + UI->>SW: probe again +``` + +## Code + +| | | +|---|---| +| `main.go` | flags, commands | +| `internal/inventory` | hosts file, flake | +| `internal/remote` | ssh: to the end, as a stream, or with the terminal; shared connections | +| `internal/swarm` | the probe and its parsing, the report, the commands for logs, descriptions and actions | +| `internal/ui` | the browser: screens, tables, cards, keys; `status`'s table and JSON | + +## Choices + +- **Agentless.** A bash script over the user's own ssh: nothing to install, and + their ssh config, keys and agent just work. +- **Read-only probe.** Actions ask first, and `--read-only` turns them off. +- **The machine's clock.** Ages are computed with the machine's time, not yours. +- **Bounded waits.** The probe stops itself on the machine before sshwarm gives up + on it, and gives `df` 2 s, as a hung mount would hang it. diff --git a/assets/logo.svg b/docs/assets/logo.svg similarity index 100% rename from assets/logo.svg rename to docs/assets/logo.svg diff --git a/assets/sshwarm.gif b/docs/assets/sshwarm.gif similarity index 100% rename from assets/sshwarm.gif rename to docs/assets/sshwarm.gif diff --git a/docs/book.toml b/docs/book.toml new file mode 100644 index 0000000..cd23b83 --- /dev/null +++ b/docs/book.toml @@ -0,0 +1,13 @@ +# The documentation website, built by mdBook from these markdown files: `just docs::serve`. +[book] +title = "sshwarm" +src = "." + +[build] +build-dir = "../.output/docs" + +[output.html] +site-url = "/sshwarm/" # served under it, as GitHub Pages serves a project: the 404 page's links need it +git-repository-url = "https://github.com/cmdoret/sshwarm" +edit-url-template = "https://github.com/cmdoret/sshwarm/edit/main/docs/{path}" +additional-js = ["mermaid.js"] diff --git a/docs/configuration.md b/docs/configuration.md new file mode 100644 index 0000000..3394430 --- /dev/null +++ b/docs/configuration.md @@ -0,0 +1,72 @@ +# Configuration + +No config file: the machines, flags, and environment variables. + +## Machines + +sshwarm takes the first of: + +1. `--hosts ` or `--flake ` +2. `SSHWARM_HOSTS` or `SSHWARM_FLAKE` +3. `$XDG_CONFIG_HOME/sshwarm/hosts.toml`, by default `~/.config/sshwarm/hosts.toml` + +A hosts file and a flake together are an error. + +### Hosts file + +```toml +[[host]] +name = "web-1" # required: sshwarm's name for it +host = "web-1.example.org" # default: name +user = "deploy" # default: ~/.ssh/config's, else yours +key = "~/.ssh/web" # default: ~/.ssh/config's, or your agent +``` + +ssh looks `host` (or `name`) up in `~/.ssh/config` for anything else: port, jump +host... Unknown keys are an error. + +### Nix flake + +```sh +sshwarm --flake ./nix +``` + +Reads the flake's `deploy.nodes`, as [deploy-rs](https://github.com/serokell/deploy-rs) +does: each node's `hostname`, and its `sshUser` and `sshOpts`, or `deploy`'s. Needs `nix`. + +## Flags + +| Flag | Variable | Default | | +|---|---|---|---| +| `--hosts ` | `SSHWARM_HOSTS` | | hosts file | +| `--flake ` | `SSHWARM_FLAKE` | | flake with `deploy.nodes` | +| `--timeout ` | `SSHWARM_TIMEOUT` | `20s` | probe timeout, per machine | +| `--refresh ` | `SSHWARM_REFRESH` | `30s` | how often the browser probes | +| `--read-only` | `SSHWARM_READ_ONLY` | off | no actions, no shells | + +A flag beats its variable. `SSHWARM_READ_ONLY` is on unless empty, `0` or `false`. + +Commands: `status [--format text|json] [machine...]`, and `probe `, which +prints a machine's raw probe output. `sshwarm --help` for more. + +## Requirements + +Each machine runs Linux with systemd and bash. ssh must log in without a prompt: +a key or an agent, and a known host key. + +| To | the ssh user | +|---|---| +| see the machine and its units | needs nothing more | +| read logs | is root, or in `systemd-journal` | +| see, start and stop compose stacks and containers | runs `docker`: docker group, rootless, or passwordless `sudo` | +| start and stop units, reboot | is root, or runs `systemctl` with passwordless `sudo` | + +Pending reboots are detected on NixOS, Debian, Ubuntu, Fedora, RHEL and Arch. On +NixOS, sshwarm also shows the last deploy and its git revision, if the flake sets +`system.configurationRevision`. + +## Connections + +The browser keeps one ssh connection per machine (`ControlMaster`), its socket in +`~/.ssh`, closed a minute after you quit. It leaves ssh alone if your ssh config +sets `ControlPath` or `ControlMaster` for the machine, or `~/.ssh` isn't writable. diff --git a/docs/mermaid.js b/docs/mermaid.js new file mode 100644 index 0000000..0169172 --- /dev/null +++ b/docs/mermaid.js @@ -0,0 +1,14 @@ +// Draws the ```mermaid blocks as diagrams, as GitHub does: mdBook leaves them as code. +import("https://cdn.jsdelivr.net/npm/mermaid@12.2.0/dist/mermaid.esm.min.mjs").then(({ default: mermaid }) => { + const dark = ["coal", "navy", "ayu"].some((theme) => document.documentElement.classList.contains(theme)); + mermaid.initialize({ startOnLoad: false, theme: dark ? "dark" : "default" }); + + for (const code of document.querySelectorAll("code.language-mermaid")) { + const diagram = document.createElement("pre"); + diagram.className = "mermaid"; + diagram.textContent = code.textContent; + code.parentElement.replaceWith(diagram); + } + + mermaid.run({ querySelector: "pre.mermaid" }); +}); diff --git a/docs/quickstart.md b/docs/quickstart.md new file mode 100644 index 0000000..aaed505 --- /dev/null +++ b/docs/quickstart.md @@ -0,0 +1,67 @@ +

sshwarm

+ +

k9s for ssh. Your Linux machines, their +services, containers and logs, in one terminal: what's deployed where, what died +overnight, what's crash-looping, and the logs of whatever is red a keypress away.

+ +# Quickstart + +## Install + +| From | | +|---|---| +| Go | `go install github.com/cmdoret/sshwarm@latest` | +| Nix | `nix run github:cmdoret/sshwarm` | +| Binary | [releases](https://github.com/cmdoret/sshwarm/releases): Linux and macOS, amd64 and arm64 | +| Container | [see below](#container) | + +## List the machines + +In `~/.config/sshwarm/hosts.toml`, one `[[host]]` per machine: + +```toml +[[host]] +name = "db" # ssh db: the rest from ~/.ssh/config + +[[host]] +name = "web-1" +host = "web-1.example.org" # ssh -i ~/.ssh/web deploy@web-1.example.org +user = "deploy" +key = "~/.ssh/web" +``` + +Using deploy-rs? Skip the file: `sshwarm --flake ./nix`. See [configuration](configuration.md). + +## Run + +```sh +sshwarm # browse +sshwarm status # print the table, exit 1 on any problem +sshwarm status --format json # for scripts +``` + +The machines need systemd, bash, and ssh without a password prompt. Logs and +containers need more: see [requirements](configuration.md#requirements). + +## Keys + +| Key | | +|---|---| +| `enter` | open | +| `l`, `d` | logs, describe | +| `/` | filter; in logs, search | +| `:ctr web` | containers of `web`; `:ctr all`, of every machine. `tab` completes | +| `?` | every key | + +## Container + +```sh +docker run --rm -it \ + -v ./hosts.toml:/hosts.toml:ro \ + -v ~/.ssh/id_ed25519:/root/.ssh/id_ed25519:ro \ + -v ~/.ssh/known_hosts:/root/.ssh/known_hosts:ro \ + ghcr.io/cmdoret/sshwarm --hosts /hosts.toml +``` + +No nix inside, so no `--flake`. ssh refuses a mounted `~/.ssh/config`: the hosts +file must set `host`, `user` and `key`. diff --git a/justfile b/justfile index 42e71c6..4f7b4d9 100644 --- a/justfile +++ b/justfile @@ -14,6 +14,9 @@ mod release 'tools/just/release.just' # Manage container images [group("modules")] mod image 'tools/just/image.just' +# Build the documentation website +[group("modules")] +mod docs 'tools/just/docs.just' # List the recipes [private] diff --git a/tools/just/docs.just b/tools/just/docs.just new file mode 100644 index 0000000..612492f --- /dev/null +++ b/tools/just/docs.just @@ -0,0 +1,18 @@ +set positional-arguments +root_dir := justfile_directory() +book_dir := root_dir / "docs" +nu_log := root_dir / "tools/nu/log.nu" + +# List the recipes +[private] +default: + @just -f "{{source_file()}}" --list --unsorted + +# Build the documentation website into .output/docs +build *args: + @"{{nu_log}}" info "Building the documentation..." + mdbook build "$@" "{{book_dir}}" + +# Serve the documentation website, rebuilt on change, and open it +serve *args: + mdbook serve --open "$@" "{{book_dir}}" diff --git a/tools/nix/shells.nix b/tools/nix/shells.nix index 11e7c55..d4c4b6c 100644 --- a/tools/nix/shells.nix +++ b/tools/nix/shells.nix @@ -8,6 +8,7 @@ let golangci-lint govulncheck just + mdbook nixfmt nushell shellcheck