underpost 2.92.0 → 2.95.3

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.
Files changed (35) hide show
  1. package/.github/workflows/pwa-microservices-template-page.cd.yml +5 -4
  2. package/README.md +4 -5
  3. package/bin/build.js +6 -1
  4. package/bin/deploy.js +2 -69
  5. package/cli.md +100 -92
  6. package/manifests/deployment/dd-default-development/deployment.yaml +4 -4
  7. package/manifests/deployment/dd-test-development/deployment.yaml +2 -2
  8. package/package.json +1 -1
  9. package/scripts/disk-clean.sh +216 -0
  10. package/scripts/ssh-cluster-info.sh +4 -3
  11. package/src/cli/cluster.js +1 -1
  12. package/src/cli/db.js +80 -89
  13. package/src/cli/deploy.js +77 -13
  14. package/src/cli/image.js +198 -133
  15. package/src/cli/index.js +60 -81
  16. package/src/cli/lxd.js +73 -74
  17. package/src/cli/monitor.js +20 -9
  18. package/src/cli/repository.js +86 -3
  19. package/src/cli/run.js +167 -63
  20. package/src/cli/ssh.js +351 -134
  21. package/src/index.js +1 -1
  22. package/src/monitor.js +11 -1
  23. package/src/server/backup.js +1 -1
  24. package/src/server/conf.js +1 -1
  25. package/src/server/dns.js +88 -1
  26. package/src/server/process.js +6 -1
  27. package/scripts/snap-clean.sh +0 -26
  28. package/src/client/public/default/plantuml/client-conf.svg +0 -1
  29. package/src/client/public/default/plantuml/client-schema.svg +0 -1
  30. package/src/client/public/default/plantuml/cron-conf.svg +0 -1
  31. package/src/client/public/default/plantuml/cron-schema.svg +0 -1
  32. package/src/client/public/default/plantuml/server-conf.svg +0 -1
  33. package/src/client/public/default/plantuml/server-schema.svg +0 -1
  34. package/src/client/public/default/plantuml/ssr-conf.svg +0 -1
  35. package/src/client/public/default/plantuml/ssr-schema.svg +0 -1
package/src/cli/lxd.js CHANGED
@@ -8,6 +8,9 @@ import { getNpmRootPath } from '../server/conf.js';
8
8
  import { getLocalIPv4Address } from '../server/dns.js';
9
9
  import { pbcopy, shellExec } from '../server/process.js';
10
10
  import fs from 'fs-extra';
11
+ import { loggerFactory } from '../server/logger.js';
12
+
13
+ const logger = loggerFactory(import.meta);
11
14
 
12
15
  /**
13
16
  * @class UnderpostLxd
@@ -37,7 +40,10 @@ class UnderpostLxd {
37
40
  * @param {string} [options.expose=''] - Expose ports from a VM to the host (format: 'vmName:port1,port2').
38
41
  * @param {string} [options.deleteExpose=''] - Delete exposed ports from a VM (format: 'vmName:port1,port2').
39
42
  * @param {string} [options.test=''] - Test health, status and network connectivity for a VM.
40
- * @param {string} [options.autoExposeK8sPorts=''] - Automatically expose common Kubernetes ports for the VM.
43
+ * @param {string} [options.workflowId=''] - Workflow identifier for workflow execution.
44
+ * @param {string} [options.vmId=''] - VM identifier for workflow execution.
45
+ * @param {string} [options.deployId=''] - Deployment identifier for workflow execution.
46
+ * @param {string} [options.namespace=''] - Namespace for workflow execution.
41
47
  * @memberof UnderpostLxd
42
48
  */
