ramdb2-cli 0.0.0-stage → 2.0.0-alpha.1
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 +7 -0
- package/LICENSE +21 -0
- package/README.md +428 -2
- package/package.json +34 -4
- package/src/cli.js +139 -0
- package/src/convert.js +90 -0
- package/src/directives.js +42 -0
- package/src/generate/classes.js +292 -0
- package/src/generate/client.js +146 -0
- package/src/generate/invalidation.js +102 -0
- package/src/generate/operations.js +98 -0
- package/src/generate/typeDefs.js +39 -0
- package/src/generate.js +51 -0
- package/src/index.js +10 -0
- package/src/infra/build.js +372 -0
- package/src/infra/config.js +165 -0
- package/src/model.js +420 -0
- package/src/new.js +619 -0
- package/src/plantuml.js +71 -0
- package/src/query.js +159 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { resolvePath } from '../model.js'
|
|
2
|
+
|
|
3
|
+
// Inverts the dependency paths of the computed fields (D12, D36) into the code of the generated methods
|
|
4
|
+
// _invalidateDependents(field, oldValue) and _invalidateLinks(), for every type a path goes through.
|
|
5
|
+
//
|
|
6
|
+
// A path from type T0 goes through relations r0..rk-1 to Tk, then ends on a field f of Tk:
|
|
7
|
+
// - f changes on a Tk object: invalidate the T0 objects that reach it;
|
|
8
|
+
// - the foreign key of a relation ri changes: the T0 objects reaching the object that changed;
|
|
9
|
+
// for a list relation, those reaching the old parent and those reaching the new one;
|
|
10
|
+
// - an object of T1..Tk is inserted or deleted: the T0 objects that reach it.
|
|
11
|
+
// Going back over a relation: an object relation (the object holds idX) is inverted by the inverse
|
|
12
|
+
// index of idX (several objects); a list relation (the other object holds the key) by store.get (one object).
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {import('../model.js').Model} model
|
|
16
|
+
* @returns {Map<string, {cases: Map<string, string[]>, links: string[]}>} code per type name
|
|
17
|
+
*/
|
|
18
|
+
export function invalidations(model) {
|
|
19
|
+
const result = new Map(model.types.map(type => [type.name, { cases: new Map(), links: [] }]))
|
|
20
|
+
// Deduplicated by whole block: two blocks may share lines ('{', '}'...) without being the same
|
|
21
|
+
const blocks = new Map()
|
|
22
|
+
const add = (key, block) => {
|
|
23
|
+
if (!blocks.has(key)) blocks.set(key, new Set())
|
|
24
|
+
blocks.get(key).add(block.join('\n'))
|
|
25
|
+
}
|
|
26
|
+
const addCase = (type, field, ...list) => {
|
|
27
|
+
for (const block of list) add(`${type.name}\0case\0${field}`, block)
|
|
28
|
+
}
|
|
29
|
+
const addLinks = (type, block) => add(`${type.name}\0links`, block)
|
|
30
|
+
|
|
31
|
+
for (const type of model.types) {
|
|
32
|
+
for (const computed of type.computed) {
|
|
33
|
+
for (const path of computed.depends) {
|
|
34
|
+
const { steps, end, last } = resolvePath(type, path.split('.'), model.byName)
|
|
35
|
+
const k = steps.length
|
|
36
|
+
const back = i => steps.slice(0, i).reverse() // the relations to go back from position i to 0
|
|
37
|
+
const target = computed.name
|
|
38
|
+
|
|
39
|
+
// The field at the end of the path
|
|
40
|
+
if (last !== undefined) addCase(end, last, code('this', back(k), target))
|
|
41
|
+
|
|
42
|
+
for (let i = 0; i < k; i++) {
|
|
43
|
+
const { relation, kind, from, to } = steps[i]
|
|
44
|
+
if (kind === 'parent') {
|
|
45
|
+
// The object at position i points to another object
|
|
46
|
+
addCase(from, relation.fk, code('this', back(i), target))
|
|
47
|
+
} else {
|
|
48
|
+
// The object at position i + 1 moves from a parent to another
|
|
49
|
+
const parentTable = from.table
|
|
50
|
+
addCase(to, relation.fk,
|
|
51
|
+
code(`store.get('${parentTable}', oldValue)`, back(i), target),
|
|
52
|
+
code(`store.get('${parentTable}', this._f.${relation.fk})`, back(i), target))
|
|
53
|
+
}
|
|
54
|
+
// The object at position i + 1 appears or disappears
|
|
55
|
+
addLinks(to, code('this', back(i + 1), target))
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
for (const [key, set] of blocks) {
|
|
61
|
+
const [typeName, kind, field] = key.split('\0')
|
|
62
|
+
const lines = [...set].flatMap(block => block.split('\n'))
|
|
63
|
+
if (kind === 'links') result.get(typeName).links = lines
|
|
64
|
+
else result.get(typeName).cases.set(field, lines)
|
|
65
|
+
}
|
|
66
|
+
return result
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Lines that invalidate `target` on every object reached from `start` by going back over `steps`.
|
|
71
|
+
* `start` may evaluate to undefined, except 'this'.
|
|
72
|
+
*/
|
|
73
|
+
function code(start, steps, target, depth = 1) {
|
|
74
|
+
// `this` and the loop variables are never undefined
|
|
75
|
+
if (steps.length === 0) return [`${start}${start === 'this' || /^o\d+$/.test(start) ? '' : '?'}.invalidate('${target}')`]
|
|
76
|
+
const [step, ...rest] = steps
|
|
77
|
+
const from = step.from
|
|
78
|
+
const fk = step.relation.fk
|
|
79
|
+
if (step.kind === 'child') {
|
|
80
|
+
// `start` holds the foreign key towards its parent
|
|
81
|
+
const access = start === 'this' ? `this._f.${fk}` : `${start}?._f.${fk}`
|
|
82
|
+
return code(`store.get('${from.table}', ${access})`, rest, target, depth)
|
|
83
|
+
}
|
|
84
|
+
// Every object of `from` whose foreign key designates `start`
|
|
85
|
+
const variable = `o${depth}`
|
|
86
|
+
const loop = source => [
|
|
87
|
+
`for (const ${variable} of store.index('${from.table}', '${fk}').get(${source}._f.id)) {`,
|
|
88
|
+
...code(variable, rest, target, depth + 1).map(line => ' ' + line),
|
|
89
|
+
'}',
|
|
90
|
+
]
|
|
91
|
+
if (start === 'this' || /^o\d+$/.test(start)) return loop(start)
|
|
92
|
+
// In a block: the same case may hold several of them
|
|
93
|
+
const holder = `p${depth}`
|
|
94
|
+
return [
|
|
95
|
+
'{',
|
|
96
|
+
` const ${holder} = ${start}`,
|
|
97
|
+
` if (${holder} !== undefined) {`,
|
|
98
|
+
...loop(holder).map(line => ' ' + line),
|
|
99
|
+
' }',
|
|
100
|
+
'}',
|
|
101
|
+
]
|
|
102
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { lowerFirst } from '../model.js'
|
|
2
|
+
|
|
3
|
+
// The schema of the standard operations of each persistent type (spec section 13). ramdb2-server builds
|
|
4
|
+
// their resolvers at startup from the classes, with the same names: see operationNames in ramdb2-server.
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Names of the GraphQL operations and types of a persistent type.
|
|
8
|
+
* @param {string} typeName e.g. Task
|
|
9
|
+
*/
|
|
10
|
+
export function operationNames(typeName) {
|
|
11
|
+
const name = lowerFirst(typeName)
|
|
12
|
+
return {
|
|
13
|
+
list: `${name}s`,
|
|
14
|
+
byId: `${name}ById`,
|
|
15
|
+
dataCheck: `${name}DataCheck`,
|
|
16
|
+
default: `${name}Default`,
|
|
17
|
+
create: `${name}Create`,
|
|
18
|
+
update: `${name}Update`,
|
|
19
|
+
delete: `${name}Delete`,
|
|
20
|
+
argument: name,
|
|
21
|
+
input: `${typeName}Input`,
|
|
22
|
+
validation: `${typeName}Validation`,
|
|
23
|
+
query: `${typeName}Query`,
|
|
24
|
+
result: `${typeName}List`,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {import('../model.js').Model} model
|
|
30
|
+
* @returns {{path: string, content: string, overwrite: boolean}}
|
|
31
|
+
*/
|
|
32
|
+
export function operationsFile(model) {
|
|
33
|
+
const lines = [
|
|
34
|
+
'# Generated by ramdb2-cli from the schema: do not edit, this file is rewritten at each generation.',
|
|
35
|
+
'# The resolvers of these operations are built by ramdb2-server. A project adds its own with "extend type Query".',
|
|
36
|
+
'',
|
|
37
|
+
'"Any JSON value: the where, aggregates and having of a query (MongoDB syntax)."',
|
|
38
|
+
'scalar JSON',
|
|
39
|
+
]
|
|
40
|
+
for (const type of model.types) {
|
|
41
|
+
const names = operationNames(type.name)
|
|
42
|
+
// Every field is optional: an update changes only the fields it gives
|
|
43
|
+
// @private fields are absent from the API
|
|
44
|
+
const inputs = [...type.parents.map(p => [p.fk, 'Int']), ...type.fields.filter(f => !f.private).map(f => [f.name, f.gqlType, f.sensitive])]
|
|
45
|
+
lines.push('')
|
|
46
|
+
lines.push(`"Values of a ${type.name} to create or modify: an update changes only the fields given."`)
|
|
47
|
+
lines.push(`input ${names.input} {`)
|
|
48
|
+
// @sensitive: the value is masked in the logs of ramdb2-server (spec section 14)
|
|
49
|
+
for (const [field, gqlType, sensitive] of inputs) lines.push(` ${field}: ${gqlType}${sensitive ? ' @sensitive' : ''}`)
|
|
50
|
+
lines.push('}')
|
|
51
|
+
lines.push('')
|
|
52
|
+
lines.push(`"One message per field, empty when the value is valid."`)
|
|
53
|
+
lines.push(`type ${names.validation} {`)
|
|
54
|
+
for (const [field] of inputs) lines.push(` ${field}: String!`)
|
|
55
|
+
lines.push('}')
|
|
56
|
+
lines.push('')
|
|
57
|
+
lines.push(`"A query on ${type.name} (spec section 10)."`)
|
|
58
|
+
lines.push(`input ${names.query} {`)
|
|
59
|
+
lines.push(' where: JSON')
|
|
60
|
+
lines.push(' orderBy: [JSON!]')
|
|
61
|
+
lines.push(' limit: Int')
|
|
62
|
+
lines.push(' offset: Int')
|
|
63
|
+
lines.push(' groupBy: [String!]')
|
|
64
|
+
lines.push(' aggregates: JSON')
|
|
65
|
+
lines.push(' having: JSON')
|
|
66
|
+
lines.push('}')
|
|
67
|
+
lines.push('')
|
|
68
|
+
lines.push(`"Result of a query on ${type.name}: items, or groups with a groupBy."`)
|
|
69
|
+
lines.push(`type ${names.result} {`)
|
|
70
|
+
lines.push(` items: [${type.name}!]`)
|
|
71
|
+
lines.push(' "Number of items, or of groups, before pagination."')
|
|
72
|
+
lines.push(' total: Int!')
|
|
73
|
+
lines.push(' aggregates: JSON')
|
|
74
|
+
lines.push(' groups: [JSON!]')
|
|
75
|
+
lines.push('}')
|
|
76
|
+
}
|
|
77
|
+
lines.push('')
|
|
78
|
+
lines.push('type Query {')
|
|
79
|
+
for (const type of model.types) {
|
|
80
|
+
const n = operationNames(type.name)
|
|
81
|
+
lines.push(` ${n.list}(query: ${n.query}): ${n.result}!`)
|
|
82
|
+
lines.push(` ${n.byId}(id: Int!): ${type.name}!`)
|
|
83
|
+
lines.push(` ${n.dataCheck}(${n.argument}: ${n.input}!, id: Int): ${n.validation}!`)
|
|
84
|
+
lines.push(` ${n.default}: ${type.name}!`)
|
|
85
|
+
}
|
|
86
|
+
lines.push('}')
|
|
87
|
+
lines.push('')
|
|
88
|
+
lines.push('type Mutation {')
|
|
89
|
+
for (const type of model.types) {
|
|
90
|
+
const n = operationNames(type.name)
|
|
91
|
+
lines.push(` ${n.create}(${n.argument}: ${n.input}!): ${type.name}!`)
|
|
92
|
+
// expected: the values read by the client, CONFLICT when one has changed (D45)
|
|
93
|
+
lines.push(` ${n.update}(id: Int!, ${n.argument}: ${n.input}!, expected: ${n.input}): ${type.name}!`)
|
|
94
|
+
lines.push(` ${n.delete}(id: Int!): ${type.name}!`)
|
|
95
|
+
}
|
|
96
|
+
lines.push('}')
|
|
97
|
+
return { path: 'schema/operations.graphql', content: lines.join('\n') + '\n', overwrite: true }
|
|
98
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Kind, parse, print, visit } from 'graphql'
|
|
2
|
+
import { DIRECTIVE_NAMES } from '../directives.js'
|
|
3
|
+
|
|
4
|
+
// The schema served by Apollo (spec section 3): the preTypeDefs without the directives of ramdb2-cli,
|
|
5
|
+
// descriptions kept, with the id and the foreign keys of the persistent types.
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {import('../model.js').Model} model
|
|
9
|
+
* @returns {{path: string, content: string, overwrite: boolean}}
|
|
10
|
+
*/
|
|
11
|
+
export function typeDefsFile(model) {
|
|
12
|
+
const document = visit(model.document, {
|
|
13
|
+
Directive(node) {
|
|
14
|
+
if (DIRECTIVE_NAMES.has(node.name.value)) return null
|
|
15
|
+
},
|
|
16
|
+
ObjectTypeDefinition(node) {
|
|
17
|
+
const type = model.byName.get(node.name.value)
|
|
18
|
+
if (type === undefined) return undefined
|
|
19
|
+
const fields = []
|
|
20
|
+
if (!node.fields.some(field => field.name.value === 'id')) fields.push(fieldNode('id: Int!'))
|
|
21
|
+
for (const field of node.fields) {
|
|
22
|
+
// @private: stored, never in the API
|
|
23
|
+
if (field.directives?.some(directive => directive.name.value === 'private')) continue
|
|
24
|
+
const parent = type.parents.find(p => p.name === field.name.value)
|
|
25
|
+
// The foreign key, before its relation: what a client sends to create or modify the object
|
|
26
|
+
if (parent !== undefined) fields.push(fieldNode(`${parent.fk}: Int!`))
|
|
27
|
+
fields.push(field)
|
|
28
|
+
}
|
|
29
|
+
return { ...node, fields }
|
|
30
|
+
},
|
|
31
|
+
})
|
|
32
|
+
const header = '# Generated by ramdb2-cli from the schema: do not edit, this file is rewritten at each generation.\n\n'
|
|
33
|
+
return { path: 'schema/typeDefs.graphql', content: header + print(document) + '\n', overwrite: true }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function fieldNode(source) {
|
|
37
|
+
const field = parse(`type T { ${source} }`, { noLocation: true }).definitions[0].fields[0]
|
|
38
|
+
return field.kind === Kind.FIELD_DEFINITION ? field : undefined
|
|
39
|
+
}
|
package/src/generate.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { dirname, join } from 'node:path'
|
|
3
|
+
import { readSchema } from './model.js'
|
|
4
|
+
import { classFiles } from './generate/classes.js'
|
|
5
|
+
import { clientFiles } from './generate/client.js'
|
|
6
|
+
import { typeDefsFile } from './generate/typeDefs.js'
|
|
7
|
+
import { operationsFile } from './generate/operations.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Generates the database layer of a project from its preTypeDefs (spec section 12).
|
|
11
|
+
* Generated files are rewritten; files written by hand (extended classes) are created only when missing.
|
|
12
|
+
* @param {{schema: string | string[], dest: string}} options schema file(s), destination folder
|
|
13
|
+
* @returns {{written: string[], kept: string[], warnings: string[]}} paths relative to dest
|
|
14
|
+
*/
|
|
15
|
+
export function generate({ schema, dest }) {
|
|
16
|
+
const model = readSchemaFiles(schema)
|
|
17
|
+
return { ...writeFiles([...classFiles(model), typeDefsFile(model), operationsFile(model)], dest), warnings: model.warnings }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Generates the classes of a ramdb2-client application from the preTypeDefs of its server (spec section 12, D49):
|
|
22
|
+
* generated/<table>.js (rewritten), extended/<table>.js (created once) and index.js, in dest.
|
|
23
|
+
* @param {{schema: string | string[], dest: string}} options schema file(s), destination folder
|
|
24
|
+
* @returns {{written: string[], kept: string[], warnings: string[]}} paths relative to dest
|
|
25
|
+
*/
|
|
26
|
+
export function generateClient({ schema, dest }) {
|
|
27
|
+
const model = readSchemaFiles(schema)
|
|
28
|
+
const { files, warnings } = clientFiles(model)
|
|
29
|
+
return { ...writeFiles(files, dest), warnings: [...model.warnings, ...warnings] }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function readSchemaFiles(schema) {
|
|
33
|
+
const sources = (Array.isArray(schema) ? schema : [schema]).map(path => readFileSync(path, 'utf8'))
|
|
34
|
+
return readSchema(sources.join('\n'))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function writeFiles(files, dest) {
|
|
38
|
+
const written = []
|
|
39
|
+
const kept = []
|
|
40
|
+
for (const file of files) {
|
|
41
|
+
const path = join(dest, file.path)
|
|
42
|
+
if (!file.overwrite && existsSync(path)) {
|
|
43
|
+
kept.push(file.path)
|
|
44
|
+
continue
|
|
45
|
+
}
|
|
46
|
+
mkdirSync(dirname(path), { recursive: true })
|
|
47
|
+
writeFileSync(path, file.content)
|
|
48
|
+
written.push(file.path)
|
|
49
|
+
}
|
|
50
|
+
return { written, kept }
|
|
51
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { generate, generateClient } from './generate.js'
|
|
2
|
+
export { convert } from './convert.js'
|
|
3
|
+
export { newProject } from './new.js'
|
|
4
|
+
export { plantuml, diagram } from './plantuml.js'
|
|
5
|
+
export { query, expiryOf } from './query.js'
|
|
6
|
+
export { readSchema, SchemaError } from './model.js'
|
|
7
|
+
export { DIRECTIVES } from './directives.js'
|
|
8
|
+
export { operationNames } from './generate/operations.js'
|
|
9
|
+
export { readInfra, InfraError } from './infra/config.js'
|
|
10
|
+
export { buildInfra, serverFiles, certificates } from './infra/build.js'
|
|
@@ -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
|
+
`
|