@cdwr/fly-node 0.3.1 → 0.3.2
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/CHANGELOG.md +17 -0
- package/README.md +3 -1
- package/index.cjs +257 -106
- package/index.js +252 -101
- package/package.json +1 -1
- package/src/index.d.ts +1 -1
- package/src/lib/fly.class.d.ts +65 -4
- package/src/lib/mocks.d.ts +8 -1
- package/src/lib/schemas/ips-list.schema.d.ts +75 -0
- package/src/lib/types.d.ts +15 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
## 0.3.2 (2026-10-10)
|
|
2
|
+
|
|
3
|
+
### ✨ Features
|
|
4
|
+
|
|
5
|
+
- **fly-node:** manage an app's public ip addresses ([21d1813e](https://github.com/codeware-sthlm/cdwr/commit/21d1813e))
|
|
6
|
+
|
|
7
|
+
### 🐞 Bug Fixes
|
|
8
|
+
|
|
9
|
+
- **fly-node:** machines.restart waits on the machine's state ([07297c53](https://github.com/codeware-sthlm/cdwr/commit/07297c53))
|
|
10
|
+
- **repo:** ship the licence in published tarballs ([aea1f581](https://github.com/codeware-sthlm/cdwr/commit/aea1f581))
|
|
11
|
+
- **fly-node:** stage every secret a deploy is given ([524abc74](https://github.com/codeware-sthlm/cdwr/commit/524abc74))
|
|
12
|
+
- **fly-node:** drop the unsupported confirm flag from ipv6 allocation ([778b3444](https://github.com/codeware-sthlm/cdwr/commit/778b3444))
|
|
13
|
+
|
|
14
|
+
### ⚙️ Miscellaneous Chores
|
|
15
|
+
|
|
16
|
+
- **repo:** platform under FSL-1.1-MIT, npm packages stay MIT ([ce949b26](https://github.com/codeware-sthlm/cdwr/commit/ce949b26))
|
|
17
|
+
|
|
1
18
|
## 0.3.1 (2026-08-25)
|
|
2
19
|
|
|
3
20
|
### ✨ Features
|
package/README.md
CHANGED
|
@@ -364,10 +364,12 @@ await fly.machines.start('foo-app', 'machine-id-123');
|
|
|
364
364
|
// Stop a machine
|
|
365
365
|
await fly.machines.stop('foo-app', 'machine-id-123');
|
|
366
366
|
|
|
367
|
-
// Restart a machine (stop
|
|
367
|
+
// Restart a machine (stop, wait until stopped, start, wait until started)
|
|
368
368
|
await fly.machines.restart('foo-app', 'machine-id-123');
|
|
369
369
|
```
|
|
370
370
|
|
|
371
|
+
`restart` handles one machine: it waits until Fly reports it stopped (or suspended) before starting it, then until it is `started`. If the start fails after the stop, the error says the machine was left stopped.
|
|
372
|
+
|
|
371
373
|
### Configuration & scaling
|
|
372
374
|
|
|
373
375
|
#### Add a custom domain
|
package/index.cjs
CHANGED
|
@@ -28,8 +28,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
|
|
30
30
|
// packages/fly-node/src/index.ts
|
|
31
|
-
var
|
|
32
|
-
__export(
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
33
|
CertificateApiResponseSchema: () => CertificateApiResponseSchema,
|
|
34
34
|
CertificateListApiResponseSchema: () => CertificateListApiResponseSchema,
|
|
35
35
|
DnsInstructionsSchema: () => DnsInstructionsSchema,
|
|
@@ -40,7 +40,7 @@ __export(src_exports, {
|
|
|
40
40
|
MachineApiResponseSchema: () => MachineApiResponseSchema,
|
|
41
41
|
MachineListApiResponseSchema: () => MachineListApiResponseSchema
|
|
42
42
|
});
|
|
43
|
-
module.exports = __toCommonJS(
|
|
43
|
+
module.exports = __toCommonJS(index_exports);
|
|
44
44
|
|
|
45
45
|
// packages/fly-node/src/lib/schemas/certificate.schema.ts
|
|
46
46
|
var import_zod = require("zod");
|
|
@@ -615,7 +615,7 @@ Output: ${output}`)
|
|
|
615
615
|
}
|
|
616
616
|
|
|
617
617
|
// packages/fly-node/src/lib/fly.class.ts
|
|
618
|
-
var
|
|
618
|
+
var import_zod38 = require("zod");
|
|
619
619
|
|
|
620
620
|
// libs/shared/util/zod/src/lib/coerce-boolean.ts
|
|
621
621
|
var import_zod3 = require("zod");
|
|
@@ -798,114 +798,128 @@ var import_zod21 = require("zod");
|
|
|
798
798
|
var AllowEmptyUrlSchema = import_zod21.z.string().url().or(import_zod21.z.literal(""));
|
|
799
799
|
var NameSchema = import_zod21.z.string({ message: "App name must be a valid slug" }).regex(/^[a-z0-9-]+$/);
|
|
800
800
|
|
|
801
|
-
// packages/fly-node/src/lib/schemas/
|
|
801
|
+
// packages/fly-node/src/lib/schemas/ips-list.schema.ts
|
|
802
802
|
var import_zod23 = require("zod");
|
|
803
|
-
var
|
|
803
|
+
var IpTypeSchema = import_zod23.z.enum(["v4", "v6", "shared_v4", "private_v6"]).or(import_zod23.z.string());
|
|
804
|
+
var IpsListFlyResponseElementSchema = import_zod23.z.object({
|
|
805
|
+
id: import_zod23.z.string(),
|
|
806
|
+
address: import_zod23.z.string(),
|
|
807
|
+
type: IpTypeSchema,
|
|
808
|
+
region: import_zod23.z.string(),
|
|
809
|
+
createdAt: import_zod23.z.string()
|
|
810
|
+
});
|
|
811
|
+
var IpsListTransformedResponseSchema = import_zod23.z.array(
|
|
812
|
+
withCamelCase(IpsListFlyResponseElementSchema)
|
|
813
|
+
);
|
|
814
|
+
|
|
815
|
+
// packages/fly-node/src/lib/schemas/postgres-list.ts
|
|
816
|
+
var import_zod25 = require("zod");
|
|
817
|
+
var PostgresListTransformedResponseSchema = import_zod25.z.array(
|
|
804
818
|
withCamelCase(
|
|
805
|
-
|
|
806
|
-
deployed:
|
|
807
|
-
hostname:
|
|
808
|
-
id:
|
|
809
|
-
name:
|
|
810
|
-
organization:
|
|
811
|
-
status:
|
|
812
|
-
version:
|
|
819
|
+
import_zod25.z.object({
|
|
820
|
+
deployed: import_zod25.z.boolean(),
|
|
821
|
+
hostname: import_zod25.z.string(),
|
|
822
|
+
id: import_zod25.z.string(),
|
|
823
|
+
name: import_zod25.z.string(),
|
|
824
|
+
organization: import_zod25.z.object({ id: import_zod25.z.string(), slug: import_zod25.z.string() }),
|
|
825
|
+
status: import_zod25.z.string(),
|
|
826
|
+
version: import_zod25.z.number()
|
|
813
827
|
})
|
|
814
828
|
)
|
|
815
|
-
).or(
|
|
829
|
+
).or(import_zod25.z.string());
|
|
816
830
|
|
|
817
831
|
// packages/fly-node/src/lib/schemas/postgres-users-list.ts
|
|
818
|
-
var
|
|
819
|
-
var PostgresUsersListTransformedResponseSchema =
|
|
832
|
+
var import_zod27 = require("zod");
|
|
833
|
+
var PostgresUsersListTransformedResponseSchema = import_zod27.z.array(
|
|
820
834
|
withCamelCase(
|
|
821
|
-
|
|
822
|
-
username:
|
|
823
|
-
superuser:
|
|
824
|
-
databases:
|
|
835
|
+
import_zod27.z.object({
|
|
836
|
+
username: import_zod27.z.string(),
|
|
837
|
+
superuser: import_zod27.z.boolean(),
|
|
838
|
+
databases: import_zod27.z.array(import_zod27.z.string())
|
|
825
839
|
})
|
|
826
840
|
)
|
|
827
841
|
);
|
|
828
842
|
|
|
829
843
|
// packages/fly-node/src/lib/schemas/secrets-list-with-app.schema.ts
|
|
830
|
-
var
|
|
844
|
+
var import_zod31 = require("zod");
|
|
831
845
|
|
|
832
846
|
// packages/fly-node/src/lib/schemas/secrets-list.schema.ts
|
|
833
|
-
var
|
|
834
|
-
var SecretsListFlyResponseElementSchema =
|
|
835
|
-
name:
|
|
836
|
-
digest:
|
|
847
|
+
var import_zod29 = require("zod");
|
|
848
|
+
var SecretsListFlyResponseElementSchema = import_zod29.z.object({
|
|
849
|
+
name: import_zod29.z.string(),
|
|
850
|
+
digest: import_zod29.z.string()
|
|
837
851
|
});
|
|
838
|
-
var SecretsListTransformedResponseSchema =
|
|
852
|
+
var SecretsListTransformedResponseSchema = import_zod29.z.array(
|
|
839
853
|
withCamelCase(SecretsListFlyResponseElementSchema)
|
|
840
854
|
);
|
|
841
855
|
|
|
842
856
|
// packages/fly-node/src/lib/schemas/secrets-list-with-app.schema.ts
|
|
843
|
-
var SecretsListWithAppTransformedResponseSchema =
|
|
857
|
+
var SecretsListWithAppTransformedResponseSchema = import_zod31.z.array(
|
|
844
858
|
withCamelCase(
|
|
845
859
|
SecretsListFlyResponseElementSchema.extend({
|
|
846
|
-
app:
|
|
860
|
+
app: import_zod31.z.string()
|
|
847
861
|
})
|
|
848
862
|
)
|
|
849
863
|
);
|
|
850
864
|
|
|
851
865
|
// packages/fly-node/src/lib/schemas/status-extended.schema.ts
|
|
852
|
-
var
|
|
866
|
+
var import_zod35 = require("zod");
|
|
853
867
|
|
|
854
868
|
// packages/fly-node/src/lib/schemas/status.schema.ts
|
|
855
|
-
var
|
|
856
|
-
var statusFlyResponseSchema =
|
|
857
|
-
deployed:
|
|
858
|
-
hostname:
|
|
859
|
-
id:
|
|
860
|
-
machines:
|
|
861
|
-
|
|
862
|
-
id:
|
|
863
|
-
name:
|
|
864
|
-
state:
|
|
865
|
-
region:
|
|
866
|
-
imageRef:
|
|
867
|
-
registry:
|
|
868
|
-
repository:
|
|
869
|
-
tag:
|
|
870
|
-
digest:
|
|
871
|
-
labels:
|
|
869
|
+
var import_zod33 = require("zod");
|
|
870
|
+
var statusFlyResponseSchema = import_zod33.z.object({
|
|
871
|
+
deployed: import_zod33.z.boolean(),
|
|
872
|
+
hostname: import_zod33.z.string(),
|
|
873
|
+
id: import_zod33.z.string(),
|
|
874
|
+
machines: import_zod33.z.array(
|
|
875
|
+
import_zod33.z.object({
|
|
876
|
+
id: import_zod33.z.string(),
|
|
877
|
+
name: import_zod33.z.string(),
|
|
878
|
+
state: import_zod33.z.string(),
|
|
879
|
+
region: import_zod33.z.string(),
|
|
880
|
+
imageRef: import_zod33.z.object({
|
|
881
|
+
registry: import_zod33.z.string(),
|
|
882
|
+
repository: import_zod33.z.string(),
|
|
883
|
+
tag: import_zod33.z.string(),
|
|
884
|
+
digest: import_zod33.z.string(),
|
|
885
|
+
labels: import_zod33.z.record(import_zod33.z.string()).optional()
|
|
872
886
|
}),
|
|
873
|
-
createdAt:
|
|
874
|
-
updatedAt:
|
|
875
|
-
config:
|
|
876
|
-
env:
|
|
877
|
-
metadata:
|
|
878
|
-
guest:
|
|
879
|
-
cpu_kind:
|
|
880
|
-
cpus:
|
|
881
|
-
memory_mb:
|
|
887
|
+
createdAt: import_zod33.z.string().datetime(),
|
|
888
|
+
updatedAt: import_zod33.z.string().datetime(),
|
|
889
|
+
config: import_zod33.z.object({
|
|
890
|
+
env: import_zod33.z.record(import_zod33.z.string()),
|
|
891
|
+
metadata: import_zod33.z.record(import_zod33.z.string()),
|
|
892
|
+
guest: import_zod33.z.object({
|
|
893
|
+
cpu_kind: import_zod33.z.string().optional(),
|
|
894
|
+
cpus: import_zod33.z.number().optional(),
|
|
895
|
+
memory_mb: import_zod33.z.number().optional()
|
|
882
896
|
}).optional()
|
|
883
897
|
}),
|
|
884
|
-
events:
|
|
885
|
-
|
|
886
|
-
type:
|
|
887
|
-
status:
|
|
888
|
-
timestamp:
|
|
898
|
+
events: import_zod33.z.array(
|
|
899
|
+
import_zod33.z.object({
|
|
900
|
+
type: import_zod33.z.string(),
|
|
901
|
+
status: import_zod33.z.string(),
|
|
902
|
+
timestamp: import_zod33.z.number()
|
|
889
903
|
})
|
|
890
904
|
),
|
|
891
|
-
checks:
|
|
892
|
-
|
|
893
|
-
name:
|
|
894
|
-
status:
|
|
895
|
-
output:
|
|
896
|
-
updatedAt:
|
|
905
|
+
checks: import_zod33.z.array(
|
|
906
|
+
import_zod33.z.object({
|
|
907
|
+
name: import_zod33.z.string(),
|
|
908
|
+
status: import_zod33.z.string(),
|
|
909
|
+
output: import_zod33.z.string(),
|
|
910
|
+
updatedAt: import_zod33.z.string().datetime()
|
|
897
911
|
})
|
|
898
912
|
).optional(),
|
|
899
|
-
hostStatus:
|
|
913
|
+
hostStatus: import_zod33.z.string()
|
|
900
914
|
})
|
|
901
915
|
),
|
|
902
|
-
name:
|
|
903
|
-
organization:
|
|
904
|
-
id:
|
|
905
|
-
slug:
|
|
916
|
+
name: import_zod33.z.string(),
|
|
917
|
+
organization: import_zod33.z.object({
|
|
918
|
+
id: import_zod33.z.string(),
|
|
919
|
+
slug: import_zod33.z.string()
|
|
906
920
|
}),
|
|
907
|
-
status:
|
|
908
|
-
version:
|
|
921
|
+
status: import_zod33.z.string(),
|
|
922
|
+
version: import_zod33.z.number()
|
|
909
923
|
});
|
|
910
924
|
var StatusTransformedResponseSchema = withCamelCase(
|
|
911
925
|
statusFlyResponseSchema,
|
|
@@ -922,8 +936,8 @@ var StatusTransformedResponseSchema = withCamelCase(
|
|
|
922
936
|
// packages/fly-node/src/lib/schemas/status-extended.schema.ts
|
|
923
937
|
var StatusExtendedTransformedResponseSchema = withCamelCase(
|
|
924
938
|
statusFlyResponseSchema.extend({
|
|
925
|
-
domains:
|
|
926
|
-
secrets:
|
|
939
|
+
domains: import_zod35.z.array(import_zod35.z.object({ hostname: import_zod35.z.string() })),
|
|
940
|
+
secrets: import_zod35.z.array(import_zod35.z.object({ name: import_zod35.z.string() }))
|
|
927
941
|
}),
|
|
928
942
|
{
|
|
929
943
|
preserve: [
|
|
@@ -935,21 +949,23 @@ var StatusExtendedTransformedResponseSchema = withCamelCase(
|
|
|
935
949
|
);
|
|
936
950
|
|
|
937
951
|
// packages/fly-node/src/lib/schemas/version.schema.ts
|
|
938
|
-
var
|
|
952
|
+
var import_zod37 = require("zod");
|
|
939
953
|
var VersionTransformedResponseSchema = withCamelCase(
|
|
940
|
-
|
|
941
|
-
name:
|
|
942
|
-
version:
|
|
943
|
-
commit:
|
|
944
|
-
branchName:
|
|
945
|
-
buildDate:
|
|
946
|
-
os:
|
|
947
|
-
architecture:
|
|
948
|
-
environment:
|
|
954
|
+
import_zod37.z.object({
|
|
955
|
+
name: import_zod37.z.string(),
|
|
956
|
+
version: import_zod37.z.string(),
|
|
957
|
+
commit: import_zod37.z.string(),
|
|
958
|
+
branchName: import_zod37.z.string(),
|
|
959
|
+
buildDate: import_zod37.z.string().datetime(),
|
|
960
|
+
os: import_zod37.z.string(),
|
|
961
|
+
architecture: import_zod37.z.string(),
|
|
962
|
+
environment: import_zod37.z.string()
|
|
949
963
|
})
|
|
950
964
|
);
|
|
951
965
|
|
|
952
966
|
// packages/fly-node/src/lib/fly.class.ts
|
|
967
|
+
var MACHINE_STATE_INTERVAL_MS = 2e3;
|
|
968
|
+
var MACHINE_STATE_TIMEOUT_MS = 12e4;
|
|
953
969
|
var Fly = class {
|
|
954
970
|
/** Use underscore to avoid conflict with public `config` property */
|
|
955
971
|
_config;
|
|
@@ -1265,6 +1281,62 @@ ${error}`
|
|
|
1265
1281
|
);
|
|
1266
1282
|
}
|
|
1267
1283
|
};
|
|
1284
|
+
/**
|
|
1285
|
+
* Manage public IP addresses
|
|
1286
|
+
*/
|
|
1287
|
+
ips = {
|
|
1288
|
+
/**
|
|
1289
|
+
* List the public IP addresses allocated to an application
|
|
1290
|
+
*
|
|
1291
|
+
* @param options - The application or configuration to list addresses for
|
|
1292
|
+
* @returns The allocated addresses, empty when the app has none
|
|
1293
|
+
* @throws An error if listing addresses fails
|
|
1294
|
+
*/
|
|
1295
|
+
list: async (options) => {
|
|
1296
|
+
try {
|
|
1297
|
+
await this.ensureInitialized();
|
|
1298
|
+
return await this.fetchAppIps("throwOnError", options);
|
|
1299
|
+
} catch (error) {
|
|
1300
|
+
throw new Error(`[list ips] something broke
|
|
1301
|
+
${error}`);
|
|
1302
|
+
}
|
|
1303
|
+
},
|
|
1304
|
+
/**
|
|
1305
|
+
* Give an application the public addresses it needs to be reachable.
|
|
1306
|
+
*
|
|
1307
|
+
* Idempotent by inspection: it allocates only what is missing, so it is
|
|
1308
|
+
* safe on every deploy rather than only on create. That matters because
|
|
1309
|
+
* `flyctl` allocates addresses only for an app it creates itself — an app
|
|
1310
|
+
* that arrives any other way, such as a registry push auto-creating it,
|
|
1311
|
+
* deploys green and answers nothing.
|
|
1312
|
+
*
|
|
1313
|
+
* @param options - The application, and which addresses to ensure
|
|
1314
|
+
* @returns The addresses the application holds afterwards
|
|
1315
|
+
* @throws An error if an address cannot be allocated
|
|
1316
|
+
*/
|
|
1317
|
+
ensure: async (options) => {
|
|
1318
|
+
try {
|
|
1319
|
+
await this.ensureInitialized();
|
|
1320
|
+
const { sharedV4 = true, v6 = true } = options ?? {};
|
|
1321
|
+
const existing = await this.fetchAppIps("throwOnError", options);
|
|
1322
|
+
const has = (...types) => existing.some((ip) => types.includes(ip.type));
|
|
1323
|
+
if (sharedV4 && !has("v4", "shared_v4")) {
|
|
1324
|
+
await this.allocateIp("allocate-v4", options);
|
|
1325
|
+
this.logger.info("Allocated a shared IPv4 address");
|
|
1326
|
+
}
|
|
1327
|
+
if (v6 && !has("v6")) {
|
|
1328
|
+
await this.allocateIp("allocate-v6", options);
|
|
1329
|
+
this.logger.info("Allocated a dedicated IPv6 address");
|
|
1330
|
+
}
|
|
1331
|
+
return await this.fetchAppIps("throwOnError", options);
|
|
1332
|
+
} catch (error) {
|
|
1333
|
+
throw new Error(
|
|
1334
|
+
`[ensure ips] something broke, check your app addresses
|
|
1335
|
+
${error}`
|
|
1336
|
+
);
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
};
|
|
1268
1340
|
/**
|
|
1269
1341
|
* Manage machines
|
|
1270
1342
|
*/
|
|
@@ -1304,7 +1376,11 @@ ${error}`);
|
|
|
1304
1376
|
}
|
|
1305
1377
|
},
|
|
1306
1378
|
/**
|
|
1307
|
-
* Restart a machine by stopping then starting it
|
|
1379
|
+
* Restart a machine by stopping then starting it.
|
|
1380
|
+
*
|
|
1381
|
+
* Waits until Fly reports the machine stopped (or suspended) before
|
|
1382
|
+
* starting it, then until it is started. If the start fails after the
|
|
1383
|
+
* stop, the error says the machine was left stopped.
|
|
1308
1384
|
*
|
|
1309
1385
|
* @param app - The name of the application
|
|
1310
1386
|
* @param machineId - The machine ID to restart
|
|
@@ -1553,7 +1629,6 @@ ${error}`);
|
|
|
1553
1629
|
*/
|
|
1554
1630
|
async deployApp(options) {
|
|
1555
1631
|
let appName = "";
|
|
1556
|
-
let appSecrets = [];
|
|
1557
1632
|
let deployConfig = options?.config || this.instanceConfig;
|
|
1558
1633
|
let lookupApp = options?.app || this.instanceApp;
|
|
1559
1634
|
const lookupConfig = deployConfig;
|
|
@@ -1618,26 +1693,30 @@ ${error}`);
|
|
|
1618
1693
|
appName = name;
|
|
1619
1694
|
this.logger.info(`Application '${appName}' was created`);
|
|
1620
1695
|
} else {
|
|
1621
|
-
appSecrets = await this.fetchAppSecrets("throwOnError", { app: appName });
|
|
1622
1696
|
this.logger.info(`Updating existing application '${appName}'`);
|
|
1623
1697
|
}
|
|
1624
1698
|
NameSchema.parse(appName);
|
|
1625
1699
|
if (!options?.buildOnly && options?.secrets) {
|
|
1626
|
-
const
|
|
1627
|
-
const
|
|
1700
|
+
const staged = {};
|
|
1701
|
+
const empty = [];
|
|
1628
1702
|
for (const [key, value] of Object.entries(options.secrets)) {
|
|
1629
|
-
if (
|
|
1630
|
-
|
|
1703
|
+
if (value === "") {
|
|
1704
|
+
empty.push(key);
|
|
1631
1705
|
} else {
|
|
1632
|
-
|
|
1633
|
-
`Secret '${key}' already exists for '${appName}', skipping`
|
|
1634
|
-
);
|
|
1706
|
+
staged[key] = value;
|
|
1635
1707
|
}
|
|
1636
1708
|
}
|
|
1637
|
-
|
|
1709
|
+
if (empty.length > 0) {
|
|
1710
|
+
this.logger.error(
|
|
1711
|
+
`Refusing to blank secrets with no value for '${appName}': ${empty.join(", ")}`
|
|
1712
|
+
);
|
|
1713
|
+
}
|
|
1714
|
+
const keys = Object.keys(staged);
|
|
1638
1715
|
if (keys.length > 0) {
|
|
1639
|
-
this.logger.info(
|
|
1640
|
-
|
|
1716
|
+
this.logger.info(
|
|
1717
|
+
`Staging secrets for '${appName}': ${keys.join(", ")}`
|
|
1718
|
+
);
|
|
1719
|
+
await this.setAppSecrets(staged, {
|
|
1641
1720
|
app: appName,
|
|
1642
1721
|
stage: true
|
|
1643
1722
|
});
|
|
@@ -1789,6 +1868,45 @@ ${error}`);
|
|
|
1789
1868
|
await this.execFly(args);
|
|
1790
1869
|
return NameSchema.parse(status.name);
|
|
1791
1870
|
}
|
|
1871
|
+
/**
|
|
1872
|
+
* Allocate one public address.
|
|
1873
|
+
*
|
|
1874
|
+
* A dedicated v4 is billed, so the v4 path is always `--shared`.
|
|
1875
|
+
*
|
|
1876
|
+
* The two commands do not take the same flags: `allocate-v4` has `--yes`
|
|
1877
|
+
* because a dedicated v4 costs money and prompts, while `allocate-v6` is
|
|
1878
|
+
* free, never prompts, and rejects the flag outright with `unknown flag`.
|
|
1879
|
+
* The asymmetry is flyctl's, not ours — do not tidy it away.
|
|
1880
|
+
*
|
|
1881
|
+
* @private
|
|
1882
|
+
* @throws An error if the address cannot be allocated
|
|
1883
|
+
*/
|
|
1884
|
+
async allocateIp(command, options) {
|
|
1885
|
+
const args = ["ips", command];
|
|
1886
|
+
if (command === "allocate-v4") {
|
|
1887
|
+
args.push("--shared");
|
|
1888
|
+
}
|
|
1889
|
+
args.push(...this.getAppOrConfigArgs(options));
|
|
1890
|
+
if (command === "allocate-v4") {
|
|
1891
|
+
args.push("--yes");
|
|
1892
|
+
}
|
|
1893
|
+
await this.execFly(args);
|
|
1894
|
+
}
|
|
1895
|
+
async fetchAppIps(onError, options) {
|
|
1896
|
+
const args = ["ips", "list"];
|
|
1897
|
+
args.push(...this.getAppOrConfigArgs(options));
|
|
1898
|
+
args.push("--json");
|
|
1899
|
+
try {
|
|
1900
|
+
return await IpsListTransformedResponseSchema.parseAsync(
|
|
1901
|
+
await this.execFly(args)
|
|
1902
|
+
);
|
|
1903
|
+
} catch (error) {
|
|
1904
|
+
if (onError === "throwOnError") {
|
|
1905
|
+
throw error;
|
|
1906
|
+
}
|
|
1907
|
+
return null;
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1792
1910
|
/**
|
|
1793
1911
|
* @private
|
|
1794
1912
|
* @returns The name of the application
|
|
@@ -1841,13 +1959,47 @@ ${error}`);
|
|
|
1841
1959
|
}
|
|
1842
1960
|
/**
|
|
1843
1961
|
* @private
|
|
1844
|
-
* Restart a machine by stopping then starting it
|
|
1845
|
-
*
|
|
1962
|
+
* Restart a machine by stopping then starting it, waiting on the state Fly
|
|
1963
|
+
* reports rather than a fixed delay: the start is only issued once the
|
|
1964
|
+
* machine is stopped (or suspended), and the restart is done once it is
|
|
1965
|
+
* started again.
|
|
1966
|
+
* @throws An error saying the machine was left stopped when anything after
|
|
1967
|
+
* the stop fails
|
|
1846
1968
|
*/
|
|
1847
1969
|
async restartMachine(app, machineId) {
|
|
1848
1970
|
await this.stopMachine(app, machineId);
|
|
1849
|
-
|
|
1850
|
-
|
|
1971
|
+
try {
|
|
1972
|
+
await this.waitForMachineState(app, machineId, ["stopped", "suspended"]);
|
|
1973
|
+
await this.startMachine(app, machineId);
|
|
1974
|
+
await this.waitForMachineState(app, machineId, ["started"]);
|
|
1975
|
+
} catch (error) {
|
|
1976
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
1977
|
+
throw new Error(
|
|
1978
|
+
`Machine '${machineId}' in app '${app}' was stopped but did not start again: ${reason}`,
|
|
1979
|
+
{ cause: error }
|
|
1980
|
+
);
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
/**
|
|
1984
|
+
* @private
|
|
1985
|
+
* Poll the app status until the machine reports one of the given states
|
|
1986
|
+
* @throws An error if the machine does not reach a state in time
|
|
1987
|
+
*/
|
|
1988
|
+
async waitForMachineState(app, machineId, states) {
|
|
1989
|
+
const deadline = Date.now() + MACHINE_STATE_TIMEOUT_MS;
|
|
1990
|
+
while (Date.now() < deadline) {
|
|
1991
|
+
const status = await this.fetchAppStatus("nullOnError", { app });
|
|
1992
|
+
const machine = status?.machines.find(({ id }) => id === machineId);
|
|
1993
|
+
if (machine && states.includes(machine.state)) {
|
|
1994
|
+
return;
|
|
1995
|
+
}
|
|
1996
|
+
await new Promise(
|
|
1997
|
+
(resolve) => setTimeout(resolve, MACHINE_STATE_INTERVAL_MS)
|
|
1998
|
+
);
|
|
1999
|
+
}
|
|
2000
|
+
throw new Error(
|
|
2001
|
+
`Machine '${machineId}' in app '${app}' did not reach ${states.join("/")} in time`
|
|
2002
|
+
);
|
|
1851
2003
|
}
|
|
1852
2004
|
/**
|
|
1853
2005
|
* @private
|
|
@@ -1943,7 +2095,7 @@ ${error}`);
|
|
|
1943
2095
|
if (onError === "throwOnError") {
|
|
1944
2096
|
throw error;
|
|
1945
2097
|
}
|
|
1946
|
-
if (error instanceof
|
|
2098
|
+
if (error instanceof import_zod38.ZodError) {
|
|
1947
2099
|
this.logger.info(`Failed to parse response:
|
|
1948
2100
|
${error.message}`);
|
|
1949
2101
|
this.logger.info(`Raw response:
|
|
@@ -2054,8 +2206,7 @@ ${JSON.stringify(response, null, 2)}`);
|
|
|
2054
2206
|
* @throws An error if Fly CLI is not installed or not authenticated
|
|
2055
2207
|
*/
|
|
2056
2208
|
async ensureInitialized() {
|
|
2057
|
-
if (this.initialized)
|
|
2058
|
-
return;
|
|
2209
|
+
if (this.initialized) return;
|
|
2059
2210
|
try {
|
|
2060
2211
|
if (!await this.isInstalled()) {
|
|
2061
2212
|
throw new Error("Fly CLI must be installed to use this library");
|
package/index.js
CHANGED
|
@@ -756,114 +756,128 @@ import { z as z16 } from "zod";
|
|
|
756
756
|
var AllowEmptyUrlSchema = z16.string().url().or(z16.literal(""));
|
|
757
757
|
var NameSchema = z16.string({ message: "App name must be a valid slug" }).regex(/^[a-z0-9-]+$/);
|
|
758
758
|
|
|
759
|
-
// packages/fly-node/src/lib/schemas/
|
|
759
|
+
// packages/fly-node/src/lib/schemas/ips-list.schema.ts
|
|
760
760
|
import { z as z17 } from "zod";
|
|
761
|
-
var
|
|
761
|
+
var IpTypeSchema = z17.enum(["v4", "v6", "shared_v4", "private_v6"]).or(z17.string());
|
|
762
|
+
var IpsListFlyResponseElementSchema = z17.object({
|
|
763
|
+
id: z17.string(),
|
|
764
|
+
address: z17.string(),
|
|
765
|
+
type: IpTypeSchema,
|
|
766
|
+
region: z17.string(),
|
|
767
|
+
createdAt: z17.string()
|
|
768
|
+
});
|
|
769
|
+
var IpsListTransformedResponseSchema = z17.array(
|
|
770
|
+
withCamelCase(IpsListFlyResponseElementSchema)
|
|
771
|
+
);
|
|
772
|
+
|
|
773
|
+
// packages/fly-node/src/lib/schemas/postgres-list.ts
|
|
774
|
+
import { z as z18 } from "zod";
|
|
775
|
+
var PostgresListTransformedResponseSchema = z18.array(
|
|
762
776
|
withCamelCase(
|
|
763
|
-
|
|
764
|
-
deployed:
|
|
765
|
-
hostname:
|
|
766
|
-
id:
|
|
767
|
-
name:
|
|
768
|
-
organization:
|
|
769
|
-
status:
|
|
770
|
-
version:
|
|
777
|
+
z18.object({
|
|
778
|
+
deployed: z18.boolean(),
|
|
779
|
+
hostname: z18.string(),
|
|
780
|
+
id: z18.string(),
|
|
781
|
+
name: z18.string(),
|
|
782
|
+
organization: z18.object({ id: z18.string(), slug: z18.string() }),
|
|
783
|
+
status: z18.string(),
|
|
784
|
+
version: z18.number()
|
|
771
785
|
})
|
|
772
786
|
)
|
|
773
|
-
).or(
|
|
787
|
+
).or(z18.string());
|
|
774
788
|
|
|
775
789
|
// packages/fly-node/src/lib/schemas/postgres-users-list.ts
|
|
776
|
-
import { z as
|
|
777
|
-
var PostgresUsersListTransformedResponseSchema =
|
|
790
|
+
import { z as z19 } from "zod";
|
|
791
|
+
var PostgresUsersListTransformedResponseSchema = z19.array(
|
|
778
792
|
withCamelCase(
|
|
779
|
-
|
|
780
|
-
username:
|
|
781
|
-
superuser:
|
|
782
|
-
databases:
|
|
793
|
+
z19.object({
|
|
794
|
+
username: z19.string(),
|
|
795
|
+
superuser: z19.boolean(),
|
|
796
|
+
databases: z19.array(z19.string())
|
|
783
797
|
})
|
|
784
798
|
)
|
|
785
799
|
);
|
|
786
800
|
|
|
787
801
|
// packages/fly-node/src/lib/schemas/secrets-list-with-app.schema.ts
|
|
788
|
-
import { z as
|
|
802
|
+
import { z as z21 } from "zod";
|
|
789
803
|
|
|
790
804
|
// packages/fly-node/src/lib/schemas/secrets-list.schema.ts
|
|
791
|
-
import { z as
|
|
792
|
-
var SecretsListFlyResponseElementSchema =
|
|
793
|
-
name:
|
|
794
|
-
digest:
|
|
805
|
+
import { z as z20 } from "zod";
|
|
806
|
+
var SecretsListFlyResponseElementSchema = z20.object({
|
|
807
|
+
name: z20.string(),
|
|
808
|
+
digest: z20.string()
|
|
795
809
|
});
|
|
796
|
-
var SecretsListTransformedResponseSchema =
|
|
810
|
+
var SecretsListTransformedResponseSchema = z20.array(
|
|
797
811
|
withCamelCase(SecretsListFlyResponseElementSchema)
|
|
798
812
|
);
|
|
799
813
|
|
|
800
814
|
// packages/fly-node/src/lib/schemas/secrets-list-with-app.schema.ts
|
|
801
|
-
var SecretsListWithAppTransformedResponseSchema =
|
|
815
|
+
var SecretsListWithAppTransformedResponseSchema = z21.array(
|
|
802
816
|
withCamelCase(
|
|
803
817
|
SecretsListFlyResponseElementSchema.extend({
|
|
804
|
-
app:
|
|
818
|
+
app: z21.string()
|
|
805
819
|
})
|
|
806
820
|
)
|
|
807
821
|
);
|
|
808
822
|
|
|
809
823
|
// packages/fly-node/src/lib/schemas/status-extended.schema.ts
|
|
810
|
-
import { z as
|
|
824
|
+
import { z as z23 } from "zod";
|
|
811
825
|
|
|
812
826
|
// packages/fly-node/src/lib/schemas/status.schema.ts
|
|
813
|
-
import { z as
|
|
814
|
-
var statusFlyResponseSchema =
|
|
815
|
-
deployed:
|
|
816
|
-
hostname:
|
|
817
|
-
id:
|
|
818
|
-
machines:
|
|
819
|
-
|
|
820
|
-
id:
|
|
821
|
-
name:
|
|
822
|
-
state:
|
|
823
|
-
region:
|
|
824
|
-
imageRef:
|
|
825
|
-
registry:
|
|
826
|
-
repository:
|
|
827
|
-
tag:
|
|
828
|
-
digest:
|
|
829
|
-
labels:
|
|
827
|
+
import { z as z22 } from "zod";
|
|
828
|
+
var statusFlyResponseSchema = z22.object({
|
|
829
|
+
deployed: z22.boolean(),
|
|
830
|
+
hostname: z22.string(),
|
|
831
|
+
id: z22.string(),
|
|
832
|
+
machines: z22.array(
|
|
833
|
+
z22.object({
|
|
834
|
+
id: z22.string(),
|
|
835
|
+
name: z22.string(),
|
|
836
|
+
state: z22.string(),
|
|
837
|
+
region: z22.string(),
|
|
838
|
+
imageRef: z22.object({
|
|
839
|
+
registry: z22.string(),
|
|
840
|
+
repository: z22.string(),
|
|
841
|
+
tag: z22.string(),
|
|
842
|
+
digest: z22.string(),
|
|
843
|
+
labels: z22.record(z22.string()).optional()
|
|
830
844
|
}),
|
|
831
|
-
createdAt:
|
|
832
|
-
updatedAt:
|
|
833
|
-
config:
|
|
834
|
-
env:
|
|
835
|
-
metadata:
|
|
836
|
-
guest:
|
|
837
|
-
cpu_kind:
|
|
838
|
-
cpus:
|
|
839
|
-
memory_mb:
|
|
845
|
+
createdAt: z22.string().datetime(),
|
|
846
|
+
updatedAt: z22.string().datetime(),
|
|
847
|
+
config: z22.object({
|
|
848
|
+
env: z22.record(z22.string()),
|
|
849
|
+
metadata: z22.record(z22.string()),
|
|
850
|
+
guest: z22.object({
|
|
851
|
+
cpu_kind: z22.string().optional(),
|
|
852
|
+
cpus: z22.number().optional(),
|
|
853
|
+
memory_mb: z22.number().optional()
|
|
840
854
|
}).optional()
|
|
841
855
|
}),
|
|
842
|
-
events:
|
|
843
|
-
|
|
844
|
-
type:
|
|
845
|
-
status:
|
|
846
|
-
timestamp:
|
|
856
|
+
events: z22.array(
|
|
857
|
+
z22.object({
|
|
858
|
+
type: z22.string(),
|
|
859
|
+
status: z22.string(),
|
|
860
|
+
timestamp: z22.number()
|
|
847
861
|
})
|
|
848
862
|
),
|
|
849
|
-
checks:
|
|
850
|
-
|
|
851
|
-
name:
|
|
852
|
-
status:
|
|
853
|
-
output:
|
|
854
|
-
updatedAt:
|
|
863
|
+
checks: z22.array(
|
|
864
|
+
z22.object({
|
|
865
|
+
name: z22.string(),
|
|
866
|
+
status: z22.string(),
|
|
867
|
+
output: z22.string(),
|
|
868
|
+
updatedAt: z22.string().datetime()
|
|
855
869
|
})
|
|
856
870
|
).optional(),
|
|
857
|
-
hostStatus:
|
|
871
|
+
hostStatus: z22.string()
|
|
858
872
|
})
|
|
859
873
|
),
|
|
860
|
-
name:
|
|
861
|
-
organization:
|
|
862
|
-
id:
|
|
863
|
-
slug:
|
|
874
|
+
name: z22.string(),
|
|
875
|
+
organization: z22.object({
|
|
876
|
+
id: z22.string(),
|
|
877
|
+
slug: z22.string()
|
|
864
878
|
}),
|
|
865
|
-
status:
|
|
866
|
-
version:
|
|
879
|
+
status: z22.string(),
|
|
880
|
+
version: z22.number()
|
|
867
881
|
});
|
|
868
882
|
var StatusTransformedResponseSchema = withCamelCase(
|
|
869
883
|
statusFlyResponseSchema,
|
|
@@ -880,8 +894,8 @@ var StatusTransformedResponseSchema = withCamelCase(
|
|
|
880
894
|
// packages/fly-node/src/lib/schemas/status-extended.schema.ts
|
|
881
895
|
var StatusExtendedTransformedResponseSchema = withCamelCase(
|
|
882
896
|
statusFlyResponseSchema.extend({
|
|
883
|
-
domains:
|
|
884
|
-
secrets:
|
|
897
|
+
domains: z23.array(z23.object({ hostname: z23.string() })),
|
|
898
|
+
secrets: z23.array(z23.object({ name: z23.string() }))
|
|
885
899
|
}),
|
|
886
900
|
{
|
|
887
901
|
preserve: [
|
|
@@ -893,21 +907,23 @@ var StatusExtendedTransformedResponseSchema = withCamelCase(
|
|
|
893
907
|
);
|
|
894
908
|
|
|
895
909
|
// packages/fly-node/src/lib/schemas/version.schema.ts
|
|
896
|
-
import { z as
|
|
910
|
+
import { z as z24 } from "zod";
|
|
897
911
|
var VersionTransformedResponseSchema = withCamelCase(
|
|
898
|
-
|
|
899
|
-
name:
|
|
900
|
-
version:
|
|
901
|
-
commit:
|
|
902
|
-
branchName:
|
|
903
|
-
buildDate:
|
|
904
|
-
os:
|
|
905
|
-
architecture:
|
|
906
|
-
environment:
|
|
912
|
+
z24.object({
|
|
913
|
+
name: z24.string(),
|
|
914
|
+
version: z24.string(),
|
|
915
|
+
commit: z24.string(),
|
|
916
|
+
branchName: z24.string(),
|
|
917
|
+
buildDate: z24.string().datetime(),
|
|
918
|
+
os: z24.string(),
|
|
919
|
+
architecture: z24.string(),
|
|
920
|
+
environment: z24.string()
|
|
907
921
|
})
|
|
908
922
|
);
|
|
909
923
|
|
|
910
924
|
// packages/fly-node/src/lib/fly.class.ts
|
|
925
|
+
var MACHINE_STATE_INTERVAL_MS = 2e3;
|
|
926
|
+
var MACHINE_STATE_TIMEOUT_MS = 12e4;
|
|
911
927
|
var Fly = class {
|
|
912
928
|
/** Use underscore to avoid conflict with public `config` property */
|
|
913
929
|
_config;
|
|
@@ -1223,6 +1239,62 @@ ${error}`
|
|
|
1223
1239
|
);
|
|
1224
1240
|
}
|
|
1225
1241
|
};
|
|
1242
|
+
/**
|
|
1243
|
+
* Manage public IP addresses
|
|
1244
|
+
*/
|
|
1245
|
+
ips = {
|
|
1246
|
+
/**
|
|
1247
|
+
* List the public IP addresses allocated to an application
|
|
1248
|
+
*
|
|
1249
|
+
* @param options - The application or configuration to list addresses for
|
|
1250
|
+
* @returns The allocated addresses, empty when the app has none
|
|
1251
|
+
* @throws An error if listing addresses fails
|
|
1252
|
+
*/
|
|
1253
|
+
list: async (options) => {
|
|
1254
|
+
try {
|
|
1255
|
+
await this.ensureInitialized();
|
|
1256
|
+
return await this.fetchAppIps("throwOnError", options);
|
|
1257
|
+
} catch (error) {
|
|
1258
|
+
throw new Error(`[list ips] something broke
|
|
1259
|
+
${error}`);
|
|
1260
|
+
}
|
|
1261
|
+
},
|
|
1262
|
+
/**
|
|
1263
|
+
* Give an application the public addresses it needs to be reachable.
|
|
1264
|
+
*
|
|
1265
|
+
* Idempotent by inspection: it allocates only what is missing, so it is
|
|
1266
|
+
* safe on every deploy rather than only on create. That matters because
|
|
1267
|
+
* `flyctl` allocates addresses only for an app it creates itself — an app
|
|
1268
|
+
* that arrives any other way, such as a registry push auto-creating it,
|
|
1269
|
+
* deploys green and answers nothing.
|
|
1270
|
+
*
|
|
1271
|
+
* @param options - The application, and which addresses to ensure
|
|
1272
|
+
* @returns The addresses the application holds afterwards
|
|
1273
|
+
* @throws An error if an address cannot be allocated
|
|
1274
|
+
*/
|
|
1275
|
+
ensure: async (options) => {
|
|
1276
|
+
try {
|
|
1277
|
+
await this.ensureInitialized();
|
|
1278
|
+
const { sharedV4 = true, v6 = true } = options ?? {};
|
|
1279
|
+
const existing = await this.fetchAppIps("throwOnError", options);
|
|
1280
|
+
const has = (...types) => existing.some((ip) => types.includes(ip.type));
|
|
1281
|
+
if (sharedV4 && !has("v4", "shared_v4")) {
|
|
1282
|
+
await this.allocateIp("allocate-v4", options);
|
|
1283
|
+
this.logger.info("Allocated a shared IPv4 address");
|
|
1284
|
+
}
|
|
1285
|
+
if (v6 && !has("v6")) {
|
|
1286
|
+
await this.allocateIp("allocate-v6", options);
|
|
1287
|
+
this.logger.info("Allocated a dedicated IPv6 address");
|
|
1288
|
+
}
|
|
1289
|
+
return await this.fetchAppIps("throwOnError", options);
|
|
1290
|
+
} catch (error) {
|
|
1291
|
+
throw new Error(
|
|
1292
|
+
`[ensure ips] something broke, check your app addresses
|
|
1293
|
+
${error}`
|
|
1294
|
+
);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
};
|
|
1226
1298
|
/**
|
|
1227
1299
|
* Manage machines
|
|
1228
1300
|
*/
|
|
@@ -1262,7 +1334,11 @@ ${error}`);
|
|
|
1262
1334
|
}
|
|
1263
1335
|
},
|
|
1264
1336
|
/**
|
|
1265
|
-
* Restart a machine by stopping then starting it
|
|
1337
|
+
* Restart a machine by stopping then starting it.
|
|
1338
|
+
*
|
|
1339
|
+
* Waits until Fly reports the machine stopped (or suspended) before
|
|
1340
|
+
* starting it, then until it is started. If the start fails after the
|
|
1341
|
+
* stop, the error says the machine was left stopped.
|
|
1266
1342
|
*
|
|
1267
1343
|
* @param app - The name of the application
|
|
1268
1344
|
* @param machineId - The machine ID to restart
|
|
@@ -1511,7 +1587,6 @@ ${error}`);
|
|
|
1511
1587
|
*/
|
|
1512
1588
|
async deployApp(options) {
|
|
1513
1589
|
let appName = "";
|
|
1514
|
-
let appSecrets = [];
|
|
1515
1590
|
let deployConfig = options?.config || this.instanceConfig;
|
|
1516
1591
|
let lookupApp = options?.app || this.instanceApp;
|
|
1517
1592
|
const lookupConfig = deployConfig;
|
|
@@ -1576,26 +1651,30 @@ ${error}`);
|
|
|
1576
1651
|
appName = name;
|
|
1577
1652
|
this.logger.info(`Application '${appName}' was created`);
|
|
1578
1653
|
} else {
|
|
1579
|
-
appSecrets = await this.fetchAppSecrets("throwOnError", { app: appName });
|
|
1580
1654
|
this.logger.info(`Updating existing application '${appName}'`);
|
|
1581
1655
|
}
|
|
1582
1656
|
NameSchema.parse(appName);
|
|
1583
1657
|
if (!options?.buildOnly && options?.secrets) {
|
|
1584
|
-
const
|
|
1585
|
-
const
|
|
1658
|
+
const staged = {};
|
|
1659
|
+
const empty = [];
|
|
1586
1660
|
for (const [key, value] of Object.entries(options.secrets)) {
|
|
1587
|
-
if (
|
|
1588
|
-
|
|
1661
|
+
if (value === "") {
|
|
1662
|
+
empty.push(key);
|
|
1589
1663
|
} else {
|
|
1590
|
-
|
|
1591
|
-
`Secret '${key}' already exists for '${appName}', skipping`
|
|
1592
|
-
);
|
|
1664
|
+
staged[key] = value;
|
|
1593
1665
|
}
|
|
1594
1666
|
}
|
|
1595
|
-
|
|
1667
|
+
if (empty.length > 0) {
|
|
1668
|
+
this.logger.error(
|
|
1669
|
+
`Refusing to blank secrets with no value for '${appName}': ${empty.join(", ")}`
|
|
1670
|
+
);
|
|
1671
|
+
}
|
|
1672
|
+
const keys = Object.keys(staged);
|
|
1596
1673
|
if (keys.length > 0) {
|
|
1597
|
-
this.logger.info(
|
|
1598
|
-
|
|
1674
|
+
this.logger.info(
|
|
1675
|
+
`Staging secrets for '${appName}': ${keys.join(", ")}`
|
|
1676
|
+
);
|
|
1677
|
+
await this.setAppSecrets(staged, {
|
|
1599
1678
|
app: appName,
|
|
1600
1679
|
stage: true
|
|
1601
1680
|
});
|
|
@@ -1747,6 +1826,45 @@ ${error}`);
|
|
|
1747
1826
|
await this.execFly(args);
|
|
1748
1827
|
return NameSchema.parse(status.name);
|
|
1749
1828
|
}
|
|
1829
|
+
/**
|
|
1830
|
+
* Allocate one public address.
|
|
1831
|
+
*
|
|
1832
|
+
* A dedicated v4 is billed, so the v4 path is always `--shared`.
|
|
1833
|
+
*
|
|
1834
|
+
* The two commands do not take the same flags: `allocate-v4` has `--yes`
|
|
1835
|
+
* because a dedicated v4 costs money and prompts, while `allocate-v6` is
|
|
1836
|
+
* free, never prompts, and rejects the flag outright with `unknown flag`.
|
|
1837
|
+
* The asymmetry is flyctl's, not ours — do not tidy it away.
|
|
1838
|
+
*
|
|
1839
|
+
* @private
|
|
1840
|
+
* @throws An error if the address cannot be allocated
|
|
1841
|
+
*/
|
|
1842
|
+
async allocateIp(command, options) {
|
|
1843
|
+
const args = ["ips", command];
|
|
1844
|
+
if (command === "allocate-v4") {
|
|
1845
|
+
args.push("--shared");
|
|
1846
|
+
}
|
|
1847
|
+
args.push(...this.getAppOrConfigArgs(options));
|
|
1848
|
+
if (command === "allocate-v4") {
|
|
1849
|
+
args.push("--yes");
|
|
1850
|
+
}
|
|
1851
|
+
await this.execFly(args);
|
|
1852
|
+
}
|
|
1853
|
+
async fetchAppIps(onError, options) {
|
|
1854
|
+
const args = ["ips", "list"];
|
|
1855
|
+
args.push(...this.getAppOrConfigArgs(options));
|
|
1856
|
+
args.push("--json");
|
|
1857
|
+
try {
|
|
1858
|
+
return await IpsListTransformedResponseSchema.parseAsync(
|
|
1859
|
+
await this.execFly(args)
|
|
1860
|
+
);
|
|
1861
|
+
} catch (error) {
|
|
1862
|
+
if (onError === "throwOnError") {
|
|
1863
|
+
throw error;
|
|
1864
|
+
}
|
|
1865
|
+
return null;
|
|
1866
|
+
}
|
|
1867
|
+
}
|
|
1750
1868
|
/**
|
|
1751
1869
|
* @private
|
|
1752
1870
|
* @returns The name of the application
|
|
@@ -1799,13 +1917,47 @@ ${error}`);
|
|
|
1799
1917
|
}
|
|
1800
1918
|
/**
|
|
1801
1919
|
* @private
|
|
1802
|
-
* Restart a machine by stopping then starting it
|
|
1803
|
-
*
|
|
1920
|
+
* Restart a machine by stopping then starting it, waiting on the state Fly
|
|
1921
|
+
* reports rather than a fixed delay: the start is only issued once the
|
|
1922
|
+
* machine is stopped (or suspended), and the restart is done once it is
|
|
1923
|
+
* started again.
|
|
1924
|
+
* @throws An error saying the machine was left stopped when anything after
|
|
1925
|
+
* the stop fails
|
|
1804
1926
|
*/
|
|
1805
1927
|
async restartMachine(app, machineId) {
|
|
1806
1928
|
await this.stopMachine(app, machineId);
|
|
1807
|
-
|
|
1808
|
-
|
|
1929
|
+
try {
|
|
1930
|
+
await this.waitForMachineState(app, machineId, ["stopped", "suspended"]);
|
|
1931
|
+
await this.startMachine(app, machineId);
|
|
1932
|
+
await this.waitForMachineState(app, machineId, ["started"]);
|
|
1933
|
+
} catch (error) {
|
|
1934
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
1935
|
+
throw new Error(
|
|
1936
|
+
`Machine '${machineId}' in app '${app}' was stopped but did not start again: ${reason}`,
|
|
1937
|
+
{ cause: error }
|
|
1938
|
+
);
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
/**
|
|
1942
|
+
* @private
|
|
1943
|
+
* Poll the app status until the machine reports one of the given states
|
|
1944
|
+
* @throws An error if the machine does not reach a state in time
|
|
1945
|
+
*/
|
|
1946
|
+
async waitForMachineState(app, machineId, states) {
|
|
1947
|
+
const deadline = Date.now() + MACHINE_STATE_TIMEOUT_MS;
|
|
1948
|
+
while (Date.now() < deadline) {
|
|
1949
|
+
const status = await this.fetchAppStatus("nullOnError", { app });
|
|
1950
|
+
const machine = status?.machines.find(({ id }) => id === machineId);
|
|
1951
|
+
if (machine && states.includes(machine.state)) {
|
|
1952
|
+
return;
|
|
1953
|
+
}
|
|
1954
|
+
await new Promise(
|
|
1955
|
+
(resolve) => setTimeout(resolve, MACHINE_STATE_INTERVAL_MS)
|
|
1956
|
+
);
|
|
1957
|
+
}
|
|
1958
|
+
throw new Error(
|
|
1959
|
+
`Machine '${machineId}' in app '${app}' did not reach ${states.join("/")} in time`
|
|
1960
|
+
);
|
|
1809
1961
|
}
|
|
1810
1962
|
/**
|
|
1811
1963
|
* @private
|
|
@@ -2012,8 +2164,7 @@ ${JSON.stringify(response, null, 2)}`);
|
|
|
2012
2164
|
* @throws An error if Fly CLI is not installed or not authenticated
|
|
2013
2165
|
*/
|
|
2014
2166
|
async ensureInitialized() {
|
|
2015
|
-
if (this.initialized)
|
|
2016
|
-
return;
|
|
2167
|
+
if (this.initialized) return;
|
|
2017
2168
|
try {
|
|
2018
2169
|
if (!await this.isInstalled()) {
|
|
2019
2170
|
throw new Error("Fly CLI must be installed to use this library");
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export * from './api';
|
|
2
2
|
export * from './lib/fly.class';
|
|
3
|
-
export type { BuildResponse, Config, DeployAppOptions, DeployResponse, MachineOperationOptions, SaveConfigOptions, StatusResponse } from './lib/types';
|
|
3
|
+
export type { AllocateIpOptions, BuildResponse, Config, DeployAppOptions, DeployResponse, ListIpResponse, MachineOperationOptions, SaveConfigOptions, StatusResponse } from './lib/types';
|
package/src/lib/fly.class.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SpawnOptions, SpawnPtyOptions } from '@codeware/shared/util/misc';
|
|
2
|
-
import type { AppOrConfig, BuildResponse, Config, CreateAppOptions, CreateAppResponse, DeployAppOptions, DeployResponse, ListAppResponse, ListCertForAllResponse, ListCertForAppResponse, ListSecretForAllResponse, ListSecretForAppResponse, SaveConfigOptions, SetAppSecretsOptions, ShowConfigOptions, ShowConfigResponse, StatusExtendedResponse, StatusResponse, UnsetAppSecretsOptions, VersionResponse } from './types';
|
|
2
|
+
import type { AllocateIpOptions, AppOrConfig, BuildResponse, Config, CreateAppOptions, CreateAppResponse, DeployAppOptions, DeployResponse, ListAppResponse, ListCertForAllResponse, ListCertForAppResponse, ListIpResponse, ListSecretForAllResponse, ListSecretForAppResponse, SaveConfigOptions, SetAppSecretsOptions, ShowConfigOptions, ShowConfigResponse, StatusExtendedResponse, StatusResponse, UnsetAppSecretsOptions, VersionResponse } from './types';
|
|
3
3
|
export type ExecFlyOptions = SpawnOptions & SpawnPtyOptions & {
|
|
4
4
|
/**
|
|
5
5
|
* How to assemble the returned output string from the spawned process.
|
|
@@ -159,6 +159,33 @@ export declare class Fly {
|
|
|
159
159
|
deploy: <T extends DeployAppOptions>(options?: T) => Promise<T extends {
|
|
160
160
|
buildOnly: true;
|
|
161
161
|
} ? BuildResponse : DeployResponse>;
|
|
162
|
+
/**
|
|
163
|
+
* Manage public IP addresses
|
|
164
|
+
*/
|
|
165
|
+
ips: {
|
|
166
|
+
/**
|
|
167
|
+
* List the public IP addresses allocated to an application
|
|
168
|
+
*
|
|
169
|
+
* @param options - The application or configuration to list addresses for
|
|
170
|
+
* @returns The allocated addresses, empty when the app has none
|
|
171
|
+
* @throws An error if listing addresses fails
|
|
172
|
+
*/
|
|
173
|
+
list: (options?: AppOrConfig) => Promise<ListIpResponse>;
|
|
174
|
+
/**
|
|
175
|
+
* Give an application the public addresses it needs to be reachable.
|
|
176
|
+
*
|
|
177
|
+
* Idempotent by inspection: it allocates only what is missing, so it is
|
|
178
|
+
* safe on every deploy rather than only on create. That matters because
|
|
179
|
+
* `flyctl` allocates addresses only for an app it creates itself — an app
|
|
180
|
+
* that arrives any other way, such as a registry push auto-creating it,
|
|
181
|
+
* deploys green and answers nothing.
|
|
182
|
+
*
|
|
183
|
+
* @param options - The application, and which addresses to ensure
|
|
184
|
+
* @returns The addresses the application holds afterwards
|
|
185
|
+
* @throws An error if an address cannot be allocated
|
|
186
|
+
*/
|
|
187
|
+
ensure: (options?: AllocateIpOptions) => Promise<ListIpResponse>;
|
|
188
|
+
};
|
|
162
189
|
/**
|
|
163
190
|
* Manage machines
|
|
164
191
|
*/
|
|
@@ -180,7 +207,11 @@ export declare class Fly {
|
|
|
180
207
|
*/
|
|
181
208
|
stop: (app: string, machineId: string) => Promise<void>;
|
|
182
209
|
/**
|
|
183
|
-
* Restart a machine by stopping then starting it
|
|
210
|
+
* Restart a machine by stopping then starting it.
|
|
211
|
+
*
|
|
212
|
+
* Waits until Fly reports the machine stopped (or suspended) before
|
|
213
|
+
* starting it, then until it is started. If the start fails after the
|
|
214
|
+
* stop, the error says the machine was left stopped.
|
|
184
215
|
*
|
|
185
216
|
* @param app - The name of the application
|
|
186
217
|
* @param machineId - The machine ID to restart
|
|
@@ -342,6 +373,26 @@ export declare class Fly {
|
|
|
342
373
|
* @throws An error if the domain cannot be added
|
|
343
374
|
*/
|
|
344
375
|
private addAppCertificate;
|
|
376
|
+
/**
|
|
377
|
+
* Allocate one public address.
|
|
378
|
+
*
|
|
379
|
+
* A dedicated v4 is billed, so the v4 path is always `--shared`.
|
|
380
|
+
*
|
|
381
|
+
* The two commands do not take the same flags: `allocate-v4` has `--yes`
|
|
382
|
+
* because a dedicated v4 costs money and prompts, while `allocate-v6` is
|
|
383
|
+
* free, never prompts, and rejects the flag outright with `unknown flag`.
|
|
384
|
+
* The asymmetry is flyctl's, not ours — do not tidy it away.
|
|
385
|
+
*
|
|
386
|
+
* @private
|
|
387
|
+
* @throws An error if the address cannot be allocated
|
|
388
|
+
*/
|
|
389
|
+
private allocateIp;
|
|
390
|
+
/**
|
|
391
|
+
* @private
|
|
392
|
+
* @returns The addresses for an app or `null` if they cannot be retrieved and `nullOnError` is used
|
|
393
|
+
* @throws An error if the addresses cannot be retrieved and `throwOnError` is used
|
|
394
|
+
*/
|
|
395
|
+
private fetchAppIps;
|
|
345
396
|
/**
|
|
346
397
|
* @private
|
|
347
398
|
* @returns The name of the application
|
|
@@ -375,10 +426,20 @@ export declare class Fly {
|
|
|
375
426
|
private stopMachine;
|
|
376
427
|
/**
|
|
377
428
|
* @private
|
|
378
|
-
* Restart a machine by stopping then starting it
|
|
379
|
-
*
|
|
429
|
+
* Restart a machine by stopping then starting it, waiting on the state Fly
|
|
430
|
+
* reports rather than a fixed delay: the start is only issued once the
|
|
431
|
+
* machine is stopped (or suspended), and the restart is done once it is
|
|
432
|
+
* started again.
|
|
433
|
+
* @throws An error saying the machine was left stopped when anything after
|
|
434
|
+
* the stop fails
|
|
380
435
|
*/
|
|
381
436
|
private restartMachine;
|
|
437
|
+
/**
|
|
438
|
+
* @private
|
|
439
|
+
* Poll the app status until the machine reports one of the given states
|
|
440
|
+
* @throws An error if the machine does not reach a state in time
|
|
441
|
+
*/
|
|
442
|
+
private waitForMachineState;
|
|
382
443
|
/**
|
|
383
444
|
* @private
|
|
384
445
|
* Update a machine's per-machine config (env vars, etc.).
|
package/src/lib/mocks.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CreateAppResponse, ListAppResponse, ListCertForAllResponse, ListCertForAppResponse, ListPostgresResponse, ListPostgresUsersResponse, ListSecretForAllResponse, ListSecretForAppResponse, StatusResponse } from './types';
|
|
1
|
+
import type { CreateAppResponse, ListAppResponse, ListCertForAllResponse, ListCertForAppResponse, ListIpResponse, ListPostgresResponse, ListPostgresUsersResponse, ListSecretForAllResponse, ListSecretForAppResponse, StatusResponse } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* Mock definitions for the tests which are used in the mock rules and data.
|
|
4
4
|
*/
|
|
@@ -106,3 +106,10 @@ export declare const mockVersionResponse: {
|
|
|
106
106
|
Architecture: string;
|
|
107
107
|
Environment: string;
|
|
108
108
|
};
|
|
109
|
+
/**
|
|
110
|
+
* A production-shaped address pair: free shared v4 plus dedicated v6.
|
|
111
|
+
*
|
|
112
|
+
* The shared v4 carries an empty region and a zero timestamp, which is what
|
|
113
|
+
* Fly actually answers and the reason neither is parsed as a datetime.
|
|
114
|
+
*/
|
|
115
|
+
export declare const mockListIpResponse: ListIpResponse;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* The kinds of public address an app can hold.
|
|
4
|
+
*
|
|
5
|
+
* `shared_v4` is the free one every app with an `http_service` needs; a
|
|
6
|
+
* dedicated `v4` is billed. `v6` is dedicated and free.
|
|
7
|
+
*
|
|
8
|
+
* Kept open with a string union so an address type added upstream reads as
|
|
9
|
+
* itself rather than failing the whole list.
|
|
10
|
+
*/
|
|
11
|
+
export declare const IpTypeSchema: z.ZodUnion<[z.ZodEnum<["v4", "v6", "shared_v4", "private_v6"]>, z.ZodString]>;
|
|
12
|
+
/**
|
|
13
|
+
* Fly ips list response element schema
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* fly ips list --app [name] --json
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Fly answers in PascalCase (`ID`, `Address`, …), which `withCamelCase`
|
|
20
|
+
* rewrites before validation — so the keys here are the transformed ones.
|
|
21
|
+
*
|
|
22
|
+
* A shared v4 carries an empty `region` and a zero `createdAt`, so neither can
|
|
23
|
+
* be treated as a datetime.
|
|
24
|
+
*/
|
|
25
|
+
export declare const IpsListFlyResponseElementSchema: z.ZodObject<{
|
|
26
|
+
id: z.ZodString;
|
|
27
|
+
address: z.ZodString;
|
|
28
|
+
type: z.ZodUnion<[z.ZodEnum<["v4", "v6", "shared_v4", "private_v6"]>, z.ZodString]>;
|
|
29
|
+
region: z.ZodString;
|
|
30
|
+
createdAt: z.ZodString;
|
|
31
|
+
}, "strip", z.ZodTypeAny, {
|
|
32
|
+
id: string;
|
|
33
|
+
createdAt: string;
|
|
34
|
+
type: string;
|
|
35
|
+
region: string;
|
|
36
|
+
address: string;
|
|
37
|
+
}, {
|
|
38
|
+
id: string;
|
|
39
|
+
createdAt: string;
|
|
40
|
+
type: string;
|
|
41
|
+
region: string;
|
|
42
|
+
address: string;
|
|
43
|
+
}>;
|
|
44
|
+
/**
|
|
45
|
+
* Transformed ips list response schema
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* fly ips list --app [name] --json
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare const IpsListTransformedResponseSchema: z.ZodArray<z.ZodEffects<z.ZodObject<{
|
|
52
|
+
id: z.ZodString;
|
|
53
|
+
address: z.ZodString;
|
|
54
|
+
type: z.ZodUnion<[z.ZodEnum<["v4", "v6", "shared_v4", "private_v6"]>, z.ZodString]>;
|
|
55
|
+
region: z.ZodString;
|
|
56
|
+
createdAt: z.ZodString;
|
|
57
|
+
}, "strip", z.ZodTypeAny, {
|
|
58
|
+
id: string;
|
|
59
|
+
createdAt: string;
|
|
60
|
+
type: string;
|
|
61
|
+
region: string;
|
|
62
|
+
address: string;
|
|
63
|
+
}, {
|
|
64
|
+
id: string;
|
|
65
|
+
createdAt: string;
|
|
66
|
+
type: string;
|
|
67
|
+
region: string;
|
|
68
|
+
address: string;
|
|
69
|
+
}>, {
|
|
70
|
+
id: string;
|
|
71
|
+
createdAt: string;
|
|
72
|
+
type: string;
|
|
73
|
+
region: string;
|
|
74
|
+
address: string;
|
|
75
|
+
}, unknown>, "many">;
|
package/src/lib/types.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { CertsListWithAppTransformedResponseSchema } from './schemas/certs-list-
|
|
|
6
6
|
import { CertsListTransformedResponseSchema } from './schemas/certs-list.schema';
|
|
7
7
|
import { ConfigShowResponseSchema } from './schemas/config-show.schema';
|
|
8
8
|
import { DeployResponseSchema } from './schemas/deploy.schema';
|
|
9
|
+
import { IpsListTransformedResponseSchema } from './schemas/ips-list.schema';
|
|
9
10
|
import { PostgresListTransformedResponseSchema } from './schemas/postgres-list';
|
|
10
11
|
import { PostgresUsersListTransformedResponseSchema } from './schemas/postgres-users-list';
|
|
11
12
|
import { SecretsListWithAppTransformedResponseSchema } from './schemas/secrets-list-with-app.schema';
|
|
@@ -22,6 +23,20 @@ export type BuildResponse = z.infer<typeof BuildResponseSchema>;
|
|
|
22
23
|
export type DeployResponse = z.infer<typeof DeployResponseSchema>;
|
|
23
24
|
/** List certificates for an application */
|
|
24
25
|
export type ListCertForAppResponse = z.infer<typeof CertsListTransformedResponseSchema>;
|
|
26
|
+
/** List public IP addresses for an application */
|
|
27
|
+
export type ListIpResponse = z.infer<typeof IpsListTransformedResponseSchema>;
|
|
28
|
+
/**
|
|
29
|
+
* Which public addresses to allocate.
|
|
30
|
+
*
|
|
31
|
+
* Both defaults are free. A dedicated v4 is billed, which is why it is not
|
|
32
|
+
* offered here — `sharedV4` is what an app with an `http_service` needs.
|
|
33
|
+
*/
|
|
34
|
+
export type AllocateIpOptions = AppOrConfig & {
|
|
35
|
+
/** Allocate a free shared IPv4 @default true */
|
|
36
|
+
sharedV4?: boolean;
|
|
37
|
+
/** Allocate a free dedicated IPv6 @default true */
|
|
38
|
+
v6?: boolean;
|
|
39
|
+
};
|
|
25
40
|
/** List certificates for all applications */
|
|
26
41
|
export type ListCertForAllResponse = z.infer<typeof CertsListWithAppTransformedResponseSchema>;
|
|
27
42
|
/** List secrets for an application */
|