43
49
  async callback(
@@ -59,7 +65,10 @@ class UnderpostLxd {
59
65
  expose: '',
60
66
  deleteExpose: '',
61
67
  test: '',
62
- autoExposeK8sPorts: '',
68
+ workflowId: '',
69
+ vmId: '',
70
+ deployId: '',
71
+ namespace: '',
63
72
  },
64
73
  ) {
65
74
  const npmRoot = getNpmRootPath();
@@ -86,9 +95,21 @@ ipv4.dhcp=true \
86
95
  ipv6.address=none`);
87
96
  }
88
97
  if (options.createAdminProfile === true) {
89
- pbcopy(`lxc profile create admin-profile`);
90
- shellExec(`cat ${underpostRoot}/manifests/lxd/lxd-admin-profile.yaml | lxc profile edit admin-profile`);
91
- shellExec(`lxc profile show admin-profile`);
98
+ const existingProfiles = await new Promise((resolve) => {
99
+ shellExec(`lxc profile show admin-profile`, {
100
+ silent: true,
101
+ callback: (...args) => {
102
+ return resolve(JSON.stringify(args));
103
+ },
104
+ });
105
+ });
106
+ if (existingProfiles.toLowerCase().match('error')) {
107
+ logger.warn('Profile does not exist. Using following command to create admin-profile:');
108
+ pbcopy(`lxc profile create admin-profile`);
109
+ } else {
110
+ shellExec(`cat ${underpostRoot}/manifests/lxd/lxd-admin-profile.yaml | lxc profile edit admin-profile`);
111
+ shellExec(`lxc profile show admin-profile`);
112
+ }
92
113
  }
93
114
  if (options.createVm && typeof options.createVm === 'string') {
94
115
  pbcopy(
@@ -108,9 +129,10 @@ ipv6.address=none`);
108
129
  // Default to kubeadm if not K3s
109
130
  flag = ' -s -- --kubeadm';
110
131
  }
111
- shellExec(`lxc exec ${options.initVm} -- bash -c 'mkdir -p /home/dd/engine'`);
112
- shellExec(`lxc file push /home/dd/engine/engine-private ${options.initVm}/home/dd/engine --recursive`);
113
- shellExec(`lxc file push /home/dd/engine/manifests ${options.initVm}/home/dd/engine --recursive`);
132
+ await UnderpostLxd.API.runWorkflow({
133
+ workflowId: 'engine',
134
+ vmName: options.initVm,
135
+ });
114
136
  } else if (options.worker == true) {
115
137
  if (options.k3s === true) {
116
138
  flag = ' -s -- --worker --k3s';
@@ -123,74 +145,16 @@ ipv6.address=none`);
123
145
  shellExec(`cat ${underpostRoot}/manifests/lxd/underpost-setup.sh | lxc exec ${options.initVm} -- bash${flag}`);
124
146
  console.log(`underpost-setup.sh execution completed on VM: ${options.initVm}`);
125
147
  }
126
- // --- Automatic Kubernetes Port Exposure ---
127
- if (options.autoExposeK8sPorts && typeof options.autoExposeK8sPorts === 'string') {
128
- console.log(`Automatically exposing Kubernetes ports for VM: ${options.autoExposeK8sPorts}`);
129
- const vmName = options.autoExposeK8sPorts;
130
- const hostIp = getLocalIPv4Address();
131
- let vmIp = '';
132
- let retries = 0;
133
- const maxRetries = 10;
134
- const delayMs = 5000; // 5 seconds
135
148
 
136
- // Wait for VM to get an IP address
137
- while (!vmIp && retries < maxRetries) {
138
- try {
139
- console.log(`Attempting to get IPv4 address for ${vmName} (Attempt ${retries + 1}/${maxRetries})...`);
140
- vmIp = shellExec(
141
- `lxc list ${vmName} --format json | jq -r '.[0].state.network.enp5s0.addresses[] | select(.family=="inet") | .address'`,
142
- { stdout: true },
143
- ).trim();
144
- if (vmIp) {
145
- console.log(`IPv4 address found for ${vmName}: ${vmIp}`);
146
- } else {
147
- console.log(`IPv4 address not yet available for ${vmName}. Retrying in ${delayMs / 1000} seconds...`);
148
- await new Promise((resolve) => setTimeout(resolve, delayMs));
149
- }
150
- } catch (error) {
151
- console.error(`Error getting IPv4 address for exposure: ${error.message}`);
152
- console.log(`Retrying in ${delayMs / 1000} seconds...`);
153
- await new Promise((resolve) => setTimeout(resolve, delayMs));
154
- }
155
- retries++;
156
- }
157
-
158
- if (!vmIp) {
159
- console.error(`Failed to get VM IP for ${vmName} after ${maxRetries} attempts. Cannot expose ports.`);
160
- return;
161
- }
162
-
163
- let portsToExpose = [];
164
- if (options.control === true) {
165
- // Kubernetes API Server (Kubeadm and K3s both use 6443 by default)
166
- portsToExpose.push('6443');
167
- // Standard HTTP/HTTPS for Ingress if deployed
168
- portsToExpose.push('80');
169
- portsToExpose.push('443');
170
- }
171
- // Add common NodePorts if needed, or rely on explicit 'expose'
172
- portsToExpose.push('30000'); // Example NodePort
173
- portsToExpose.push('30001'); // Example NodePort
174
- portsToExpose.push('30002'); // Example NodePort
175
-
176
- const protocols = ['tcp']; // Most K8s services are TCP, UDP for some like DNS
177
-
178
- for (const port of portsToExpose) {
179
- for (const protocol of protocols) {
180
- const deviceName = `${vmName}-${protocol}-port-${port}`;
181
- try {
182
- // Remove existing device first to avoid conflicts if re-running
183
- shellExec(`lxc config device remove ${vmName} ${deviceName} || true`);
184
- shellExec(
185
- `lxc config device add ${vmName} ${deviceName} proxy listen=${protocol}:${hostIp}:${port} connect=${protocol}:${vmIp}:${port} nat=true`,
186
- );
187
- console.log(`Exposed ${protocol}:${hostIp}:${port} -> ${vmIp}:${port} for ${vmName}`);
188
- } catch (error) {
189
- console.error(`Failed to expose port ${port} for ${vmName}: ${error.message}`);
190
- }
191
- }
192
- }
149
+ if (options.workflowId) {
150
+ await UnderpostLxd.API.runWorkflow({
151
+ workflowId: options.workflowId,
152
+ vmName: options.vmId,
153
+ deployId: options.deployId,
154
+ dev: options.dev,
155
+ });
193
156
  }
157
+
194
158
  if (options.joinNode && typeof options.joinNode === 'string') {
195
159
  const [workerNode, controlNode] = options.joinNode.split(',');
196
160
  // Determine if it's a Kubeadm or K3s join
@@ -396,6 +360,41 @@ ipv6.address=none`);
396
360
  console.log(`\nComprehensive test for VM: ${vmName} completed.`);
397
361
  }
