GitHub 0
Skip to content

Hosts

Check fleet entries, expand Tailscale tags into machines, run one command on each over ssh, and record the fleet.

The fleethosts entry takes a list of fleet entries and runs a command on the machines they name.

import { recordedHosts, resolveHosts, runOnHosts, standardPlaces } from "fleethosts";

const places = standardPlaces("mytool");
const entries = recordedHosts(places) ?? ["vps", "tag:servers"];

const runs = await runOnHosts(
  places,
  async (remoteCommand) => (await resolveHosts(places, entries, { remoteCommand })).targets,
  ["df", "-h", "/"],
);
ts

A fleet entry is one of:

EntryExample
A host name or ssh_config aliasvps
A user and hostme@build.example.com
An IP address100.64.0.7, fd7a:115c::7
An ssh URL, with a portssh://deploy@build.example.com:2222, ssh://[fd7a::7]:22
A Tailscale tagtag:servers

fleetEntryProblem(entry) says why an entry can't be used, or returns undefined:

import { fleetEntryProblem } from "fleethosts";

fleetEntryProblem("vps:2222");
// '"vps:2222" has a port; write it as ssh://vps:2222'
fleetEntryProblem("-oProxyCommand=x");
// '"-oProxyCommand=x" starts with "-", so ssh would read it as an option'
fleetEntryProblem("tag:1st");
// '"tag:1st" is not a Tailscale tag: write tag: and then a letter followed by letters, digits or dashes, such as "tag:servers"'
ts

Nothing that passes the check is read by ssh as an option or split by a shell. hostProblem and tagProblem check one kind each, and isTag tells them apart.

resolveHosts(places, entries, options) turns entries into FleetTargets, the machines to run on.

  • A named host stays as written.
  • A tag: entry becomes every machine on the tailnet with that tag, except this one. Each gets its MagicDNS name, or its Tailscale IP when MagicDNS is off or the name doesn't work. Tags match without regard to case.
  • A host you named wins over a tag's match for the same machine, and the first tag wins over later ones. Distinct explicit SSH destinations, such as deploy@box and admin@box or different ports, can still produce separate runs on the same machine.

It returns:

FieldWhat it holds
targetsThe FleetTargets to run on
warningsA tag that matches no machine, or a machine with no name or address ssh takes
enrollmentEach entry's targets, or null when a tag skipped one of its machines
selfThis machine as Tailscale lists it, when Tailscale was asked

A target can't always be run on. Then it says why:

  • offline: true: Tailscale says the machine is offline, so ssh isn't tried.
  • error: the tag couldn't be expanded (Tailscale isn't installed, logged in or running), or no route was verified.

Before it trusts a tag's machine, fleethosts asks ssh -G where your ssh config sends each name, and checks that the name resolves to that machine's Tailscale addresses. A Host block that sends a name through a proxy, or to another machine, fails that check, and the target gets the error could not verify an SSH route to the Tailscale peer.

The check runs with the exact command ssh will run, because ssh's config can send a host elsewhere for one command. That is why resolveHosts needs remoteCommand, and why runOnHosts takes a resolver function: it builds the command, then hands it to your resolver.

runOnHosts(places, targets, argv, timeoutMs = 30_000, { here = true })
ts

targets is a FleetTarget[], or a function that gets the remote command and returns them. argv is the command and its arguments; each one is quoted for the remote shell. Every machine runs it at the same time.

Each machine gives one RunResult:

interface RunResult {
  host: string;
  reachable: boolean;
  exitCode?: number;
  stdout: string;
  stderr: string;
  error?: string;
  truncated?: true;
}
ts
ResultreachableexitCodeerror
The command exitedtrueits codenone
The command was still running at the deadlinetruenonetimed out after 30 s, so the command was told to stop
The connection droppedtruenonethe connection dropped before the command finished
ssh got no answerfalsenoneno answer within 30 s, or ssh's last lines
Offline in Tailscale, or a bad targetfalsenonewhy

Read exitCode only when it is there. A machine that didn't answer has no exit code, so it never reads as a failed command.

ssh runs with BatchMode=yes, so a host that asks for a password is unreachable. The script goes to the host's login shell as base64, so fish and tcsh run it like sh.

With here: true, the default, the command also runs on this machine, from the home folder, and its result comes first under hostname(). A target that turns out to be this machine is then left out, whatever it is called: an ssh alias, a MagicDNS name and an IP all can be. Each host checks for a folder this run made, which only this user on this machine owns.

hereRuns hereTargets that are this machine
trueyesleft out
"leave out"noleft out
falsenorun like any host

A command still running at timeoutMs is stopped, with what it started. Over ssh, the command runs in its own process group (from setsid, or perl on macOS) and watches its stdin. When the connection drops, the group gets SIGTERM, then SIGKILL two seconds later. A command that traps SIGTERM still stops.

Output is kept up to 64 MiB per stream. Past that, truncated is true and only the end is kept.

recordHosts(places, entries) writes the entries to <state>/hosts.json, replacing it whole. recordedHosts(places) reads them back.

recordHosts(places, ["vps", "tag:servers"]);
recordedHosts(places); // ["vps", "tag:servers"]
ts

recordedHosts returns undefined when there is no file, the file is malformed, or another config folder wrote it. An empty list is a fleet with no hosts, which is not the same as no record. The record lets a second program run on the fleet the first one last saw.

tailnet(places) lists the other machines on the tailnet, from tailscale status --json. When tailscale isn't on PATH, fleethosts looks where the installers put it: Homebrew and the Mac app on macOS, Program Files on Windows. <PROGRAM>_TAILSCALE names another CLI and turns that search off.