-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·197 lines (177 loc) · 5.73 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env node
// todo: use import assertions once they're supported by Node.js & ESLint
// https://github.com/tc39/proposal-import-assertions
import {createRequire} from 'module'
const require = createRequire(import.meta.url)
import {parseArgs} from 'node:util'
const pkg = require('./package.json')
const {
values: flags,
} = parseArgs({
options: {
help: {
type: 'boolean',
short: 'h',
},
version: {
type: 'boolean',
short: 'v',
},
watch: {
type: 'boolean',
short: 'w',
},
'etcd-prefix': {
type: 'string',
short: 'p',
},
'no-atomic-writes': {
type: 'boolean',
},
'path-to-pgbouncer-ini': {
type: 'string',
short: 'c',
},
'path-to-userlist-txt': {
type: 'string',
short: 'u',
},
quiet: {
type: 'boolean',
short: 'q',
},
'listen-for-sigusr1': {
type: 'boolean',
},
'no-pgbouncer-reload': {
type: 'boolean',
},
},
})
if (flags.help) {
process.stdout.write(`
Usage:
configure-pgbouncer-using-etcd
Options:
-w --watch Watch the etcd namespace, and regenerate the pgbouncer
config as soon as any value has changed.
Default: false
-p --etcd-prefix Key prefix in etcd to query/watch.
Default: pgbouncer.
-c --path-to-pgbouncer-ini Where pgbouncer's pgbouncer.ini shall be written to.
Default: $PWD/pgbouncer.ini
-u --path-to-userlist-txt Where pgbouncer's userlist.txt shall be written to.
Default: $PWD/pgbouncer.ini
-q --quiet Do not print a message to stdout whenever pgbouncer's
config has been modified.
Default: false
--no-atomic-writes Instead of writing atomically by
1) writing into a temporary file and
2) moving this temp file to the target path,
*do not* write atomically.
Default: false
-d --debounce With bursts of changes coming from etcds, the time to
delay regeneration of the config for, at most, in
milliseconds. Pass 0 to regenerate on every change
immediately.
Default: 200
--listen-for-sigusr1 Reconfigure pgbouncer when the process receives a
SIGUSR1 signal.
Default: false
--no-pgbouncer-reload Once the config has been regenerated, *do not* tell
pgbouncer to
1. reload the config (using \`RELOAD\`)
2. reestablish all connections to DBs & clients
using \`SUSPEND; RESUME\`
Default: false
Notes:
Unless --no-pgbouncer-reload is passed, this tool will connect to pgbouncer's
special \`pgbouncer\` "admin console" DB that allows controlling it via SQL. [1]
It will respect up the libpg environment variables [2].
[1] https://www.pgbouncer.org/usage.html#admin-console
[2] https://www.postgresql.org/docs/16/libpq-envars.html
Examples:
configure-pgbouncer-using-etcd -c /etc/pgbouncer/pgbouncer.ini --watch
configure-pgbouncer-using-etcd --etcd-prefix pgb --no-atomic-writes
\n`)
process.exit(0)
}
if (flags.version) {
process.stdout.write(`${pkg.name} v${pkg.version}\n`)
process.exit(0)
}
import {
generatePgbouncerConfigFromEtc,
GenerationError,
} from './index.js'
const onGenerationFailed = ({error}) => {
if (!flags.quiet) {
console.error('failed to generate pgbouncer config')
console.error(error)
}
process.exitCode = 1
}
const opt = {
onGenerationFailed,
}
if ('watch' in flags) {
opt.watch = flags.watch
}
if ('etcd-prefix' in flags) {
opt.etcdPrefix = flags['etcd-prefix']
}
if ('no-atomic-writes' in flags) {
opt.writeAtomically = !flags['no-atomic-writes']
}
if ('path-to-pgbouncer-ini' in flags) {
opt.pathToPgbouncerIni = flags['path-to-pgbouncer-ini']
}
if ('path-to-userlist-txt' in flags) {
opt.pathToUserlistTxt = flags['path-to-userlist-txt']
}
if (!flags.quiet) {
opt.onConfigWritten = ({pgbouncerIniWritten, userlistTxtWritten}) => {
const filesWritten = [
...(pgbouncerIniWritten ? ['pgbouncer.ini'] : []),
...(userlistTxtWritten ? ['userlist.txt'] : []),
]
console.info(filesWritten.join(' & ') + ' written')
}
opt.onPgbouncerReloaded = () => {
console.info('pgbouncer reloaded')
}
}
if ('debounce' in flags) {
const ms = parseInt(flags.debounce)
if (Number.isInteger(ms) || ms < 0) {
console.error('Invalid --debounce value.')
process.exit(1)
}
opt.debounce = ms
}
if ('no-pgbouncer-reload' in flags) {
opt.reloadPgbouncer = !flags['no-pgbouncer-reload']
}
const handleError = (error) => {
if (error instanceof GenerationError) {
onGenerationFailed({error})
} else {
throw error
}
}
let generator = null
try {
generator = await generatePgbouncerConfigFromEtc(opt)
} catch (error) {
handleError(error)
}
if (flags['listen-for-sigusr1']) {
// Begin reading from stdin so the process does not exit.
process.stdin.resume()
// > 'SIGUSR1' is reserved by Node.js to start the debugger. It's possible to install a listener but doing so might interfere with the debugger.
// https://nodejs.org/docs/latest-v20.x/api/process.html#signal-events
process.on('SIGUSR1', () => {
generator.generate()
.catch(handleError)
})
}