@nosana/node 1.1.76-rc → 1.1.77-rc

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.
package/dist/.env CHANGED
@@ -1,6 +1,6 @@
1
1
  BACKEND_SOLANA_ADDRESS=7rFPFnxjXHC2sfDy3qrDa9pEb4j49oivMuV7e8sYDPmB
2
2
  FRP_SERVER_IMAGE=registry.hub.docker.com/nosana/frpc:v1.0.45
3
- JOB_RUNTIME_IMAGE="nosana/job-runtime:v0.2.2"
3
+ JOB_RUNTIME_IMAGE="nosana/job-runtime:v0.2.3"
4
4
  RESOURCE_MANAGER_IMAGE=registry.hub.docker.com/nosana/resource-manager:v1.1.19
5
5
  FRP_SERVER_PORT=7000
6
6
  MIN_DISK_SPACE=25
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nosana/node",
3
- "version": "1.1.76-rc",
3
+ "version": "1.1.77-rc",
4
4
  "description": "",
5
5
  "bin": {
6
6
  "nosana-node": "./dist/src/index.js"
@@ -1,3 +1,3 @@
1
1
  import { ContainerCreateOptions } from "dockerode";
2
2
  import { RunContainerArgs } from "../interface.js";
3
- export declare function createDockerRunOptions(image: string, { name, cmd, gpu, volumes, env, work_dir, entrypoint, aliases, restart_policy, runtime, bind_network_to_container, labels, security, pid_mode, }: RunContainerArgs, gpuOption: string): ContainerCreateOptions;
3
+ export declare function createDockerRunOptions(image: string, { name, cmd, gpu, volumes, env, work_dir, entrypoint, aliases, restart_policy, runtime, bind_network_to_container, labels, security, pid_mode, user, group_add, }: RunContainerArgs, gpuOption: string): ContainerCreateOptions;
@@ -10,7 +10,7 @@ function parseRestartPolicy(restart_policy) {
10
10
  }
11
11
  return undefined;
12
12
  }
