ramdb2-cli 2.0.0-alpha.0 → 2.0.0-alpha.10
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 +55 -0
- package/README.md +187 -2
- package/package.json +5 -4
- package/src/cli.js +114 -2
- package/src/directives.js +6 -3
- package/src/generate/classes.js +29 -0
- package/src/generate/client.js +14 -6
- package/src/generate/typeDefs.js +16 -2
- package/src/generate.js +45 -7
- package/src/index.js +6 -0
- package/src/infra/backups.js +77 -0
- package/src/infra/build.js +372 -0
- package/src/infra/config.js +165 -0
- package/src/infra/deploy.js +41 -0
- package/src/infra/down.js +19 -0
- package/src/infra/keygen.js +54 -0
- package/src/infra/server.js +106 -0
- package/src/model.js +41 -1
|
@@ -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,19 @@
|
|
|
1
|
+
// ramdb2-cli infra down (D55): stops and removes the containers and the network of a server. The volumes (the
|
|
2
|
+
// databases), the images and the certificates stay: infra deploy starts the server again as it was.
|
|
3
|
+
import { check, runCommand, withServer } from './server.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Stops one server. Its secrets are decrypted too: Compose needs them to read compose.yaml.
|
|
7
|
+
* @param {import('./config.js').Infra} infra
|
|
8
|
+
* @param {string} name the server
|
|
9
|
+
* @param {object} [options]
|
|
10
|
+
* @param {import('./server.js').Runner} [options.run]
|
|
11
|
+
* @param {(line: string) => void} [options.print]
|
|
12
|
+
*/
|
|
13
|
+
export async function down(infra, name, { run = runCommand, print = console.log } = {}) {
|
|
14
|
+
return withServer(infra, name, run, async ({ server, compose, env }) => {
|
|
15
|
+
print(`${name}: docker compose down on ${server.host} (the volumes stay)`)
|
|
16
|
+
await check(run('docker', [...compose, 'down', '--remove-orphans'], { env }), 'docker compose down')
|
|
17
|
+
print(`${name}: stopped`)
|
|
18
|
+
})
|
|
19
|
+
}
|
|
@@ -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
|
+
}
|
package/src/model.js
CHANGED
|
@@ -64,6 +64,8 @@ export class SchemaError extends Error {
|
|
|
64
64
|
* @property {string | undefined} description
|
|
65
65
|
* @property {boolean} scalar scalar, enum, custom scalar or a list of them (a type of another layer is a scalar)
|
|
66
66
|
* @property {string[]} requiredArgs arguments without a default value
|
|
67
|
+
* @property {boolean} getter @transient(getter: true): a getter of the class gives its value, a query filters and
|
|
68
|
+
* sorts on it (D62)
|
|
67
69
|
*
|
|
68
70
|
* @typedef {object} Index
|
|
69
71
|
* @property {string} name
|
|
@@ -82,6 +84,8 @@ export class SchemaError extends Error {
|
|
|
82
84
|
* @property {Computed[]} computed
|
|
83
85
|
* @property {Transient[]} transient fields resolved by the project on the server
|
|
84
86
|
* @property {Index[]} indexes declared with @index (the inverse indexes of the foreign keys are automatic)
|
|
87
|
+
* @property {string[]} restricted @restricted fields (D58): stored fields, relations with their foreign key,
|
|
88
|
+
* computed and @transient fields
|
|
85
89
|
*
|
|
86
90
|
* @typedef {object} Model
|
|
87
91
|
* @property {Type[]} types
|
|
@@ -136,6 +140,7 @@ export function readSchema(source) {
|
|
|
136
140
|
computed: [],
|
|
137
141
|
transient: [],
|
|
138
142
|
indexes: [],
|
|
143
|
+
restricted: [],
|
|
139
144
|
_gql: gqlType,
|
|
140
145
|
}
|
|
141
146
|
types.push(type)
|
|
@@ -155,7 +160,8 @@ export function readSchema(source) {
|
|
|
155
160
|
const list = isListType(isNonNullType(gqlField.type) ? gqlField.type.ofType : gqlField.type)
|
|
156
161
|
const description = gqlField.description ?? undefined
|
|
157
162
|
const computed = values('computed', node)
|
|
158
|
-
const
|
|
163
|
+
const transientArgs = values('transient', node)
|
|
164
|
+
const transient = transientArgs !== undefined
|
|
159
165
|
const pattern = values('pattern', node)
|
|
160
166
|
const onDelete = values('onDelete', node)
|
|
161
167
|
const field = values('field', node)
|
|
@@ -163,6 +169,15 @@ export function readSchema(source) {
|
|
|
163
169
|
if (isPrivate && (computed !== undefined || transient || name === 'id' || isObjectType(named) || list)) {
|
|
164
170
|
errors.push(`${at(name)}: @private only goes on a stored scalar field`)
|
|
165
171
|
}
|
|
172
|
+
if (values('restricted', node) !== undefined) {
|
|
173
|
+
if (name === 'id') errors.push(`${at(name)}: the id cannot be @restricted`)
|
|
174
|
+
else if (isPrivate) errors.push(`${at(name)}: @private and @restricted exclude each other`)
|
|
175
|
+
else if (list && isPersistent(named)) errors.push(`${at(name)}: a list relation cannot be @restricted: restrict the object relation of the other type`)
|
|
176
|
+
else {
|
|
177
|
+
type.restricted.push(name)
|
|
178
|
+
if (isPersistent(named) && computed === undefined && !transient) type.restricted.push('id' + upperFirst(name))
|
|
179
|
+
}
|
|
180
|
+
}
|
|
166
181
|
|
|
167
182
|
if (computed !== undefined && transient) errors.push(`${at(name)}: @computed and @transient exclude each other`)
|
|
168
183
|
if (computed !== undefined) {
|
|
@@ -177,7 +192,12 @@ export function readSchema(source) {
|
|
|
177
192
|
description,
|
|
178
193
|
scalar: isLeafType(named),
|
|
179
194
|
requiredArgs: gqlField.args.filter(arg => isNonNullType(arg.type) && arg.defaultValue === undefined).map(arg => arg.name),
|
|
195
|
+
getter: transientArgs.getter === true,
|
|
180
196
|
})
|
|
197
|
+
// A getter reads the object alone: one scalar value, no argument (filtered and sorted by the engine)
|
|
198
|
+
if (transientArgs.getter === true && (!isLeafType(named) || list || gqlField.args.length > 0)) {
|
|
199
|
+
errors.push(`${at(name)}: @transient(getter: true) only goes on a scalar field without arguments`)
|
|
200
|
+
}
|
|
181
201
|
continue
|
|
182
202
|
}
|
|
183
203
|
if (name === 'id') {
|
|
@@ -236,6 +256,7 @@ export function readSchema(source) {
|
|
|
236
256
|
checkRelations(types, byName, errors)
|
|
237
257
|
for (const type of types) readIndexes(type, values, errors)
|
|
238
258
|
for (const type of types) checkComputed(type, byName, errors)
|
|
259
|
+
for (const type of types) checkRestrictedDependencies(type, byName, errors)
|
|
239
260
|
|
|
240
261
|
if (errors.length > 0) throw new SchemaError(errors)
|
|
241
262
|
for (const type of types) delete type._gql
|
|
@@ -358,6 +379,25 @@ function checkComputed(type, byName, errors) {
|
|
|
358
379
|
for (const name of local.keys()) visit(name, [])
|
|
359
380
|
}
|
|
360
381
|
|
|
382
|
+
// A computed field that depends on a @restricted field would reveal it: it must be @restricted too (D58).
|
|
383
|
+
// A path is restricted when it ends on a restricted field, goes through a restricted relation, or goes through a
|
|
384
|
+
// list relation whose foreign key is restricted (the list tells which objects point here).
|
|
385
|
+
function checkRestrictedDependencies(type, byName, errors) {
|
|
386
|
+
for (const computed of type.computed) {
|
|
387
|
+
if (type.restricted.includes(computed.name)) continue
|
|
388
|
+
for (const path of computed.depends) {
|
|
389
|
+
const { steps, end, last, error } = resolvePath(type, path.split('.'), byName)
|
|
390
|
+
if (error !== undefined) continue
|
|
391
|
+
const through = steps.some(step => step.kind === 'parent'
|
|
392
|
+
? step.from.restricted.includes(step.relation.name)
|
|
393
|
+
: step.to.restricted.includes(step.relation.fk))
|
|
394
|
+
if (through || (last !== undefined && end.restricted.includes(last))) {
|
|
395
|
+
errors.push(`${type.name}.${computed.name}: depends on "${path}", which is @restricted: add @restricted to ${computed.name}`)
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
361
401
|
/**
|
|
362
402
|
* Follows a dependency path from a type.
|
|
363
403
|
* @returns {{steps?: {relation: Parent | Child, kind: 'parent' | 'child', from: Type, to: Type}[], end?: Type, last?: string, error?: string}}
|