398
362
  },
363
+ /**
364
+ * @method runWorkflow
365
+ * @description Executes predefined workflows on LXD VMs.
366
+ * @param {object} params - Parameters for the workflow.
367
+ * @param {string} params.workflowId - The workflow id to execute (e.g., 'init').
368
+ * @param {string} params.vmName - The name of the VM to run the workflow on.
369
+ * @param {string} [params.deployId] - Optional deployment identifier.
370
+ * @param {boolean} [params.dev=false] - Run in development mode (adjusts paths).
371
+ * @memberof UnderpostLxd
372
+ */
373
+ async runWorkflow({ workflowId, vmName, deployId, dev }) {
374
+ switch (workflowId) {
375
+ case 'engine': {
376
+ const basePath = `/home/dd`;
377
+ const subDir = 'engine';
378
+ shellExec(`lxc exec ${vmName} -- bash -c 'rm ${basePath} && mkdir -p ${basePath}/${subDir}'`);
379
+ shellExec(`lxc file push ${basePath}/${subDir}/package.json ${vmName}${basePath}/${subDir}/package.json`);
380
+ shellExec(`lxc file push ${basePath}/${subDir}/src ${vmName}${basePath}/${subDir} --recursive`);
381
+ shellExec(`lxc file push ${basePath}/${subDir}/${subDir}-private ${vmName}${basePath}/${subDir} --recursive`);
382
+ break;
383
+ }
384
+ case 'setup-underpost-engine': {
385
+ const basePath = `/home/dd/engine`;
386
+ shellExec(`lxc exec ${vmName} -- bash -lc 'nvm use $(node --version) && cd ${basePath} && npm install'`);
387
+ shellExec(`lxc exec ${vmName} -- bash -lc 'underpost run secret'`);
388
+ break;
389
+ }
390
+ case 'k3s-setup': {
391
+ shellExec(
392
+ `lxc exec ${vmName} -- bash -lc 'cd /home/dd/engine && node bin cluster --dev --reset && node bin cluster --dev --k3s'`,
393
+ );
394
+ break;
395
+ }
396
+ }
397
+ },
399
398
  };
