runcloud 0.2.219 → 0.2.220

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.
@@ -5,6 +5,7 @@ export const str = (value) => typeof value === 'string' && value.length > 0 ? va
5
5
  export const num = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null;
6
6
  export const stamp = (value) => str(value) && Number.isFinite(Date.parse(String(value))) ? new Date(String(value)).toISOString() : null;
7
7
  export const time = (value) => stamp(value) ? Date.parse(String(value)) : 0;
8
+ export const FLEET_ADMIN_PREFIX = '/run-cloud/admin/simulators';
8
9
  export const validFleetId = (value) => typeof value === 'string' && /^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,255}$/.test(value);
9
10
  export function fleetReadError(reason, timeoutMs) {
10
11
  if (reason === 'HTTP 401' || reason === 'HTTP 403')
@@ -0,0 +1,80 @@
1
+ import { ApiError } from '../api.js';
2
+ import { FLEET_ADMIN_PREFIX, validFleetId } from './run-cloud-fleet-observations.js';
3
+ const PRESSURE_PREFIX = FLEET_ADMIN_PREFIX;
4
+ const MAX_MINUTES = 360;
5
+ const TOO_LONG = 'pressure expires after at most 6h (6h maximum); set it again later to extend';
6
+ const hostPath = (hostId) => `${PRESSURE_PREFIX}/hosts/${encodeURIComponent(hostId)}/pressure`;
7
+ export function pressureMinutes(value) {
8
+ const match = /^(\d+)(m|h)$/.exec(value.trim());
9
+ if (!match)
10
+ throw new Error('--for takes minutes or hours, e.g. 30m, 2h, 6h');
11
+ const minutes = Number(match[1]) * (match[2] === 'h' ? 60 : 1);
12
+ if (minutes < 1)
13
+ throw new Error('--for must be at least 1m');
14
+ if (minutes > MAX_MINUTES)
15
+ throw new Error(`--for ${value}: ${TOO_LONG}`);
16
+ return minutes;
17
+ }
18
+ function expiresIn(expiresAt, now) {
19
+ const minutes = Math.floor((Date.parse(expiresAt) - now) / 60000);
20
+ if (!(minutes >= 1))
21
+ return '<1m';
22
+ return minutes >= 60 ? `${Math.floor(minutes / 60)}h ${minutes % 60}m` : `${minutes}m`;
23
+ }
24
+ export function pressureTable(pressures, now = Date.now()) {
25
+ if (!pressures.length)
26
+ return 'No host is under pressure.';
27
+ const lines = [['HOST', 'EXPIRES IN', 'SET BY', 'REASON'],
28
+ ...pressures.map(p => [p.hostId, expiresIn(p.expiresAt, now), p.createdBy, p.reason || '-'])];
29
+ const widths = [0, 1, 2].map(i => Math.max(...lines.map(line => line[i].length)));
30
+ return lines.map(line => line.map((cell, i) => i < 3 ? cell.padEnd(widths[i]) : cell).join(' ')).join('\n');
31
+ }
32
+ function explain(error, hostId) {
33
+ if (!(error instanceof ApiError))
34
+ return error;
35
+ if (error.status === 401 || error.status === 403)
36
+ return new Error(`Fleet access denied (HTTP ${error.status}); use an admin identity`);
37
+ const code = error.code ?? error.detail;
38
+ if (code === 'unknown_host')
39
+ return new Error(`Host ${hostId} is not in the fleet registry`);
40
+ if (code === 'pressure_too_long')
41
+ return new Error(`Refused by the API: ${TOO_LONG}`);
42
+ if (code === 'invalid_duration')
43
+ return new Error('Refused by the API: duration must be 1 to 360 minutes');
44
+ return error;
45
+ }
46
+ export function registerFleetPressureCommand(fleet, client, action) {
47
+ fleet.command('pressure [hostId]').description('Route real traffic to one simulator host first, for at most 6h (admin)')
48
+ .option('--for <duration>', 'How long: 30m, 2h, 6h (max 6h)').option('--reason <text>', 'Why; kept in the audit log')
49
+ .option('--clear', 'Remove the pressure from the host').option('--list', 'Show active pressures')
50
+ .option('--json', 'Structured output')
51
+ .action((hostId, opts) => action(async () => {
52
+ if (opts.list) {
53
+ if (hostId || opts.for || opts.clear || opts.reason)
54
+ throw new Error('--list takes no host ID, --for, --reason or --clear');
55
+ const body = await client().get(`${PRESSURE_PREFIX}/pressure`, { signal: AbortSignal.timeout(30000) }).catch(error => { throw explain(error); });
56
+ const pressures = body?.pressures;
57
+ if (!Array.isArray(pressures))
58
+ throw new Error('Pressure list unavailable (invalid_response); retry and check API connectivity');
59
+ console.log(opts.json ? JSON.stringify({ pressures }, null, 2) : pressureTable(pressures));
60
+ return;
61
+ }
62
+ if (!hostId || !validFleetId(hostId))
63
+ throw new Error('Give a valid host ID, e.g. runcloud fleet pressure gabriel-1 --for 2h');
64
+ if (opts.clear) {
65
+ if (opts.for || opts.reason)
66
+ throw new Error('--clear takes no --for or --reason');
67
+ await client().delete(hostPath(hostId)).catch(error => { throw explain(error, hostId); });
68
+ console.log(opts.json ? JSON.stringify({ hostId, cleared: true }, null, 2) : `Pressure cleared on ${hostId}`);
69
+ return;
70
+ }
71
+ if (!opts.for)
72
+ throw new Error('Use --for <duration> (max 6h), --clear, or --list');
73
+ if (opts.reason !== undefined && opts.reason.length > 200)
74
+ throw new Error('--reason is at most 200 characters');
75
+ const body = { durationMinutes: pressureMinutes(opts.for), ...(opts.reason ? { reason: opts.reason } : {}) };
76
+ const pressure = await client().put(hostPath(hostId), body).catch(error => { throw explain(error, hostId); });
77
+ console.log(opts.json ? JSON.stringify(pressure, null, 2)
78
+ : `${pressure.hostId} gets traffic first until ${pressure.expiresAt} (${expiresIn(pressure.expiresAt, Date.now())})`);
79
+ }));
80
+ }
@@ -1,7 +1,7 @@
1
1
  import { ApiError } from '../api.js';
2
- import { componentHosts, fleetReadError, hostReport, hostedObservation, num, observation, row, rows, safeFleetText, stamp, str, time, validFleetId } from './run-cloud-fleet-observations.js';
2
+ import { componentHosts, fleetReadError, hostReport, hostedObservation, num, observation, row, rows, safeFleetText, stamp, str, time, validFleetId, FLEET_ADMIN_PREFIX } from './run-cloud-fleet-observations.js';
3
3
  export const SURVEY_REQUEST_BUDGET_MS = 30000;
4
- const ROOT = '/run-cloud/admin/simulators';
4
+ const ROOT = FLEET_ADMIN_PREFIX;
5
5
  const RANGES = ['1h', '6h', '24h', '7d'];
6
6
  const DEVICE_LIMIT = 512, POINT_LIMIT = 800, TYPE_LIMIT = 100;
7
7
  function count(value) { const number = num(value); return number !== null && Number.isSafeInteger(number) ? number : null; }
@@ -1,4 +1,4 @@
1
- import { row, rows, str, num, stamp, time, pick, observation, componentHosts, fleetReadError, hostReport, safeFleetText, validFleetId } from './run-cloud-fleet-observations.js';
1
+ import { row, rows, str, num, stamp, time, pick, observation, componentHosts, fleetReadError, hostReport, safeFleetText, validFleetId, FLEET_ADMIN_PREFIX } from './run-cloud-fleet-observations.js';
2
2
  export { safeFleetText } from './run-cloud-fleet-observations.js';
3
3
  import { readFileSync, statSync } from 'node:fs';
4
4
  import { ApiClient, ApiError } from '../api.js';
@@ -6,7 +6,8 @@ import { requireCredentials } from '../config.js';
6
6
  import { surveyFleet, surveyText } from './run-cloud-fleet-survey.js';
7
7
  import { listReleaseChannels, promoteReleaseChannel, releaseText } from './run-cloud-fleet-releases.js';
8
8
  import { registerFleetReleaseControlCommands } from './run-cloud-fleet-release-control.js';
9
- const ROOT = '/run-cloud/admin/simulators';
9
+ import { registerFleetPressureCommand } from './run-cloud-fleet-pressure.js';
10
+ const ROOT = FLEET_ADMIN_PREFIX;
10
11
  const RANGES = ['1h', '6h', '24h', '7d', '30d', '90d', '1y', '2y'];
11
12
  export const ACCESSIBILITY_CODES = ['accessibility_device_unavailable', 'accessibility_capture_failed'];
12
13
  function bound(value, fallback, max, name) {
@@ -305,6 +306,7 @@ function printReport(report, json) {
305
306
  export function registerRunCloudFleet(program) {
306
307
  const fleet = program.command('fleet').description('Admin fleet telemetry, diagnosis, events, failure evidence and serve-sim release channels');
307
308
  registerFleetReleaseControlCommands(fleet, credentialsClient, action);
309
+ registerFleetPressureCommand(fleet, credentialsClient, action);
308
310
  fleet.command('survey [hostId]').description('Survey all fleet signals in seconds; authenticated admin reads, no local services')
309
311
  .option('--json', 'Structured metrics, diagnosis, observations and coverage')
310
312
  .option('--range <range>', 'Alert/input-lag window: 1h, 6h, 24h, 7d', '24h')
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const CLI_VERSION = '0.2.219';
1
+ export const CLI_VERSION = '0.2.220';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "runcloud",
3
- "version": "0.2.219",
3
+ "version": "0.2.220",
4
4
  "description": "Create and control run.cloud remote mobile simulators and cloud sandboxes",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
Binary file
Binary file