13
- export function createDockerRunOptions(image, { name, cmd, gpu, volumes, env, work_dir, entrypoint, aliases, restart_policy, runtime, bind_network_to_container, labels, security, pid_mode, }, gpuOption) {
13
+ export function createDockerRunOptions(image, { name, cmd, gpu, volumes, env, work_dir, entrypoint, aliases, restart_policy, runtime, bind_network_to_container, labels, security, pid_mode, user, group_add, }, gpuOption) {
14
14
  const devices = [];
15
15
  if (gpu) {
16
16
  devices.push({
@@ -25,7 +25,7 @@ export function createDockerRunOptions(image, { name, cmd, gpu, volumes, env, wo
25
25
  return {
26
26
  name: name,
27
27
  Hostname: '',
28
- User: '',
28
+ User: user ?? '',
29
29
  AttachStdin: false,
30
30
  AttachStdout: true,
31
31
  AttachStderr: true,
@@ -77,6 +77,7 @@ export function createDockerRunOptions(image, { name, cmd, gpu, volumes, env, wo
77
77
  ? { SecurityOpt: ['no-new-privileges'] }
78
78
  : {}),
79
79
  ...(pid_mode ? { PidMode: pid_mode } : {}),
80
+ ...(group_add ? { GroupAdd: group_add } : {}),
80
81
  },
81
82
  };
82
83
  }
@@ -40,6 +40,10 @@ export type RunContainerArgs = {
40
40
  };
41
41
  /** Shares the host's pid namespace with the container (`--pid=host`). */
42
42
  pid_mode?: 'host';
43
+ /** The user (`uid[:gid]`) the container runs as; the image's by default. */
44
+ user?: string;
45
+ /** Supplementary groups (gids) for the container's user (`--group-add`). */
46
+ group_add?: string[];
43
47
  /**
44
48
  * Runs between create and start with the container handle, so files can be
45
49
  * put in place before the entrypoint runs. A failure removes the created
@@ -7,6 +7,8 @@ import type { RunContainerArgs } from '../../interface.js';
7
7
  * @returns
8
8
  */
9
9
  export declare function createPodmanRunOptions(image: string, args: RunContainerArgs, gpuOption: string): {
10
+ groups?: string[] | undefined;
11
+ user?: string | undefined;
10
12
  pidns?: {
11
13
  nsmode: "host";
12
14
  } | undefined;
@@ -55,6 +57,8 @@ export declare function createPodmanRunOptions(image: string, args: RunContainer
55
57
  options: string[];
56
58
  }[] | undefined;
57
59
  } | {
60
+ groups?: string[] | undefined;
61
+ user?: string | undefined;
58
62
  pidns?: {
59
63
  nsmode: "host";
60
64
  } | undefined;
@@ -104,6 +108,8 @@ export declare function createPodmanRunOptions(image: string, args: RunContainer
104
108
  options: string[];
105
109
  }[] | undefined;
106
110
  } | {
111
+ groups?: string[] | undefined;
112
+ user?: string | undefined;
107
113
  pidns?: {
108
114
  nsmode: "host";
109
115
  } | undefined;
@@ -24,7 +24,7 @@ function parseRestartPolicy(restart_policy) {
24
24
  * @returns
25
25
  */
26
26
  export function createPodmanRunOptions(image, args, gpuOption) {
27
- const { name, cmd, gpu, volumes, env, aliases, work_dir, entrypoint, restart_policy, labels, security, runtime, pid_mode, } = args;
27
+ const { name, cmd, gpu, volumes, env, aliases, work_dir, entrypoint, restart_policy, labels, security, runtime, pid_mode, user, group_add, } = args;
28
28
  const devices = gpu
29
29
  ? gpuOption === 'all'
30
30
  ? [
@@ -81,5 +81,7 @@ export function createPodmanRunOptions(image, args, gpuOption) {
81
81
  : {}),
82
82
  ...(security?.noNewPrivileges ? { no_new_privileges: true } : {}),
83
83
  ...(pid_mode ? { pidns: { nsmode: pid_mode } } : {}),
84
+ ...(user ? { user } : {}),
85
+ ...(group_add ? { groups: group_add } : {}),
84
86
  };
85
87
  }
@@ -28,6 +28,9 @@ export declare const JOB_RUNTIME_CONTAINER_PREFIX = "job-runtime-";
28
28
  export declare const JOB_RUNTIME_PORT = 8080;
29
29
  /** Where the sidecar's data volume (its records of the job) is mounted. */
30
30
  export declare const JOB_RUNTIME_DATA_DIR = "/nosana-job-runtime";
31
+ /** The uid and gid the sidecar image runs as (its `node` user, never root). */
32
+ export declare const JOB_RUNTIME_UID = 1000;
33
+ export declare const JOB_RUNTIME_GID = 1000;
31
34
  /**
32
35
  * What the sidecar runs. An `onchain` job it resolves itself from its
33
36
  * address, serving its poster the job API and pinning its result. An `inline`
@@ -26,6 +26,9 @@ export const JOB_RUNTIME_CONTAINER_PREFIX = 'job-runtime-';
26
26
  export const JOB_RUNTIME_PORT = 8080;
27
27
  /** Where the sidecar's data volume (its records of the job) is mounted. */
28
28
  export const JOB_RUNTIME_DATA_DIR = '/nosana-job-runtime';
29
+ /** The uid and gid the sidecar image runs as (its `node` user, never root). */
30
+ export const JOB_RUNTIME_UID = 1000;
31
+ export const JOB_RUNTIME_GID = 1000;
29
32
  /** Starts a console line that is a job-log entry (ASCII record separator). */
30
33
  export const JOB_RUNTIME_LOG_ENTRY_MARK = '\u001e';
31
34
  /** The job's directory, as the sidecar has it bind-mounted. */
@@ -41,13 +41,19 @@ async function probeHostPids() {
41
41
  return { ok: false, detail: error?.message ?? String(error) };
42
42
  }
43
43
  }
44
+ /** The sidecar runs as the image's unprivileged user, never as root. */
45
+ function probeUser() {
46
+ const uid = process.getuid?.();
47
+ return { ok: uid !== undefined && uid !== 0, detail: `uid ${uid}` };
48
+ }
44
49
  async function probe() {
45
50
  const [nvidiaSmi, hostPids] = await Promise.all([
46
51
  probeNvidiaSmi(),
47
52
  probeHostPids(),
48
53
  ]);
49
- console.log(JSON.stringify({ nvidiaSmi, hostPids }, null, 2));
50
- process.exit(nvidiaSmi.ok && hostPids.ok ? 0 : 1);
54
+ const user = probeUser();
55
+ console.log(JSON.stringify({ nvidiaSmi, hostPids, user }, null, 2));
56
+ process.exit(nvidiaSmi.ok && hostPids.ok && user.ok ? 0 : 1);
51
57
  }
52
58
  /**
53
59
  * Runs the job named by `JOB_ADDRESS` (see config.ts for the rest of the
@@ -59,5 +59,12 @@ export declare function isDescendantOf(parents: Map<number, number>, pid: number
59
59
  * the sum of their framebuffer figures; when the driver reports none for that
60
60
  * device the whole-device figure stands in (exact on a device the container
61
61
  * has to itself, an overestimate on a shared one, never a false zero).
62
+ *
63
+ * A snapshot with no process at all is read the same way, with every device
64
+ * standing in whole: pmon lists none not only on an idle host but wherever it
65
+ * cannot resolve the pids the driver reports, which are the real host's. A
66
+ * sidecar whose "host" pid namespace is itself a container's (podman inside a
67
+ * docker container started without `--pid=host`) sees none of them, and the
68
+ * job's GPU would otherwise read as a false zero for as long as it runs.
62
69
  */
63
70
  export declare function attributeGpuStats(snapshot: GpuSnapshot, rootPid: number): GpuStat;
@@ -99,6 +99,13 @@ const percent = (used, total) => round(total > 0 ? (used / total) * 100 : 0);
99
99
  * the sum of their framebuffer figures; when the driver reports none for that
100
100
  * device the whole-device figure stands in (exact on a device the container
101
101
  * has to itself, an overestimate on a shared one, never a false zero).
102
+ *
103
+ * A snapshot with no process at all is read the same way, with every device
104
+ * standing in whole: pmon lists none not only on an idle host but wherever it
105
+ * cannot resolve the pids the driver reports, which are the real host's. A
106
+ * sidecar whose "host" pid namespace is itself a container's (podman inside a
107
+ * docker container started without `--pid=host`) sees none of them, and the
108
+ * job's GPU would otherwise read as a false zero for as long as it runs.
102
109
  */
103
110
  export function attributeGpuStats(snapshot, rootPid) {
104
111
  const perDevice = new Map();
@@ -117,7 +124,10 @@ export function attributeGpuStats(snapshot, rootPid) {
117
124
  }
118
125
  perDevice.set(process.gpuUuid, share);
119
126
  }
120
- const shares = [...perDevice.values()]
127
+ const attributed = snapshot.processes.length === 0
128
+ ? [...snapshot.devices.values()].map((device) => ({ device }))
129
+ : [...perDevice.values()];
130
+ const shares = attributed
121
131
  .sort((a, b) => a.device.index - b.device.index)
122
132
  .map(({ device, memoryUsedMiB, smPercent }) => ({
123
133
  device,
@@ -37,6 +37,25 @@ export declare class Provider {
37
37
  startJobRuntime(jobAddress: string, hostDir: string, kind: JobRuntimeKind,
38
38
  /** What its console carries; `entries` for `nosana node run` to render. */
39
39
  consoleOutput?: JobRuntimeConsole): Promise<void>;
40
+ /**
41
+ * The group a sidecar needs to use the runtime socket: the socket's own,
42
+ * which is opened to its group where it is root's alone (podman serves its
43
+ * socket that way; docker's is its `docker` group's already). The socket
44
+ * is the one the node itself uses, bound into its container read-write,
45
+ * so the change reaches the socket the sidecar gets bound. Undefined when
46
+ * the socket cannot be read or changed (a TCP endpoint, a dev socket the
47
+ * user does not own), in which case the sidecar runs as root, as it did
48
+ * before, rather than not at all.
49
+ */
50
+ private jobRuntimeSocketGroup;
51
+ /**
52
+ * Makes the job's data volume the sidecar user's. A volume created as the
53
+ * sidecar starts takes the ownership of the image's mount point, which is
54
+ * the user's; one that is already there (a sidecar a previous node left,
55
+ * one that may have run as root) is chowned first, by a one-shot container
56
+ * on the same image, so that the sidecar can write its records.
57
+ */
58
+ private ownJobRuntimeVolume;
40
59
  /**
41
60
  * Whether the job has a sidecar, running or exited: one that outlived the
42
61
  * node's last process, to take over. Only the runtime saying there is no
@@ -87,6 +106,11 @@ export declare class Provider {
87
106
  * Pulls the job runtime image, so the next job's sidecar finds it local.
88
107
  * Runs before each job loop; a cached image costs no registry round trip.
89
108
  */
109
+ /**
110
+ * Pulls the sidecar image `.env` names and records it in the image cache,
111
+ * where the resync between jobs ages out what goes unused, so a bump's
112
+ * predecessor goes the way of any other image.
113
+ */
90
114
  pullJobRuntimeImage(): Promise<void>;
91
115
  /**
92
116
  * Removes the job's sidecar, then whatever else of the job is left: its
@@ -1,7 +1,8 @@
1
+ import fs from 'fs/promises';
1
2
  import { configs } from '../configs/configs.js';
2
3
  import { applyLoggingProxyToClass } from '../../../packages/common/src/index.js';
3
4
  import { JOB_LABEL, NODE_LABEL, SERVICE_LABEL, } from '../../../packages/container-orchestrator/src/index.js';
4
- import { JOB_RUNTIME_CONTAINER_PREFIX, JOB_RUNTIME_DATA_DIR, JOB_RUNTIME_HOST_DIR, JOB_RUNTIME_PORT, } from '../../../packages/job-runtime/src/host/index.js';
5
+ import { JOB_RUNTIME_CONTAINER_PREFIX, JOB_RUNTIME_DATA_DIR, JOB_RUNTIME_GID, JOB_RUNTIME_HOST_DIR, JOB_RUNTIME_PORT, JOB_RUNTIME_UID, } from '../../../packages/job-runtime/src/host/index.js';
5
6
  import { getKit, nodeAddress } from '../sdk/index.js';
6
7
  import { pkg } from '../../static/index.js';
7
8
  /** How long a job runtime's address is used before it is looked up again. */
@@ -182,6 +183,8 @@ export class Provider {
182
183
  // Cached by pullJobRuntimeImage before the job loop, so normally no pull.
183
184
  this.containerOrchestration.pullImage(jobRuntime.containerImage),
184
185
  ]);
186
+ // A use, so the cache's resync keeps the image while jobs run on it.
187
+ await this.resourceManager.images.setImage(jobRuntime.containerImage);
185
188
  // The same socket path as the SSH sidecar (see withSshSidecar).
186
189
  const socketDest = this.containerOrchestration.name === 'podman'
187
190
  ? '/run/podman/podman.sock'
@@ -189,8 +192,20 @@ export class Provider {
189
192
  const rpc = getKit().config.solana.rpcEndpoint;
190
193
  const node = nodeAddress();
191
194
  const { teeRuntime, gpu } = this.containerOrchestration;
195
+ const labels = {
196
+ [JOB_LABEL]: jobAddress,
197
+ [NODE_LABEL]: node,
198
+ [SERVICE_LABEL]: 'job-runtime',
199
+ };
200
+ const socketGroup = await this.jobRuntimeSocketGroup();
201
+ await this.ownJobRuntimeVolume(name, jobRuntime.containerImage, labels);
192
202
  await this.containerOrchestration.runFlowContainer(jobRuntime.containerImage, {
193
203
  name,
204
+ // The image's unprivileged user, in the runtime socket's group so it
205
+ // can use the socket; root only where the socket cannot be shared.
206
+ ...(socketGroup === undefined
207
+ ? { user: '0:0' }
208
+ : { group_add: [String(socketGroup)] }),
194
209
  // GPU stats need the device; a node without GPUs (which may have no
195
210
  // GPU driver either) gives its sidecar none.
196
211
  ...(this.repository.getNodeInfo().gpus.devices.length > 0 ? { gpu: true } : {}),
@@ -232,11 +247,7 @@ export class Provider {
232
247
  dest: JOB_RUNTIME_HOST_DIR,
233
248
  },
234
249
  ],
235
- labels: {
236
- [JOB_LABEL]: jobAddress,
237
- [NODE_LABEL]: node,
238
- [SERVICE_LABEL]: 'job-runtime',
239
- },
250
+ labels,
240
251
  // The socket makes a compromised sidecar root on the host whatever
241
252
  // runs inside it, so the container itself gets nothing it does not
242
253
  // need: no capabilities (the API, nvidia-smi and /proc need none), no
@@ -249,6 +260,60 @@ export class Provider {
249
260
  },
250
261
  });
251
262
  }
263
+ /**
264
+ * The group a sidecar needs to use the runtime socket: the socket's own,
265
+ * which is opened to its group where it is root's alone (podman serves its
266
+ * socket that way; docker's is its `docker` group's already). The socket
267
+ * is the one the node itself uses, bound into its container read-write,
268
+ * so the change reaches the socket the sidecar gets bound. Undefined when
269
+ * the socket cannot be read or changed (a TCP endpoint, a dev socket the
270
+ * user does not own), in which case the sidecar runs as root, as it did
271
+ * before, rather than not at all.
272
+ */
273
+ async jobRuntimeSocketGroup() {
274
+ const { socketPath } = this.containerOrchestration;
275
+ try {
276
+ const { gid, mode } = await fs.stat(socketPath);
277
+ if ((mode & 0o060) !== 0o060) {
278
+ await fs.chmod(socketPath, (mode & 0o777) | 0o060);
279
+ }
280
+ return gid;
281
+ }
282
+ catch (error) {
283
+ console.warn(`The runtime socket at ${socketPath} cannot be shared with the job runtime (${error}); the job runtime runs as root.`);
284
+ return undefined;
285
+ }
286
+ }
287
+ /**
288
+ * Makes the job's data volume the sidecar user's. A volume created as the
289
+ * sidecar starts takes the ownership of the image's mount point, which is
290
+ * the user's; one that is already there (a sidecar a previous node left,
291
+ * one that may have run as root) is chowned first, by a one-shot container
292
+ * on the same image, so that the sidecar can write its records.
293
+ */
294
+ async ownJobRuntimeVolume(volume, image, labels) {
295
+ if (!(await this.containerOrchestration.hasVolume(volume)))
296
+ return;
297
+ const name = `${volume}-chown`;
298
+ await this.containerOrchestration.stopAndDeleteContainer(name);
299
+ const container = await this.containerOrchestration.runFlowContainer(image, {
300
+ name,
301
+ user: '0:0',
302
+ entrypoint: [
303
+ 'chown',
304
+ '-R',
305
+ `${JOB_RUNTIME_UID}:${JOB_RUNTIME_GID}`,
306
+ JOB_RUNTIME_DATA_DIR,
307
+ ],
308
+ volumes: [{ name: volume, dest: JOB_RUNTIME_DATA_DIR }],
309
+ labels: { ...labels, [SERVICE_LABEL]: 'job-runtime-chown' },
310
+ });
311
+ const { StatusCode } = await container.wait({ condition: 'not-running' });
312
+ await this.containerOrchestration.stopAndDeleteContainer(name);
313
+ if (StatusCode !== 0) {
314
+ throw new Error(`The data volume ${volume} could not be made the job runtime's (chown exited ${StatusCode})`);
315
+ }
316
+ }
252
317
  /**
253
318
  * Whether the job has a sidecar, running or exited: one that outlived the
254
319
  * node's last process, to take over. Only the runtime saying there is no
@@ -379,8 +444,15 @@ export class Provider {
379
444
  * Pulls the job runtime image, so the next job's sidecar finds it local.
380
445
  * Runs before each job loop; a cached image costs no registry round trip.
381
446
  */
447
+ /**
448
+ * Pulls the sidecar image `.env` names and records it in the image cache,
449
+ * where the resync between jobs ages out what goes unused, so a bump's
450
+ * predecessor goes the way of any other image.
451
+ */
382
452
  async pullJobRuntimeImage() {
383
- await this.containerOrchestration.pullImage(configs().jobRuntime.containerImage);
453
+ const image = configs().jobRuntime.containerImage;
454
+ await this.containerOrchestration.pullImage(image);
455
+ await this.resourceManager.images.setImage(image);
384
456
  }
385
457
  /**
386
458
  * Removes the job's sidecar, then whatever else of the job is left: its
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@nosana/node",
3
- "version": "1.1.76-rc",
3
+ "version": "1.1.77-rc",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@nosana/node",
9
- "version": "1.1.76-rc",
9
+ "version": "1.1.77-rc",
10
10
  "license": "ISC",
11
11
  "workspaces": [
12
12
  "packages/*"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nosana/node",
3
- "version": "1.1.76-rc",
3
+ "version": "1.1.77-rc",
4
4
  "description": "",
5
5
  "bin": {
6
6
  "nosana-node": "./dist/src/index.js"