@xeplr/monitor 1.0.0 → 1.1.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 CHANGED
@@ -118,9 +118,9 @@ Run on the server itself, signed in to it as usual. It is on npm as [`@xeplr/mon
118
118
  npx -y @xeplr/monitor
119
119
  ```
120
120
 
121
- There is no remote deploy: the installer installs the machine it runs on, from the copy npx fetched, and fetches nothing else. From a checkout of this repository, `npm install && node deploy/deploy.js` does the same with that checkout.
121
+ 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
122
 
123
- What the server needs first: Linux with systemd and D-Bus, Node.js 20+ and npm, 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.
123
+ 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
124
 
125
125
  It asks for what it needs as it goes and stores nothing but the app's own settings file:
126
126
 
@@ -132,7 +132,7 @@ It asks for what it needs as it goes and stores nothing but the app's own settin
132
132
  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
133
 
134
134
  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>`: a copy of the package the installer runs from (without `node_modules` and `.git`), `npm ci` in `api/` and `ui/`, and the page built;
135
+ 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
136
  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
137
  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
138
  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 +158,15 @@ Keep the ports closed in the firewall (our security group allows only 22, 80 and
158
158
 
159
159
  Needs **PostgreSQL** and **Redis** running. The controls work only on Linux with systemd; on a Mac the page runs and says so.
160
160
 
161
+ 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`).
162
+
161
163
  ```bash
164
+ npm install
162
165
  cd api && cp production.env.example development.env
163
166
  # in development.env: NODE_ENV=development, LOG_DIR=./logs,
164
167
  # ENCRYPTION_KEY and AUTH_JWT_SECRET (each: openssl rand -hex 32),
165
168
  # AUTH_SUPER_ADMIN_EMAIL / _PASSWORD, the AUTH_*_URL addresses on http://localhost:19400
166
- npm install && npm run db:encrypt && npm run setup
169
+ npm run db:encrypt && npm run setup
167
170
  npm run start-auth # sign-in http://localhost:19401
168
171
  npm run start-api # the API http://localhost:19402
169
172
  cd ../ui && npm install && npm run dev # the app http://localhost:19400
@@ -174,7 +177,7 @@ Open http://localhost:19400 and sign in as the Super Admin.
174
177
  ## Tests
175
178
 
176
179
  ```bash
177
- cd api && npm test
180
+ npm test # at the root: the API's tests (api/test)
178
181
  ```
179
182
 
180
183
  - `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.
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 — @xeplr/base-apis app.",
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
- XEPLR_AUTH_MIGRATIONS=./migrations-auth,./node_modules/@xeplr/jobs/migrations-auth,./node_modules/@xeplr/backups/migrations-auth
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
@@ -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 './migrations-auth,./node_modules/@xeplr/jobs/migrations-auth,./node_modules/@xeplr/backups/migrations-auth'; },
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
- // <root>/releases/<release>/api → <root>/shared
147
- var releases = path.dirname(path.dirname(path.resolve(apiDir)));
148
- var shared = path.join(path.dirname(releases), 'shared', name);
149
- if (path.basename(releases) === 'releases' && fs.existsSync(shared)) return shared;
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, from the copy it runs from (what npx
5
- // fetched from npm, or a checkout), and fetches nothing a second time.
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 xeplr-monitor ' + version() + ' from ' + SELF,
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
- XM_SOURCE_DIR: SELF, XM_PORT_BASE: v.ports,
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 copy as the new version and restart; asks nothing');
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
- console.log('\n Updating this server to xeplr-monitor ' + version() + ' from ' + SELF + '. Everything else stays as it is.\n');
165
- await install({ XM_UPDATE: '1', XM_SOURCE_DIR: SELF });
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
- # XM_SOURCE_DIR the app's source, a folder on this server: the copy the
11
- # installer itself runs from (what npx fetched, or a checkout)
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);
@@ -73,24 +73,52 @@ fi
73
73
  # ── its own user, which reads the journal ────────────────────────────────
74
74
  id "$USER_NAME" >/dev/null 2>&1 || { useradd -r -m -d "$VAR" -s /usr/sbin/nologin "$USER_NAME"; say "User $USER_NAME made"; }
