@execution-associates/isb-alchemy 0.0.0-stage → 4.3.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/LICENSE +21 -0
- package/README.md +116 -2
- package/dist/App.d.ts +118 -0
- package/dist/App.js +100 -0
- package/dist/Client.d.ts +57 -0
- package/dist/Client.js +88 -0
- package/dist/Project.d.ts +34 -0
- package/dist/Project.js +55 -0
- package/dist/Providers.d.ts +21 -0
- package/dist/Providers.js +23 -0
- package/dist/Replace.d.ts +15 -0
- package/dist/Replace.js +14 -0
- package/dist/Secret.d.ts +39 -0
- package/dist/Secret.js +48 -0
- package/dist/Stack.d.ts +52 -0
- package/dist/Stack.js +75 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/package.json +48 -4
- package/src/App.ts +210 -0
- package/src/Client.ts +139 -0
- package/src/Project.ts +87 -0
- package/src/Providers.ts +30 -0
- package/src/Replace.ts +18 -0
- package/src/Secret.ts +73 -0
- package/src/Stack.ts +124 -0
- package/src/index.ts +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Execution Associates
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,117 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @execution-associates/isb-alchemy
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An [Alchemy](https://alchemy.run) provider for [isb](https://github.com/execution-associates/isb):
|
|
4
|
+
declare the apps, stacks and secrets an isb host runs in TypeScript, and
|
|
5
|
+
`alchemy deploy` makes the host match.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun add @execution-associates/isb-alchemy alchemy effect
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
It needs alchemy 2 (the Effect-based releases, `2.0.0-beta.87` or later), an
|
|
12
|
+
isb server at 4.3 or later and an API token (`isb token create`, or the web UI) of a member
|
|
13
|
+
of the org.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// alchemy.run.ts
|
|
17
|
+
import * as Isb from "@execution-associates/isb-alchemy";
|
|
18
|
+
import * as Alchemy from "alchemy";
|
|
19
|
+
import * as Effect from "effect/Effect";
|
|
20
|
+
import * as Redacted from "effect/Redacted";
|
|
21
|
+
|
|
22
|
+
export default Alchemy.Stack(
|
|
23
|
+
"shop",
|
|
24
|
+
{ providers: Isb.providers(), state: Alchemy.localState() },
|
|
25
|
+
Effect.gen(function* () {
|
|
26
|
+
const shop = yield* Isb.Project("shop", { name: "shop" });
|
|
27
|
+
const dbUrl = yield* Isb.Secret("db-url", {
|
|
28
|
+
name: "shop-db-url",
|
|
29
|
+
value: Redacted.make(process.env.DATABASE_URL!),
|
|
30
|
+
});
|
|
31
|
+
const web = yield* Isb.App("web", {
|
|
32
|
+
name: "web",
|
|
33
|
+
project: shop.name,
|
|
34
|
+
source: { image: "ghcr:acme/shop:1.4.0" },
|
|
35
|
+
port: 8080,
|
|
36
|
+
replicas: 2,
|
|
37
|
+
domains: [{ host: "shop.example.com" }],
|
|
38
|
+
env: { LOG_LEVEL: "info", DATABASE_URL: dbUrl.ref },
|
|
39
|
+
});
|
|
40
|
+
return { urls: web.urls, service: web.serviceName };
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
export ISB_URL=https://isb.example.com ISB_TOKEN=isb_tok_... ISB_ORG=acme
|
|
47
|
+
bun alchemy deploy # plan, ask, apply; --yes in CI
|
|
48
|
+
bun alchemy destroy
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`Isb.providers()` reads the connection from `ISB_URL`, `ISB_TOKEN` and
|
|
52
|
+
`ISB_ORG` (default `default`), the variables the isb CLI reads. Pass it in
|
|
53
|
+
code instead with `Isb.providers({ url, token, org })`.
|
|
54
|
+
|
|
55
|
+
## Resources
|
|
56
|
+
|
|
57
|
+
| Resource | What it is | Destroyed |
|
|
58
|
+
|---|---|---|
|
|
59
|
+
| `Isb.Project` | A project and its environments. | Deleted once it holds no apps and no stacks. An environment dropped from `environments` stays. |
|
|
60
|
+
| `Isb.Secret` | A value in the org's secret store. `ref` is what an app's `env` takes. | Deleted; refused by isb while a deployed stack still uses it. |
|
|
61
|
+
| `Isb.App` | An app: an image, or a git repository isb builds, as a health-checked service with rolling updates, domains, volumes and secrets. | Its service is removed. Named volumes are kept. |
|
|
62
|
+
| `Isb.Stack` | A compose file run as an isb stack. | Removed, with its named volumes only when `removeVolumes: true`. |
|
|
63
|
+
|
|
64
|
+
`Isb.App` takes an app definition as `isb app export` prints it, as an
|
|
65
|
+
object: `name`, `project`, `environment`, `source`, `env`, `domains`,
|
|
66
|
+
`volumes`, `ports`, `replicas`, `port`, `healthcheck`, `resources`,
|
|
67
|
+
`command`, and any other field of a definition.
|
|
68
|
+
|
|
69
|
+
## What a deploy does
|
|
70
|
+
|
|
71
|
+
- **`Isb.App`** applies the whole definition, deploys it and waits for the
|
|
72
|
+
service to converge. A failed deployment fails the deploy, with isb's
|
|
73
|
+
reason and the end of the deployment log.
|
|
74
|
+
- **The definition is the whole desired state.** A domain, variable, volume
|
|
75
|
+
or port left out of it is removed from the app.
|
|
76
|
+
- **Deploying the same thing again restarts nothing.** isb compares the
|
|
77
|
+
rendered settings and the image's digest with what runs, and leaves the
|
|
78
|
+
instances alone when both match. A tag that moved on its registry
|
|
79
|
+
(`docker:nginx:latest`) is a change, and rolls out. `redeploy: "always"`
|
|
80
|
+
on an app replaces its instances on every deploy instead. This needs
|
|
81
|
+
isb 4.3 or later; an older one refuses the call.
|
|
82
|
+
- **A changed secret value** is a new version in isb, and the services
|
|
83
|
+
that use the secret roll. The value is compared by a hash kept in
|
|
84
|
+
alchemy's state; the same value writes nothing.
|
|
85
|
+
- **`name`, `project` and `environment` cannot change** in isb, so
|
|
86
|
+
changing one replaces the resource: the old app is deleted, then the new
|
|
87
|
+
one is created.
|
|
88
|
+
|
|
89
|
+
## Secrets and state
|
|
90
|
+
|
|
91
|
+
An `Isb.Secret`'s value is a prop, so alchemy keeps it in its state store.
|
|
92
|
+
Use a state store that encrypts at rest, or keep the value out of alchemy:
|
|
93
|
+
create the secret in isb yourself (`isb secret set`) and name it in the
|
|
94
|
+
app's `env` as `{ secret: "NAME" }`.
|
|
95
|
+
|
|
96
|
+
## Stages
|
|
97
|
+
|
|
98
|
+
Alchemy's stage (`--stage`, default `live_$USER`) names a copy of the
|
|
99
|
+
stack. isb's names are yours to choose, so put the stage where you want
|
|
100
|
+
copies to differ: as the project's environment, and in the names of apps
|
|
101
|
+
and secrets, which are unique within an org.
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
const stage = yield* Alchemy.Stage;
|
|
105
|
+
const shop = yield* Isb.Project("shop", { name: "shop", environments: [stage] });
|
|
106
|
+
yield* Isb.App("web", { name: `web-${stage}`, project: shop.name, environment: stage, ... });
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Development
|
|
110
|
+
|
|
111
|
+
```sh
|
|
112
|
+
bun install
|
|
113
|
+
bun run typecheck && bun run lint && bun test
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
The tests run the provider through alchemy's own engine against an
|
|
117
|
+
in-memory stand-in for isb's REST tools (`test/fake.ts`).
|
package/dist/App.d.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import * as Provider from "alchemy/Provider";
|
|
2
|
+
import { Resource } from "alchemy/Resource";
|
|
3
|
+
/** Where an app's image comes from. */
|
|
4
|
+
export type AppSource = {
|
|
5
|
+
image: string;
|
|
6
|
+
} | {
|
|
7
|
+
git: {
|
|
8
|
+
url: string;
|
|
9
|
+
ref?: string;
|
|
10
|
+
subdir?: string;
|
|
11
|
+
auth?: {
|
|
12
|
+
token_secret?: string;
|
|
13
|
+
ssh_key_secret?: string;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
} | {
|
|
17
|
+
database: Record<string, unknown>;
|
|
18
|
+
};
|
|
19
|
+
/** A plain value, or a secret in the org's store (`Isb.Secret`'s `ref`). */
|
|
20
|
+
export type EnvValue = string | {
|
|
21
|
+
secret: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* An app as isb's `app_apply` takes it (the YAML of `isb app export`, as
|
|
25
|
+
* an object). The fields listed are the common ones; any other field of
|
|
26
|
+
* an app definition passes through as written.
|
|
27
|
+
*/
|
|
28
|
+
export interface AppProps {
|
|
29
|
+
/** The app's name: its service, and how other apps reach it. */
|
|
30
|
+
name: string;
|
|
31
|
+
project: string;
|
|
32
|
+
/** @default "production" */
|
|
33
|
+
environment?: string;
|
|
34
|
+
source: AppSource;
|
|
35
|
+
build?: {
|
|
36
|
+
builder: Record<string, unknown>;
|
|
37
|
+
args?: Record<string, string>;
|
|
38
|
+
};
|
|
39
|
+
env?: Record<string, EnvValue>;
|
|
40
|
+
domains?: ({
|
|
41
|
+
host: string;
|
|
42
|
+
path?: string;
|
|
43
|
+
port?: number;
|
|
44
|
+
} & Record<string, unknown>)[];
|
|
45
|
+
/** `NAME:/path` named volumes. */
|
|
46
|
+
volumes?: string[];
|
|
47
|
+
/** `[IP:]PUBLISHED:TARGET` host ports. */
|
|
48
|
+
ports?: string[];
|
|
49
|
+
replicas?: number;
|
|
50
|
+
/** The port the app listens on, for domains and the health check. */
|
|
51
|
+
port?: number;
|
|
52
|
+
healthcheck?: Record<string, unknown>;
|
|
53
|
+
resources?: {
|
|
54
|
+
cpus?: string;
|
|
55
|
+
memory?: string;
|
|
56
|
+
};
|
|
57
|
+
command?: string | string[];
|
|
58
|
+
/**
|
|
59
|
+
* What a deploy does to instances that already run exactly this:
|
|
60
|
+
* `"if-changed"` leaves them running (the settings and the image digest
|
|
61
|
+
* are the same), `"always"` replaces them on every deploy.
|
|
62
|
+
* @default "if-changed"
|
|
63
|
+
*/
|
|
64
|
+
redeploy?: "if-changed" | "always";
|
|
65
|
+
/**
|
|
66
|
+
* How long a deployment may take before the deploy fails, in seconds.
|
|
67
|
+
* @default 900
|
|
68
|
+
*/
|
|
69
|
+
timeoutSeconds?: number;
|
|
70
|
+
[field: string]: unknown;
|
|
71
|
+
}
|
|
72
|
+
export interface AppAttributes {
|
|
73
|
+
name: string;
|
|
74
|
+
project: string;
|
|
75
|
+
environment: string;
|
|
76
|
+
/** The stack the app's service runs in: `<project>-<environment>`. */
|
|
77
|
+
stack: string;
|
|
78
|
+
/** The name other services in the org reach it by. */
|
|
79
|
+
serviceName: string;
|
|
80
|
+
/** The deployment that last ran, and what it deployed. */
|
|
81
|
+
deployment: number;
|
|
82
|
+
image: string | undefined;
|
|
83
|
+
digest: string | undefined;
|
|
84
|
+
/** The last deployment changed nothing, and the instances were left running. */
|
|
85
|
+
unchanged: boolean;
|
|
86
|
+
/** `https://<host><path>` for each of the app's domains. */
|
|
87
|
+
urls: string[];
|
|
88
|
+
}
|
|
89
|
+
export type App = Resource<"Isb.App", AppProps, AppAttributes>;
|
|
90
|
+
/**
|
|
91
|
+
* An app on isb: an image (or a git repository isb builds), run as a
|
|
92
|
+
* health-checked service with rolling updates, domains, volumes and
|
|
93
|
+
* secrets.
|
|
94
|
+
*
|
|
95
|
+
* Each deploy applies the whole definition (`app_apply`), deploys it and
|
|
96
|
+
* waits for the service to converge. A deploy that changes nothing leaves
|
|
97
|
+
* the running instances alone: isb compares the settings and the image's
|
|
98
|
+
* digest, so a tag that moved on its registry still rolls out.
|
|
99
|
+
*
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const web = yield* Isb.App("web", {
|
|
102
|
+
* name: "web",
|
|
103
|
+
* project: shop.name,
|
|
104
|
+
* source: { image: "ghcr:acme/shop:1.4.0" },
|
|
105
|
+
* port: 8080,
|
|
106
|
+
* replicas: 2,
|
|
107
|
+
* domains: [{ host: "shop.example.com" }],
|
|
108
|
+
* env: { LOG_LEVEL: "info", DATABASE_URL: dbUrl.ref },
|
|
109
|
+
* });
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* The definition is the app's whole desired state: a domain, variable,
|
|
113
|
+
* volume or port left out of it is removed from the app. An app's `name`,
|
|
114
|
+
* `project` and `environment` cannot change, so changing one replaces the
|
|
115
|
+
* app. Destroying it keeps its named volumes.
|
|
116
|
+
*/
|
|
117
|
+
export declare const App: import("alchemy/Resource").ResourceClass<App>;
|
|
118
|
+
export declare const AppProvider: () => import("effect/Layer").Layer<Provider.Provider<App>, never, import("./Client.ts").Credentials>;
|
package/dist/App.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import * as Provider from "alchemy/Provider";
|
|
2
|
+
import { Resource } from "alchemy/Resource";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import { call, callFound, IsbError } from "./Client.js";
|
|
5
|
+
import { replaceWhen } from "./Replace.js";
|
|
6
|
+
/** Follow a deployment's log until it finishes; its record and the log. */
|
|
7
|
+
const follow = (name, id, timeoutSeconds) => Effect.gen(function* () {
|
|
8
|
+
let offset = 0;
|
|
9
|
+
let log = "";
|
|
10
|
+
const deadline = Date.now() + timeoutSeconds * 1000;
|
|
11
|
+
while (true) {
|
|
12
|
+
const page = yield* call("app_deployment_log", { name, deployment: id, offset });
|
|
13
|
+
log += page.log;
|
|
14
|
+
offset = page.offset;
|
|
15
|
+
if (page.finished)
|
|
16
|
+
return { record: page.deployment, log };
|
|
17
|
+
if (Date.now() > deadline) {
|
|
18
|
+
return yield* Effect.fail(new IsbError({
|
|
19
|
+
tool: "app_deployment_log",
|
|
20
|
+
code: "timeout",
|
|
21
|
+
status: 0,
|
|
22
|
+
message: `isb: deployment ${id} of app ${name} did not finish within ${timeoutSeconds}s`,
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
yield* Effect.sleep("1 second");
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
/** The last lines of a deployment log, for an error message. */
|
|
29
|
+
const tail = (log, lines = 15) => log.trimEnd().split("\n").slice(-lines).join("\n");
|
|
30
|
+
/**
|
|
31
|
+
* An app on isb: an image (or a git repository isb builds), run as a
|
|
32
|
+
* health-checked service with rolling updates, domains, volumes and
|
|
33
|
+
* secrets.
|
|
34
|
+
*
|
|
35
|
+
* Each deploy applies the whole definition (`app_apply`), deploys it and
|
|
36
|
+
* waits for the service to converge. A deploy that changes nothing leaves
|
|
37
|
+
* the running instances alone: isb compares the settings and the image's
|
|
38
|
+
* digest, so a tag that moved on its registry still rolls out.
|
|
39
|
+
*
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const web = yield* Isb.App("web", {
|
|
42
|
+
* name: "web",
|
|
43
|
+
* project: shop.name,
|
|
44
|
+
* source: { image: "ghcr:acme/shop:1.4.0" },
|
|
45
|
+
* port: 8080,
|
|
46
|
+
* replicas: 2,
|
|
47
|
+
* domains: [{ host: "shop.example.com" }],
|
|
48
|
+
* env: { LOG_LEVEL: "info", DATABASE_URL: dbUrl.ref },
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* The definition is the app's whole desired state: a domain, variable,
|
|
53
|
+
* volume or port left out of it is removed from the app. An app's `name`,
|
|
54
|
+
* `project` and `environment` cannot change, so changing one replaces the
|
|
55
|
+
* app. Destroying it keeps its named volumes.
|
|
56
|
+
*/
|
|
57
|
+
export const App = Resource("Isb.App");
|
|
58
|
+
export const AppProvider = () => Provider.succeed(App, {
|
|
59
|
+
stables: ["name", "project", "environment", "stack", "serviceName"],
|
|
60
|
+
// An app's name, project and environment cannot change. The old app
|
|
61
|
+
// goes first: the replacement takes its service name.
|
|
62
|
+
diff: replaceWhen((p) => [p.name, p.project, p.environment ?? "production"], {
|
|
63
|
+
deleteFirst: true,
|
|
64
|
+
}),
|
|
65
|
+
reconcile: Effect.fn(function* ({ news }) {
|
|
66
|
+
const { redeploy = "if-changed", timeoutSeconds = 900, ...definition } = news;
|
|
67
|
+
const applied = yield* call("app_apply", {
|
|
68
|
+
definition,
|
|
69
|
+
deploy: true,
|
|
70
|
+
if_changed: redeploy === "if-changed",
|
|
71
|
+
// The definition here is the source of truth, removals included.
|
|
72
|
+
allow_removals: true,
|
|
73
|
+
});
|
|
74
|
+
const { record, log } = yield* follow(news.name, applied.deployment.id, timeoutSeconds);
|
|
75
|
+
if (record.status !== "done") {
|
|
76
|
+
return yield* Effect.fail(new IsbError({
|
|
77
|
+
tool: "app_deploy",
|
|
78
|
+
code: "deploy_failed",
|
|
79
|
+
status: 0,
|
|
80
|
+
message: `isb: deployment ${record.id} of app ${news.name} ${record.status}: ${record.error ?? "no reason given"}\n${tail(log)}`,
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
const app = yield* call("app_get", { name: news.name });
|
|
84
|
+
return {
|
|
85
|
+
name: app.name,
|
|
86
|
+
project: app.project,
|
|
87
|
+
environment: app.environment,
|
|
88
|
+
stack: app.stack,
|
|
89
|
+
serviceName: app.service_name,
|
|
90
|
+
deployment: record.id,
|
|
91
|
+
image: record.image,
|
|
92
|
+
digest: record.digest,
|
|
93
|
+
unchanged: record.unchanged ?? false,
|
|
94
|
+
urls: (app.domains ?? []).map((d) => `https://${d.host}${d.path ?? ""}`),
|
|
95
|
+
};
|
|
96
|
+
}),
|
|
97
|
+
delete: Effect.fn(function* ({ output }) {
|
|
98
|
+
yield* callFound("app_delete", { name: output.name });
|
|
99
|
+
}),
|
|
100
|
+
});
|
package/dist/Client.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The connection to an isb host: where it is, the org to act in and an API
|
|
3
|
+
* token, plus `call`, which runs one isb tool over its REST API
|
|
4
|
+
* (`POST /orgs/<org>/api/v1/tools/<tool>`).
|
|
5
|
+
*/
|
|
6
|
+
import * as Context from "effect/Context";
|
|
7
|
+
import * as Effect from "effect/Effect";
|
|
8
|
+
import * as Layer from "effect/Layer";
|
|
9
|
+
import * as Redacted from "effect/Redacted";
|
|
10
|
+
export interface IsbConfig {
|
|
11
|
+
/** The isb server, e.g. `https://isb.example.com`. */
|
|
12
|
+
readonly url: string;
|
|
13
|
+
/** The org every resource lives in. */
|
|
14
|
+
readonly org: string;
|
|
15
|
+
/** An API token (`isb token create`), a member of `org` or above. */
|
|
16
|
+
readonly token: Redacted.Redacted<string>;
|
|
17
|
+
}
|
|
18
|
+
declare const IsbError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
19
|
+
readonly _tag: "IsbError";
|
|
20
|
+
} & Readonly<A>;
|
|
21
|
+
/** A tool call isb refused or that never reached it. */
|
|
22
|
+
export declare class IsbError extends IsbError_base<{
|
|
23
|
+
/** The tool that was called. */
|
|
24
|
+
readonly tool: string;
|
|
25
|
+
/** isb's error code (`invalid`, `not_found`, `forbidden`, ...), or `unreachable`. */
|
|
26
|
+
readonly code: string;
|
|
27
|
+
/** The HTTP status, 0 when there was no answer. */
|
|
28
|
+
readonly status: number;
|
|
29
|
+
readonly message: string;
|
|
30
|
+
readonly data?: unknown;
|
|
31
|
+
}> {
|
|
32
|
+
}
|
|
33
|
+
declare const Credentials_base: Context.ServiceClass<Credentials, "IsbCredentials", Effect.Effect<IsbConfig, IsbError, never>>;
|
|
34
|
+
/**
|
|
35
|
+
* The connection, resolved when a resource first needs it, so a stack that
|
|
36
|
+
* deploys nothing to isb never asks for a token.
|
|
37
|
+
*/
|
|
38
|
+
export declare class Credentials extends Credentials_base {
|
|
39
|
+
}
|
|
40
|
+
export interface CredentialsInput {
|
|
41
|
+
readonly url: string;
|
|
42
|
+
readonly token: string | Redacted.Redacted<string>;
|
|
43
|
+
/** Default: `default`. */
|
|
44
|
+
readonly org?: string;
|
|
45
|
+
}
|
|
46
|
+
/** A connection given in code. */
|
|
47
|
+
export declare const fromConfig: (input: CredentialsInput) => Layer.Layer<Credentials>;
|
|
48
|
+
/**
|
|
49
|
+
* A connection from the environment the isb CLI reads: `ISB_URL`,
|
|
50
|
+
* `ISB_TOKEN` and, optionally, `ISB_ORG` (default `default`).
|
|
51
|
+
*/
|
|
52
|
+
export declare const fromEnv: () => Layer.Layer<Credentials>;
|
|
53
|
+
/** Run one isb tool and return its result. */
|
|
54
|
+
export declare const call: <T = unknown>(tool: string, args?: Record<string, unknown>) => Effect.Effect<T, IsbError, Credentials>;
|
|
55
|
+
/** `call`, with "there is no such thing" as `undefined`. */
|
|
56
|
+
export declare const callFound: <T = unknown>(tool: string, args?: Record<string, unknown>) => Effect.Effect<T | undefined, IsbError, Credentials>;
|
|
57
|
+
export {};
|
package/dist/Client.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The connection to an isb host: where it is, the org to act in and an API
|
|
3
|
+
* token, plus `call`, which runs one isb tool over its REST API
|
|
4
|
+
* (`POST /orgs/<org>/api/v1/tools/<tool>`).
|
|
5
|
+
*/
|
|
6
|
+
import * as Context from "effect/Context";
|
|
7
|
+
import * as Data from "effect/Data";
|
|
8
|
+
import * as Effect from "effect/Effect";
|
|
9
|
+
import * as Layer from "effect/Layer";
|
|
10
|
+
import * as Redacted from "effect/Redacted";
|
|
11
|
+
/** A tool call isb refused or that never reached it. */
|
|
12
|
+
export class IsbError extends Data.TaggedError("IsbError") {
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The connection, resolved when a resource first needs it, so a stack that
|
|
16
|
+
* deploys nothing to isb never asks for a token.
|
|
17
|
+
*/
|
|
18
|
+
export class Credentials extends Context.Service()("IsbCredentials") {
|
|
19
|
+
}
|
|
20
|
+
/** A connection given in code. */
|
|
21
|
+
export const fromConfig = (input) => Layer.succeed(Credentials, Effect.succeed({
|
|
22
|
+
url: input.url,
|
|
23
|
+
org: input.org ?? "default",
|
|
24
|
+
token: typeof input.token === "string" ? Redacted.make(input.token) : input.token,
|
|
25
|
+
}));
|
|
26
|
+
/**
|
|
27
|
+
* A connection from the environment the isb CLI reads: `ISB_URL`,
|
|
28
|
+
* `ISB_TOKEN` and, optionally, `ISB_ORG` (default `default`).
|
|
29
|
+
*/
|
|
30
|
+
export const fromEnv = () => Layer.succeed(Credentials, Effect.suspend(() => {
|
|
31
|
+
const { ISB_URL: url, ISB_TOKEN: token, ISB_ORG: org } = process.env;
|
|
32
|
+
if (!url || !token) {
|
|
33
|
+
return Effect.fail(new IsbError({
|
|
34
|
+
tool: "",
|
|
35
|
+
code: "unauthorized",
|
|
36
|
+
status: 0,
|
|
37
|
+
message: "isb: set ISB_URL (the server) and ISB_TOKEN (an API token from `isb token create`)",
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
return Effect.succeed({ url, org: org || "default", token: Redacted.make(token) });
|
|
41
|
+
}));
|
|
42
|
+
/** Run one isb tool and return its result. */
|
|
43
|
+
export const call = (tool, args = {}) => Effect.gen(function* () {
|
|
44
|
+
const config = yield* yield* Credentials;
|
|
45
|
+
const base = config.url.replace(/\/+$/, "");
|
|
46
|
+
const url = `${base}/orgs/${encodeURIComponent(config.org)}/api/v1/tools/${tool}`;
|
|
47
|
+
return yield* Effect.tryPromise({
|
|
48
|
+
try: async (signal) => {
|
|
49
|
+
const res = await fetch(url, {
|
|
50
|
+
method: "POST",
|
|
51
|
+
headers: {
|
|
52
|
+
authorization: `Bearer ${Redacted.value(config.token)}`,
|
|
53
|
+
"content-type": "application/json",
|
|
54
|
+
},
|
|
55
|
+
body: JSON.stringify(args),
|
|
56
|
+
signal,
|
|
57
|
+
});
|
|
58
|
+
const text = await res.text();
|
|
59
|
+
let body;
|
|
60
|
+
try {
|
|
61
|
+
body = JSON.parse(text);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
body = { error: "bad_response", message: text.slice(0, 300) || res.statusText };
|
|
65
|
+
}
|
|
66
|
+
if (!res.ok || body.error !== undefined) {
|
|
67
|
+
throw new IsbError({
|
|
68
|
+
tool,
|
|
69
|
+
code: body.error ?? "error",
|
|
70
|
+
status: res.status,
|
|
71
|
+
message: `isb ${tool}: ${body.message ?? res.statusText}`,
|
|
72
|
+
data: body.data,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return body.result;
|
|
76
|
+
},
|
|
77
|
+
catch: (e) => e instanceof IsbError
|
|
78
|
+
? e
|
|
79
|
+
: new IsbError({
|
|
80
|
+
tool,
|
|
81
|
+
code: "unreachable",
|
|
82
|
+
status: 0,
|
|
83
|
+
message: `isb ${tool}: ${config.url} did not answer (${String(e)})`,
|
|
84
|
+
}),
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
/** `call`, with "there is no such thing" as `undefined`. */
|
|
88
|
+
export const callFound = (tool, args = {}) => call(tool, args).pipe(Effect.catchTag("IsbError", (e) => e.code === "not_found" ? Effect.succeed(undefined) : Effect.fail(e)));
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as Provider from "alchemy/Provider";
|
|
2
|
+
import { Resource } from "alchemy/Resource";
|
|
3
|
+
export interface ProjectProps {
|
|
4
|
+
/** The project's name in isb: lowercase letters, digits and `-`. */
|
|
5
|
+
name: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
/**
|
|
8
|
+
* The environments to make sure exist. Each one runs as its own stack,
|
|
9
|
+
* `<project>-<environment>`.
|
|
10
|
+
* @default ["production"]
|
|
11
|
+
*/
|
|
12
|
+
environments?: string[];
|
|
13
|
+
}
|
|
14
|
+
export interface ProjectAttributes {
|
|
15
|
+
name: string;
|
|
16
|
+
environments: string[];
|
|
17
|
+
}
|
|
18
|
+
export type Project = Resource<"Isb.Project", ProjectProps, ProjectAttributes>;
|
|
19
|
+
/**
|
|
20
|
+
* An isb project: the group apps and stacks belong to, with one or more
|
|
21
|
+
* environments.
|
|
22
|
+
*
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const shop = yield* Isb.Project("shop", {
|
|
25
|
+
* name: "shop",
|
|
26
|
+
* environments: ["production", "staging"],
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* Removing an environment from `environments` leaves it in isb. Destroying
|
|
31
|
+
* the project deletes it only once it holds no apps and no stacks.
|
|
32
|
+
*/
|
|
33
|
+
export declare const Project: import("alchemy/Resource").ResourceClass<Project>;
|
|
34
|
+
export declare const ProjectProvider: () => import("effect/Layer").Layer<Provider.Provider<Project>, never, import("./Client.ts").Credentials>;
|
package/dist/Project.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import * as Provider from "alchemy/Provider";
|
|
2
|
+
import { Resource } from "alchemy/Resource";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import { call, callFound } from "./Client.js";
|
|
5
|
+
import { replaceWhen } from "./Replace.js";
|
|
6
|
+
const environmentsOf = (p) => p.environments.map((e) => (typeof e === "string" ? e : e.name));
|
|
7
|
+
const find = (name) => call("project_list").pipe(Effect.map((r) => r.projects.find((p) => p.name === name)));
|
|
8
|
+
/**
|
|
9
|
+
* An isb project: the group apps and stacks belong to, with one or more
|
|
10
|
+
* environments.
|
|
11
|
+
*
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const shop = yield* Isb.Project("shop", {
|
|
14
|
+
* name: "shop",
|
|
15
|
+
* environments: ["production", "staging"],
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Removing an environment from `environments` leaves it in isb. Destroying
|
|
20
|
+
* the project deletes it only once it holds no apps and no stacks.
|
|
21
|
+
*/
|
|
22
|
+
export const Project = Resource("Isb.Project");
|
|
23
|
+
export const ProjectProvider = () => Provider.succeed(Project, {
|
|
24
|
+
stables: ["name"],
|
|
25
|
+
// A project cannot be renamed.
|
|
26
|
+
diff: replaceWhen((p) => p.name),
|
|
27
|
+
reconcile: Effect.fn(function* ({ news }) {
|
|
28
|
+
const wanted = news.environments ?? ["production"];
|
|
29
|
+
let project = yield* find(news.name);
|
|
30
|
+
if (project === undefined) {
|
|
31
|
+
yield* call("project_create", {
|
|
32
|
+
name: news.name,
|
|
33
|
+
description: news.description ?? "",
|
|
34
|
+
environments: wanted,
|
|
35
|
+
});
|
|
36
|
+
project = yield* find(news.name);
|
|
37
|
+
}
|
|
38
|
+
const have = project ? environmentsOf(project) : [];
|
|
39
|
+
for (const env of wanted.filter((e) => !have.includes(e))) {
|
|
40
|
+
yield* call("environment_create", { project: news.name, name: env });
|
|
41
|
+
have.push(env);
|
|
42
|
+
}
|
|
43
|
+
return { name: news.name, environments: have };
|
|
44
|
+
}),
|
|
45
|
+
delete: Effect.fn(function* ({ output }) {
|
|
46
|
+
yield* callFound("project_delete", { name: output.name });
|
|
47
|
+
}),
|
|
48
|
+
read: Effect.fn(function* ({ olds, output }) {
|
|
49
|
+
const name = output?.name ?? olds?.name;
|
|
50
|
+
if (name === undefined)
|
|
51
|
+
return undefined;
|
|
52
|
+
const project = yield* find(name);
|
|
53
|
+
return project && { name, environments: environmentsOf(project) };
|
|
54
|
+
}),
|
|
55
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as Provider from "alchemy/Provider";
|
|
2
|
+
import * as Layer from "effect/Layer";
|
|
3
|
+
import { type CredentialsInput } from "./Client.ts";
|
|
4
|
+
declare const Providers_base: Provider.ProviderCollection<Providers, "Isb">;
|
|
5
|
+
export declare class Providers extends Providers_base {
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The isb resources (`Project`, `Secret`, `App`, `Stack`) and the
|
|
9
|
+
* connection they use: the one given, or `ISB_URL`, `ISB_TOKEN` and
|
|
10
|
+
* `ISB_ORG` from the environment.
|
|
11
|
+
*
|
|
12
|
+
* ```typescript
|
|
13
|
+
* export default Alchemy.Stack(
|
|
14
|
+
* "shop",
|
|
15
|
+
* { providers: Isb.providers(), state: Alchemy.localState() },
|
|
16
|
+
* Effect.gen(function* () { ... }),
|
|
17
|
+
* );
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare const providers: (connection?: CredentialsInput) => Layer.Layer<import("./Client.ts").Credentials | Providers, never, never>;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as Provider from "alchemy/Provider";
|
|
2
|
+
import * as Layer from "effect/Layer";
|
|
3
|
+
import { App, AppProvider } from "./App.js";
|
|
4
|
+
import { fromConfig, fromEnv } from "./Client.js";
|
|
5
|
+
import { Project, ProjectProvider } from "./Project.js";
|
|
6
|
+
import { Secret, SecretProvider } from "./Secret.js";
|
|
7
|
+
import { Stack, StackProvider } from "./Stack.js";
|
|
8
|
+
export class Providers extends Provider.ProviderCollection()("Isb") {
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The isb resources (`Project`, `Secret`, `App`, `Stack`) and the
|
|
12
|
+
* connection they use: the one given, or `ISB_URL`, `ISB_TOKEN` and
|
|
13
|
+
* `ISB_ORG` from the environment.
|
|
14
|
+
*
|
|
15
|
+
* ```typescript
|
|
16
|
+
* export default Alchemy.Stack(
|
|
17
|
+
* "shop",
|
|
18
|
+
* { providers: Isb.providers(), state: Alchemy.localState() },
|
|
19
|
+
* Effect.gen(function* () { ... }),
|
|
20
|
+
* );
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export const providers = (connection) => Layer.effect(Providers, Provider.collection([App, Project, Secret, Stack])).pipe(Layer.provide(Layer.mergeAll(AppProvider(), ProjectProvider(), SecretProvider(), StackProvider())), Layer.provideMerge(connection ? fromConfig(connection) : fromEnv()));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ReplaceDiff } from "alchemy/Diff";
|
|
2
|
+
import type { Input } from "alchemy/Input";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
/**
|
|
5
|
+
* A provider `diff` for resources whose identity isb cannot change: when
|
|
6
|
+
* what `identity` returns differs between the old and new props, the
|
|
7
|
+
* resource is replaced. Anything else is left to alchemy's own comparison
|
|
8
|
+
* of the props.
|
|
9
|
+
*/
|
|
10
|
+
export declare const replaceWhen: <P>(identity: (props: P) => unknown, options?: {
|
|
11
|
+
deleteFirst?: boolean;
|
|
12
|
+
}) => ({ news, olds }: {
|
|
13
|
+
news: Input<P>;
|
|
14
|
+
olds: P;
|
|
15
|
+
}) => Effect.Effect<ReplaceDiff | undefined>;
|
package/dist/Replace.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { isResolved } from "alchemy/Diff";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
/**
|
|
4
|
+
* A provider `diff` for resources whose identity isb cannot change: when
|
|
5
|
+
* what `identity` returns differs between the old and new props, the
|
|
6
|
+
* resource is replaced. Anything else is left to alchemy's own comparison
|
|
7
|
+
* of the props.
|
|
8
|
+
*/
|
|
9
|
+
export const replaceWhen = (identity, options = {}) => ({ news, olds }) => Effect.sync(() => {
|
|
10
|
+
if (!isResolved(news) || olds === undefined)
|
|
11
|
+
return undefined;
|
|
12
|
+
const same = JSON.stringify(identity(news)) === JSON.stringify(identity(olds));
|
|
13
|
+
return same ? undefined : { action: "replace", deleteFirst: options.deleteFirst };
|
|
14
|
+
});
|