ramdb2-cli 2.0.0-alpha.0 → 2.0.0-alpha.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 ADDED
@@ -0,0 +1,14 @@
1
+ # ramdb2-cli
2
+
3
+ ## 2.0.0-alpha.2
4
+
5
+ ### Minor Changes
6
+
7
+ - 989ba8b: `ramdb2-cli infra deploy <server>`: build, secrets decrypted by sops, `docker compose up` on the server through `DOCKER_HOST`, Let's Encrypt certificates, nginx reloaded.
8
+ - ee78e45: `ramdb2-cli infra keygen`, `infra backups`, `infra restore` and `infra fetch`: the backup key of a server, encrypted by sops; the backups of a site listed, restored or fetched through its server.
9
+
10
+ ## 2.0.0-alpha.1
11
+
12
+ ### Minor Changes
13
+
14
+ - 14d1f42: `ramdb2-cli infra build`: the Docker files of each server from `infra.yaml`, a front nginx with HTTPS and one domain per site, the API, its backups and the web site of each ramdb2 project.
package/README.md CHANGED
@@ -357,6 +357,174 @@ each list relation to check; write an `@onDelete` where the policy is not the de
357
357
 
358
358
  ---
359
359
 
360
+ ## infra
361
+
362
+ The servers of your sites, in Docker: one front nginx per server (HTTPS, one domain per site), and for each ramdb2
363
+ site its API, its backups and its web site. Only Docker is needed on the server: the images are built there, from
364
+ your machine, with `DOCKER_HOST=ssh://`.
365
+
366
+ ```sh
367
+ ramdb2-cli infra build # build/<server>/ for every server of ./infra.yaml
368
+ ramdb2-cli infra build ovh1 # one server
369
+ ```
370
+
371
+ An infra folder (a git repository of its own):
372
+
373
+ | Path | Content |
374
+ |---|---|
375
+ | `infra.yaml` | the backups, the servers and their sites |
376
+ | `custom/<name>.nginx.conf`, `custom/<name>.compose.yaml` | a site that is not a ramdb2 project, written by hand |
377
+ | `build/` | generated, rewritten by each build: keep it out of git |
378
+
379
+ ```yaml
380
+ backup:
381
+ target: sftp://backup@backups.example.org:22/ramdb2 # one folder per site under it
382
+ servers:
383
+ ovh1:
384
+ host: ssh://manu@ovh1.example.org
385
+ email: me@example.org # the Let's Encrypt account
386
+ sites:
387
+ ilife: # unique on all the servers: containers, volume, backup folder
388
+ source: ../ilife # the project, made by ramdb2-cli new, relative to infra.yaml
389
+ domain: ilife.fr
390
+ aliases: [www.ilife.fr] # redirected (301) to domain
391
+ memory: 512m
392
+ custom:
393
+ - gancio # custom/gancio.nginx.conf, and custom/gancio.compose.yaml if any
394
+ - name: wiki
395
+ domains: [wiki.example.org] # certbot keeps a certificate, in /etc/nginx/certs/wiki.example.org/
396
+ ```
397
+
398
+ All the errors of `infra.yaml` are reported at once. `build/<server>/` holds:
399
+
400
+ | File | Content |
401
+ |---|---|
402
+ | `compose.yaml` | the Compose project of the server, the custom compose files included |
403
+ | `nginx/` | the front nginx image: its configuration, one file per site, the custom ones copied |
404
+ | `<site>/api.Dockerfile` | the API (target `api`, `node src/index.js`) and its backups (target `backup`, `ramdb2-server-backup schedule`) |
405
+ | `<site>/web.Dockerfile` | for a project made with `--with-client`: the site compiled by Vite, served by nginx |
406
+ | `certificates.json` | the certificates of the server and their domains |
407
+
408
+ - Each domain: HTTP redirected to HTTPS, `/graphql` to the API, the rest to the web site (all to the API for a
409
+ project without a web site).
410
+ - The database lives in a Docker volume, `<site>-data`. The images never contain `node_modules` from your machine,
411
+ a database, a backup or a `.env`.
412
+ - Nothing is mounted from your machine: the nginx configuration is in its image; the secrets come from the
413
+ environment of `docker compose` (`RAMDB2_SECRETS`, the folder of the site `.env` files, and `BACKUP_SSH_KEY`).
414
+ - The healthcheck of the API sends the GraphQL query `health`: keep it open without a login.
415
+ - Until a certificate exists, nginx serves a self-signed one, so that it can start.
416
+
417
+ ### Deploy
418
+
419
+ ```sh
420
+ ramdb2-cli infra deploy ovh1 # build, then up on the server, then the certificates
421
+ ramdb2-cli infra deploy ovh1 --staging # with the test certificates of Let's Encrypt
422
+ ```
423
+
424
+ The secrets live in the infra folder, encrypted by [sops](https://github.com/getsops/sops) (with
425
+ [age](https://github.com/FiloSottile/age) keys listed in `.sops.yaml`):
426
+
427
+ | File | Content |
428
+ |---|---|
429
+ | `secrets/<site>.env` | the environment of the API of a site (`TOKEN_SECRET`…) |
430
+ | `secrets/<server>.key` | the private SSH key of the server, for its backups |
431
+
432
+ `deploy` needs `docker`, `sops` and an SSH access to the server on your machine. It:
433
+
434
+ 1. runs `build` for the server;
435
+ 2. decrypts the secrets into a private temporary folder, deleted at the end;
436
+ 3. runs `docker compose up -d --build --remove-orphans` with `DOCKER_HOST` set to the `host` of the server: the
437
+ images are built on the server;
438
+ 4. asks Let's Encrypt for each certificate (nothing is done while a certificate is valid; it is made again when
439
+ its domains change), then reloads nginx.
440
+
441
+ It stops before Docker when a secret is missing or cannot be decrypted. A refused certificate (a domain that does
442
+ not point to the server yet) does not stop the others; the exit code is then 1.
443
+
444
+ The decrypted secrets live in `build/<server>/.secrets-*` (only you can read it) while a command runs, and are
445
+ deleted at the end, even on an error. Not in `/tmp`: Docker installed as a snap cannot read it.
446
+
447
+ ### Secrets: sops and age, once
448
+
449
+ ```sh
450
+ age-keygen -o ~/.config/sops/age/keys.txt # your age key, where sops looks for it
451
+ age-keygen -y ~/.config/sops/age/keys.txt # its public key, for .sops.yaml
452
+ ```
453
+
454
+ `.sops.yaml`, at the root of the infra folder:
455
+
456
+ ```yaml
457
+ creation_rules:
458
+ - path_regex: secrets/.*
459
+ age: age1...your public key...
460
+ ```
461
+
462
+ Keep a copy of your age key somewhere safe (a password manager): without it, no secret can be decrypted, and no
463
+ server can be deployed. Then the secrets of a site:
464
+
465
+ ```sh
466
+ sops secrets/ilife.env # opens your editor; saved encrypted. TOKEN_SECRET=..., one variable per line
467
+ ```
468
+
469
+ ### The backup key of a server
470
+
471
+ ```sh
472
+ ramdb2-cli infra keygen ovh1
473
+ ```
474
+
475
+ Creates an SSH key for the backups of the server: `secrets/ovh1.key`, encrypted by sops, and `secrets/ovh1.pub`.
476
+ It prints the line to add to `~backup/.ssh/authorized_keys` on the backup server:
477
+ `restrict ssh-ed25519 AAAA... ramdb2-backup-ovh1`. An existing key is replaced only with `--force`; install the
478
+ new public key then, and deploy again.
479
+
480
+ ### The backup server
481
+
482
+ Any OpenSSH server. A user `backup`, which can only use SFTP, locked in the backup folder, with keys only. In
483
+ `/etc/ssh/sshd_config`:
484
+
485
+ ```
486
+ PasswordAuthentication no
487
+ KbdInteractiveAuthentication no
488
+ Subsystem sftp internal-sftp
489
+
490
+ Match User backup
491
+ ChrootDirectory /volume1/backup
492
+ ForceCommand internal-sftp
493
+ AllowTcpForwarding no
494
+ X11Forwarding no
495
+ ```
496
+
497
+ - The chroot folder (`/volume1/backup`) belongs to root and only root can write it (an OpenSSH rule); the folder
498
+ of the backups inside it (`ramdb2`) belongs to `backup`.
499
+ - `backup.target` in `infra.yaml` is then `sftp://backup@host:22/ramdb2`: the path is seen from the chroot.
500
+ - Each server key sees the backups of every site: a site can move to another server and keep its backups.
501
+ - The first connection of a server accepts the host key of the backup server, then checks it.
502
+
503
+ ### Backups and restore
504
+
505
+ Each ramdb2 site has its backup container: a compressed image of the database every 5 minutes, sent to the
506
+ backup server, then pruned there (every backup of the last 24 hours, the last of each day for 30 days, the last of
507
+ each month forever). See [`ramdb2-server-backup`](https://www.npmjs.com/package/ramdb2-server). From your machine:
508
+
509
+ ```sh
510
+ ramdb2-cli infra backups ilife # the backups of the site, oldest first
511
+ ramdb2-cli infra restore ilife # the most recent backup
512
+ ramdb2-cli infra restore ilife 2026-11-06T14:00 # the last one at or before that moment (UTC)
513
+ ramdb2-cli infra fetch ilife ilife.db # the most recent backup, to a local file
514
+ ramdb2-cli infra fetch ilife ilife.db 2026-11-06T14:00
515
+ ```
516
+
517
+ - The server of a site is read in `infra.yaml`. The commands run on it, in the backup container of the site, with
518
+ the key of the server: your machine needs no access to the backup server.
519
+ - `restore` asks first (`--yes` to skip the question). It stops the API and the backups of the site, checks the
520
+ backup (`PRAGMA integrity_check`), replaces the database, then starts them again; when the backup is damaged,
521
+ the database is left as it was and the site starts again.
522
+ - A new server: install Docker, `ramdb2-cli infra deploy <server>`, then `ramdb2-cli infra restore <site>` for each
523
+ site.
524
+ - `fetch` writes a database you can open with any SQLite tool, or use as `DB_PATH` of the project on your machine.
525
+
526
+ ---
527
+
360
528
  ## Programmatic use
361
529
 
362
530
  ```js
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "ramdb2-cli",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0-alpha.2",
4
4
  "description": "Code generator and project scaffolding for ramdb2 and ramdb2-server",
5
5
  "type": "module",
6
6
  "exports": "./src/index.js",
7
7
  "bin": {
8
- "ramdb2-cli": "./src/cli.js"
8
+ "ramdb2-cli": "src/cli.js"
9
9
  },
10
10
  "license": "MIT",
11
11
  "author": "Emmanuel Leparquier",
@@ -23,11 +23,12 @@
23
23
  "test": "node --test \"test/**/*.test.js\""
24
24
  },
25
25
  "dependencies": {
26
- "graphql": "^16.14.2"
26
+ "graphql": "^16.14.2",
27
+ "yaml": "^2.9.1"
27
28
  },
28
29
  "devDependencies": {
29
- "ramdb2-core": "^2.0.0-alpha.0",
30
30
  "@vitejs/plugin-vue": "^6.0.10",
31
+ "ramdb2-core": "^2.0.0-alpha.2",
31
32
  "vite": "^8.3.5",
32
33
  "vue": "^3.5.44",
33
34
  "vue-router": "^5.4.0"
package/src/cli.js CHANGED
@@ -6,6 +6,12 @@ import { convert } from './convert.js'
6
6
  import { newProject } from './new.js'
7
7
  import { plantuml } from './plantuml.js'
8
8
  import { query } from './query.js'
9
+ import { readInfra } from './infra/config.js'
10
+ import { buildInfra } from './infra/build.js'
11
+ import { deploy } from './infra/deploy.js'
12
+ import { keygen } from './infra/keygen.js'
13
+ import { fetch as fetchBackup, listBackups, restore } from './infra/backups.js'
14
+ import { createInterface } from 'node:readline/promises'
9
15
 
10
16
  const HELP = `ramdb2-cli: code generator of ramdb2
11
17
 
@@ -35,11 +41,43 @@ const HELP = `ramdb2-cli: code generator of ramdb2
35
41
  --packages: the packages folder of a ramdb2 repository, to depend on it through file: paths
36
42
  (default: the packages from npm, at the version of this ramdb2-cli).
37
43
 
44
+ ramdb2-cli infra build [<server>...] [--config <infra.yaml>]
45
+ Writes build/<server>/ next to infra.yaml (all the servers by default): compose.yaml, the front nginx,
46
+ the Dockerfiles of each site. Generated, rewritten each time: keep build/ out of git.
47
+
48
+ ramdb2-cli infra deploy <server> [--staging] [--config <infra.yaml>]
49
+ build, then the secrets decrypted by sops (secrets/<site>.env, secrets/<server>.key), docker compose up
50
+ on the server (DOCKER_HOST=ssh://...), the missing Let's Encrypt certificates, and nginx reloaded.
51
+ --staging: the test certificates of Let's Encrypt, not trusted by browsers.
52
+
53
+ ramdb2-cli infra keygen <server> [--force]
54
+ Creates the SSH key of the server for its backups: secrets/<server>.key, encrypted by sops (.sops.yaml),
55
+ and secrets/<server>.pub, printed, to install on the backup server. --force replaces an existing key.
56
+
57
+ ramdb2-cli infra backups <site>
58
+ The backups of the site, oldest first, listed by its server.
59
+
60
+ ramdb2-cli infra restore <site> [<moment>] [--yes]
61
+ Stops the API and the backups of the site, restores the last backup at or before moment (UTC, e.g.
62
+ 2026-11-06T14:00; the most recent by default), checks it, starts them again. Asks first, unless --yes.
63
+
64
+ ramdb2-cli infra fetch <site> <file> [<moment>]
65
+ Writes the backup (the most recent, or the last at or before moment) to a local file, decompressed.
66
+
38
67
  ramdb2-cli convert --input <v1 preTypeDefs> --output <v2 preTypeDefs> [--force]
39
68
  Converts the v1 annotations ([PERSISTENT], [GROUP=], [FIELD=]) into directives, in a new file.
40
69
  `
41
70
 
42
71
  const commands = {
72
+ async infra(values, args) {
73
+ // The infra commands fail with messages for the user: no stack trace
74
+ try {
75
+ await infra(values, args)
76
+ } catch (error) {
77
+ console.error(error.message)
78
+ process.exitCode = 1
79
+ }
80
+ },
43
81
  new(values, [dir]) {
44
82
  if (dir === undefined) return usage('new needs the folder of the project')
45
83
  const withClient = values['with-client'] ?? false
@@ -90,6 +128,62 @@ function report({ written, kept, warnings }) {
90
128
  for (const warning of warnings) console.log(`warning ${warning}`)
91
129
  }
92
130
 
131
+ async function infra(values, [action, ...args]) {
132
+ const actions = ['build', 'deploy', 'keygen', 'backups', 'restore', 'fetch']
133
+ if (!actions.includes(action)) return usage(`infra needs an action: ${actions.join(', ')}`)
134
+ const infra = readInfra(values.config ?? 'infra.yaml')
135
+ switch (action) {
136
+ case 'build':
137
+ for (const { server, files } of buildInfra(infra, args)) {
138
+ console.log(`${server}: ${files.length} files`)
139
+ for (const path of files) console.log(`written ${path}`)
140
+ }
141
+ return
142
+ case 'deploy': {
143
+ if (args.length !== 1) return usage('infra deploy needs one server')
144
+ const { certificates } = await deploy(infra, args[0], { staging: values.staging ?? false })
145
+ if (certificates.some(certificate => !certificate.ok)) process.exitCode = 1
146
+ return
147
+ }
148
+ case 'keygen':
149
+ if (args.length !== 1) return usage('infra keygen needs one server')
150
+ await keygen(infra, args[0], { force: values.force ?? false })
151
+ return
152
+ case 'backups':
153
+ if (args.length !== 1) return usage('infra backups needs one site')
154
+ for (const name of await listBackups(infra, args[0])) console.log(name)
155
+ return
156
+ case 'restore': {
157
+ const [site, moment] = args
158
+ if (site === undefined || args.length > 2) return usage('infra restore needs a site, and maybe a moment')
159
+ const what = `the database of ${site} by its backup ${moment === undefined ? 'the most recent' : `at or before ${moment} (UTC)`}`
160
+ if (!values.yes && !(await confirm(`Replace ${what}? The site stops meanwhile. [y/N] `))) {
161
+ console.log('Nothing done')
162
+ return
163
+ }
164
+ await restore(infra, site, { moment })
165
+ return
166
+ }
167
+ case 'fetch': {
168
+ const [site, file, moment] = args
169
+ if (site === undefined || file === undefined || args.length > 3) return usage('infra fetch needs a site and a file')
170
+ await fetchBackup(infra, site, file, { moment })
171
+ console.log(`written ${file}`)
172
+ return
173
+ }
174
+ }
175
+ }
176
+
177
+ async function confirm(question) {
178
+ if (!process.stdin.isTTY) throw new Error('Not a terminal: add --yes to confirm')
179
+ const terminal = createInterface({ input: process.stdin, output: process.stdout })
180
+ try {
181
+ return /^(y|yes|o|oui)$/i.test((await terminal.question(question)).trim())
182
+ } finally {
183
+ terminal.close()
184
+ }
185
+ }
186
+
93
187
  function usage(message) {
94
188
  console.error(`${message}\n\n${HELP}`)
95
189
  process.exitCode = 1
@@ -107,6 +201,9 @@ const { values, positionals } = parseArgs({
107
201
  'with-client': { type: 'boolean' },
108
202
  variables: { type: 'string' },
109
203
  file: { type: 'string' },
204
+ config: { type: 'string' },
205
+ staging: { type: 'boolean' },
206
+ yes: { type: 'boolean', short: 'y' },
110
207
  help: { type: 'boolean', short: 'h' },
111
208
  },
112
209
  })
@@ -118,7 +215,7 @@ if (values.help || command === undefined) {
118
215
  try {
119
216
  await command(values, positionals.slice(1))
120
217
  } catch (error) {
121
- console.error(error.name === 'SchemaError' || error.message.startsWith('No profile') ? error.message : error)
218
+ console.error(['SchemaError', 'InfraError'].includes(error.name) || error.message.startsWith('No profile') ? error.message : error)
122
219
  process.exitCode = 1
123
220
  }
124
221
  }
package/src/index.js CHANGED
@@ -6,3 +6,8 @@ export { query, expiryOf } from './query.js'
6
6
  export { readSchema, SchemaError } from './model.js'
7
7
  export { DIRECTIVES } from './directives.js'
8
8
  export { operationNames } from './generate/operations.js'
9
+ export { readInfra, InfraError } from './infra/config.js'
10
+ export { buildInfra, serverFiles, certificates } from './infra/build.js'
11
+ export { deploy } from './infra/deploy.js'
12
+ export { keygen } from './infra/keygen.js'
13
+ export { listBackups, restore, fetch as fetchBackup } from './infra/backups.js'
@@ -0,0 +1,77 @@
1
+ // ramdb2-cli infra backups, restore and fetch (D56), from the deployment machine: ramdb2-server-backup run in the
2
+ // backup container of the site, on its server (DOCKER_HOST), with the key of the server. The deployment machine
3
+ // needs no access to the backup server.
4
+ import { renameSync, rmSync } from 'node:fs'
5
+ import { check, runCommand, serverOf, withServer } from './server.js'
6
+
7
+ const BACKUP = '/app/node_modules/.bin/ramdb2-server-backup'
8
+
9
+ /**
10
+ * The backups of a site, oldest first.
11
+ * @param {import('./config.js').Infra} infra
12
+ * @param {string} site
13
+ * @param {{run?: import('./server.js').Runner}} [options]
14
+ * @returns {Promise<string[]>}
15
+ */
16
+ export async function listBackups(infra, site, { run = runCommand } = {}) {
17
+ return withServer(infra, serverOf(infra, site), run, async ({ compose, env }) => {
18
+ const result = await run('docker', [...compose, 'run', '--rm', '--no-deps', '-T', '-e', 'LOG_LEVEL=warn',
19
+ `${site}-backup`, BACKUP, 'list'], { env, capture: true })
20
+ if (result.code !== 0) throw new Error(`The backups of ${site} could not be listed: ${result.stderr.trim()}`)
21
+ return result.stdout.split('\n').filter(line => line !== '')
22
+ })
23
+ }
24
+
25
+ /**
26
+ * Restores the last backup of a site at or before `moment` (UTC; the last one by default): stops its API and its
27
+ * backups, restores, starts them again, even when the restore failed (the database is then unchanged).
28
+ * @param {import('./config.js').Infra} infra
29
+ * @param {string} site
30
+ * @param {object} [options]
31
+ * @param {string} [options.moment]
32
+ * @param {import('./server.js').Runner} [options.run]
33
+ * @param {(line: string) => void} [options.print]
34
+ */
35
+ export async function restore(infra, site, { moment, run = runCommand, print = console.log } = {}) {
36
+ const server = serverOf(infra, site)
37
+ return withServer(infra, server, run, async ({ compose, env }) => {
38
+ const services = [`${site}-api`, `${site}-backup`]
39
+ print(`${site}: stopping ${services.join(', ')} on ${server}`)
40
+ await check(run('docker', [...compose, 'stop', ...services], { env }), 'docker compose stop')
41
+ let restored
42
+ try {
43
+ restored = await run('docker', [...compose, 'run', '--rm', '--no-deps', '-T', `${site}-backup`, BACKUP, 'restore',
44
+ ...(moment === undefined ? [] : [moment])], { env })
45
+ } finally {
46
+ print(`${site}: starting ${services.join(', ')}`)
47
+ await check(run('docker', [...compose, 'start', ...services], { env }), 'docker compose start')
48
+ }
49
+ if (restored.code !== 0) throw new Error(`The restore of ${site} failed: its database is unchanged`)
50
+ print(`${site}: restored`)
51
+ })
52
+ }
53
+
54
+ /**
55
+ * Writes a backup of a site to a local file, decompressed and checked: the last one at or before `moment`.
56
+ * @param {import('./config.js').Infra} infra
57
+ * @param {string} site
58
+ * @param {string} file
59
+ * @param {object} [options]
60
+ * @param {string} [options.moment]
61
+ * @param {import('./server.js').Runner} [options.run]
62
+ */
63
+ export async function fetch(infra, site, file, { moment, run = runCommand } = {}) {
64
+ return withServer(infra, serverOf(infra, site), run, async ({ compose, env }) => {
65
+ // The database comes on the standard output; the messages of the command go to the error output
66
+ const script = `${BACKUP} fetch /tmp/fetched.db "$@" >&2 && cat /tmp/fetched.db`
67
+ const part = `${file}.part`
68
+ try {
69
+ const result = await run('docker', [...compose, 'run', '--rm', '--no-deps', '-T', '-e', 'LOG_LEVEL=warn',
70
+ `${site}-backup`, 'sh', '-c', script, 'sh', ...(moment === undefined ? [] : [moment])], { env, stdout: part })
71
+ if (result.code !== 0) throw new Error(`The backup of ${site} could not be fetched`)
72
+ renameSync(part, file)
73
+ } finally {
74
+ rmSync(part, { force: true })
75
+ }
76
+ })
77
+ }
@@ -0,0 +1,372 @@
1
+ // ramdb2-cli infra build (D55): the files of each server, in build/<server>/, from infra.yaml.
2
+ // Nothing is mounted from the deployment machine (DOCKER_HOST=ssh://): what comes from it goes through a build
3
+ // context (nginx configuration, Dockerfiles) or the environment of docker compose (secrets).
4
+ import { copyFileSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
5
+ import { join, relative } from 'node:path'
6
+ import { stringify } from 'yaml'
7
+
8
+ const HEADER = 'Generated by ramdb2-cli infra build from infra.yaml: do not edit, it is rewritten.'
9
+ const LOGGING = { driver: 'local', options: { 'max-size': '10m', 'max-file': '5' } }
10
+ const API_PORT = 4000
11
+
12
+ /**
13
+ * Writes build/<server>/ for each server (or for the given ones), replacing what was there.
14
+ * @param {import('./config.js').Infra} infra
15
+ * @param {string[]} [only] server names
16
+ * @returns {{server: string, dir: string, files: string[]}[]}
17
+ */
18
+ export function buildInfra(infra, only) {
19
+ const servers = only === undefined || only.length === 0 ? infra.servers : only.map(name => {
20
+ const server = infra.servers.find(candidate => candidate.name === name)
21
+ if (server === undefined) throw new Error(`No server ${name} in infra.yaml`)
22
+ return server
23
+ })
24
+ return servers.map(server => {
25
+ const dir = join(infra.dir, 'build', server.name)
26
+ rmSync(dir, { recursive: true, force: true })
27
+ const files = []
28
+ const write = (path, content) => {
29
+ const file = join(dir, path)
30
+ mkdirSync(join(file, '..'), { recursive: true })
31
+ writeFileSync(file, content, path.endsWith('.sh') ? { mode: 0o755 } : undefined)
32
+ files.push(file)
33
+ }
34
+ for (const [path, content] of Object.entries(serverFiles(infra, server, dir))) write(path, content)
35
+ for (const custom of server.custom) {
36
+ const file = join(dir, 'nginx', 'conf.d', `50-custom-${custom.name}.conf`)
37
+ copyFileSync(custom.nginx, file)
38
+ files.push(file)
39
+ }
40
+ return { server: server.name, dir, files }
41
+ })
42
+ }
43
+
44
+ /**
45
+ * The generated files of one server, by path in build/<server>/ (the custom nginx files are copied apart).
46
+ * @param {import('./config.js').Infra} infra
47
+ * @param {import('./config.js').Server} server
48
+ * @param {string} dir build/<server>/
49
+ * @returns {Record<string, string>}
50
+ */
51
+ export function serverFiles(infra, server, dir) {
52
+ const files = {
53
+ 'compose.yaml': compose(infra, server, dir),
54
+ 'certificates.json': JSON.stringify(certificates(server), null, 2) + '\n',
55
+ 'nginx/Dockerfile': FRONT_DOCKERFILE,
56
+ 'nginx/ramdb2-certs.sh': CERTS_SCRIPT,
57
+ 'nginx/certificates': certificates(server).map(certificate => certificate.name).join('\n') + '\n',
58
+ 'nginx/conf.d/00-ramdb2.conf': FRONT_DEFAULTS,
59
+ }
60
+ for (const site of server.sites) {
61
+ files[`nginx/conf.d/10-${site.name}.conf`] = siteNginx(site)
62
+ files[`${site.name}/api.Dockerfile`] = apiDockerfile(site)
63
+ files[`${site.name}/api.Dockerfile.dockerignore`] = DOCKERIGNORE
64
+ if (site.withClient) {
65
+ files[`${site.name}/web.Dockerfile`] = WEB_DOCKERFILE
66
+ files[`${site.name}/web.Dockerfile.dockerignore`] = DOCKERIGNORE
67
+ files[`${site.name}/web.nginx.conf`] = WEB_NGINX
68
+ }
69
+ }
70
+ return files
71
+ }
72
+
73
+ /**
74
+ * The certificates of a server: one per ramdb2 site (its domain and aliases), one per custom site with domains.
75
+ * The certificate is named after its first domain, its folder in /etc/letsencrypt/live/ and /etc/nginx/certs/.
76
+ * @param {import('./config.js').Server} server
77
+ * @returns {{name: string, domains: string[]}[]}
78
+ */
79
+ export function certificates(server) {
80
+ return [
81
+ ...server.sites.map(site => ({ name: site.domain, domains: [site.domain, ...site.aliases] })),
82
+ ...server.custom.filter(custom => custom.domains.length > 0).map(custom => ({ name: custom.domains[0], domains: custom.domains })),
83
+ ]
84
+ }
85
+
86
+ function compose(infra, server, dir) {
87
+ const services = {
88
+ nginx: {
89
+ build: './nginx',
90
+ ports: ['80:80', '443:443'],
91
+ volumes: ['letsencrypt:/etc/letsencrypt:ro', 'certbot-www:/var/www/certbot:ro'],
92
+ restart: 'unless-stopped',
93
+ logging: LOGGING,
94
+ },
95
+ certbot: {
96
+ image: 'certbot/certbot',
97
+ entrypoint: ['/bin/sh', '-c',
98
+ 'trap exit TERM; while :; do certbot renew --webroot -w /var/www/certbot --quiet; sleep 12h & wait $${!}; done'],
99
+ volumes: ['letsencrypt:/etc/letsencrypt', 'certbot-www:/var/www/certbot'],
100
+ restart: 'unless-stopped',
101
+ logging: LOGGING,
102
+ },
103
+ }
104
+ const volumes = { letsencrypt: {}, 'certbot-www': {} }
105
+ for (const site of server.sites) {
106
+ const context = relative(dir, site.source) || '.'
107
+ const dockerfile = join(dir, site.name, 'api.Dockerfile')
108
+ const database = `/data/${site.name}.db`
109
+ services[`${site.name}-api`] = {
110
+ build: { context, dockerfile, target: 'api' },
111
+ env_file: [`\${RAMDB2_SECRETS:?run ramdb2-cli infra deploy}/${site.name}.env`],
112
+ environment: { PORT: String(API_PORT), DB_PATH: database, NODE_ENV: 'production', TRUST_PROXY: '1' },
113
+ volumes: [`${site.name}-data:/data`],
114
+ ...(site.memory === undefined ? {} : { mem_limit: site.memory }),
115
+ stop_grace_period: '30s',
116
+ healthcheck: {
117
+ test: ['CMD', 'node', '-e', HEALTH_CHECK],
118
+ interval: '30s',
119
+ timeout: '5s',
120
+ retries: 3,
121
+ start_period: '60s',
122
+ },
123
+ restart: 'unless-stopped',
124
+ logging: LOGGING,
125
+ }
126
+ services[`${site.name}-backup`] = {
127
+ build: { context, dockerfile, target: 'backup' },
128
+ environment: {
129
+ DB_PATH: database,
130
+ BACKUP_TARGET: infra.backup.target,
131
+ BACKUP_SITE: site.name,
132
+ BACKUP_SSH_KEY: '${BACKUP_SSH_KEY:?run ramdb2-cli infra deploy}',
133
+ },
134
+ volumes: [`${site.name}-data:/data`],
135
+ stop_grace_period: '2m',
136
+ restart: 'unless-stopped',
137
+ logging: LOGGING,
138
+ }
139
+ if (site.withClient) {
140
+ services[`${site.name}-web`] = {
141
+ build: {
142
+ context,
143
+ dockerfile: join(dir, site.name, 'web.Dockerfile'),
144
+ additional_contexts: { infra: `./${site.name}` },
145
+ },
146
+ restart: 'unless-stopped',
147
+ logging: LOGGING,
148
+ }
149
+ }
150
+ volumes[`${site.name}-data`] = {}
151
+ }
152
+ const include = server.custom.filter(custom => custom.compose !== undefined).map(custom => relative(dir, custom.compose))
153
+ const document = { name: server.name, ...(include.length > 0 ? { include } : {}), services, volumes }
154
+ return `# ${HEADER}\n# Server ${server.name}: deploy with ramdb2-cli infra deploy ${server.name}\n`
155
+ + stringify(document, { lineWidth: 0, aliasDuplicateObjects: false })
156
+ }
157
+
158
+ // The GraphQL query health of the project (ramdb2-cli new), answered by the API itself
159
+ const HEALTH_CHECK = `fetch('http://127.0.0.1:${API_PORT}/', { method: 'POST', headers: { 'content-type': 'application/json' }, `
160
+ + `body: '{"query":"{ health }"}' }).then(response => response.json())`
161
+ + `.then(body => process.exit(body.data?.health?.open ? 0 : 1), () => process.exit(1))`
162
+
163
+ function siteNginx(site) {
164
+ const cert = ` ssl_certificate /etc/nginx/certs/${site.domain}/fullchain.pem;\n`
165
+ + ` ssl_certificate_key /etc/nginx/certs/${site.domain}/privkey.pem;\n`
166
+ const proxy = ' proxy_set_header Host $host;\n'
167
+ + ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n'
168
+ + ' proxy_set_header X-Forwarded-Proto $scheme;\n'
169
+ const aliases = site.aliases.length === 0 ? '' : `
170
+ # The aliases: redirected to ${site.domain}
171
+ server {
172
+ listen 443 ssl;
173
+ http2 on;
174
+ server_name ${site.aliases.join(' ')};
175
+ ${cert} return 301 https://${site.domain}$request_uri;
176
+ }
177
+ `
178
+ const locations = site.withClient ? ` # The API (the server answers on /, as with the proxy of Vite in development)
179
+ location = /graphql {
180
+ rewrite ^ / break;
181
+ proxy_pass $api;
182
+ ${proxy} }
183
+
184
+ # The web site
185
+ location / {
186
+ proxy_pass $web;
187
+ ${proxy} }
188
+ ` : ` # The API
189
+ location / {
190
+ proxy_pass $api;
191
+ ${proxy} }
192
+ `
193
+ return `# ${HEADER}
194
+ # Site ${site.name}
195
+
196
+ server {
197
+ listen 80;
198
+ server_name ${[site.domain, ...site.aliases].join(' ')};
199
+
200
+ location /.well-known/acme-challenge/ {
201
+ root /var/www/certbot;
202
+ }
203
+
204
+ location / {
205
+ return 301 https://${site.domain}$request_uri;
206
+ }
207
+ }
208
+ ${aliases}
209
+ server {
210
+ listen 443 ssl;
211
+ http2 on;
212
+ server_name ${site.domain};
213
+ ${cert} add_header Strict-Transport-Security "max-age=31536000" always;
214
+ client_max_body_size 10m;
215
+
216
+ # Resolved at each request: nginx starts even when a container of the site is down
217
+ set $api http://${site.name}-api:${API_PORT};
218
+ ${site.withClient ? ` set $web http://${site.name}-web:80;\n` : ''}
219
+ ${locations}}
220
+ `
221
+ }
222
+
223
+ function apiDockerfile(site) {
224
+ const install = site.withClient
225
+ ? `COPY package.json package-lock.json ./
226
+ COPY server/package.json server/
227
+ COPY web/package.json web/
228
+ RUN npm ci --omit=dev --ignore-scripts -w server
229
+ COPY server/ server/
230
+ WORKDIR /app/server`
231
+ : `COPY package.json package-lock.json ./
232
+ RUN npm ci --omit=dev --ignore-scripts
233
+ COPY . .`
234
+ return `# syntax=docker/dockerfile:1
235
+ # ${HEADER}
236
+ # Site ${site.name}: the API (target api) and its backups (target backup), on the same image.
237
+
238
+ FROM node:22-alpine AS api
239
+ WORKDIR /app
240
+ # --ignore-scripts: better-sqlite3 ships its compiled binaries (musl, x64 and arm64), found when it loads; without
241
+ # it, npm would compile it again (node-gyp, which needs Python and a compiler)
242
+ ${install}
243
+ # The database volume, owned by the user that runs the server
244
+ RUN mkdir -p /data && chown node:node /data
245
+ USER node
246
+ ENV NODE_ENV=production
247
+ # node itself, not npm: SIGTERM reaches the server, which closes SQLite
248
+ CMD ["node", "src/index.js"]
249
+
250
+ FROM api AS backup
251
+ USER root
252
+ RUN apk add --no-cache openssh-client
253
+ USER node
254
+ CMD ["/app/node_modules/.bin/ramdb2-server-backup", "schedule"]
255
+ `
256
+ }
257
+
258
+ const WEB_DOCKERFILE = `# syntax=docker/dockerfile:1
259
+ # ${HEADER}
260
+ # The web site: compiled by Vite, then served by nginx.
261
+
262
+ FROM node:22-alpine AS build
263
+ WORKDIR /app
264
+ COPY package.json package-lock.json ./
265
+ COPY server/package.json server/
266
+ COPY web/package.json web/
267
+ RUN npm ci --ignore-scripts -w web
268
+ COPY web/ web/
269
+ RUN npm run build -w web
270
+
271
+ FROM nginx:alpine
272
+ RUN rm /etc/nginx/conf.d/default.conf
273
+ COPY --from=infra web.nginx.conf /etc/nginx/conf.d/web.conf
274
+ COPY --from=build /app/web/dist /usr/share/nginx/html
275
+ `
276
+
277
+ const WEB_NGINX = `# ${HEADER}
278
+ server {
279
+ listen 80;
280
+ root /usr/share/nginx/html;
281
+
282
+ # The pages of the Vue router: index.html, never cached, so that a new version is seen at once
283
+ location / {
284
+ try_files $uri $uri/ /index.html;
285
+ add_header Cache-Control "no-cache";
286
+ }
287
+
288
+ # The compiled files carry a hash in their name: cached for a year
289
+ location /assets/ {
290
+ try_files $uri =404;
291
+ add_header Cache-Control "public, max-age=31536000, immutable";
292
+ }
293
+ }
294
+ `
295
+
296
+ const DOCKERIGNORE = `# ${HEADER}
297
+ # Never in an image: the dependencies (installed by npm ci), the databases, the backups, the secrets.
298
+ **/node_modules
299
+ data
300
+ backups
301
+ server/data
302
+ server/backups
303
+ dist
304
+ web/dist
305
+ .env
306
+ .env.*
307
+ **/.env
308
+ **/.env.*
309
+ .git
310
+ **/test-results
311
+ **/playwright-report
312
+ `
313
+
314
+ const FRONT_DOCKERFILE = `# syntax=docker/dockerfile:1
315
+ # ${HEADER}
316
+ # The front nginx: HTTPS, one domain per site. The configuration is in the image (nothing mounted).
317
+
318
+ FROM nginx:alpine
319
+ RUN apk add --no-cache openssl && rm /etc/nginx/conf.d/default.conf
320
+ COPY --chmod=755 ramdb2-certs.sh /usr/local/bin/ramdb2-certs
321
+ COPY certificates /etc/nginx/ramdb2-certificates
322
+ COPY conf.d/ /etc/nginx/conf.d/
323
+ # The certificates linked, then a reload every 6 hours, for the certificates renewed by certbot
324
+ CMD ["/bin/sh", "-c", "ramdb2-certs || exit 1; (while :; do sleep 6h; ramdb2-certs && nginx -s reload; done) & exec nginx -g 'daemon off;'"]
325
+ `
326
+
327
+ const CERTS_SCRIPT = `#!/bin/sh
328
+ # ${HEADER}
329
+ # Points /etc/nginx/certs/<name> to the Let's Encrypt certificate <name>, or, until certbot has it, to a self-signed
330
+ # one: nginx can start before the first certificate.
331
+ set -e
332
+ mkdir -p /etc/nginx/certs /etc/nginx/self-signed
333
+ while read -r name; do
334
+ [ -n "$name" ] || continue
335
+ if [ -f "/etc/letsencrypt/live/$name/fullchain.pem" ]; then
336
+ ln -sfn "/etc/letsencrypt/live/$name" "/etc/nginx/certs/$name"
337
+ else
338
+ if [ ! -f "/etc/nginx/self-signed/$name/fullchain.pem" ]; then
339
+ mkdir -p "/etc/nginx/self-signed/$name"
340
+ openssl req -x509 -nodes -newkey rsa:2048 -days 30 -subj "/CN=$name" \\
341
+ -keyout "/etc/nginx/self-signed/$name/privkey.pem" -out "/etc/nginx/self-signed/$name/fullchain.pem" 2>/dev/null
342
+ fi
343
+ ln -sfn "/etc/nginx/self-signed/$name" "/etc/nginx/certs/$name"
344
+ fi
345
+ done < /etc/nginx/ramdb2-certificates
346
+ `
347
+
348
+ const FRONT_DEFAULTS = `# ${HEADER}
349
+ server_tokens off;
350
+ # The DNS of Docker: the containers are found by their name, at each request
351
+ resolver 127.0.0.11 valid=10s ipv6=off;
352
+ gzip on;
353
+ gzip_types text/css application/javascript application/json image/svg+xml;
354
+
355
+ # An unknown domain: no answer
356
+ server {
357
+ listen 80 default_server;
358
+
359
+ location /.well-known/acme-challenge/ {
360
+ root /var/www/certbot;
361
+ }
362
+
363
+ location / {
364
+ return 444;
365
+ }
366
+ }
367
+
368
+ server {
369
+ listen 443 ssl default_server;
370
+ ssl_reject_handshake on;
371
+ }
372
+ `
@@ -0,0 +1,165 @@
1
+ // infra.yaml (D55): the backups, the servers and their sites. Read, checked, with every path made absolute.
2
+ import { existsSync, readFileSync } from 'node:fs'
3
+ import { dirname, join, resolve } from 'node:path'
4
+ import { parse } from 'yaml'
5
+
6
+ const NAME = /^[a-z][a-z0-9-]*$/
7
+ const DOMAIN = /^(?=.{1,253}$)([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/
8
+ const MEMORY = /^\d+[kmg]$/
9
+
10
+ export class InfraError extends Error {
11
+ constructor(errors) {
12
+ super(`infra.yaml is not valid:\n${errors.map(error => ` - ${error}`).join('\n')}`)
13
+ this.name = 'InfraError'
14
+ this.errors = errors
15
+ }
16
+ }
17
+
18
+ /**
19
+ * @typedef {object} Site
20
+ * @property {string} name unique on all the servers
21
+ * @property {string} source the absolute path of the ramdb2 project
22
+ * @property {boolean} withClient a project of `new --with-client`: server/ and web/
23
+ * @property {string} domain
24
+ * @property {string[]} aliases
25
+ * @property {string | undefined} memory
26
+ *
27
+ * @typedef {object} Custom a site maintained by hand
28
+ * @property {string} name
29
+ * @property {string} nginx the absolute path of its nginx configuration
30
+ * @property {string | undefined} compose the absolute path of its compose file, if any
31
+ * @property {string[]} domains the domains whose certificates certbot keeps
32
+ *
33
+ * @typedef {object} Server
34
+ * @property {string} name
35
+ * @property {string} host
36
+ * @property {string} email
37
+ * @property {Site[]} sites
38
+ * @property {Custom[]} custom
39
+ *
40
+ * @typedef {object} Infra
41
+ * @property {string} dir the folder of infra.yaml
42
+ * @property {{target: string}} backup
43
+ * @property {Server[]} servers
44
+ */
45
+
46
+ /**
47
+ * Reads and checks infra.yaml. All the errors are reported at once.
48
+ * @param {string} file
49
+ * @returns {Infra}
50
+ */
51
+ export function readInfra(file) {
52
+ const dir = dirname(resolve(file))
53
+ let raw
54
+ try {
55
+ raw = parse(readFileSync(file, 'utf8'))
56
+ } catch (error) {
57
+ throw new InfraError([error.code === 'ENOENT' ? `${file} not found` : error.message])
58
+ }
59
+ const errors = []
60
+ const check = (condition, message) => {
61
+ if (!condition) errors.push(message)
62
+ return condition
63
+ }
64
+ if (!check(isObject(raw), 'expected an object with backup and servers')) throw new InfraError(errors)
65
+ unknownKeys(raw, ['backup', 'servers'], '', errors)
66
+
67
+ const backup = { target: raw.backup?.target }
68
+ if (check(isObject(raw.backup), 'backup: required, with target')) {
69
+ unknownKeys(raw.backup, ['target'], 'backup.', errors)
70
+ check(typeof backup.target === 'string' && /^sftp:\/\/[^@/]+@[^/]+(\/.*)?$/.test(backup.target),
71
+ 'backup.target: sftp://user@host[:port]/path')
72
+ }
73
+
74
+ const servers = []
75
+ const siteNames = new Map()
76
+ const domains = new Map()
77
+ const claim = (domain, owner) => {
78
+ if (domains.has(domain)) errors.push(`${owner}: ${domain} is already used by ${domains.get(domain)}`)
79
+ else domains.set(domain, owner)
80
+ }
81
+ if (check(isObject(raw.servers) && Object.keys(raw.servers).length > 0, 'servers: at least one server')) {
82
+ for (const [name, server] of Object.entries(raw.servers)) {
83
+ const at = `servers.${name}`
84
+ if (!check(NAME.test(name), `${at}: a name in lowercase letters, digits and dashes`)) continue
85
+ if (!check(isObject(server), `${at}: expected host, email and sites`)) continue
86
+ unknownKeys(server, ['host', 'email', 'sites', 'custom'], `${at}.`, errors)
87
+ check(typeof server.host === 'string' && /^ssh:\/\/[^/]+$/.test(server.host), `${at}.host: ssh://user@host[:port]`)
88
+ check(typeof server.email === 'string' && /^[^@\s]+@[^@\s]+$/.test(server.email), `${at}.email: the Let's Encrypt account`)
89
+ const sites = []
90
+ for (const [siteName, site] of Object.entries(server.sites ?? {})) {
91
+ const siteAt = `${at}.sites.${siteName}`
92
+ if (!check(NAME.test(siteName), `${siteAt}: a name in lowercase letters, digits and dashes`)) continue
93
+ if (siteNames.has(siteName)) {
94
+ errors.push(`${siteAt}: the name is already used on ${siteNames.get(siteName)} (a site name is unique on all the servers)`)
95
+ continue
96
+ }
97
+ siteNames.set(siteName, `servers.${name}`)
98
+ if (!check(isObject(site), `${siteAt}: expected source and domain`)) continue
99
+ unknownKeys(site, ['source', 'domain', 'aliases', 'memory'], `${siteAt}.`, errors)
100
+ const source = typeof site.source === 'string' ? resolve(dir, site.source) : undefined
101
+ let withClient = false
102
+ if (check(source !== undefined, `${siteAt}.source: the folder of the ramdb2 project`)) {
103
+ const pkg = readPackage(join(source, 'package.json'))
104
+ if (check(pkg !== null, `${siteAt}.source: no package.json in ${source}`)) {
105
+ withClient = Array.isArray(pkg.workspaces) && pkg.workspaces.includes('server') && pkg.workspaces.includes('web')
106
+ const lock = existsSync(join(source, 'package-lock.json'))
107
+ check(lock, `${siteAt}.source: no package-lock.json in ${source} (run npm install there)`)
108
+ }
109
+ }
110
+ const aliases = site.aliases ?? []
111
+ if (check(typeof site.domain === 'string' && DOMAIN.test(site.domain), `${siteAt}.domain: a domain name in lowercase`)) {
112
+ claim(site.domain, siteAt)
113
+ }
114
+ if (check(Array.isArray(aliases) && aliases.every(alias => typeof alias === 'string' && DOMAIN.test(alias)),
115
+ `${siteAt}.aliases: a list of domain names in lowercase`)) {
116
+ for (const alias of aliases) claim(alias, siteAt)
117
+ }
118
+ check(site.memory === undefined || (typeof site.memory === 'string' && MEMORY.test(site.memory)),
119
+ `${siteAt}.memory: a size, e.g. 512m or 2g`)
120
+ sites.push({ name: siteName, source, withClient, domain: site.domain, aliases, memory: site.memory })
121
+ }
122
+ const custom = []
123
+ if (check(server.custom === undefined || Array.isArray(server.custom), `${at}.custom: a list`)) {
124
+ for (const [index, entry] of (server.custom ?? []).entries()) {
125
+ const customAt = `${at}.custom[${index}]`
126
+ const item = typeof entry === 'string' ? { name: entry } : entry
127
+ if (!check(isObject(item) && typeof item.name === 'string' && NAME.test(item.name),
128
+ `${customAt}: a name, or { name, domains }`)) continue
129
+ unknownKeys(item, ['name', 'domains'], `${customAt}.`, errors)
130
+ const nginx = join(dir, 'custom', `${item.name}.nginx.conf`)
131
+ const compose = join(dir, 'custom', `${item.name}.compose.yaml`)
132
+ check(existsSync(nginx), `${customAt}: custom/${item.name}.nginx.conf not found`)
133
+ const customDomains = item.domains ?? []
134
+ if (check(Array.isArray(customDomains) && customDomains.every(domain => typeof domain === 'string' && DOMAIN.test(domain)),
135
+ `${customAt}.domains: a list of domain names in lowercase`)) {
136
+ for (const domain of customDomains) claim(domain, customAt)
137
+ }
138
+ custom.push({ name: item.name, nginx, compose: existsSync(compose) ? compose : undefined, domains: customDomains })
139
+ }
140
+ }
141
+ check(sites.length + custom.length > 0, `${at}: no site`)
142
+ servers.push({ name, host: server.host, email: server.email, sites, custom })
143
+ }
144
+ }
145
+ if (errors.length > 0) throw new InfraError(errors)
146
+ return { dir, backup, servers }
147
+ }
148
+
149
+ function isObject(value) {
150
+ return value !== null && typeof value === 'object' && !Array.isArray(value)
151
+ }
152
+
153
+ function unknownKeys(object, known, at, errors) {
154
+ for (const key of Object.keys(object)) {
155
+ if (!known.includes(key)) errors.push(`${at}${key}: unknown key (expected ${known.join(', ')})`)
156
+ }
157
+ }
158
+
159
+ function readPackage(file) {
160
+ try {
161
+ return JSON.parse(readFileSync(file, 'utf8'))
162
+ } catch {
163
+ return null
164
+ }
165
+ }
@@ -0,0 +1,41 @@
1
+ // ramdb2-cli infra deploy (D55): build, decrypt the secrets with sops, docker compose up on the server through
2
+ // DOCKER_HOST=ssh://, then the Let's Encrypt certificates, and nginx reloaded.
3
+ import { readFileSync } from 'node:fs'
4
+ import { join } from 'node:path'
5
+ import { check, runCommand, withServer } from './server.js'
6
+
7
+ /**
8
+ * Deploys one server.
9
+ * @param {import('./config.js').Infra} infra
10
+ * @param {string} name the server
11
+ * @param {object} [options]
12
+ * @param {boolean} [options.staging] the staging certificates of Let's Encrypt (not trusted by browsers, no rate limit)
13
+ * @param {import('./server.js').Runner} [options.run]
14
+ * @param {(line: string) => void} [options.print]
15
+ * @returns {Promise<{certificates: {name: string, ok: boolean}[]}>}
16
+ */
17
+ export function deploy(infra, name, { staging = false, run = runCommand, print = console.log } = {}) {
18
+ return withServer(infra, name, run, async ({ server, dir, compose, env }) => {
19
+ print(`${name}: files written in ${dir}`)
20
+ print(`${name}: docker compose up on ${server.host}`)
21
+ await check(run('docker', [...compose, 'up', '-d', '--build', '--remove-orphans'], { env }), 'docker compose up')
22
+
23
+ const certificates = []
24
+ for (const certificate of JSON.parse(readFileSync(join(dir, 'certificates.json'), 'utf8'))) {
25
+ print(`${name}: certificate ${certificate.name} (${certificate.domains.join(', ')})`)
26
+ const result = await run('docker', [...compose, 'run', '--rm', '--entrypoint', 'certbot', 'certbot',
27
+ 'certonly', '--webroot', '-w', '/var/www/certbot', '--cert-name', certificate.name,
28
+ ...certificate.domains.flatMap(domain => ['-d', domain]),
29
+ '--email', server.email, '--agree-tos', '--no-eff-email', '--non-interactive', '--keep-until-expiring',
30
+ ...(staging ? ['--staging'] : [])], { env })
31
+ if (result.code !== 0) print(`${name}: certificate ${certificate.name} not obtained: do its domains point to the server?`)
32
+ certificates.push({ name: certificate.name, ok: result.code === 0 })
33
+ }
34
+ if (certificates.length > 0) {
35
+ await check(run('docker', [...compose, 'exec', '-T', 'nginx', 'sh', '-c', 'ramdb2-certs && nginx -s reload'], { env }),
36
+ 'nginx reload')
37
+ }
38
+ print(`${name}: deployed`)
39
+ return { certificates }
40
+ })
41
+ }
@@ -0,0 +1,54 @@
1
+ // ramdb2-cli infra keygen (D56): the SSH key of a server for its backups, encrypted by sops in secrets/<server>.key,
2
+ // its public half in secrets/<server>.pub, to install on the backup server.
3
+ import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
4
+ import { tmpdir } from 'node:os'
5
+ import { join } from 'node:path'
6
+ import { runCommand } from './server.js'
7
+
8
+ /**
9
+ * Creates the backup key of a server. An existing key is replaced only with `force`.
10
+ * @param {import('./config.js').Infra} infra
11
+ * @param {string} name the server
12
+ * @param {object} [options]
13
+ * @param {boolean} [options.force]
14
+ * @param {import('./server.js').Runner} [options.run]
15
+ * @param {(line: string) => void} [options.print]
16
+ * @returns {Promise<{publicKey: string, files: string[]}>}
17
+ */
18
+ export async function keygen(infra, name, { force = false, run = runCommand, print = console.log } = {}) {
19
+ if (!infra.servers.some(server => server.name === name)) throw new Error(`No server ${name} in infra.yaml`)
20
+ const key = join(infra.dir, 'secrets', `${name}.key`)
21
+ const pub = join(infra.dir, 'secrets', `${name}.pub`)
22
+ if (existsSync(key) && !force) {
23
+ throw new Error(`secrets/${name}.key exists: --force replaces it (then install the new public key on the backup server)`)
24
+ }
25
+ if (!existsSync(join(infra.dir, '.sops.yaml'))) {
26
+ throw new Error('No .sops.yaml in the infra folder: it gives the age keys that encrypt secrets/ (see the README of ramdb2-cli)')
27
+ }
28
+ const work = mkdtempSync(join(tmpdir(), 'ramdb2-keygen-'))
29
+ chmodSync(work, 0o700)
30
+ try {
31
+ const comment = `ramdb2-backup-${name}`
32
+ const generated = await run('ssh-keygen', ['-q', '-t', 'ed25519', '-N', '', '-C', comment, '-f', join(work, 'key')], { capture: true })
33
+ if (generated.code !== 0) throw new Error(`ssh-keygen failed: ${generated.stderr.trim()}`)
34
+ // The rules of .sops.yaml are matched on the input file name: the final name is given instead
35
+ const encrypted = await run('sops', ['--encrypt', '--input-type', 'binary', '--output-type', 'binary',
36
+ '--filename-override', `secrets/${name}.key`, join(work, 'key')], { capture: true, cwd: infra.dir })
37
+ if (encrypted.code !== 0) throw new Error(`sops could not encrypt the key: ${encrypted.stderr.trim()}`)
38
+ mkdirSync(join(infra.dir, 'secrets'), { recursive: true })
39
+ writeFileSync(`${key}.part`, encrypted.stdout, { mode: 0o600 })
40
+ renameSync(`${key}.part`, key)
41
+ const publicKey = readFileSync(join(work, 'key.pub'), 'utf8').trim()
42
+ writeFileSync(pub, publicKey + '\n')
43
+ print(`written ${key} (encrypted by sops)`)
44
+ print(`written ${pub}`)
45
+ print(`\nThe public key of ${name}, for ~backup/.ssh/authorized_keys on the backup server (restrict: no shell,`)
46
+ print('no forwarding; the SFTP-only access comes from the sshd configuration, see the README of ramdb2-cli):\n')
47
+ print(`restrict ${publicKey}`)
48
+ print(`\nThen: ramdb2-cli infra deploy ${name}`)
49
+ return { publicKey, files: [key, pub] }
50
+ } finally {
51
+ rmSync(work, { recursive: true, force: true })
52
+ rmSync(`${key}.part`, { force: true })
53
+ }
54
+ }
@@ -0,0 +1,106 @@
1
+ // What the infra commands that talk to a server share: the files built, the secrets decrypted by sops into a
2
+ // private temporary folder, the environment of docker compose (DOCKER_HOST and the secrets) and the commands run.
3
+ import { spawn } from 'node:child_process'
4
+ import { chmodSync, createWriteStream, existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
5
+ import { join } from 'node:path'
6
+ import { buildInfra } from './build.js'
7
+
8
+ /**
9
+ * Runs a command; its output goes to the terminal unless `capture`. `stdout` may be a file, for binary output.
10
+ * @typedef {(command: string, args: string[], options?: {env?: Record<string, string>, capture?: boolean,
11
+ * cwd?: string, stdout?: string}) => Promise<{code: number, stdout: string, stderr: string}>} Runner
12
+ */
13
+
14
+ /**
15
+ * @typedef {object} Connection
16
+ * @property {import('./config.js').Server} server
17
+ * @property {string} dir build/<server>/
18
+ * @property {string[]} compose `compose -f build/<server>/compose.yaml`, for docker
19
+ * @property {Record<string, string>} env DOCKER_HOST, RAMDB2_SECRETS and BACKUP_SSH_KEY
20
+ */
21
+
22
+ /**
23
+ * Builds the files of a server, decrypts its secrets, runs `action`, then deletes the decrypted secrets.
24
+ * Stops before `action` when a secret is missing or cannot be decrypted.
25
+ * @template T
26
+ * @param {import('./config.js').Infra} infra
27
+ * @param {string} name the server
28
+ * @param {Runner} run
29
+ * @param {(connection: Connection) => Promise<T>} action
30
+ * @returns {Promise<T>}
31
+ */
32
+ export async function withServer(infra, name, run, action) {
33
+ const [{ dir }] = buildInfra(infra, [name])
34
+ const server = infra.servers.find(candidate => candidate.name === name)
35
+ const missing = server.sites.map(site => `secrets/${site.name}.env`)
36
+ .concat(server.sites.length > 0 ? [`secrets/${name}.key`] : [])
37
+ .filter(path => !existsSync(join(infra.dir, path)))
38
+ if (missing.length > 0) {
39
+ throw new Error(`Missing secrets, encrypted with sops: ${missing.join(', ')}`
40
+ + (missing.includes(`secrets/${name}.key`) ? ` (ramdb2-cli infra keygen ${name} creates the key)` : ''))
41
+ }
42
+ // In build/<server>/, not in the system temporary folder: Docker installed as a snap cannot read /tmp
43
+ const secrets = mkdtempSync(join(dir, '.secrets-'))
44
+ chmodSync(secrets, 0o700)
45
+ try {
46
+ const env = { DOCKER_HOST: server.host, RAMDB2_SECRETS: secrets, BACKUP_SSH_KEY: '' }
47
+ for (const site of server.sites) {
48
+ const values = await decrypt(run, join(infra.dir, 'secrets', `${site.name}.env`), 'dotenv')
49
+ writeFileSync(join(secrets, `${site.name}.env`), values, { mode: 0o600 })
50
+ }
51
+ if (server.sites.length > 0) env.BACKUP_SSH_KEY = await decrypt(run, join(infra.dir, 'secrets', `${name}.key`), 'binary')
52
+ return await action({ server, dir, compose: ['compose', '-f', join(dir, 'compose.yaml')], env })
53
+ } finally {
54
+ rmSync(secrets, { recursive: true, force: true })
55
+ }
56
+ }
57
+
58
+ /**
59
+ * The server of a site: a site name is unique on all the servers.
60
+ * @param {import('./config.js').Infra} infra
61
+ * @param {string} site
62
+ * @returns {string}
63
+ */
64
+ export function serverOf(infra, site) {
65
+ const server = infra.servers.find(candidate => candidate.sites.some(entry => entry.name === site))
66
+ if (server === undefined) throw new Error(`No ramdb2 site ${site} in infra.yaml`)
67
+ return server.name
68
+ }
69
+
70
+ async function decrypt(run, file, type) {
71
+ const result = await run('sops', ['--decrypt', '--input-type', type, '--output-type', type, file], { capture: true })
72
+ if (result.code !== 0) throw new Error(`sops could not decrypt ${file}: ${result.stderr.trim()}`)
73
+ return result.stdout
74
+ }
75
+
76
+ /**
77
+ * Fails with `what` when the command failed.
78
+ * @param {Promise<{code: number}>} promise
79
+ * @param {string} what
80
+ */
81
+ export async function check(promise, what) {
82
+ const result = await promise
83
+ if (result.code !== 0) throw new Error(`${what} failed (exit code ${result.code})`)
84
+ return result
85
+ }
86
+
87
+ /** @type {Runner} */
88
+ export function runCommand(command, args, { env = {}, capture = false, cwd, stdout: outputFile } = {}) {
89
+ return new Promise((resolve, reject) => {
90
+ const child = spawn(command, args, {
91
+ env: { ...process.env, ...env },
92
+ cwd,
93
+ stdio: ['inherit', capture || outputFile !== undefined ? 'pipe' : 'inherit', capture ? 'pipe' : 'inherit'],
94
+ })
95
+ let stdout = ''
96
+ let stderr = ''
97
+ // With an output file, the command is done once the file is written too
98
+ const file = outputFile === undefined ? null : createWriteStream(outputFile)
99
+ const written = file === null ? Promise.resolve() : new Promise((done, fail) => file.on('finish', done).on('error', fail))
100
+ if (file !== null) child.stdout.pipe(file)
101
+ else child.stdout?.on('data', chunk => { stdout += chunk })
102
+ child.stderr?.on('data', chunk => { stderr += chunk })
103
+ child.on('error', error => reject(error.code === 'ENOENT' ? new Error(`${command} is not installed`) : error))
104
+ child.on('close', code => written.then(() => resolve({ code, stdout, stderr }), reject))
105
+ })
106
+ }