@cdwr/fly-node 0.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ ## 0.1.0 (2025-01-12)
2
+
3
+ ### ✨ Features
4
+
5
+ - **fly-node:** support attach and detach postgres cluster ([1e5b749](https://github.com/codeware-sthlm/codeware/commit/1e5b749))
6
+ - **fly-node:** add support for environment variables in deploy ([8af48bc](https://github.com/codeware-sthlm/codeware/commit/8af48bc))
7
+ - **fly-node:** add fly cli node wrapper package ([aaab584](https://github.com/codeware-sthlm/codeware/commit/aaab584))
8
+
9
+ ### 🐞 Bug Fixes
10
+
11
+ - **fly-node:** postgres detach has no yes flag ([bb9a482](https://github.com/codeware-sthlm/codeware/commit/bb9a482))
12
+
13
+ ### ⚙️ Miscellaneous Chores
14
+
15
+ - **repo:** fix nx project dependencies ([383f50e](https://github.com/codeware-sthlm/codeware/commit/383f50e))
package/README.md ADDED
@@ -0,0 +1,441 @@
1
+ <p align="center">
2
+ <br />
3
+ <img width="200" src="../../assets/cdwr-cloud.png" alt="codeware sthlm logo">
4
+ <br />
5
+ <br />
6
+ </p>
7
+
8
+ <h1 align='center'>Fly Node</h1>
9
+
10
+ <p align='center'>
11
+ The Fly CLI node wrapper for programmatic deployments to <a href='https://fly.io'>Fly.io</a>.
12
+ <br />
13
+ <br />
14
+ <a href='https://www.npmjs.com/package/@cdwr/fly-node'><img src='https://img.shields.io/npm/v/@cdwr/fly-node?label=npm%20version' alt='@cdwr/fly-node npm'></a>
15
+ &nbsp;
16
+ <a href='https://opensource.org/licenses/MIT'><img src='https://img.shields.io/badge/License-MIT-green.svg' alt='MIT'></a>
17
+ <br />
18
+ <br />
19
+ </p>
20
+
21
+ This library is a lightweight, fully typed Node wrapper for the Fly CLI tool for programmatic deployments to [Fly.io](https://fly.io).
22
+
23
+ It has only one dependency, [Zod](https://zod.dev/), for validating the responses from the Fly API.
24
+
25
+ It's built with a configuration-first approach, meaning that you provide a `fly.toml` file and an API token, and let the library handle the necessary boilerplate stuff.
26
+
27
+ > [!NOTE]
28
+ > The library aims to be unopinionated and follow the `flyctl` commands structure as much as possible.
29
+ >
30
+ > There are tons of CLI commands that could be supported, but since blueprint deployments are the primary use case, this is where the focus lies.
31
+ >
32
+ > For now the library is built for the needs at Codeware Sthlm, but it aims to be flexible and work for any Fly.io app.
33
+
34
+ ## Contents <!-- omit in toc -->
35
+
36
+ - [Installation](#installation)
37
+ - [Usage](#usage)
38
+ - [Create a Fly instance](#create-a-fly-instance)
39
+ - [Verify that the Fly client is ready](#verify-that-the-fly-client-is-ready)
40
+ - [Deploying apps \& machines](#deploying-apps--machines)
41
+ - [Create an application](#create-an-application)
42
+ - [Destroy an application](#destroy-an-application)
43
+ - [Deploy an application](#deploy-an-application)
44
+ - [Application status](#application-status)
45
+ - [List applications](#list-applications)
46
+ - [Configuration \& scaling](#configuration--scaling)
47
+ - [Add a custom domain](#add-a-custom-domain)
48
+ - [Remove a custom domain](#remove-a-custom-domain)
49
+ - [List certificates](#list-certificates)
50
+ - [Show application configuration](#show-application-configuration)
51
+ - [Add application secrets](#add-application-secrets)
52
+ - [Remove application secrets](#remove-application-secrets)
53
+ - [List secrets](#list-secrets)
54
+
55
+ ## Installation
56
+
57
+ ```bash
58
+ npm install @cdwr/fly-node
59
+ ```
60
+
61
+ ### Fly CLI <!-- omit in toc -->
62
+
63
+ > [!IMPORTANT]
64
+ > It's required to have the Fly CLI installed to use this library.
65
+
66
+ Instructions to [install manually](https://github.com/superfly/flyctl?tab=readme-ov-file#installation).
67
+
68
+ For using in GitHub actions it's best to use the [action](https://github.com/superfly/flyctl-actions) provided by the Fly CLI project.
69
+
70
+ ## Usage
71
+
72
+ The library behaves just like the Fly CLI tool regarding all default values.
73
+
74
+ For any command you run, your local `flyctl` installation will be verified. If it's not present, the command will abort with exception.
75
+
76
+ ### Create a Fly instance
77
+
78
+ Fly will try to connect to Fly.io in the following order:
79
+
80
+ 1. A user has already authenticated locally using `flyctl auth login`
81
+ 2. The library authenticates using the provided Fly API `token`
82
+ 3. The library authenticates using environment variable `FLY_API_TOKEN`
83
+
84
+ ```ts
85
+ import { Fly } from '@cdwr/fly-node';
86
+
87
+ const fly = new Fly();
88
+
89
+ // Provide a Fly API token
90
+ const fly = new Fly({ token: 'fly-api-token' });
91
+ ```
92
+
93
+ > [!TIP]
94
+ > Built-in monorepo support!
95
+ >
96
+ > When you run `flyctl` in your terminal, it will look for a `fly.toml` file in your current working directory. If that's not the case, you have to provide the `app` or `config` option for any command to run.
97
+ >
98
+ > This is what you have in a monorepo setup, where you probably have the `fly.toml` configuration files in the applications folders.
99
+ >
100
+ > The library `Fly` class works the same way at its default configuration. However, you can also provide the `app` or `config` options when creating the instance to let the commands know where to run.
101
+ >
102
+ > So, to create an instance to `my-app`, you can do it like this:
103
+ >
104
+ > ```ts
105
+ > const fly = new Fly({ config: 'apps/my-app/fly.toml' });
106
+ > ```
107
+ >
108
+ > Any command run on this instance will then be executed on `my-app`.
109
+ >
110
+ > Similarly when you have an application named `your-app` on Fly.io, you can create an instance to it like this:
111
+ >
112
+ > ```ts
113
+ > const fly = new Fly({ app: 'your-app' });
114
+ > ```
115
+ >
116
+ > Any command run on this instance will instead be executed on `your-app`.
117
+ >
118
+ > One more thing. The commands supported by this library and have `app` or `config` flags, will override the instance application when provided. This gives you the default `flyctl` CLI behaviour but with improved flexibility depending on your repository structure.
119
+
120
+ **Options**
121
+
122
+ | Name | Description |
123
+ | ----------------- | ------------------------------------------------------------------------------------------------------------------------------- |
124
+ | `token` | Fly API access token. |
125
+ | `app` or `config` | Name of the application or the path to the fly configuration file to run all commands on. |
126
+ | `org` | Target organisation for your created apps. Defaults to your personal organisation. |
127
+ | `region` | Target region for your deployed apps. Defaults to auto-detect the fastest location. |
128
+ | `logger` | Custom logger for the library. Defaults to using `console.log()` and `console.error()`. CLI tracing can be enabled when needed. |
129
+
130
+ #### Verify that the Fly client is ready
131
+
132
+ As described in the [installation](#installation) section, the library will verify that the Fly client is ready for any command you run.
133
+
134
+ However, you can also verify that the Fly client is ready manually, without running any commands.
135
+
136
+ ```ts
137
+ const isReady = await fly.isReady(); // true or false
138
+
139
+ // Or use assertion mode
140
+ const isReady = await fly.isReady('assert'); // throws error if false
141
+ ```
142
+
143
+ For both modes a message will be logged to console when the Fly client is ready.
144
+
145
+ Use this command to check if the Fly CLI is installed.
146
+
147
+ ```ts
148
+ const isInstalled = await fly.cli.isInstalled(); // true or false
149
+ ```
150
+
151
+ ### Deploying apps & machines
152
+
153
+ #### Create an application
154
+
155
+ This will create an application which is not deployed yet. You can add secrets, domains etc. and deploy later when ready.
156
+
157
+ > [!TIP]
158
+ > Creating an application is not necessary before deploying, the library will do it for you.
159
+
160
+ ```ts
161
+ const name = await fly.apps.create(); // 'some-app-name-123'
162
+
163
+ // Create application 'foo-app' in the instance organisation
164
+ const name = await fly.apps.create({ app: 'foo-app' });
165
+
166
+ // Create application 'bar-app' in organisation 'baz'
167
+ const name = await fly.apps.create({ app: 'bar-app', org: 'baz' });
168
+ ```
169
+
170
+ #### Destroy an application
171
+
172
+ This will destroy the application completely.
173
+
174
+ > [!NOTE]
175
+ > When the application is attached to one or many Postgres clusters, it will be detached from all before it gets destroyed
176
+
177
+ ```ts
178
+ await fly.apps.destroy('foo-app');
179
+
180
+ // Force destroy 'foo-app'
181
+ await fly.apps.destroy('foo-app', { force: true });
182
+ ```
183
+
184
+ #### Deploy an application
185
+
186
+ > [!IMPORTANT]
187
+ > Currently there's a small limitation to detecting which configuration to use for deployment. To make sure the correct configuration is used you must provide the configuration file, either globally via instance or in the command options.
188
+ >
189
+ > Without providing the configuration file, the command will throw an error.
190
+
191
+ ```ts
192
+ // deploy the instance application, if provided
193
+ const response = await fly.deploy();
194
+
195
+ // deploy 'foo-app'
196
+ const response = await fly.deploy({ config: 'apps/foo-app/fly.toml' });
197
+
198
+ // deploy 'bar-app' configuration as 'preview-bar-app'
199
+ const response = await fly.deploy({
200
+ app: 'preview-bar-app',
201
+ config: 'apps/bar-app/fly.toml'
202
+ });
203
+
204
+ // deploy 'bar-app' and attach postgres cluster 'db-app'
205
+ const response = await fly.deploy({
206
+ config: 'apps/bar-app/fly.toml',
207
+ postgres: 'db-app'
208
+ });
209
+
210
+ // With all options, could be a pull request preview deployment
211
+ const response = await fly.deploy({
212
+ app: 'pr-19-foo-app',
213
+ config: 'apps/foo-app/fly.toml',
214
+ environment: 'preview',
215
+ org: 'baz',
216
+ postgres: 'db-preview',
217
+ region: 'arn',
218
+ env: {
219
+ DEPLOY_ID: 'qwerty'
220
+ },
221
+ secrets: {
222
+ LICENCE_KEY: '1234567890'
223
+ }
224
+ });
225
+
226
+ // {
227
+ // app: 'pr-19-foo-app',
228
+ // hostname: 'pr-19-foo-app.fly.dev',
229
+ // url: 'https://pr-19-foo-app.fly.dev'
230
+ // }
231
+ ```
232
+
233
+ > [!TIP]
234
+ > When the application is attached to a Postgres cluster, Fly will provide the secret `DATABASE_URL` to the application.
235
+ >
236
+ > Read more about [attach or detach a Fly app](https://fly.io/docs/postgres/managing/attach-detach/#attach-a-fly-app).
237
+
238
+ #### Application status
239
+
240
+ ```ts
241
+ // Get the status of the instance application
242
+ const status = await fly.status();
243
+
244
+ // Get the status of 'foo-app'
245
+ const status = await fly.status({ app: 'foo-app' });
246
+ const status = await fly.status({ config: 'apps/foo-app/fly.toml' });
247
+
248
+ // {
249
+ // name: 'foo-app',
250
+ // hostname: 'foo-app.fly.dev',
251
+ // url: 'https://foo-app.fly.dev',
252
+ // organization: {
253
+ // name: 'personal'
254
+ // },
255
+ // status: 'running',
256
+ // ... more
257
+ // }
258
+
259
+ // Get the status of an application that doesn't exist
260
+ const status = await fly.status({ app: 'non-existent-app' }); // null
261
+
262
+ // There is also an extended status command with more details
263
+ const status = await fly.statusExtended();
264
+
265
+ // {
266
+ // ... same as above
267
+ // domains: [
268
+ // 'foo.domain.com'
269
+ // ],
270
+ // secrets: [
271
+ // 'FOO',
272
+ // 'BAR'
273
+ // ],
274
+ // ... more
275
+ // }
276
+ ```
277
+
278
+ #### List applications
279
+
280
+ ```ts
281
+ // Get all applications
282
+ const apps = await fly.apps.list();
283
+ ```
284
+
285
+ ### Configuration & scaling
286
+
287
+ #### Add a custom domain
288
+
289
+ Adding a domain will create a certificate for it with automatic renewal.
290
+
291
+ > [!TIP]
292
+ > Read the guide about how to [add a custom domain for your app](https://fly.io/docs/networking/custom-domain/#add-a-custom-domain-for-your-app).
293
+
294
+ ```ts
295
+ // Add a Domain to the instance application
296
+ await fly.certs.add('foo.domain.com');
297
+
298
+ // Add a domain to 'bar-app'
299
+ await fly.certs.add('bar.domain.com', { app: 'bar-app' });
300
+ await fly.certs.add('baz.domain.com', { config: 'apps/bar-app/fly.toml' });
301
+ ```
302
+
303
+ #### Remove a custom domain
304
+
305
+ ```ts
306
+ // Remove a domain from the instance application
307
+ await fly.certs.remove('foo.domain.com');
308
+
309
+ // Remove a domain from 'bar-app'
310
+ await fly.certs.remove('bar.domain.com', { app: 'bar-app' });
311
+ await fly.certs.remove('baz.domain.com', { config: 'apps/bar-app/fly.toml' });
312
+ ```
313
+
314
+ #### List certificates
315
+
316
+ ```ts
317
+ // List certificates for the instance application
318
+ const certs = await fly.certs.list();
319
+
320
+ // List certificates for 'foo-app'
321
+ const certs = await fly.certs.list({ app: 'foo-app' });
322
+ const certs = await fly.certs.list({ config: 'apps/foo-app/fly.toml' });
323
+
324
+ // [
325
+ // {
326
+ // clientStatus: 'Ready',
327
+ // createdAt: '2024-01-01T00:00:00.000Z',
328
+ // hostname: 'foo.domain.com',
329
+ // },
330
+ // ...
331
+ // ]
332
+
333
+ // List certificates for all your applications
334
+ const certs = await fly.certs.list('all');
335
+
336
+ // [
337
+ // {
338
+ // app: 'foo-app',
339
+ // [cert details]
340
+ // },
341
+ // ...
342
+ // {
343
+ // app: 'bar-app',
344
+ // [cert details]
345
+ // },
346
+ // ...
347
+ // ]
348
+ ```
349
+
350
+ #### Show application configuration
351
+
352
+ The configuration is always fetched from Fly.io remote configuration, but you can also fetch the local configuration file.
353
+
354
+ When a configuration cannot be shown, the command will throw an error.
355
+
356
+ > [!TIP]
357
+ > Fetching local configuration is the only option for applications never deployed. It will also assure that the file exists.
358
+
359
+ ```ts
360
+ // Show the instance application configuration
361
+ const config = await fly.config.show();
362
+
363
+ // Show 'foo-app' configuration
364
+ const config = await fly.config.show({ app: 'foo-app' });
365
+ const config = await fly.config.show({ config: 'apps/foo-app/fly.toml' });
366
+
367
+ // Show local 'new-app' configuration
368
+ const config = await fly.config.show({
369
+ config: 'apps/new-app/fly.toml',
370
+ local: true
371
+ });
372
+ ```
373
+
374
+ #### Add application secrets
375
+
376
+ Adding secrets to an application will trigger a deployment for machines running the application.
377
+
378
+ ```ts
379
+ // Add secrets to the instance application
380
+ await fly.secrets.set({
381
+ LICENCE_KEY: '1234567890',
382
+ SOME_SECRET: 'super-secret'
383
+ });
384
+
385
+ // Add secrets to 'foo-app'
386
+ await fly.secrets.set({ FOO: 'bar' }, { app: 'foo-app' });
387
+ await fly.secrets.set({ BEE: 'baz' }, { config: 'apps/foo-app/fly.toml' });
388
+
389
+ // Add secrets to 'foo-app' and skip auto-deploy
390
+ await fly.secrets.set({ FOO: 'bar' }, { app: 'foo-app', stage: true });
391
+ ```
392
+
393
+ #### Remove application secrets
394
+
395
+ ```ts
396
+ // Remove secret from the instance application
397
+ await fly.secrets.unset('FOO');
398
+
399
+ // Remove secret from 'foo-app'
400
+ await fly.secrets.unset('FOO', { app: 'foo-app' });
401
+ await fly.secrets.unset('BEE', { config: 'apps/foo-app/fly.toml' });
402
+
403
+ // Remove secret from 'foo-app' and skip auto-deploy
404
+ await fly.secrets.unset('FOO', { app: 'foo-app', stage: true });
405
+ ```
406
+
407
+ #### List secrets
408
+
409
+ ```ts
410
+ // List secrets for the instance application
411
+ const secrets = await fly.secrets.list();
412
+
413
+ // List secrets for 'foo-app'
414
+ const secrets = await fly.secrets.list({ app: 'foo-app' });
415
+ const secrets = await fly.secrets.list({ config: 'apps/foo-app/fly.toml' });
416
+
417
+ // [
418
+ // {
419
+ // createdAt: '2024-01-01T00:00:00.000Z',
420
+ // digest: '1234567890',
421
+ // name: 'FOO',
422
+ // },
423
+ // ...
424
+ // ]
425
+
426
+ // List secrets for all your applications
427
+ const secrets = await fly.secrets.list('all');
428
+
429
+ // [
430
+ // {
431
+ // app: 'foo-app',
432
+ // [secret details]
433
+ // },
434
+ // ...
435
+ // {
436
+ // app: 'bar-app',
437
+ // [secret details]
438
+ // },
439
+ // ...
440
+ // ]
441
+ ```