75
75
  usermod -aG systemd-journal "$USER_NAME"
76
+
77
+ # ── the Node its services run on: one THEIR user can run ─────────────────
78
+ # The services run as $USER_NAME, not as whoever runs this. A Node from nvm
79
+ # lives in that person's home (/root/.nvm/…), which $USER_NAME cannot enter:
80
+ # systemd then fails every start with "Permission denied" (status 203/EXEC).
81
+ # So the Node found above is used only if $USER_NAME can run it; else the
82
+ # server's own (/usr/bin, /usr/local/bin). Checked before anything changes,
83
+ # so a refusal leaves the running install as it was.
84
+ SERVICE_NODE=""
85
+ for candidate in "$NODE" /usr/bin/node /usr/local/bin/node; do
86
+ [ -x "$candidate" ] || continue
87
+ major=$(sudo -u "$USER_NAME" "$candidate" -p 'process.versions.node.split(".")[0]' 2>/dev/null || true)
88
+ if [ -n "$major" ] && [ "$major" -ge 20 ]; then SERVICE_NODE=$candidate; break; fi
89
+ done
90
+ [ -n "$SERVICE_NODE" ] || stop "the monitor's services run as $USER_NAME, which cannot run Node.js at $NODE (in a home folder, e.g. from nvm) and there is no Node.js 20+ at /usr/bin or /usr/local/bin. Install Node.js system-wide (e.g. NodeSource's package), then run this again — the running install was not changed"
91
+ [ "$SERVICE_NODE" = "$NODE" ] || say "Services run on $SERVICE_NODE ($NODE is in a home folder $USER_NAME cannot enter)"
76
92
  mkdir -p "$VAR/logs" "$VAR/data" "$SHARED" "$APP/releases"
77
93
  chown -R "$USER_NAME:$USER_NAME" "$VAR" && chmod 750 "$VAR"
78
94
 
79
- # ── this release: the source, its packages, the page built ───────────────
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.
80
100
  REL=$APP/releases/$(date -u +%Y%m%d%H%M%S)
81
101
  mkdir -p "$REL"
82
- # The copy the installer runs from; nothing is fetched.
83
- [ -n "${XM_SOURCE_DIR:-}" ] && [ -d "$XM_SOURCE_DIR" ] || stop "no source folder to install from (run deploy/deploy.js)"
84
- tar -C "$XM_SOURCE_DIR" --exclude=node_modules --exclude=.git -cf - . | tar -xf - -C "$REL" \
85
- || stop "could not copy the source from $XM_SOURCE_DIR"
86
- say "Source: $XM_SOURCE_DIR"
87
- [ -f "$REL/api/bin/www" ] && [ -f "$REL/ui/package.json" ] || stop "that source is not xeplr-monitor (no api/bin/www or ui/)"
88
- say "Installing packages (api, ui)"
89
- (cd "$REL/api" && npm ci --no-fund --no-audit --no-update-notifier --loglevel=error >/dev/null)
90
- (cd "$REL/ui" && npm ci --no-fund --no-audit --no-update-notifier --loglevel=error >/dev/null && npx vite build --logLevel error >/dev/null)
91
- [ -f "$REL/ui/dist/index.html" ] || stop "the page did not build"
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")"
92
112
  chown -R "$USER_NAME:$USER_NAME" "$REL"
93
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
+
94
122
  # ── settings: written once, kept after; secrets made here, never asked ───
95
123
  # Through the app's own schema (api/setup.schema.js, @xeplr/setup): what is
96
124
  # missing after this is asked on the setup page, never a failed deploy.
@@ -102,7 +130,7 @@ env XM_ENV_FILE="$ENV_FILE" XM_AUTH_PORT="$AUTH_PORT" XM_API_PORT="$API_PORT" XM
102
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" \
103
131
  XM_REDIS_HOST="$XM_REDIS_HOST" XM_REDIS_PORT="$XM_REDIS_PORT" XM_REDIS_PASSWORD="$XM_REDIS_PASSWORD" \
