relayrunner 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +8 -3
  2. package/dist/cli.js +185 -36
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -29,7 +29,10 @@ folder can't sign in as the machine.
29
29
  While it runs, the machine is online: it keeps a WebSocket open to the gateway,
30
30
  runs the jobs it's sent (one at a time) and prints what happens, including the
31
31
  repo each job is from. It reconnects on its own if the connection drops. Ctrl-C
32
- takes it offline; a job that's running stops with it.
32
+ takes it offline; a job that's running stops with it. Press `p` to pause or
33
+ resume it without going offline (see `relayrunner pause`). The terminal tab's
34
+ title shows the machine and what it's doing, such as
35
+ `RelayRunner · M3P0 · Waiting for jobs`.
33
36
 
34
37
  Before it starts a job, `connect` checks the job's runner config: it must hold
35
38
  only the files GitHub's do, every server in it must be GitHub's, over https, and
@@ -46,7 +49,8 @@ approved the machine also gets an email, since someone else may have its token.
46
49
  - `--server <url>` or `RELAYRUNNER_URL` points it at another gateway. Changing it signs in again.
47
50
  It must be https, except for `localhost` while developing, since the token goes
48
51
  out on every connect.
49
- - `--name <name>` changes the suggested machine name (default: the hostname).
52
+ - `--name <name>` changes the suggested machine name (default: the hostname if
53
+ one is set, otherwise the Mac's Computer Name from System Settings).
50
54
  - `--share only-me|anyone` sets who can run jobs on this machine without asking.
51
55
  It works on a machine that's already signed in too.
52
56
  - If the machine is removed in the dashboard, `connect` deletes the saved token.
@@ -66,7 +70,7 @@ as [`relayrunner init`](#relayrunner-init). No is remembered for that repo in
66
70
  works there. It never asks outside a repo, in a public repo, in a repo that's
67
71
  already set up, or without a terminal to answer.
68
72
 
69
- Jobs run in `actions/runner`, which is downloaded into
73
+ Jobs run in `actions/runner`, GitHub's official self-hosted runner, which is downloaded into
70
74
  `~/.relayrunner/runner/<version>` on first connect and checked against its
71
75
  SHA-256. Set `RELAYRUNNER_HOME` to keep everything somewhere else.
72
76
 
@@ -146,6 +150,7 @@ Pausing stops new jobs routing to this machine while it stays connected and
146
150
  enrolled. A job it's already running finishes. `resume` lets jobs route here
147
151
  again. Anyone in the workspace can do the same from the machine's ⋯ menu on the
148
152
  Runners page, and a running `relayrunner connect` prints when that happens.
153
+ In the terminal running `relayrunner connect`, `p` does the same.
149
154
  Both exit 1 when not signed in or when the token is no longer valid.
150
155
 
151
156
  ## `relayrunner share`
package/dist/cli.js CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
- import { arch, cpus, homedir, hostname, platform, totalmem } from "node:os";
3
2
  import * as clack from "@clack/prompts";
4
3
  import { defineCommand, runMain } from "citty";
5
4
  import { z } from "zod";
5
+ import { arch, cpus, homedir, hostname, platform, totalmem } from "node:os";
6
6
  import { basename, dirname, join, resolve } from "node:path";
7
7
  import { chmodSync, createWriteStream, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, renameSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
8
8
  import { execFile, spawn } from "node:child_process";
@@ -447,7 +447,7 @@ async function ensureRunner(options = {}) {
447
447
  version: download.version,
448
448
  dir
449
449
  };
450
- log(`Downloading actions/runner ${download.version}…`);
450
+ log(`Downloading GitHub's official runner (actions/runner ${download.version})…`);
451
451
  await install(root, download);
452
452
  log(`Installed actions/runner ${download.version} in ${dir}`);
453
453
  return {
@@ -1331,6 +1331,18 @@ function shortFile(file) {
1331
1331
  function colorDiff(diff) {
1332
1332
  return diff.split("\n").map((line) => line.startsWith("+") && !line.startsWith("+++") ? styleText("green", line) : line.startsWith("-") && !line.startsWith("---") ? styleText("red", line) : styleText("dim", line)).join("\n");
1333
1333
  }
1334
+ // machine-name.ts
1335
+ const MAX_LENGTH = 64;
1336
+ async function defaultMachineName(run$1 = run, system = platform(), host = hostname()) {
1337
+ const names = [];
1338
+ if (system === "darwin") for (const key of [
1339
+ "HostName",
1340
+ "ComputerName",
1341
+ "LocalHostName"
1342
+ ]) names.push(await run$1("scutil", ["--get", key]));
1343
+ names.push(host.replace(/\.local$/i, ""));
1344
+ return (names.find((name) => name?.trim()) ?? "").trim().slice(0, MAX_LENGTH);
1345
+ }
1334
1346
  // jit-config.ts
1335
1347
  const GITHUB_HOSTS = [
1336
1348
  /^github\.com$/,
@@ -1560,6 +1572,118 @@ function isAlive(pid) {
1560
1572
  return error.code === "EPERM";
1561
1573
  }
1562
1574
  }
1575
+ // live.ts
1576
+ const INITIAL_STATE = {
1577
+ link: "connecting",
1578
+ paused: false,
1579
+ job: null
1580
+ };
1581
+ /** The state after a connection event. */
1582
+ function nextState(state, event) {
1583
+ switch (event.type) {
1584
+ case "connecting": return {
1585
+ ...state,
1586
+ link: state.link === "online" ? "reconnecting" : state.link
1587
+ };
1588
+ case "connected": return {
1589
+ ...state,
1590
+ link: "online"
1591
+ };
1592
+ case "disconnected": return {
1593
+ ...state,
1594
+ link: "reconnecting"
1595
+ };
1596
+ case "run": return {
1597
+ ...state,
1598
+ job: event.routeId
1599
+ };
1600
+ case "job": return event.update.state === "started" || event.update.routeId !== state.job ? state : {
1601
+ ...state,
1602
+ job: null
1603
+ };
1604
+ case "paused": return {
1605
+ ...state,
1606
+ paused: event.paused
1607
+ };
1608
+ default: return state;
1609
+ }
1610
+ }
1611
+ /**
1612
+ * The terminal title for a state, such as "RelayRunner · M3P0 · Waiting for jobs". It leads with
1613
+ * the product so the tab says what's running in it, and ends with the part that changes.
1614
+ */
1615
+ function titleFor(state, machine) {
1616
+ return `RelayRunner · ${machine} · ${statusText(state)}`;
1617
+ }
1618
+ function statusText({ link, paused, job }) {
1619
+ if (link === "connecting") return "Connecting…";
1620
+ if (link === "reconnecting") return job ? "Running a job, reconnecting…" : "Reconnecting…";
1621
+ if (job) return paused ? "Running a job, then paused" : "Running a job";
1622
+ return paused ? "Paused" : "Waiting for jobs";
1623
+ }
1624
+ const ESC = "\x1B";
1625
+ const BEL = "\x07";
1626
+ const SAVE_TITLE = `${ESC}[22;0t`;
1627
+ const RESTORE_TITLE = `${ESC}[23;0t`;
1628
+ function titleSequence(title) {
1629
+ return `${ESC}]0;${title.replace(/[\u0000-\u001f\u007f]/g, "")}${BEL}`;
1630
+ }
1631
+ /** What a key pressed in the terminal asks for. */
1632
+ function keyAction(key) {
1633
+ if (key === "") return "interrupt";
1634
+ if (key === "p" || key === "P") return "toggle-pause";
1635
+ return null;
1636
+ }
1637
+ /**
1638
+ * Starts the live title (when stdout is a terminal) and the keys (when stdin is one): `p` calls
1639
+ * onTogglePause, and Ctrl-C is sent on as SIGINT, since raw mode stops the terminal doing that.
1640
+ */
1641
+ function startLive(options) {
1642
+ const { stdin, stdout } = process;
1643
+ const showTitle = stdout.isTTY;
1644
+ const keys = stdin.isTTY;
1645
+ let state = INITIAL_STATE;
1646
+ let title = titleFor(state, options.machine);
1647
+ let ended = false;
1648
+ if (showTitle) stdout.write(SAVE_TITLE + titleSequence(title));
1649
+ const onKey = (key) => {
1650
+ const action = keyAction(key);
1651
+ if (action === "toggle-pause") options.onTogglePause(state);
1652
+ else if (action === "interrupt") try {
1653
+ process.kill(0, "SIGINT");
1654
+ } catch {
1655
+ process.emit("SIGINT", "SIGINT");
1656
+ }
1657
+ };
1658
+ if (keys) {
1659
+ stdin.setRawMode(true);
1660
+ stdin.setEncoding("utf8");
1661
+ stdin.on("data", onKey);
1662
+ stdin.resume();
1663
+ }
1664
+ return {
1665
+ get state() {
1666
+ return state;
1667
+ },
1668
+ onEvent(event) {
1669
+ if (keys && event.type === "connected" && state.link === "connecting") clack.log.message("Press p to pause or resume, Ctrl-C to go offline.");
1670
+ state = nextState(state, event);
1671
+ const next = titleFor(state, options.machine);
1672
+ if (showTitle && next !== title) stdout.write(titleSequence(next));
1673
+ title = next;
1674
+ },
1675
+ end() {
1676
+ if (ended) return;
1677
+ ended = true;
1678
+ if (showTitle) stdout.write(RESTORE_TITLE);
1679
+ if (keys) {
1680
+ stdin.off("data", onKey);
1681
+ stdin.setRawMode(false);
1682
+ stdin.pause();
1683
+ }
1684
+ }
1685
+ };
1686
+ }
1563
1687
  // login.ts
1564
1688
  const CLIENT_ID = "relayrunner-cli";
1565
1689
  const DEVICE_GRANT = "urn:ietf:params:oauth:grant-type:device_code";
@@ -1735,6 +1859,14 @@ async function setPaused(options) {
1735
1859
  busy: parsed.data.machine.status?.state === "busy"
1736
1860
  };
1737
1861
  }
1862
+ /** setPaused with the machine's saved token, retrying with a newer one if it was rotated. */
1863
+ function pauseMachine(credentials, paused) {
1864
+ return withCurrentToken(credentials, (token) => setPaused({
1865
+ server: credentials.server,
1866
+ token,
1867
+ paused
1868
+ }));
1869
+ }
1738
1870
  // share.ts
1739
1871
  const SHARE_CHOICES = ["only-me", "anyone"];
1740
1872
  const MachineSharing = z.enum(["only_me", "anyone"]);
@@ -1860,7 +1992,7 @@ const connectCommand = defineCommand({
1860
1992
  },
1861
1993
  name: {
1862
1994
  type: "string",
1863
- description: "Suggested machine name when signing in (default: this computer's hostname)"
1995
+ description: "Suggested machine name when signing in (default: this computer's name)"
1864
1996
  },
1865
1997
  share: {
1866
1998
  type: "string",
@@ -1896,36 +2028,57 @@ async function connectMachine(args) {
1896
2028
  const runner = await ensureRunner({ log: (line) => clack.log.info(line) });
1897
2029
  const device = await detectDevice();
1898
2030
  const stop = new AbortController();
2031
+ const enrolled = credentials;
2032
+ let toggling = false;
2033
+ const live = startLive({
2034
+ machine: enrolled.machine.name,
2035
+ onTogglePause: async ({ paused }) => {
2036
+ if (toggling || stop.signal.aborted) return;
2037
+ toggling = true;
2038
+ const result = await pauseMachine(enrolled, !paused);
2039
+ toggling = false;
2040
+ if (result.status === "failed") clack.log.error(`Couldn't ${paused ? "resume" : "pause"}: ${result.reason}.`);
2041
+ }
2042
+ });
1899
2043
  const onInterrupt = () => {
1900
- if (stop.signal.aborted) process.exit(130);
2044
+ if (stop.signal.aborted) {
2045
+ live.end();
2046
+ process.exit(130);
2047
+ }
1901
2048
  stop.abort();
1902
2049
  };
1903
2050
  process.on("SIGINT", onInterrupt);
1904
2051
  let online = false;
1905
- const end = await connect({
1906
- server: credentials.server,
1907
- token: credentials.token,
1908
- hello: {
1909
- machineId: credentials.machine.id,
1910
- ...platform,
1911
- runnerVersion: runner.version,
1912
- device
1913
- },
1914
- runJob: (run, report) => runJob(run, {
1915
- runner,
1916
- report,
1917
- onChecked: (target) => {
1918
- if (target) clack.log.info(`${(/* @__PURE__ */ new Date()).toLocaleTimeString()} Job ${run.routeId} is from ${target}`);
1919
- }
1920
- }),
1921
- saveToken: (token) => saveRotatedToken(credentials, token),
1922
- onEvent: (event) => {
1923
- if (event.type === "connected") online = true;
1924
- show(event);
1925
- },
1926
- signal: stop.signal
1927
- });
1928
- process.off("SIGINT", onInterrupt);
2052
+ let end;
2053
+ try {
2054
+ end = await connect({
2055
+ server: credentials.server,
2056
+ token: credentials.token,
2057
+ hello: {
2058
+ machineId: credentials.machine.id,
2059
+ ...platform,
2060
+ runnerVersion: runner.version,
2061
+ device
2062
+ },
2063
+ runJob: (run, report) => runJob(run, {
2064
+ runner,
2065
+ report,
2066
+ onChecked: (target) => {
2067
+ if (target) clack.log.info(`${(/* @__PURE__ */ new Date()).toLocaleTimeString()} Job ${run.routeId} is from ${target}`);
2068
+ }
2069
+ }),
2070
+ saveToken: (token) => saveRotatedToken(credentials, token),
2071
+ onEvent: (event) => {
2072
+ if (event.type === "connected") online = true;
2073
+ show(event);
2074
+ live.onEvent(event);
2075
+ },
2076
+ signal: stop.signal
2077
+ });
2078
+ } finally {
2079
+ live.end();
2080
+ process.off("SIGINT", onInterrupt);
2081
+ }
1929
2082
  switch (end.reason) {
1930
2083
  case "stopped":
1931
2084
  clack.outro("Offline.");
@@ -2000,7 +2153,7 @@ const loginCommand = defineCommand({
2000
2153
  },
2001
2154
  name: {
2002
2155
  type: "string",
2003
- description: "Suggested machine name (default: this computer's hostname)"
2156
+ description: "Suggested machine name (default: this computer's name)"
2004
2157
  },
2005
2158
  share: {
2006
2159
  type: "string",
@@ -2051,7 +2204,7 @@ function machinePlatform() {
2051
2204
  }
2052
2205
  /** Signs in through the browser, saves the machine token, and settles who can run jobs here. */
2053
2206
  async function enroll(server, name, platform, share) {
2054
- const signedIn = await signIn(server, name || hostname(), platform);
2207
+ const signedIn = await signIn(server, name || await defaultMachineName(), platform);
2055
2208
  const credentials = signedIn.credentials;
2056
2209
  writeCredentials(credentials);
2057
2210
  clack.log.success(`Enrolled as ${credentials.machine.name} in ${credentials.workspace.name}. Token saved to ${credentialsPath()}.`);
@@ -2102,7 +2255,7 @@ function show(event) {
2102
2255
  clack.log.step(`${at} Starting job ${event.routeId}`);
2103
2256
  break;
2104
2257
  case "paused":
2105
- if (event.paused) clack.log.warn(`${at} Paused${event.by ? ` by ${event.by}` : ""}. Staying online, but new jobs skip this machine until it's resumed.`);
2258
+ if (event.paused) clack.log.warn(`${at} Paused${event.by ? ` by ${event.by}` : ""}. Staying online, but new jobs skip this machine until it's resumed.${process.stdin.isTTY ? " Press p to resume." : ""}`);
2106
2259
  else clack.log.success(`${at} Resumed${event.by ? ` by ${event.by}` : ""}. Waiting for jobs.`);
2107
2260
  break;
2108
2261
  case "token-not-saved":
@@ -2214,11 +2367,7 @@ function pauseCommand(paused) {
2214
2367
  process.exitCode = 1;
2215
2368
  return;
2216
2369
  }
2217
- const result = await withCurrentToken(credentials, (token) => setPaused({
2218
- server: credentials.server,
2219
- token,
2220
- paused
2221
- }));
2370
+ const result = await pauseMachine(credentials, paused);
2222
2371
  const name = credentials.machine.name;
2223
2372
  switch (result.status) {
2224
2373
  case "ok":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "relayrunner",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Run GitHub Actions jobs on your machine through the RelayRunner gateway.",
5
5
  "repository": {
6
6
  "type": "git",