gigadrive 2.14.0 → 2.16.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.
package/dist/index.cjs CHANGED
@@ -32,7 +32,7 @@ var import_effect31 = require("effect");
32
32
  // package.json
33
33
  var package_default = {
34
34
  name: "gigadrive",
35
- version: "2.14.0",
35
+ version: "2.16.0",
36
36
  repository: {
37
37
  type: "git",
38
38
  url: "https://github.com/Gigadrive/sdk.git",
@@ -3073,6 +3073,7 @@ var describeMicroVm = (vm, now = Date.now()) => {
3073
3073
  `Size: ${vm.vcpus} vCPU, ${formatMemory(vm.memoryMb)} memory, ${formatMemory(vm.diskMb)} disk`,
3074
3074
  `Region: ${vm.region}`,
3075
3075
  `Network: ${vm.network.egress === "deny" ? "outbound traffic blocked" : vm.network.allow && vm.network.allow.length > 0 ? `only ${vm.network.allow.join(", ")}` : "internet access"}`,
3076
+ ...(vm.network.inject ?? []).map((rule) => `Injects: ${rule.headers.join(", ")} for ${rule.domain}`),
3076
3077
  `Previews: ${vm.network.previews === "private" ? "private (preview token required)" : "public"}`,
3077
3078
  `Application: ${vm.applicationId}`,
3078
3079
  `Created: ${vm.createdAt}`
@@ -3108,6 +3109,17 @@ var formatWebhookTable = (webhooks) => formatTable([
3108
3109
  webhook.id
3109
3110
  ])
3110
3111
  ]);
3112
+ var formatVolumeTable = (volumes) => formatTable([
3113
+ ["NAME", "SIZE", "STORED", "STATUS", "LAST SAVED", "ID"],
3114
+ ...volumes.map((volume) => [
3115
+ volume.name,
3116
+ `${String(volume.sizeMb)} MiB`,
3117
+ volume.storedBytes === null ? "-" : formatBytes(volume.storedBytes),
3118
+ volume.attachedMicrovmId ? `${volume.status} (${volume.attachedMicrovmId})` : volume.status,
3119
+ volume.lastSaveError ? `failed: ${volume.lastSaveError}` : volume.lastSavedAt ?? "never",
3120
+ volume.id
3121
+ ])
3122
+ ]);
3111
3123
  var formatDeliveryTable = (deliveries) => formatTable([
3112
3124
  ["CREATED", "EVENT", "STATUS", "ATTEMPTS", "RESPONSE", "MICROVM"],
3113
3125
  ...deliveries.map((delivery) => [
@@ -3353,6 +3365,45 @@ var withClient = (run) => import_effect24.Effect.gen(function* () {
3353
3365
  return yield* import_effect24.Effect.tryPromise({ try: () => run(client), catch: toApiRequestError });
3354
3366
  });
3355
3367
  var signalOnInterrupt = (signal) => (command, exit) => import_effect24.Exit.isInterrupted(exit) ? import_effect24.Effect.tryPromise(() => command.kill(signal)).pipe(import_effect24.Effect.timeout("2 seconds"), import_effect24.Effect.ignore) : import_effect24.Effect.void;
3368
+ var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
3369
+ var parseVolumeFlags = (values) => import_effect24.Effect.gen(function* () {
3370
+ if (values.length > 4) {
3371
+ return yield* new InvalidMicroVmOptionsError({ message: "--volume takes at most 4 volumes." });
3372
+ }
3373
+ const volumes = [];
3374
+ for (const value of values) {
3375
+ const separator = value.indexOf(":");
3376
+ const volumeId = separator > 0 ? value.slice(0, separator) : "";
3377
+ const mountPath = separator > 0 ? value.slice(separator + 1) : "";
3378
+ if (volumeId === "" || !mountPath.startsWith("/")) {
3379
+ return yield* new InvalidMicroVmOptionsError({
3380
+ message: `--volume must be VOLUME:PATH with an absolute path, such as cache:/cache (got ${value}).`
3381
+ });
3382
+ }
3383
+ volumes.push({ volumeId, mountPath });
3384
+ }
3385
+ return volumes;
3386
+ });
3387
+ var parseInjectFlags = (values) => import_effect24.Effect.gen(function* () {
3388
+ const rules = /* @__PURE__ */ new Map();
3389
+ for (const value of values) {
3390
+ const colon = value.indexOf(":");
3391
+ const equals = value.indexOf("=", colon + 1);
3392
+ const domain = colon > 0 ? value.slice(0, colon).trim().toLowerCase() : "";
3393
+ const name = colon > 0 && equals > colon ? value.slice(colon + 1, equals).trim() : "";
3394
+ if (domain === "" || name === "" || equals === value.length - 1) {
3395
+ return yield* new InvalidMicroVmOptionsError({
3396
+ // The entry is not echoed: it may hold the credential.
3397
+ message: '--inject must be DOMAIN:HEADER=VALUE, such as "api.openai.com:Authorization=Bearer $OPENAI_API_KEY".'
3398
+ });
3399
+ }
3400
+ rules.set(domain, { ...rules.get(domain), [name]: value.slice(equals + 1) });
3401
+ }
3402
+ if (rules.size > 8) {
3403
+ return yield* new InvalidMicroVmOptionsError({ message: "--inject takes at most 8 domains." });
3404
+ }
3405
+ return [...rules].map(([domain, headers]) => ({ domain, headers }));
3406
+ });
3356
3407
  var checkEgressFlags = (denyEgress, allowEgress) => import_effect24.Effect.gen(function* () {
3357
3408
  if (denyEgress && allowEgress.length > 0) {
3358
3409
  return yield* new InvalidMicroVmOptionsError({
@@ -3383,6 +3434,14 @@ var createInput = (flags) => import_effect24.Effect.gen(function* () {
3383
3434
  if (badPort !== void 0) {
3384
3435
  return yield* new InvalidMicroVmOptionsError({ message: `--port must be between 1 and 65535 (got ${badPort}).` });
3385
3436
  }
3437
+ const inject = yield* parseInjectFlags(flags.inject);
3438
+ if (inject.length > 0 && flags.denyEgress) {
3439
+ return yield* new InvalidMicroVmOptionsError({ message: "--inject cannot be combined with --deny-egress." });
3440
+ }
3441
+ const volumes = yield* parseVolumeFlags(flags.volumes);
3442
+ if (volumes.length > 0 && flags.pauseOnTimeout) {
3443
+ return yield* new InvalidMicroVmOptionsError({ message: "--volume cannot be combined with --pause-on-timeout." });
3444
+ }
3386
3445
  const applicationId = yield* resolveApplicationId(flags.app);
3387
3446
  const input = {
3388
3447
  ...applicationId ? { applicationId } : {},
@@ -3399,11 +3458,13 @@ var createInput = (flags) => import_effect24.Effect.gen(function* () {
3399
3458
  ...flags.ports.length > 0 ? { ports: [...flags.ports] } : {},
3400
3459
  ...env ? { env } : {},
3401
3460
  ...metadata ? { metadata } : {},
3402
- ...flags.denyEgress || flags.privatePreviews || flags.allowEgress.length > 0 ? {
3461
+ ...volumes.length > 0 ? { volumes } : {},
3462
+ ...flags.denyEgress || flags.privatePreviews || flags.allowEgress.length > 0 || inject.length > 0 ? {
3403
3463
  network: {
3404
3464
  egress: flags.denyEgress ? "deny" : "allow",
3405
3465
  ...flags.privatePreviews ? { previews: "private" } : {},
3406
- ...flags.allowEgress.length > 0 ? { allow: flags.allowEgress.map((entry) => entry.trim()) } : {}
3466
+ ...flags.allowEgress.length > 0 ? { allow: flags.allowEgress.map((entry) => entry.trim()) } : {},
3467
+ ...inject.length > 0 ? { inject } : {}
3407
3468
  }
3408
3469
  } : {},
3409
3470
  ...import_effect24.Option.match(flags.region, { onNone: () => ({}), onSome: (region) => ({ region }) })
@@ -3412,7 +3473,26 @@ var createInput = (flags) => import_effect24.Effect.gen(function* () {
3412
3473
  });
3413
3474
  var createMicroVm = (flags) => import_effect24.Effect.gen(function* () {
3414
3475
  const apiClient = yield* ApiClientService;
3415
- const input = yield* createInput(flags);
3476
+ const parsed = yield* createInput(flags);
3477
+ const named = (parsed.volumes ?? []).filter((volume) => !UUID.test(volume.volumeId));
3478
+ const known = named.length > 0 ? yield* apiClient.request(
3479
+ (client) => client.microvms.volumes.list({ ...parsed.applicationId ? { applicationId: parsed.applicationId } : {} })
3480
+ ) : [];
3481
+ const volumes = [];
3482
+ for (const volume of parsed.volumes ?? []) {
3483
+ if (UUID.test(volume.volumeId)) {
3484
+ volumes.push(volume);
3485
+ continue;
3486
+ }
3487
+ const match = known.find((entry) => entry.name === volume.volumeId);
3488
+ if (match === void 0) {
3489
+ return yield* new InvalidMicroVmOptionsError({
3490
+ message: `No volume is named ${volume.volumeId}. Create it with "gigadrive microvms volumes create ${volume.volumeId}".`
3491
+ });
3492
+ }
3493
+ volumes.push({ volumeId: match.id, mountPath: volume.mountPath });
3494
+ }
3495
+ const input = volumes.length > 0 ? { ...parsed, volumes } : parsed;
3416
3496
  if (!flags.json) yield* import_effect24.Console.error(`Creating a microVM from ${input.image ?? "node22"}...`);
3417
3497
  const vm = yield* apiClient.request((client) => client.microvms.create(input));
3418
3498
  if (flags.json) {
@@ -3475,6 +3555,12 @@ var createCommand = import_cli12.Command.make(
3475
3555
  ),
3476
3556
  import_cli12.Options.repeated
3477
3557
  ),
3558
+ inject: import_cli12.Options.text("inject").pipe(
3559
+ import_cli12.Options.withDescription(
3560
+ "Add a header to HTTPS requests to a domain, as DOMAIN:HEADER=VALUE (repeatable, up to 8 domains); the value never enters the microVM"
3561
+ ),
3562
+ import_cli12.Options.repeated
3563
+ ),
3478
3564
  pauseOnTimeout: import_cli12.Options.boolean("pause-on-timeout").pipe(
3479
3565
  import_cli12.Options.withDescription("Pause the microVM when its timeout ends instead of stopping it")
3480
3566
  ),
@@ -3483,6 +3569,13 @@ var createCommand = import_cli12.Command.make(
3483
3569
  "Resume the paused microVM when a command, file call or public preview request reaches it"
3484
3570
  )
3485
3571
  ),
3572
+ volumes: import_cli12.Options.text("volume").pipe(
3573
+ import_cli12.Options.withAlias("v"),
3574
+ import_cli12.Options.withDescription(
3575
+ "Attach a volume as VOLUME:PATH, by ID or name, such as cache:/cache (repeatable, up to 4; saved when the microVM stops)"
3576
+ ),
3577
+ import_cli12.Options.repeated
3578
+ ),
3486
3579
  region: import_cli12.Options.text("region").pipe(import_cli12.Options.withDescription("Region to place the microVM in"), import_cli12.Options.optional),
3487
3580
  json: jsonOption2
3488
3581
  },
@@ -3837,15 +3930,31 @@ var updateMicroVmNetwork = (microvmId, flags) => import_effect24.Effect.gen(func
3837
3930
  });
3838
3931
  }
3839
3932
  yield* checkEgressFlags(flags.denyEgress, flags.allowEgress);
3933
+ const inject = yield* parseInjectFlags(flags.inject);
3934
+ if (inject.length > 0 && flags.denyEgress) {
3935
+ return yield* new InvalidMicroVmOptionsError({ message: "--inject cannot be combined with --deny-egress." });
3936
+ }
3937
+ if (flags.clearInject && (inject.length > 0 || flags.denyEgress)) {
3938
+ return yield* new InvalidMicroVmOptionsError({
3939
+ message: "--clear-inject cannot be combined with --inject or --deny-egress."
3940
+ });
3941
+ }
3840
3942
  const apiClient = yield* ApiClientService;
3841
3943
  const vm = yield* apiClient.request(
3842
3944
  (client) => client.microvms.updateNetwork(
3843
3945
  microvmId,
3844
- flags.denyEgress ? { egress: "deny" } : flags.allowEgress.length > 0 ? { egress: "allow", allow: flags.allowEgress.map((entry) => entry.trim()) } : { egress: "allow" }
3946
+ flags.denyEgress ? { egress: "deny" } : {
3947
+ egress: "allow",
3948
+ ...flags.allowEgress.length > 0 ? { allow: flags.allowEgress.map((entry) => entry.trim()) } : {},
3949
+ ...inject.length > 0 || flags.clearInject ? { inject } : {}
3950
+ }
3845
3951
  )
3846
3952
  );
3847
3953
  const reach = vm.network.egress === "deny" ? "no outbound traffic" : vm.network.allow ? vm.network.allow.join(", ") : "the whole internet";
3848
3954
  yield* import_effect24.Console.log(`${vm.id} now reaches ${reach}.`);
3955
+ for (const rule of vm.network.inject ?? []) {
3956
+ yield* import_effect24.Console.log(`Adds ${rule.headers.join(", ")} to HTTPS requests to ${rule.domain}.`);
3957
+ }
3849
3958
  return vm;
3850
3959
  });
3851
3960
  var networkCommand = import_cli12.Command.make(
@@ -3861,6 +3970,15 @@ var networkCommand = import_cli12.Command.make(
3861
3970
  ),
3862
3971
  allowAllEgress: import_cli12.Options.boolean("allow-all-egress").pipe(
3863
3972
  import_cli12.Options.withDescription("Reach the whole internet again (private networks stay blocked)")
3973
+ ),
3974
+ inject: import_cli12.Options.text("inject").pipe(
3975
+ import_cli12.Options.withDescription(
3976
+ "Add a header to HTTPS requests to a domain, as DOMAIN:HEADER=VALUE (repeatable, up to 8 domains); the value never enters the microVM"
3977
+ ),
3978
+ import_cli12.Options.repeated
3979
+ ),
3980
+ clearInject: import_cli12.Options.boolean("clear-inject").pipe(
3981
+ import_cli12.Options.withDescription("Remove every header rule added with --inject")
3864
3982
  )
3865
3983
  },
3866
3984
  ({ microvmId, ...flags }) => updateMicroVmNetwork(microvmId, flags).pipe(import_effect24.Effect.asVoid, reportErrors2("update the microVM network"))
@@ -3965,6 +4083,90 @@ var imagesCommand = import_cli12.Command.make("images", {}, () => import_effect2
3965
4083
  import_cli12.Command.withDescription("Built-in and custom microVM images"),
3966
4084
  import_cli12.Command.withSubcommands([imagesListCommand, imagesCreateCommand, imagesRemoveCommand])
3967
4085
  );
4086
+ var volumeIdArg = import_cli12.Args.text({ name: "volume-id" }).pipe(import_cli12.Args.withDescription("The volume ID"));
4087
+ var volumesListCommand = import_cli12.Command.make(
4088
+ "ls",
4089
+ { app: appOption6, json: jsonOption2 },
4090
+ ({ app, json }) => import_effect24.Effect.gen(function* () {
4091
+ const apiClient = yield* ApiClientService;
4092
+ const applicationId = yield* resolveApplicationId(app);
4093
+ const volumes = yield* apiClient.request((client) => client.microvms.volumes.list({ applicationId }));
4094
+ if (json) return yield* import_effect24.Console.log(JSON.stringify(volumes, null, 2));
4095
+ if (volumes.length === 0)
4096
+ return yield* import_effect24.Console.log('No volumes yet. Create one with "gigadrive microvms volumes create <name>".');
4097
+ for (const line of formatVolumeTable(volumes)) yield* import_effect24.Console.log(line);
4098
+ }).pipe(reportErrors2("list volumes"))
4099
+ ).pipe(import_cli12.Command.withDescription("List the application's volumes"));
4100
+ var createVolume = (flags) => import_effect24.Effect.gen(function* () {
4101
+ const apiClient = yield* ApiClientService;
4102
+ const applicationId = yield* resolveApplicationId(flags.app);
4103
+ const volume = yield* apiClient.request(
4104
+ (client) => client.microvms.volumes.create({
4105
+ name: flags.name,
4106
+ ...import_effect24.Option.match(flags.size, { onNone: () => ({}), onSome: (sizeMb) => ({ sizeMb }) }),
4107
+ ...applicationId ? { applicationId } : {}
4108
+ })
4109
+ );
4110
+ if (flags.json) {
4111
+ yield* import_effect24.Console.log(JSON.stringify(volume, null, 2));
4112
+ } else {
4113
+ yield* import_effect24.Console.log(`Created volume ${volume.name} (${String(volume.sizeMb)} MiB, ${volume.id}).`);
4114
+ yield* import_effect24.Console.log(`Attach it: gigadrive microvms create --volume ${volume.name}:/data`);
4115
+ }
4116
+ return volume;
4117
+ });
4118
+ var volumesCreateCommand = import_cli12.Command.make(
4119
+ "create",
4120
+ {
4121
+ name: import_cli12.Args.text({ name: "name" }).pipe(
4122
+ import_cli12.Args.withDescription("Lowercase letters, digits and -, unique per application")
4123
+ ),
4124
+ size: import_cli12.Options.integer("size").pipe(
4125
+ import_cli12.Options.withDescription("Size in MiB, 1024 to 51200 (default: 10240)"),
4126
+ import_cli12.Options.optional
4127
+ ),
4128
+ app: appOption6,
4129
+ json: jsonOption2
4130
+ },
4131
+ (flags) => createVolume(flags).pipe(import_effect24.Effect.asVoid, reportErrors2("create the volume"))
4132
+ ).pipe(import_cli12.Command.withDescription("Create an empty volume"));
4133
+ var volumesRenameCommand = import_cli12.Command.make(
4134
+ "rename",
4135
+ { volumeId: volumeIdArg, name: import_cli12.Args.text({ name: "name" }).pipe(import_cli12.Args.withDescription("The new name")) },
4136
+ ({ volumeId, name }) => import_effect24.Effect.gen(function* () {
4137
+ const apiClient = yield* ApiClientService;
4138
+ const volume = yield* apiClient.request((client) => client.microvms.volumes.rename(volumeId, name));
4139
+ yield* import_effect24.Console.log(`Renamed the volume to ${volume.name}.`);
4140
+ }).pipe(reportErrors2("rename the volume"))
4141
+ ).pipe(import_cli12.Command.withDescription("Rename a volume"));
4142
+ var volumesSaveCommand = import_cli12.Command.make(
4143
+ "save",
4144
+ { microvmId: import_cli12.Args.text({ name: "microvm-id" }).pipe(import_cli12.Args.withDescription("A running microVM with volumes")) },
4145
+ ({ microvmId }) => import_effect24.Effect.gen(function* () {
4146
+ const apiClient = yield* ApiClientService;
4147
+ yield* apiClient.request((client) => client.microvms.saveVolumes(microvmId));
4148
+ yield* import_effect24.Console.log(`Saved the volumes of ${microvmId}.`);
4149
+ }).pipe(reportErrors2("save the volumes"))
4150
+ ).pipe(import_cli12.Command.withDescription("Save a running microVM's volumes without stopping it"));
4151
+ var volumesRemoveCommand = import_cli12.Command.make(
4152
+ "rm",
4153
+ { volumeId: volumeIdArg },
4154
+ ({ volumeId }) => import_effect24.Effect.gen(function* () {
4155
+ const apiClient = yield* ApiClientService;
4156
+ yield* apiClient.request((client) => client.microvms.volumes.delete(volumeId));
4157
+ yield* import_effect24.Console.log(`Deleted volume ${volumeId}.`);
4158
+ }).pipe(reportErrors2("delete the volume"))
4159
+ ).pipe(import_cli12.Command.withDescription("Delete a volume and its contents (it must not be attached)"));
4160
+ var volumesCommand = import_cli12.Command.make("volumes", {}, () => import_effect24.Effect.void).pipe(
4161
+ import_cli12.Command.withDescription("Persistent filesystems that outlive the microVMs they are attached to"),
4162
+ import_cli12.Command.withSubcommands([
4163
+ volumesListCommand,
4164
+ volumesCreateCommand,
4165
+ volumesRenameCommand,
4166
+ volumesSaveCommand,
4167
+ volumesRemoveCommand
4168
+ ])
4169
+ );
3968
4170
  var WEBHOOK_EVENTS = [
3969
4171
  "microvm.created",
3970
4172
  "microvm.running",
@@ -4093,6 +4295,7 @@ var SUBCOMMANDS = [
4093
4295
  extendCommand,
4094
4296
  networkCommand,
4095
4297
  imagesCommand,
4298
+ volumesCommand,
4096
4299
  webhooksCommand
4097
4300
  ];
4098
4301
  var microvmsCommand = import_cli12.Command.make("microvms", {}, () => import_effect24.Effect.void).pipe(