104
132
  XM_ADMIN_EMAIL="$XM_ADMIN_EMAIL" XM_ADMIN_PASSWORD="$XM_ADMIN_PASSWORD" \
105
- "$NODE" "$REL/deploy/settings.cjs" "$REL/api" || { [ $? -eq 3 ] && SETUP_NEEDED=1; } || stop "the settings could not be written"
133
+ "$NODE" "$PKG/deploy/settings.cjs" "$PKG/api" || { [ $? -eq 3 ] && SETUP_NEEDED=1; } || stop "the settings could not be written"
106
134
  # THE APP OWNS ITS SETTINGS: the setup page and App settings (Super Admin)
107
135
  # rewrite this file — atomically, a temp file beside it renamed over it — so
108
136
  # the service user owns the file and its folder. Nobody else reads either.
@@ -110,16 +138,19 @@ chown "$USER_NAME:$USER_NAME" "$SHARED" "$ENV_FILE" && chmod 700 "$SHARED" && ch
110
138
  [ ! -f "$SHARED/FIRST_LOGIN" ] || chown "$USER_NAME:$USER_NAME" "$SHARED/FIRST_LOGIN"
111
139
 
112
140
  # ── the databases, the sign-in tables and the permissions ────────────────
113
- run_api() { (cd "$REL/api" && sudo -u "$USER_NAME" env NODE_ENV=production node_modules/.bin/dotenv -e "$ENV_FILE" -- "$@"); }
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" "$@"); }
114
145
  if [ "$SETUP_NEEDED" = 1 ]; then
115
146
  say "Databases: after the setup page (the API and the sign-in service make theirs when they start)"
116
147
  else
117
148
  say "Databases: creating any that are missing, then the sign-in migrations"
118
- run_api node_modules/.bin/xeplr-migrate create-db --connectionName auth --connection-env AUTH_DB_CONNECTION_INFO_ENCRYPTED --db-env AUTH_DB_NAME >/dev/null \
149
+ run_api "$MIGRATE_BIN" create-db --connectionName auth --connection-env AUTH_DB_CONNECTION_INFO_ENCRYPTED --db-env AUTH_DB_NAME >/dev/null \
119
150
  || stop "could not reach or create the sign-in database (is Postgres running, and may that user create databases?)"
120
- run_api node_modules/.bin/xeplr-migrate create-db --connectionName api --connection-env XEPLR_MONITOR_CONNECTION --db-env DB_API >/dev/null \
151
+ run_api "$MIGRATE_BIN" create-db --connectionName api --connection-env XEPLR_MONITOR_CONNECTION --db-env DB_API >/dev/null \
121
152
  || stop "could not create the app's database"
122
- run_api node_modules/.bin/xeplr-auth-migrate up --connection-env AUTH_DB_CONNECTION_INFO_ENCRYPTED >/dev/null \
153
+ run_api "$AUTH_MIGRATE_BIN" up --connection-env AUTH_DB_CONNECTION_INFO_ENCRYPTED >/dev/null \
123
154
  || stop "the sign-in migrations failed"
124
155
  fi
125
156
 
@@ -128,7 +159,7 @@ fi
128
159
  # alone — no wildcard, which newer sudo (sudo-rs) refuses anyway.
129
160
  CONTROL=/usr/local/libexec/xeplr-monitor-control
130
161
  mkdir -p /usr/local/libexec
131
- sed -e "s|__SYSTEMCTL__|$SYSTEMCTL|" "$REL/deploy/control.sh" > "$CONTROL.new"
162
+ sed -e "s|__SYSTEMCTL__|$SYSTEMCTL|" "$PKG/deploy/control.sh" > "$CONTROL.new"
132
163
  chown root:root "$CONTROL.new" && chmod 755 "$CONTROL.new" && mv -f "$CONTROL.new" "$CONTROL"
133
164
  SUDOERS_TMP=$(mktemp)
134
165
  {
@@ -168,10 +199,10 @@ UNIT
168
199
  }
169
200
  # Restart=always is also how settings take effect: the setup page and App