400
399
  }
401
400
 
@@ -37,9 +37,9 @@ class UnderpostMonitor {
37
37
  * @param {boolean} [options.single=false] - Perform a single health check and exit.
38
38
  * @param {string} [options.msInterval=''] - Interval in milliseconds for periodic health checks.
39
39
  * @param {string} [options.type=''] - Type of deployment (e.g., 'blue-green', 'remote').
40
- * @param {string} [options.replicas=''] - Number of replicas for the deployment.
40
+ * @param {string} [options.replicas='1'] - Number of replicas for the deployment. Defaults to 1.
41
41
  * @param {boolean} [options.sync=false] - Synchronize traffic switching with the deployment.
42
- * @param {string} [options.namespace=''] - Kubernetes namespace for the deployment.
42
+ * @param {string} [options.namespace='default'] - Kubernetes namespace for the deployment. Defaults to 'default'.
43
43
  * @param {object} [commanderOptions] - Options passed from the command line interface.
44
44
  * @param {object} [auxRouter] - Optional router configuration for the deployment.
45
45
  * @memberof UnderpostMonitor
@@ -47,11 +47,20 @@ class UnderpostMonitor {
47
47
  async callback(
48
48
  deployId,
49
49
  env = 'development',
50
- options = { now: false, single: false, msInterval: '', type: '', replicas: '', sync: false, namespace: '' },
50
+ options = {
51
+ now: false,
52
+ single: false,
53
+ msInterval: '',
54
+ type: '',
55
+ replicas: '1',
56
+ sync: false,
57
+ namespace: 'default',
58
+ },
51
59
  commanderOptions,
52
60
  auxRouter,
53
61
  ) {
54
62
  if (!options.namespace) options.namespace = 'default';
63
+ if (!options.replicas) options.replicas = '1';
55
64
  if (deployId === 'dd' && fs.existsSync(`./engine-private/deploy/dd.router`)) {
56
65
  for (const _deployId of fs.readFileSync(`./engine-private/deploy/dd.router`, 'utf8').split(','))
57
66
  UnderpostMonitor.API.callback(
@@ -97,10 +106,10 @@ class UnderpostMonitor {
97
106
  if (traffic === 'blue') traffic = 'green';
98
107
  else traffic = 'blue';
99
108
  UnderpostRootEnv.API.set(`${deployId}-${env}-traffic`, traffic);
100
- const namespace = options.namespace || 'default';
109
+ const namespace = options.namespace;
101
110
  shellExec(
102
111
  `node bin deploy --info-router --build-manifest --traffic ${traffic} --replicas ${
103
- options.replicas ? options.replicas : 1
112
+ options.replicas
104
113
  } --namespace ${namespace} ${deployId} ${env}`,
105
114
  );
106
115
  shellExec(`sudo kubectl apply -f ./engine-private/conf/${deployId}/build/${env}/proxy.yaml -n ${namespace}`);
@@ -155,7 +164,7 @@ class UnderpostMonitor {
155
164
  fs.readFileSync(`./engine-private/conf/${deployId}/conf.server.json`, 'utf8'),
156
165
  );
157
166
 
158
- const namespace = options.namespace || 'default';
167
+ const namespace = options.namespace;
159
168
  UnderpostDeploy.API.configMap(env, namespace);
160
169
 
161
170
  for (const host of Object.keys(confServer)) {
@@ -208,11 +217,13 @@ class UnderpostMonitor {
208
217
  monitorTrafficName = undefined;
209
218
  monitorPodName = undefined;
210
219
  }
211
- const checkDeploymentReadyStatus = () => {
212
- const { ready, notReadyPods, readyPods } = UnderpostDeploy.API.checkDeploymentReadyStatus(
220
+ const checkDeploymentReadyStatus = async () => {
221
+ const { ready, notReadyPods, readyPods } = await UnderpostDeploy.API.checkDeploymentReadyStatus(
213
222
  deployId,
214
223
  env,
215
224
  traffic,
225
+ [],
226
+ options.namespace,
216
227
  );
217
228
  if (ready) {
218
229
  monitorPodName = readyPods[0].NAME;
@@ -220,7 +231,7 @@ class UnderpostMonitor {
220
231
  }
221
232
  };
222
233
  if (!monitorPodName) {
223
- checkDeploymentReadyStatus();
234
+ await checkDeploymentReadyStatus();
224
235
  monitorCallBack(resolve, reject);
225
236
  return;
226
237
  }
@@ -10,8 +10,8 @@ import { pbcopy, shellCd, shellExec } from '../server/process.js';
10
10
  import { actionInitLog, loggerFactory } from '../server/logger.js';
11
11
  import fs from 'fs-extra';
12
12
  import { getNpmRootPath } from '../server/conf.js';
13
- import UnderpostStartUp from '../server/start.js';
14
13
  import { Config } from '../server/conf.js';
14
+ import { DefaultConf } from '../../conf.js';
15
15
 
16
16
  dotenv.config();
17
17
 
@@ -244,6 +244,8 @@ class UnderpostRepository {
244
244
  * @param {boolean} [options.cleanTemplate=false] - If true, cleans the pwa-microservices-template build directory.
245
245
  * @param {boolean} [options.build=false] - If true, builds the deployment to pwa-microservices-template (requires deployId).
246
246
  * @param {boolean} [options.syncConf=false] - If true, syncs configuration to private repositories (requires deployId).
247
+ * @param {boolean} [options.defaultConf=false] - If true, updates the default configuration file (requires deployId).
248
+ * @param {string} [options.confWorkflowId=''] - If provided, uses this configuration workflow ID.
247
249
  * @returns {Promise<boolean>} A promise that resolves when the initialization is complete.
248
250
  * @memberof UnderpostRepository
249
251
  */
@@ -259,6 +261,8 @@ class UnderpostRepository {
259
261
  cleanTemplate: false,
260
262
  build: false,
261
263
  syncConf: false,
264
+ defaultConf: false,
265
+ confWorkflowId: '',
262
266
  },
263
267
  ) {
264
268
  return new Promise(async (resolve, reject) => {
@@ -277,10 +281,15 @@ class UnderpostRepository {
277
281
  return resolve(true);
278
282
  }
279
283
 
284
+ // Handle defaultConf operation
285
+ if (options.defaultConf) {
286
+ UnderpostRepository.API.updateDefaultConf(options);
287
+ return resolve(true);
288
+ }
289
+
280
290
  if (options.deployId) {
281
291
  let deployId = options.deployId;
282
292
  if (!deployId.startsWith('dd-')) deployId = `dd-${deployId}`;
283
-
284
293
  // Handle purge operation
285
294
  if (options.purge) {
286
295
  logger.info(`Purging deploy ID: ${deployId}`);
@@ -450,7 +459,9 @@ class UnderpostRepository {
450
459
  const privateRepoPath = `../${privateRepoName}`;
451
460
  if (fs.existsSync(privateRepoPath)) fs.removeSync(privateRepoPath);
452
461
  shellExec(`cd .. && underpost clone ${process.env.GITHUB_USERNAME}/${privateRepoName}`);
453
- shellExec(`cd ${privateRepoPath} && underpost pull . ${process.env.GITHUB_USERNAME}/${privateRepoName}`);
462
+ shellExec(`cd ${privateRepoPath} && underpost pull . ${process.env.GITHUB_USERNAME}/${privateRepoName}`, {
463
+ silent: true,
464
+ });
454
465
  shellExec(`underpost run secret`);
455
466
  shellExec(`underpost run underpost-config`);
456
467
  const packageJsonDeploy = JSON.parse(fs.readFileSync(`./engine-private/conf/${deployId}/package.json`, 'utf8'));
@@ -504,6 +515,78 @@ Prevent build private config repo.`,
504
515
  return line;
505
516
  });
506
517
  },
518
+ /**
519
+ * Updates the default configuration file based on the provided options.
520
+ * @param {object} [options={ deployId: '' }] - The options for updating the configuration.
521
+ * @param {string} [options.deployId=''] - The deployment ID to use for configuration.
522
+ * @param {string} [options.confWorkflowId=''] - The configuration workflow ID to use.
523
+ * @memberof UnderpostRepository
524
+ */
525
+ updateDefaultConf(options = { deployId: '', confWorkflowId: '' }) {
526
+ const defaultServer = DefaultConf.server['default.net']['/'];
527
+ let { deployId, confWorkflowId } = options;
528
+ let defaultConf = false;
529
+
530
+ // Custom workflow configurations
531
+ if (confWorkflowId)
532
+ switch (confWorkflowId) {
533
+ case 'dd-github-pages': {
534
+ const host = `${process.env.GITHUB_USERNAME ? process.env.GITHUB_USERNAME : 'underpostnet'}.github.io`;
535
+ const path = '/pwa-microservices-template-ghpkg';
536
+ DefaultConf.server = {
537
+ [host]: { [path]: defaultServer },
538
+ };
539
+ DefaultConf.server[host][path].apiBaseProxyPath = '/';
540
+ DefaultConf.server[host][path].apiBaseHost = 'www.nexodev.org';
541
+ defaultConf = true;
542
+ break;
543
+ }
544
+ case 'template': {
545
+ const host = 'default.net';
546
+ const path = '/';
547
+ DefaultConf.server[host][path].valkey = {
548
+ port: 6379,
549
+ host: 'valkey-service.default.svc.cluster.local',
550
+ };
551
+ // mongodb-0.mongodb-service
552
+ DefaultConf.server[host][path].db.host = 'mongodb://mongodb-service:27017';
553
+ defaultConf = true;
554
+ break;
555
+ }
556
+ default:
557
+ logger.error(`Unknown confWorkflowId: ${confWorkflowId}.`);
558
+ return;
559
+ }
560
+ else if (deployId && fs.existsSync(`./engine-private/conf/${deployId}`)) {
561
+ DefaultConf.client = JSON.parse(fs.readFileSync(`./engine-private/conf/${deployId}/conf.client.json`, 'utf8'));
562
+ DefaultConf.server = JSON.parse(fs.readFileSync(`./engine-private/conf/${deployId}/conf.server.json`, 'utf8'));
563
+ DefaultConf.ssr = JSON.parse(fs.readFileSync(`./engine-private/conf/${deployId}/conf.ssr.json`, 'utf8'));
564
+ // DefaultConf.cron = JSON.parse(fs.readFileSync(`./engine-private/conf/${deployId}/conf.cron.json`, 'utf8'));
565
+
566
+ for (const host of Object.keys(DefaultConf.server)) {
567
+ for (const path of Object.keys(DefaultConf.server[host])) {
568
+ DefaultConf.server[host][path].db = defaultServer.db;
569
+ DefaultConf.server[host][path].mailer = defaultServer.mailer;
570
+
571
+ delete DefaultConf.server[host][path]._wp_client;
572
+ delete DefaultConf.server[host][path]._wp_git;
573
+ delete DefaultConf.server[host][path]._wp_directory;
574
+ delete DefaultConf.server[host][path].wp;
575
+ delete DefaultConf.server[host][path].git;
576
+ delete DefaultConf.server[host][path].directory;
577
+ }
578
+ }
579
+ } else
580
+ logger.warn(
581
+ `Deploy ID configuration not found: ./engine-private/conf/${deployId}, using default configuration.`,
582
+ );
583
+ const sepRender = '/**/';
584
+ const confRawPaths = fs.readFileSync('./conf.js', 'utf8').split(sepRender);
585
+ confRawPaths[1] = `${JSON.stringify(DefaultConf)};`;
586
+ const targetConfPath = `./conf${defaultConf ? '' : `.${deployId}`}.js`;
587
+ fs.writeFileSync(targetConfPath, confRawPaths.join(sepRender), 'utf8');
588
+ shellExec(`prettier --write ${targetConfPath}`);
589
+ },
507
590
  };
508
591
  }
509
592