@xeplr/monitor 1.0.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -4
- package/api/app.js +5 -1
- package/api/bin/auth.js +4 -0
- package/api/package.json +1 -20
- package/api/production.env.example +2 -1
- package/api/routes/catalog.js +71 -0
- package/api/routes/index.js +40 -18
- package/api/setup.schema.js +41 -6
- package/deploy/bin-of.cjs +19 -0
- package/deploy/deploy.js +28 -8
- package/deploy/install.sh +34 -19
- package/deploy/settings.cjs +4 -2
- package/{api/package-lock.json → npm-shrinkwrap.json} +98 -40
- package/package.json +22 -6
- package/ui/dist/assets/index-BzsqfSIx.js +75 -0
- package/ui/dist/assets/index-CbqbGtaD.css +1 -0
- package/ui/{index.html → dist/index.html} +2 -1
- package/ui/.env +0 -8
- package/ui/package-lock.json +0 -2094
- package/ui/package.json +0 -31
- package/ui/pages.json +0 -33
- package/ui/src/App.jsx +0 -130
- package/ui/src/api/monitor.js +0 -6
- package/ui/src/index.css +0 -414
- package/ui/src/main.jsx +0 -27
- package/ui/src/pages/Connections.jsx +0 -9
- package/ui/src/pages/Home.jsx +0 -8
- package/ui/src/pages/Monitor.jsx +0 -9
- package/ui/vite.config.js +0 -56
- /package/ui/{public → dist}/logo-wide.svg +0 -0
- /package/ui/{public → dist}/logo.svg +0 -0
package/README.md
CHANGED
|
@@ -47,6 +47,8 @@ Each route checks its own permission (`requireApi` from `@xeplr/base-apis`) agai
|
|
|
47
47
|
|
|
48
48
|
A route somebody's roles do not include answers 403, and the page says so.
|
|
49
49
|
|
|
50
|
+
**Every URL is registered with auth, by path.** At startup the API sends auth what each router serves (`api/routes/catalog.js` for its own; `@xeplr/backups`' and `@xeplr/jobs`' own catalogs for theirs), with the mount prefix applied (`@xeplr/base-apis` `createApp` routes `{ router, catalog }`). Auth adds a `METHOD /path` row for each route it lacks, and gives it the roles of the label row that guards it today (`label: 'Start, stop, restart or kill a service'`), or of its group when it names none. So the auth gate itself, not only each route's `requireApi`, knows who may reach `/api/monitor/services/:unit/restart`. Rows already there are never changed; nothing is deleted. You do nothing: the API registers again on every start, and the auth log says what it added.
|
|
51
|
+
|
|
50
52
|
**App settings** (the settings menu, top right) is **Super Admin only**: `settings:app`, three `system`-scope APIs (`api/migrations-auth/0009_app_settings.sql`) that no other role — CompanyAdmin included — is ever given, because they rewrite the whole install. Every setting in `production.env`, changed in place (`@xeplr/ui-setup` on `/api/app-settings`, `@xeplr/setup` `settingsRouter`): secrets show only *set*, `ENCRYPTION_KEY` and `AUTH_JWT_SECRET` can never be changed there, a new database login is signed in to before anything is written, each change is recorded in `settings_audit` (who, when, which names — never values), and both services restart into it (exit 75, `Restart=always`).
|
|
51
53
|
|
|
52
54
|
## How a service is doing
|
|
@@ -118,7 +120,7 @@ Run on the server itself, signed in to it as usual. It is on npm as [`@xeplr/mon
|
|
|
118
120
|
npx -y @xeplr/monitor
|
|
119
121
|
```
|
|
120
122
|
|
|
121
|
-
There is no remote deploy: the installer installs the machine it runs on
|
|
123
|
+
There is no remote deploy: the installer installs the machine it runs on. **The package is published ready to run**: the page is built when it is published, and the API's packages are pinned to the exact versions its tests ran on (`npm-shrinkwrap.json`), so a release on the server is one `npm install @xeplr/monitor@<this version>` — nothing copied, nothing built, no build tools on the server; an update takes seconds. From a checkout of this repository, `npm install && node deploy/deploy.js` packs that checkout and installs the pack (`--package <version or .tgz>` installs another, to test a build).
|
|
122
124
|
|
|
123
125
|
What the server needs first: Linux with systemd and D-Bus, Node.js 20+ and npm **installed system-wide** (`/usr/bin` or `/usr/local/bin`, e.g. NodeSource's package — the services run as their own `xeplr-monitor` user, which cannot run a Node from someone's home folder such as nvm's; the installer uses the system one and stops before changing anything when there is none), Postgres and Redis (on it or reachable from it), and nginx + certbot for a web address. The installer runs `deploy/install.sh` as root: run it as root, or `sudo` asks its password once — before any question, so the install never stops for it.
|
|
124
126
|
|
|
@@ -132,7 +134,7 @@ It asks for what it needs as it goes and stores nothing but the app's own settin
|
|
|
132
134
|
Then it shows what it will do, asks to confirm, and runs `deploy/install.sh` as root with the answers on its input: no value is a file or a command-line argument. The install:
|
|
133
135
|
|
|
134
136
|
1. makes the `xeplr-monitor` user (in `systemd-journal`) and `/var/lib/xeplr-monitor/logs`;
|
|
135
|
-
2. puts the release in `/opt/xeplr-monitor/releases/<time>`:
|
|
137
|
+
2. puts the release in `/opt/xeplr-monitor/releases/<time>`: `npm install --omit=dev @xeplr/monitor@<version>` there (the package in `node_modules/@xeplr/monitor`, its pinned packages beside it), and two links, `api` and `ui`, into it — so the services (`current/api`) and the nginx site (`current/ui/dist`) keep the paths they had before 1.1. Each command the next steps run (`xeplr-migrate`, `xeplr-auth-migrate`, `dotenv`) is found in the release first, before the settings are touched;
|
|
136
138
|
3. writes `/opt/xeplr-monitor/shared/production.env` (`deploy/settings.cjs`, through the app's own `api/setup.schema.js` and `@xeplr/setup` `prepare()`; owned by the `xeplr-monitor` user, 600, because App settings rewrites it): **`ENCRYPTION_KEY` and `AUTH_JWT_SECRET` are made on the server the first time and kept on every later install**, so nobody is signed out; the database login is encrypted with `@xeplr/utils` and its plain password cleared; an empty Super Admin password on a first install is generated into `shared/FIRST_LOGIN` (never printed). **A missing answer does not fail the install**: with no database login (or no web address for the email links) the API comes up on its **setup page** — `https://<domain>/setup`, or `http://127.0.0.1:19402/setup` on the server, behind the one-time code in `shared/SETUP_CODE` — asks for the rest, and restarts into the app; sign-in waits meanwhile;
|
|
137
139
|
4. creates the two databases if missing and runs the sign-in migrations (users, roles, the monitor's permissions) — after the setup page, the API and the sign-in service make theirs when they start;
|
|
138
140
|
5. installs the control command and writes `/etc/sudoers.d/xeplr-monitor` naming that command alone (checked with `visudo` first), then checks the monitor's user can ask systemd (D-Bus);
|
|
@@ -158,12 +160,15 @@ Keep the ports closed in the firewall (our security group allows only 22, 80 and
|
|
|
158
160
|
|
|
159
161
|
Needs **PostgreSQL** and **Redis** running. The controls work only on Linux with systemd; on a Mac the page runs and says so.
|
|
160
162
|
|
|
163
|
+
The API's packages are the package's: one `npm install` at the root (`api/package.json` holds only its development scripts; Node finds the packages in the root's `node_modules`).
|
|
164
|
+
|
|
161
165
|
```bash
|
|
166
|
+
npm install
|
|
162
167
|
cd api && cp production.env.example development.env
|
|
163
168
|
# in development.env: NODE_ENV=development, LOG_DIR=./logs,
|
|
164
169
|
# ENCRYPTION_KEY and AUTH_JWT_SECRET (each: openssl rand -hex 32),
|
|
165
170
|
# AUTH_SUPER_ADMIN_EMAIL / _PASSWORD, the AUTH_*_URL addresses on http://localhost:19400
|
|
166
|
-
npm
|
|
171
|
+
npm run db:encrypt && npm run setup
|
|
167
172
|
npm run start-auth # sign-in http://localhost:19401
|
|
168
173
|
npm run start-api # the API http://localhost:19402
|
|
169
174
|
cd ../ui && npm install && npm run dev # the app http://localhost:19400
|
|
@@ -174,10 +179,11 @@ Open http://localhost:19400 and sign in as the Super Admin.
|
|
|
174
179
|
## Tests
|
|
175
180
|
|
|
176
181
|
```bash
|
|
177
|
-
|
|
182
|
+
npm test # at the root: the API's tests (api/test)
|
|
178
183
|
```
|
|
179
184
|
|
|
180
185
|
- `test/apiRoleMappings.test.cjs` — the auth catalog built from `migrations-auth`: every route `routes/monitor.js` checks has its `apis` row, every row a group, every group a role besides Super Admin, and the Monitor menu row is the key the side rail uses.
|
|
186
|
+
- `test/catalog.test.cjs` — what the API registers with auth (`routes/catalog.js`) is exactly what its routers serve, listed from the real routers; every label is an `apis` row of `migrations-auth` in the route's group; the backups and jobs routers are mounted with their own catalogs.
|
|
181
187
|
- `test/systemd.test.cjs` — `lib/systemd` against a real systemd: a small unit written into `/run/systemd/system`, then read (what runs it, memory against its limit, CPU %, its processes), followed, searched, restarted, killed, stopped and started; and `deploy/control.sh` through sudo — refusing a name that is not plain, an option, another action, a second name — then starting, restarting, killing and stopping a service; and `test/host.test.cjs` — the Home page's numbers: memory the way `free` counts it, CPU use from two readings, and on Linux the same figures as `free -b` and `df` on that machine. Needs Linux with systemd and passwordless sudo — GitHub's ubuntu runners have both, so CI runs it; elsewhere it is skipped and says why.
|
|
182
188
|
|
|
183
189
|
- `test/stats.test.cjs` — the history: request lines (ids out, query strings dropped), counts per minute, CPU over the minute and spikes, journal lines; on a real Postgres, a read written with its cursor and the next started from it, the three routes (steps, the spike kept, permission, 30-day limit), pruning, and **leaving the page stops the query in Postgres** (gone from `pg_stat_activity`); on Linux with systemd, a real service's log read from journald, counted, and never counted twice.
|
|
@@ -189,6 +195,7 @@ cd api && npm test
|
|
|
189
195
|
| Rule | Why |
|
|
190
196
|
|---|---|
|
|
191
197
|
| Every route checks its own permission, and has its `apis` row, group and role grants in `migrations-auth`, in the same change. | The catalog is the only place that says who can reach a URL. |
|
|
198
|
+
| Every route is in `api/routes/catalog.js` (with its group, and its `requireApi` name as `label`), and every mounted package router carries its package's catalog. | Auth then holds a row for every path the API serves, so it can refuse one it does not know. |
|
|
192
199
|
| sudoers allows one command, which takes one action and one plain name. | No wildcard, no shell, nothing else run as root. |
|
|
193
200
|
| The monitor's tables are real tables (`monitor_services`, `monitor_audit`). | Who watched what and who did what are queries. |
|
|
194
201
|
| No setting for where data lives is defaulted; folders are absolute. | A wrong-but-present value reads as empty data, not as an error. |
|
package/api/app.js
CHANGED
|
@@ -41,6 +41,10 @@ function buildApp(extraRoutes, opts) {
|
|
|
41
41
|
log: { logDir: process.env.LOG_DIR || './logs' },
|
|
42
42
|
// /api/app-settings: the Super Admin's App settings page — the env file
|
|
43
43
|
// seen and changed; the app restarts into it (routes/appSettings.js).
|
|
44
|
-
|
|
44
|
+
// Each router with its catalog (routes/index.js mounts()): base-apis
|
|
45
|
+
// registers every route with auth at startup, prefix applied.
|
|
46
|
+
routes: Object.assign({}, extraRoutes || {},
|
|
47
|
+
{ '/api/app-settings': { router: appSettings(opts.setup), catalog: require('./routes/catalog').appSettings } },
|
|
48
|
+
require('./routes').mounts())
|
|
45
49
|
});
|
|
46
50
|
}
|
package/api/bin/auth.js
CHANGED
|
@@ -22,6 +22,10 @@ if (!waiting && process.env.NODE_ENV === 'production') {
|
|
|
22
22
|
setup.restartOnChange({ envFile: setupConfig.envFile, schema: setupConfig.schema, appName: 'auth' });
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
// The permission files of THIS release, worked out where npm put the
|
|
26
|
+
// packages — never a stored path, which could name another release.
|
|
27
|
+
process.env.XEPLR_AUTH_MIGRATIONS = require('../setup.schema.js').authMigrations().join(',');
|
|
28
|
+
|
|
25
29
|
async function start() {
|
|
26
30
|
// The sign-in database, made when missing — after the setup page nobody is
|
|
27
31
|
// there to run `npm run setup`. Then @xeplr/auth migrates it itself.
|
package/api/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"version": "1.0.0",
|
|
4
4
|
"private": true,
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
|
-
"description": "xeplr-monitor backend
|
|
6
|
+
"description": "xeplr-monitor backend \u2014 @xeplr/base-apis app. Its dependencies are the package's (../package.json); this file holds its development scripts.",
|
|
7
7
|
"main": "app.js",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"check-env": "dotenv -e development.env -- xeplr-check-env",
|
|
@@ -16,24 +16,5 @@
|
|
|
16
16
|
"start-auth": "node ./bin/auth.js",
|
|
17
17
|
"start-api": "node ./bin/www",
|
|
18
18
|
"test": "node --test test/*.test.cjs"
|
|
19
|
-
},
|
|
20
|
-
"dependencies": {
|
|
21
|
-
"@aws-sdk/client-s3": "^3.1168.0",
|
|
22
|
-
"@aws-sdk/lib-storage": "^3.1168.0",
|
|
23
|
-
"@xeplr/actions": "^2.2.4",
|
|
24
|
-
"@xeplr/auth": "^1.3.0",
|
|
25
|
-
"@xeplr/backups": "^0.1.1",
|
|
26
|
-
"@xeplr/base-apis": "^3.1.3",
|
|
27
|
-
"@xeplr/db": "^1.1.0",
|
|
28
|
-
"@xeplr/jobs": "^3.2.0",
|
|
29
|
-
"@xeplr/setup": "^0.2.0",
|
|
30
|
-
"@xeplr/utils": "^1.0.11",
|
|
31
|
-
"dotenv": "^17.4.2",
|
|
32
|
-
"express": "^5.2.0",
|
|
33
|
-
"mysql2": "^3.24.6"
|
|
34
|
-
},
|
|
35
|
-
"devDependencies": {
|
|
36
|
-
"@xeplr/cli": "^0.1.8",
|
|
37
|
-
"dotenv-cli": "^8.0.0"
|
|
38
19
|
}
|
|
39
20
|
}
|
|
@@ -42,7 +42,8 @@ REDIS_PREFIX=xeplr-monitor:
|
|
|
42
42
|
AUTH_JWT_SECRET=
|
|
43
43
|
# Where THIS server reaches the sign-in service (not the public address).
|
|
44
44
|
AUTH_URL=http://127.0.0.1:19401
|
|
45
|
-
|
|
45
|
+
# Written by every install: absolute folders of this app's, @xeplr/jobs' and @xeplr/backups' migrations-auth.
|
|
46
|
+
XEPLR_AUTH_MIGRATIONS=
|
|
46
47
|
AUTH_SUPER_ADMIN_EMAIL=
|
|
47
48
|
AUTH_SUPER_ADMIN_PASSWORD=
|
|
48
49
|
AUTH_TENANT_HEADER=none
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// WHAT THIS APP SERVES, by group — registered with auth at startup.
|
|
2
|
+
//
|
|
3
|
+
// app.js mounts each router with its catalog (createApp routes
|
|
4
|
+
// `{ router, catalog }`, @xeplr/base-apis 3.3), and base-apis sends it to auth
|
|
5
|
+
// with the mount prefix in front. Auth adds the `METHOD /path` rows it lacks:
|
|
6
|
+
// a route that names its `label` (the requireApi row guarding it today) gets
|
|
7
|
+
// exactly that row's roles; a route without one gets the roles holding its
|
|
8
|
+
// group (migrations-auth/0006…0010). Rows already there are never changed.
|
|
9
|
+
//
|
|
10
|
+
// The packages bring their own: @xeplr/backups' catalog (/api/monitor/connections)
|
|
11
|
+
// and @xeplr/jobs' (/api). Every route each router serves is here and nothing
|
|
12
|
+
// else: test/catalog.test.cjs lists the real routers and fails when they differ.
|
|
13
|
+
var monitorApi = require('./monitor').API;
|
|
14
|
+
var settingsApi = require('./appSettings').API;
|
|
15
|
+
|
|
16
|
+
function on(method, path, group, label) {
|
|
17
|
+
var r = { method: method, path: path, group: group };
|
|
18
|
+
if (label) r.label = label;
|
|
19
|
+
return r;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// routes/index.js, at '/'
|
|
23
|
+
exports.base = {
|
|
24
|
+
source: '@xeplr/monitor',
|
|
25
|
+
groups: ['monitor:view'],
|
|
26
|
+
routes: [
|
|
27
|
+
{ method: 'GET', path: '/health', public: true },
|
|
28
|
+
on('GET', '/', 'monitor:view'),
|
|
29
|
+
on('GET', '/whoami', 'monitor:view')
|
|
30
|
+
],
|
|
31
|
+
menus: [
|
|
32
|
+
{ name: 'Home', group: 'monitor:view' },
|
|
33
|
+
{ name: 'Monitor', group: 'monitor:view' }
|
|
34
|
+
]
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// routes/monitor.js, at /api/monitor
|
|
38
|
+
exports.monitor = {
|
|
39
|
+
source: '@xeplr/monitor',
|
|
40
|
+
groups: ['monitor:view', 'monitor:control', 'monitor:manage', 'monitor:audit'],
|
|
41
|
+
routes: [
|
|
42
|
+
on('GET', '/settings', 'monitor:view', monitorApi.settings),
|
|
43
|
+
on('GET', '/host', 'monitor:view', monitorApi.host),
|
|
44
|
+
on('GET', '/services', 'monitor:view', monitorApi.list),
|
|
45
|
+
on('POST', '/services', 'monitor:manage', monitorApi.add),
|
|
46
|
+
on('PATCH', '/services/:unit', 'monitor:manage', monitorApi.edit),
|
|
47
|
+
on('DELETE', '/services/:unit', 'monitor:manage', monitorApi.remove),
|
|
48
|
+
on('POST', '/services/:unit/:action', 'monitor:control', monitorApi.control),
|
|
49
|
+
on('GET', '/services/:unit/logs', 'monitor:view', monitorApi.logs),
|
|
50
|
+
on('GET', '/services/:unit/logs/live', 'monitor:view', monitorApi.live),
|
|
51
|
+
on('GET', '/services/:unit/history', 'monitor:view', monitorApi.history),
|
|
52
|
+
on('GET', '/history', 'monitor:view', monitorApi.serverHistory),
|
|
53
|
+
on('GET', '/traffic', 'monitor:view', monitorApi.traffic),
|
|
54
|
+
on('GET', '/audit', 'monitor:audit', monitorApi.audit)
|
|
55
|
+
],
|
|
56
|
+
menus: []
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// routes/appSettings.js, at /api/app-settings — Super Admin alone (system scope)
|
|
60
|
+
exports.appSettings = {
|
|
61
|
+
source: '@xeplr/monitor',
|
|
62
|
+
groups: ['settings:app'],
|
|
63
|
+
routes: [
|
|
64
|
+
on('GET', '/', 'settings:app', settingsApi.view),
|
|
65
|
+
on('POST', '/check', 'settings:app', settingsApi.check),
|
|
66
|
+
on('POST', '/', 'settings:app', settingsApi.save)
|
|
67
|
+
],
|
|
68
|
+
menus: [
|
|
69
|
+
{ name: 'App settings', group: 'settings:app' }
|
|
70
|
+
]
|
|
71
|
+
};
|
package/api/routes/index.js
CHANGED
|
@@ -24,24 +24,46 @@ router.get('/whoami', function(req, res) {
|
|
|
24
24
|
// MOUNTED UNDER /api, and that prefix matters: the UI's dev server forwards
|
|
25
25
|
// /api to this API and serves every other path as a page.
|
|
26
26
|
//
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
27
|
+
// mounts() gives app.js every router with its catalog (routes/catalog.js, and
|
|
28
|
+
// the packages' own), so @xeplr/base-apis registers each route with auth at
|
|
29
|
+
// startup, prefix applied. A function, not built here: jobs' router needs its
|
|
30
|
+
// connection, which setup mode does not have.
|
|
31
|
+
//
|
|
32
|
+
// /api/monitor/connections the logins the backups use (@xeplr/backups'
|
|
33
|
+
// router): each route checked by this app's
|
|
34
|
+
// requireApi; each change in the monitor's audit
|
|
35
|
+
// log, by the connection's name
|
|
36
|
+
// /api/monitor the server's services (routes/monitor.js)
|
|
37
|
+
// /api the backup jobs (@xeplr/jobs, lib/jobs.js):
|
|
38
|
+
// /api/jobs, /api/job-occurrences, /api/actions —
|
|
39
|
+
// checked against jobs' own apis rows
|
|
40
|
+
// (node_modules/@xeplr/jobs/migrations-auth)
|
|
41
|
+
// / /health, /, /whoami (above)
|
|
42
|
+
var catalog = require('./catalog');
|
|
39
43
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
function mounts() {
|
|
45
|
+
var { requireApi } = require('@xeplr/base-apis');
|
|
46
|
+
var backups = require('@xeplr/backups');
|
|
47
|
+
var jobs = require('@xeplr/jobs');
|
|
48
|
+
var services = require('../lib/services');
|
|
49
|
+
return {
|
|
50
|
+
'/api/monitor/connections': {
|
|
51
|
+
router: backups.router({
|
|
52
|
+
requireApi: requireApi,
|
|
53
|
+
audit: function(e) { return services.audit({ email: e.email, ip: e.ip, action: e.action, unit: 'connection: ' + e.name, ok: e.ok, error: e.error }); }
|
|
54
|
+
}),
|
|
55
|
+
catalog: backups.catalog
|
|
56
|
+
},
|
|
57
|
+
'/api/monitor': { router: require('./monitor'), catalog: catalog.monitor },
|
|
58
|
+
'/api': {
|
|
59
|
+
router: jobs.router({
|
|
60
|
+
auth: { list: requireApi('List jobs'), getById: requireApi('Get job'), save: requireApi('Save job'), delete: requireApi('Delete job') }
|
|
61
|
+
}),
|
|
62
|
+
catalog: jobs.catalog
|
|
63
|
+
},
|
|
64
|
+
'/': { router: router, catalog: catalog.base }
|
|
65
|
+
};
|
|
66
|
+
}
|
|
46
67
|
|
|
47
68
|
module.exports = router;
|
|
69
|
+
module.exports.mounts = mounts;
|
package/api/setup.schema.js
CHANGED
|
@@ -90,7 +90,7 @@ function about(encrypt, decrypt) {
|
|
|
90
90
|
AUTH_TENANT_HEADER: { kind: 'derived', from: [], derive: function() { return 'none'; }, label: 'Company header for roles', locked: 'Part of how this app was generated.' },
|
|
91
91
|
AUTH_REGISTER_ROLES: { kind: 'derived', from: [], derive: function() { return 'Viewer'; }, label: 'Role of a new sign-up' },
|
|
92
92
|
XEPLR_AUTH_MIGRATIONS: { kind: 'derived', from: [],
|
|
93
|
-
derive: function() { return '
|
|
93
|
+
derive: function() { return authMigrations().join(','); },
|
|
94
94
|
label: 'Sign-in permission files', locked: 'The permission files this app ships, in order.' },
|
|
95
95
|
|
|
96
96
|
XEPLR_DATA_DIR: { where: 'path', label: 'Data folder', help: 'Absolute path, e.g. /var/lib/xeplr-monitor/data — where a backup is written before it goes to S3.' },
|
|
@@ -141,15 +141,50 @@ function schemaFor(env, required, encrypt, decrypt) {
|
|
|
141
141
|
* release, read by the systemd units (EnvironmentFile) — and the setup code
|
|
142
142
|
* and FIRST_LOGIN sit beside it. Otherwise <api>/<NODE_ENV>.env.
|
|
143
143
|
*/
|
|
144
|
+
/**
|
|
145
|
+
* The env file: in an install, <root>/shared/<NODE_ENV>.env, found by walking
|
|
146
|
+
* up from the API to the `releases` folder — the API sits at
|
|
147
|
+
* <root>/releases/<release>/node_modules/@xeplr/monitor/api (installed from
|
|
148
|
+
* npm), or <root>/releases/<release>/api (installs before 1.1). Elsewhere
|
|
149
|
+
* (a checkout) <api>/<NODE_ENV>.env.
|
|
150
|
+
*/
|
|
144
151
|
function envFileFor(apiDir, env) {
|
|
145
152
|
var name = (env.NODE_ENV || 'development') + '.env';
|
|
146
|
-
|
|
147
|
-
var
|
|
148
|
-
|
|
149
|
-
|
|
153
|
+
var dir = path.resolve(apiDir);
|
|
154
|
+
for (var i = 0; i < 6; i++) {
|
|
155
|
+
var up = path.dirname(dir);
|
|
156
|
+
if (up === dir) break;
|
|
157
|
+
if (path.basename(up) === 'releases') {
|
|
158
|
+
var shared = path.join(path.dirname(up), 'shared', name);
|
|
159
|
+
if (fs.existsSync(shared)) return shared;
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
dir = up;
|
|
163
|
+
}
|
|
150
164
|
return path.join(apiDir, name);
|
|
151
165
|
}
|
|
152
166
|
|
|
167
|
+
/** The folder of an installed package, found from this API the way Node finds one: <dir>/node_modules/<name>, upward. */
|
|
168
|
+
function packageDir(name) {
|
|
169
|
+
for (var dir = __dirname; ; dir = path.dirname(dir)) {
|
|
170
|
+
var found = path.join(dir, 'node_modules', name);
|
|
171
|
+
if (fs.existsSync(path.join(found, 'package.json'))) return found;
|
|
172
|
+
if (path.dirname(dir) === dir) throw new Error('setup: cannot find the folder of ' + name);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The permission files this app ships, in order: its own, then @xeplr/jobs'
|
|
178
|
+
* and @xeplr/backups' — absolute, from where npm actually installed them (in
|
|
179
|
+
* a release they are beside this package, not under api/). The sign-in
|
|
180
|
+
* service sets XEPLR_AUTH_MIGRATIONS from this when it starts (bin/auth.js),
|
|
181
|
+
* so it always names the release that is running, whatever the env file
|
|
182
|
+
* says; the installer passes it to its migrate step the same way.
|
|
183
|
+
*/
|
|
184
|
+
function authMigrations() {
|
|
185
|
+
return [path.join(__dirname, 'migrations-auth'), path.join(packageDir('@xeplr/jobs'), 'migrations-auth'), path.join(packageDir('@xeplr/backups'), 'migrations-auth')];
|
|
186
|
+
}
|
|
187
|
+
|
|
153
188
|
function config(apiDir, env, opts) {
|
|
154
189
|
opts = opts || {};
|
|
155
190
|
env = env || process.env;
|
|
@@ -163,4 +198,4 @@ function config(apiDir, env, opts) {
|
|
|
163
198
|
};
|
|
164
199
|
}
|
|
165
200
|
|
|
166
|
-
module.exports = { config: config, schemaFor: schemaFor, envFileFor: envFileFor, CONNECTIONS: CONNECTIONS };
|
|
201
|
+
module.exports = { config: config, schemaFor: schemaFor, envFileFor: envFileFor, authMigrations: authMigrations, CONNECTIONS: CONNECTIONS };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// The absolute path of a package's command, found from the API the way Node
|
|
2
|
+
// finds a package: <dir>/node_modules/<package> from the API's folder upward
|
|
3
|
+
// (beside it in a release, the checkout's root locally). Not require.resolve:
|
|
4
|
+
// a command-only package (dotenv-cli) has nothing to require.
|
|
5
|
+
// node deploy/bin-of.cjs <apiDir> <package> <command>
|
|
6
|
+
var fs = require('fs');
|
|
7
|
+
var path = require('path');
|
|
8
|
+
var apiDir = path.resolve(process.argv[2]), name = process.argv[3], command = process.argv[4];
|
|
9
|
+
for (var dir = apiDir; ; dir = path.dirname(dir)) {
|
|
10
|
+
var pj = path.join(dir, 'node_modules', name, 'package.json');
|
|
11
|
+
if (fs.existsSync(pj)) {
|
|
12
|
+
var p = JSON.parse(fs.readFileSync(pj, 'utf8'));
|
|
13
|
+
var bin = typeof p.bin === 'string' ? p.bin : (p.bin || {})[command];
|
|
14
|
+
if (!bin) { console.error(name + ' has no command ' + command); process.exit(1); }
|
|
15
|
+
console.log(path.join(path.dirname(pj), bin));
|
|
16
|
+
process.exit(0);
|
|
17
|
+
}
|
|
18
|
+
if (path.dirname(dir) === dir) { console.error('cannot find ' + name + ' from ' + apiDir); process.exit(1); }
|
|
19
|
+
}
|
package/deploy/deploy.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// INSTALL OR UPDATE XEPLR-MONITOR ON THIS SERVER — run on the server itself,
|
|
3
3
|
// signed in there the usual way. There is no remote deploy: the installer
|
|
4
|
-
// installs the machine it runs on
|
|
5
|
-
//
|
|
4
|
+
// installs the machine it runs on. A release is `npm install` of this very
|
|
5
|
+
// version of @xeplr/monitor (published ready to run: the page built, the
|
|
6
|
+
// API's packages pinned), so nothing is copied or built on the server. From
|
|
7
|
+
// a checkout, the checkout is packed and that pack is installed.
|
|
6
8
|
//
|
|
7
9
|
// npx -y @xeplr/monitor install here, asking what it needs
|
|
8
10
|
// npx -y @xeplr/monitor@latest --update a new version, nothing asked
|
|
@@ -19,7 +21,7 @@
|
|
|
19
21
|
var fs = require('fs');
|
|
20
22
|
var os = require('os');
|
|
21
23
|
var path = require('path');
|
|
22
|
-
var { spawn, spawnSync } = require('child_process');
|
|
24
|
+
var { spawn, spawnSync, execFileSync } = require('child_process');
|
|
23
25
|
var { createSession } = require('@xeplr/cli/lib/prompt');
|
|
24
26
|
|
|
25
27
|
var OPTIONS = {
|
|
@@ -37,6 +39,20 @@ var DEFAULTS = { ports: '19400', 'db-port': '5432', 'db-user': 'postgres', 'redi
|
|
|
37
39
|
/** This installer's own folder: the whole app, as git or npx put it here. */
|
|
38
40
|
var SELF = path.resolve(__dirname, '..');
|
|
39
41
|
|
|
42
|
+
/**
|
|
43
|
+
* What install.sh installs with npm: --package when given (a version or a
|
|
44
|
+
* packed .tgz, for testing a build); run from npm (npx), this very version;
|
|
45
|
+
* from a checkout, a pack of it (its prepack builds the page).
|
|
46
|
+
*/
|
|
47
|
+
function packageSpec(given) {
|
|
48
|
+
if (given.package) return /\.tgz$/.test(given.package) ? path.resolve(given.package) : given.package;
|
|
49
|
+
if (SELF.split(path.sep).indexOf('node_modules') >= 0) return '@xeplr/monitor@' + version();
|
|
50
|
+
var out = execFileSync('npm', ['pack', '--pack-destination', os.tmpdir(), '--loglevel=error'], { cwd: SELF, encoding: 'utf8', stdio: ['ignore', 'pipe', 'inherit'] });
|
|
51
|
+
var tgz = path.join(os.tmpdir(), out.trim().split('\n').pop());
|
|
52
|
+
fs.chmodSync(tgz, 0o644);
|
|
53
|
+
return tgz;
|
|
54
|
+
}
|
|
55
|
+
|
|
40
56
|
function parseArgs(argv) {
|
|
41
57
|
var out = {};
|
|
42
58
|
for (var i = 0; i < argv.length; i++) {
|
|
@@ -44,6 +60,7 @@ function parseArgs(argv) {
|
|
|
44
60
|
if (!m) throw new Error('Not an option: ' + argv[i] + ' (--help lists them)');
|
|
45
61
|
if (m[1] === 'help' || m[1] === 'yes' || m[1] === 'no-certbot' || m[1] === 'update') { out[m[1]] = true; continue; }
|
|
46
62
|
if (m[1] === 'certbot') { out.certbot = m[2] !== undefined ? m[2] : 'y'; continue; }
|
|
63
|
+
if (m[1] === 'package') { out.package = m[2] !== undefined ? m[2] : argv[++i]; continue; }
|
|
47
64
|
if (OPTIONS[m[1]] === undefined) throw new Error('Unknown option --' + m[1] + ' (--help lists them)');
|
|
48
65
|
out[m[1]] = m[2] !== undefined ? m[2] : argv[++i];
|
|
49
66
|
}
|
|
@@ -105,7 +122,7 @@ function summary(v) {
|
|
|
105
122
|
var p = Number(v.ports);
|
|
106
123
|
return [
|
|
107
124
|
' Server this one (' + os.hostname() + ')',
|
|
108
|
-
' Installs
|
|
125
|
+
' Installs ' + v.packageSpec,
|
|
109
126
|
' Runs sign-in on ' + (p + 1) + ', the API on ' + (p + 2) + ' (xeplr-monitor-auth, xeplr-monitor-api)',
|
|
110
127
|
' Postgres ' + (v['db-host'] ? v['db-user'] + '@' + v['db-host'] + ':' + v['db-port'] + ' (password set)' : 'the login set before, or the setup page'),
|
|
111
128
|
' Redis ' + v['redis-host'] + ':' + v['redis-port'],
|
|
@@ -141,7 +158,7 @@ function install(values) {
|
|
|
141
158
|
/** The answers as install.sh's XM_* values. */
|
|
142
159
|
function valuesOf(v) {
|
|
143
160
|
return {
|
|
144
|
-
|
|
161
|
+
XM_PACKAGE: v.packageSpec, XM_PORT_BASE: v.ports,
|
|
145
162
|
XM_DB_HOST: v['db-host'] || '', XM_DB_PORT: v['db-port'] || '', XM_DB_USER: v['db-user'] || '', XM_DB_PASSWORD: v.dbPassword || '',
|
|
146
163
|
XM_REDIS_HOST: v['redis-host'], XM_REDIS_PORT: v['redis-port'], XM_REDIS_PASSWORD: v.redisPassword || '',
|
|
147
164
|
XM_ADMIN_EMAIL: v['admin-email'], XM_ADMIN_PASSWORD: v.adminPassword || '',
|
|
@@ -155,20 +172,23 @@ async function main() {
|
|
|
155
172
|
console.log('xeplr-monitor install [options] — run on the server it installs. Anything not given is asked; passwords are always\nasked (or XM_DB_PASSWORD, XM_REDIS_PASSWORD, XM_ADMIN_PASSWORD in the environment, for a script).\n');
|
|
156
173
|
Object.keys(OPTIONS).forEach(function(k) { console.log(' --' + k.padEnd(12) + OPTIONS[k].replace(/\?$/, '') + (DEFAULTS[k] ? ' (default ' + DEFAULTS[k] + ')' : '')); });
|
|
157
174
|
console.log(' --certbot / --no-certbot, --yes (install without asking to confirm)');
|
|
158
|
-
console.log(' --update install this
|
|
175
|
+
console.log(' --update install this version and restart; asks nothing');
|
|
176
|
+
console.log(' --package <x> what npm installs instead of this version: another version, or a packed .tgz (to test a build)');
|
|
159
177
|
return;
|
|
160
178
|
}
|
|
161
179
|
if (process.platform !== 'linux') throw new Error('Run this on the server it installs: the monitor runs on Linux with systemd');
|
|
162
180
|
sudoFirst();
|
|
163
181
|
if (given.update) {
|
|
164
|
-
|
|
165
|
-
|
|
182
|
+
var spec = packageSpec(given);
|
|
183
|
+
console.log('\n Updating this server to ' + spec + '. Everything else stays as it is.\n');
|
|
184
|
+
await install({ XM_UPDATE: '1', XM_PACKAGE: spec });
|
|
166
185
|
console.log('\n Updated.');
|
|
167
186
|
return;
|
|
168
187
|
}
|
|
169
188
|
var session = createSession();
|
|
170
189
|
try {
|
|
171
190
|
var v = await gather(given, session);
|
|
191
|
+
v.packageSpec = packageSpec(given);
|
|
172
192
|
session.say('\n' + summary(v) + '\n');
|
|
173
193
|
if (!given.yes && !(await session.confirm(' Install?', false))) { session.say(' Nothing done.'); return; }
|
|
174
194
|
session.say('');
|
package/deploy/install.sh
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
# XM_UPDATE 1: only install this version and restart — every other value
|
|
8
8
|
# is read from the settings the first deploy wrote; nginx and its
|
|
9
9
|
# certificate are left alone
|
|
10
|
-
#
|
|
11
|
-
#
|
|
10
|
+
# XM_PACKAGE what npm installs as the release: @xeplr/monitor@<version>
|
|
11
|
+
# (the version the installer itself is), or a packed .tgz
|
|
12
12
|
# XM_PORT_BASE ui slot; the sign-in service is +1, the API +2 (19400 → 19401, 19402)
|
|
13
13
|
# XM_DB_HOST, XM_DB_PORT, XM_DB_USER, XM_DB_PASSWORD
|
|
14
14
|
# the Postgres it uses (a user that may create databases);
|
|
@@ -92,21 +92,33 @@ done
|
|
|
92
92
|
mkdir -p "$VAR/logs" "$VAR/data" "$SHARED" "$APP/releases"
|
|
93
93
|
chown -R "$USER_NAME:$USER_NAME" "$VAR" && chmod 750 "$VAR"
|
|
94
94
|
|
|
95
|
-
# ── this release: the
|
|
95
|
+
# ── this release: the package, installed by npm, ready to run ─────────────
|
|
96
|
+
# The page is built and the API's packages are pinned (npm-shrinkwrap.json)
|
|
97
|
+
# when the package is published, so a release is one `npm install` — nothing
|
|
98
|
+
# copied, nothing built here. Two links keep the paths the services and the
|
|
99
|
+
# nginx site use (current/api, current/ui/dist) what they were before 1.1.
|
|
96
100
|
REL=$APP/releases/$(date -u +%Y%m%d%H%M%S)
|
|
97
101
|
mkdir -p "$REL"
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
102
|
+
[ -n "${XM_PACKAGE:-}" ] || stop "no package to install (run it with npx @xeplr/monitor)"
|
|
103
|
+
say "Installing $XM_PACKAGE"
|
|
104
|
+
printf '{ "private": true, "description": "One release of xeplr-monitor (deploy/install.sh)" }\n' > "$REL/package.json"
|
|
105
|
+
(cd "$REL" && npm install --omit=dev --no-audit --no-fund --no-update-notifier --loglevel=error "$XM_PACKAGE" >/dev/null) \
|
|
106
|
+
|| stop "npm could not install $XM_PACKAGE (can this server reach registry.npmjs.org?)"
|
|
107
|
+
PKG=$REL/node_modules/@xeplr/monitor
|
|
108
|
+
[ -f "$PKG/api/bin/www" ] && [ -f "$PKG/ui/dist/index.html" ] || stop "$XM_PACKAGE is not a ready-to-run xeplr-monitor (no api/bin/www or ui/dist)"
|
|
109
|
+
ln -s node_modules/@xeplr/monitor/api "$REL/api"
|
|
110
|
+
ln -s node_modules/@xeplr/monitor/ui "$REL/ui"
|
|
111
|
+
say "Installed xeplr-monitor $("$NODE" -p "require('$PKG/package.json').version")"
|
|
108
112
|
chown -R "$USER_NAME:$USER_NAME" "$REL"
|
|
109
113
|
|
|
114
|
+
# What the steps below run, found in this release now — before the settings
|
|
115
|
+
# are touched, so a release missing one stops with nothing changed.
|
|
116
|
+
bin_of() { "$NODE" "$PKG/deploy/bin-of.cjs" "$PKG/api" "$1" "$2" || stop "cannot find the command $2 of $1 in this release"; }
|
|
117
|
+
DOTENV_BIN=$(bin_of dotenv-cli dotenv)
|
|
118
|
+
MIGRATE_BIN=$(bin_of @xeplr/db xeplr-migrate)
|
|
119
|
+
AUTH_MIGRATE_BIN=$(bin_of @xeplr/auth xeplr-auth-migrate)
|
|
120
|
+
AUTH_MIGRATIONS=$("$NODE" -p "require('$PKG/api/setup.schema.js').authMigrations().join(',')") || stop "this release's permission files cannot be found"
|
|
121
|
+
|
|
110
122
|
# ── settings: written once, kept after; secrets made here, never asked ───
|
|
111
123
|
# Through the app's own schema (api/setup.schema.js, @xeplr/setup): what is
|
|
112
124
|
# missing after this is asked on the setup page, never a failed deploy.
|
|
@@ -118,7 +130,7 @@ env XM_ENV_FILE="$ENV_FILE" XM_AUTH_PORT="$AUTH_PORT" XM_API_PORT="$API_PORT" XM
|
|
|
118
130
|
XM_DB_HOST="$XM_DB_HOST" XM_DB_PORT="$XM_DB_PORT" XM_DB_USER="$XM_DB_USER" XM_DB_PASSWORD="$XM_DB_PASSWORD" \
|
|
119
131
|
XM_REDIS_HOST="$XM_REDIS_HOST" XM_REDIS_PORT="$XM_REDIS_PORT" XM_REDIS_PASSWORD="$XM_REDIS_PASSWORD" \
|
|
120
132
|
XM_ADMIN_EMAIL="$XM_ADMIN_EMAIL" XM_ADMIN_PASSWORD="$XM_ADMIN_PASSWORD" \
|
|
121
|
-
"$NODE" "$
|
|
133
|
+
"$NODE" "$PKG/deploy/settings.cjs" "$PKG/api" || { [ $? -eq 3 ] && SETUP_NEEDED=1; } || stop "the settings could not be written"
|
|
122
134
|
# THE APP OWNS ITS SETTINGS: the setup page and App settings (Super Admin)
|
|
123
135
|
# rewrite this file — atomically, a temp file beside it renamed over it — so
|
|
124
136
|
# the service user owns the file and its folder. Nobody else reads either.
|
|
@@ -126,16 +138,19 @@ chown "$USER_NAME:$USER_NAME" "$SHARED" "$ENV_FILE" && chmod 700 "$SHARED" && ch
|
|
|
126
138
|
[ ! -f "$SHARED/FIRST_LOGIN" ] || chown "$USER_NAME:$USER_NAME" "$SHARED/FIRST_LOGIN"
|
|
127
139
|
|
|
128
140
|
# ── the databases, the sign-in tables and the permissions ────────────────
|
|
129
|
-
|
|
141
|
+
# A command of this release, run as the service user with the settings
|
|
142
|
+
# loaded (dotenv never overrides what is already set: XEPLR_AUTH_MIGRATIONS
|
|
143
|
+
# below is this release's, whatever the env file says).
|
|
144
|
+
run_api() { (cd "$PKG/api" && sudo -u "$USER_NAME" env NODE_ENV=production XEPLR_AUTH_MIGRATIONS="$AUTH_MIGRATIONS" "$SERVICE_NODE" "$DOTENV_BIN" -e "$ENV_FILE" -- "$SERVICE_NODE" "$@"); }
|
|
130
145
|
if [ "$SETUP_NEEDED" = 1 ]; then
|
|
131
146
|
say "Databases: after the setup page (the API and the sign-in service make theirs when they start)"
|
|
132
147
|
else
|
|
133
148
|
say "Databases: creating any that are missing, then the sign-in migrations"
|
|
134
|
-
run_api
|
|
149
|
+
run_api "$MIGRATE_BIN" create-db --connectionName auth --connection-env AUTH_DB_CONNECTION_INFO_ENCRYPTED --db-env AUTH_DB_NAME >/dev/null \
|
|
135
150
|
|| stop "could not reach or create the sign-in database (is Postgres running, and may that user create databases?)"
|
|
136
|
-
run_api
|
|
151
|
+
run_api "$MIGRATE_BIN" create-db --connectionName api --connection-env XEPLR_MONITOR_CONNECTION --db-env DB_API >/dev/null \
|
|
137
152
|
|| stop "could not create the app's database"
|
|
138
|
-
run_api
|
|
153
|
+
run_api "$AUTH_MIGRATE_BIN" up --connection-env AUTH_DB_CONNECTION_INFO_ENCRYPTED >/dev/null \
|
|
139
154
|
|| stop "the sign-in migrations failed"
|
|
140
155
|
fi
|
|
141
156
|
|
|
@@ -144,7 +159,7 @@ fi
|
|
|
144
159
|
# alone — no wildcard, which newer sudo (sudo-rs) refuses anyway.
|
|
145
160
|
CONTROL=/usr/local/libexec/xeplr-monitor-control
|
|
146
161
|
mkdir -p /usr/local/libexec
|
|
147
|
-
sed -e "s|__SYSTEMCTL__|$SYSTEMCTL|" "$
|
|
162
|
+
sed -e "s|__SYSTEMCTL__|$SYSTEMCTL|" "$PKG/deploy/control.sh" > "$CONTROL.new"
|
|
148
163
|
chown root:root "$CONTROL.new" && chmod 755 "$CONTROL.new" && mv -f "$CONTROL.new" "$CONTROL"
|
|
149
164
|
SUDOERS_TMP=$(mktemp)
|
|
150
165
|
{
|
package/deploy/settings.cjs
CHANGED
|
@@ -23,8 +23,10 @@ var path = require('path');
|
|
|
23
23
|
|
|
24
24
|
var apiDir = path.resolve(process.argv[2]);
|
|
25
25
|
var file = process.env.XM_ENV_FILE;
|
|
26
|
-
// From the release's own packages
|
|
27
|
-
|
|
26
|
+
// From the release's own packages — resolved from the API, wherever npm put
|
|
27
|
+
// them (beside the package in a release, the checkout's root in development):
|
|
28
|
+
// the same @xeplr/setup the app runs.
|
|
29
|
+
var setup = require(require.resolve('@xeplr/setup', { paths: [apiDir] }));
|
|
28
30
|
var schema = require(path.join(apiDir, 'setup.schema.js')).config(apiDir, { NODE_ENV: 'production' }).schema;
|
|
29
31
|
|
|
30
32
|
function need(name) {
|