170
201
  # settings exit 75 after writing $ENV_FILE, and systemd starts them again.
171
- unit xeplr-monitor-auth "xeplr-monitor: sign-in (@xeplr/auth)" "$NODE $APP/current/api/bin/auth.js" ""
202
+ unit xeplr-monitor-auth "xeplr-monitor: sign-in (@xeplr/auth)" "$SERVICE_NODE $APP/current/api/bin/auth.js" ""
172
203
  # The API controls services: the journal group reads their logs; sudo needs
173
204
  # to gain privileges, so NoNewPrivileges stays off — sudoers is the limit.
174
- unit xeplr-monitor-api "xeplr-monitor: the API (services, logs, start/stop)" "$NODE $APP/current/api/bin/www" "SupplementaryGroups=systemd-journal"
205
+ unit xeplr-monitor-api "xeplr-monitor: the API (services, logs, start/stop)" "$SERVICE_NODE $APP/current/api/bin/www" "SupplementaryGroups=systemd-journal"
175
206
  systemctl daemon-reload
176
207
  systemctl enable xeplr-monitor-auth xeplr-monitor-api >/dev/null 2>&1
177
208
  systemctl restart xeplr-monitor-auth xeplr-monitor-api
@@ -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: the same @xeplr/setup the app runs.
27
- var setup = require(path.join(apiDir, 'node_modules', '@xeplr', 'setup'));
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) {
@@ -1,13 +1,13 @@
1
1
  {
2
- "name": "@xeplr-monitor/api",
3
- "version": "1.0.0",
2
+ "name": "@xeplr/monitor",
3
+ "version": "1.1.0",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
- "name": "@xeplr-monitor/api",
9
- "version": "1.0.0",
10
- "license": "UNLICENSED",
8
+ "name": "@xeplr/monitor",
9
+ "version": "1.1.0",
10
+ "license": "MIT",
11
11
  "dependencies": {
12
12
  "@aws-sdk/client-s3": "^3.1168.0",
13
13
  "@aws-sdk/lib-storage": "^3.1168.0",
@@ -15,17 +15,21 @@
15
15
  "@xeplr/auth": "^1.3.0",
16
16
  "@xeplr/backups": "^0.1.1",
17
17
  "@xeplr/base-apis": "^3.1.3",
18
+ "@xeplr/cli": "^0.3.8",
18
19
  "@xeplr/db": "^1.1.0",
19
20
  "@xeplr/jobs": "^3.2.0",
20
21
  "@xeplr/setup": "^0.2.0",
21
22
  "@xeplr/utils": "^1.0.11",
22
23
  "dotenv": "^17.4.2",
24
+ "dotenv-cli": "^8.0.0",
23
25
  "express": "^5.2.0",
24
26
  "mysql2": "^3.24.6"
25
27
  },
26
- "devDependencies": {
27
- "@xeplr/cli": "^0.1.8",
28
- "dotenv-cli": "^8.0.0"
28
+ "bin": {
29
+ "xeplr-monitor": "deploy/deploy.js"
30
+ },
31
+ "engines": {
32
+ "node": ">=20"
29
33
  }
30
34
  },
31
35
  "node_modules/@aws-sdk/checksums": {
@@ -656,10 +660,9 @@
656
660
  }
657
661
  },
658
662
  "node_modules/@xeplr/cli": {
659
- "version": "0.1.11",
660
- "resolved": "https://registry.npmjs.org/@xeplr/cli/-/cli-0.1.11.tgz",
661
- "integrity": "sha512-ATn8TQGYyhlq0H3HYEikBKsj+5COcWMJ2+Oj60iKqVHcisJeqYAfMvzO5q9HB77FDBoxfaxpUgQ4Eu9V7cWm/A==",
662
- "dev": true,
663
+ "version": "0.3.15",
664
+ "resolved": "https://registry.npmjs.org/@xeplr/cli/-/cli-0.3.15.tgz",
665
+ "integrity": "sha512-vhEL5QB+8+iYA3o7FSlkiWJ10u09wtfBmAifsXnKPJSHl/Efchs1gVeP+d0Pd6/Y5BdIH2G3uqaxaLhyPI7nhg==",
663
666
  "license": "MIT",
664
667
  "dependencies": {
665
668
  "@xeplr/utils": "^1.0.7"
@@ -1078,7 +1081,6 @@
1078
1081
  "version": "7.1.2",
1079
1082
  "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.1.2.tgz",
1080
1083
  "integrity": "sha512-8BPK2r+sKWC8sDinRwLwrFMq2lXTBBwzYYEB9LiCznfMurOi4FOvQflq3XOLGG+HPdMSVpe+k/s96+AtDEOgPg==",
1081
- "dev": true,
1082
1084
  "license": "MIT",
1083
1085
  "dependencies": {
1084
1086
  "path-key": "^3.1.0",
@@ -1146,7 +1148,6 @@
1146
1148
  "version": "8.0.0",
1147
1149
  "resolved": "https://registry.npmjs.org/dotenv-cli/-/dotenv-cli-8.0.0.tgz",
1148
1150
  "integrity": "sha512-aLqYbK7xKOiTMIRf1lDPbI+Y+Ip/wo5k3eyp6ePysVaSqbyxjyK3dK35BTxG+rmd7djf5q2UPs4noPNH+cj0Qw==",
1149
- "dev": true,
1150
1151
  "license": "MIT",
1151
1152
  "dependencies": {
1152
1153
  "cross-spawn": "^7.0.6",
@@ -1162,7 +1163,6 @@
1162
1163
  "version": "16.6.1",
1163
1164
  "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.6.1.tgz",
1164
1165
  "integrity": "sha512-uBq4egWHTcTt33a72vpSG0z3HnPuIl6NqYcTrKEg2azoEyl2hpW0zqlxysq2pK9HlDIHyHyakeYaYnSAwd8bow==",
1165
- "dev": true,
1166
1166
  "license": "BSD-2-Clause",
1167
1167
  "engines": {
1168
1168
  "node": ">=12"
@@ -1175,7 +1175,6 @@
1175
1175
  "version": "10.0.0",
1176
1176
  "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-10.0.0.tgz",
1177
1177
  "integrity": "sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==",
1178
- "dev": true,
1179
1178
  "license": "BSD-2-Clause",
1180
1179
  "engines": {
1181
1180
  "node": ">=12"
@@ -1647,7 +1646,6 @@
1647
1646
  "version": "2.0.0",
1648
1647
  "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
1649
1648
  "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==",
1650
- "dev": true,
1651
1649
  "license": "ISC"
1652
1650
  },
1653
1651
  "node_modules/json-schema-traverse": {
@@ -1923,7 +1921,6 @@
1923
1921
  "version": "1.2.8",
1924
1922
  "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
1925
1923
  "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
1926
- "dev": true,
1927
1924
  "license": "MIT",
1928
1925
  "funding": {
1929
1926
  "url": "https://github.com/sponsors/ljharb"
@@ -2141,7 +2138,6 @@
2141
2138
  "version": "3.1.1",
2142
2139
  "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz",
2143
2140
  "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==",
2144
- "dev": true,
2145
2141
  "license": "MIT",
2146
2142
  "engines": {
2147
2143
  "node": ">=8"
@@ -2553,7 +2549,6 @@
2553
2549
  "version": "2.0.0",
2554
2550
  "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
2555
2551
  "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==",
2556
- "dev": true,
2557
2552
  "license": "MIT",
2558
2553
  "dependencies": {
2559
2554
  "shebang-regex": "^3.0.0"
@@ -2566,7 +2561,6 @@
2566
2561
  "version": "3.0.0",
2567
2562
  "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz",
2568
2563
  "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==",
2569
- "dev": true,
2570
2564
  "license": "MIT",
2571
2565
  "engines": {
2572
2566
  "node": ">=8"
@@ -2808,7 +2802,6 @@
2808
2802
  "version": "2.0.2",
2809
2803
  "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
2810
2804
  "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
2811
- "dev": true,
2812
2805
  "license": "ISC",
2813
2806
  "dependencies": {
2814
2807
  "isexe": "^2.0.0"