Skip to content
Merged
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
48 changes: 48 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -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
92 changes: 17 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<p align="center">
<img src="assets/logo.svg" alt="" width="120">
<img src="docs/assets/logo.svg" alt="" width="120">
</p>

<h1 align="center">sshwarm</h1>

<p align="center"><b><a href="https://k9scli.io">k9s</a> for ssh.</b> Your Linux machines, their services, containers and logs, in one terminal.</p>

<p align="center"><a href="https://asciinema.org/a/jHeN9sWGwwmMRVq2" target="_blank"><img src="assets/sshwarm.gif" alt="" width="720"></a></p>
<p align="center">
<a href="https://github.com/cmdoret/sshwarm/actions/workflows/checks.yml"><img src="https://img.shields.io/github/actions/workflow/status/cmdoret/sshwarm/checks.yml?branch=main&amp;label=checks" alt="checks"></a>
<a href="https://github.com/cmdoret/sshwarm/releases/latest"><img src="https://img.shields.io/github/v/release/cmdoret/sshwarm" alt="release"></a>
<a href="https://cmdoret.net/sshwarm"><img src="https://img.shields.io/badge/docs-cmdoret.net%2Fsshwarm-blue" alt="docs"></a>
<a href="LICENSE"><img src="https://img.shields.io/github/license/cmdoret/sshwarm" alt="license"></a>
</p>

<p align="center"><a href="https://asciinema.org/a/jHeN9sWGwwmMRVq2" target="_blank"><img src="docs/assets/sshwarm.gif" alt="" width="720"></a></p>

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
Expand All @@ -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,<br>with ~/.ssh/config" --> m["each machine<br>runs probe.sh"]
m -- "OS, reboot, usage,<br>units, stacks" --> sw
sw --> ui["browser:<br>machines, services,<br>stacks, containers"]
ui -. "logs, describe, shell,<br>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).
50 changes: 50 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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 <machine>`.
- 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.
7 changes: 7 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Summary

[Quickstart](quickstart.md)

- [Configuration](configuration.md)
- [Architecture](architecture.md)
- [Contributing](CONTRIBUTING.md)
72 changes: 72 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Architecture

```mermaid
flowchart LR
src["hosts.toml<br>or flake"] --> sw[sshwarm]
sw -- "ssh, every --refresh:<br>probe.sh on stdin" --> m["each machine"]
m -- "### sections:<br>OS, usage, units, stacks" --> sw
sw --> ui["browser,<br>or status"]
ui -. "logs, describe,<br>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
`### <name>` sections; a command that fails prints `### error <name>`.
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.
File renamed without changes
File renamed without changes
13 changes: 13 additions & 0 deletions docs/book.toml
Original file line number Diff line number Diff line change
@@ -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"]
72 changes: 72 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Configuration

No config file: the machines, flags, and environment variables.

## Machines

sshwarm takes the first of:

1. `--hosts <file>` or `--flake <dir>`
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 <file>` | `SSHWARM_HOSTS` | | hosts file |
| `--flake <dir>` | `SSHWARM_FLAKE` | | flake with `deploy.nodes` |
| `--timeout <duration>` | `SSHWARM_TIMEOUT` | `20s` | probe timeout, per machine |
| `--refresh <duration>` | `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 <machine>`, which
prints a machine's raw probe output. `sshwarm <command> --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.
14 changes: 14 additions & 0 deletions docs/mermaid.js
Original file line number Diff line number Diff line change
@@ -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" });
});
Loading
Loading