create-dacosta-proj 1.0.21 → 1.0.23
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/package.json
CHANGED
|
@@ -12,7 +12,7 @@ const rootDir = path.resolve(__dirname, '..');
|
|
|
12
12
|
|
|
13
13
|
// Get All Servers
|
|
14
14
|
|
|
15
|
-
let servers = Object.keys(process.env).filter(key => key.startsWith('DEPLOY_')).length
|
|
15
|
+
let servers = Object.keys(process.env).filter(key => key.startsWith('DEPLOY_') && key.endsWith('_IP')).length;
|
|
16
16
|
servers = new Array(servers).fill().map((_,server) => {
|
|
17
17
|
return {
|
|
18
18
|
ip: process.env[`DEPLOY_${server+1}_IP`],
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Global Tools
|
|
2
|
+
|
|
3
|
+
require('dotenv').config({ quiet: true });
|
|
4
|
+
|
|
5
|
+
// Packages / Helpers
|
|
6
|
+
|
|
7
|
+
const { NodeSSH } = require('node-ssh');
|
|
8
|
+
|
|
9
|
+
// Get All Servers
|
|
10
|
+
|
|
11
|
+
const servers = Object.keys(process.env)
|
|
12
|
+
.filter(key => /^DEPLOY_\d+_IP$/.test(key))
|
|
13
|
+
.length;
|
|
14
|
+
|
|
15
|
+
const targets = new Array(servers).fill().map((_, server) => {
|
|
16
|
+
const names = (process.env[`DEPLOY_${server+1}_PM2_NAME`] || '')
|
|
17
|
+
.split(';')
|
|
18
|
+
.map(p => p.trim())
|
|
19
|
+
.filter(Boolean);
|
|
20
|
+
const argsList = (process.env[`DEPLOY_${server+1}_PM2_ARGS`] || '')
|
|
21
|
+
.split(';')
|
|
22
|
+
.map(a => a.trim());
|
|
23
|
+
const pm2 = names.map((name, i) => ({ name, args: argsList[i] || '' }));
|
|
24
|
+
return {
|
|
25
|
+
ip: process.env[`DEPLOY_${server+1}_IP`],
|
|
26
|
+
password: process.env[`DEPLOY_${server+1}_PASSWORD`],
|
|
27
|
+
path: process.env[`DEPLOY_${server+1}_PATH`],
|
|
28
|
+
src: process.env[`DEPLOY_${server+1}_PM2_SRC`],
|
|
29
|
+
pm2,
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Build a shell command that loads nvm first
|
|
34
|
+
|
|
35
|
+
function withNvm(cmd) {
|
|
36
|
+
return `. ~/.nvm/nvm.sh && ${cmd}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Restart on Server
|
|
40
|
+
|
|
41
|
+
async function restartOn(server) {
|
|
42
|
+
|
|
43
|
+
console.log(`\n→ ${server.ip}`);
|
|
44
|
+
|
|
45
|
+
if (!server.pm2.length) {
|
|
46
|
+
console.warn(' ⚠ no pm2 processes configured, skipping');
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const ssh = new NodeSSH();
|
|
51
|
+
await ssh.connect({
|
|
52
|
+
host: server.ip,
|
|
53
|
+
username: 'root',
|
|
54
|
+
password: server.password,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const install = await ssh.execCommand(withNvm(`cd ${server.path} && npm install --no-audit --no-fund`));
|
|
58
|
+
if (install.stdout) console.log(install.stdout.split('\n').map(l => ` ${l}`).join('\n'));
|
|
59
|
+
if (install.code === 0) {
|
|
60
|
+
console.log(' ✓ npm install');
|
|
61
|
+
} else {
|
|
62
|
+
console.error(` ✗ npm install — ${install.stderr || install.stdout}`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
for (const { name, args } of server.pm2) {
|
|
66
|
+
|
|
67
|
+
const exists = await ssh.execCommand(withNvm(`pm2 describe ${name} > /dev/null 2>&1`));
|
|
68
|
+
|
|
69
|
+
if (exists.code === 0) {
|
|
70
|
+
const result = await ssh.execCommand(withNvm(`pm2 restart ${name}`));
|
|
71
|
+
if (result.code === 0) {
|
|
72
|
+
console.log(` ✓ ${name} (restarted)`);
|
|
73
|
+
} else {
|
|
74
|
+
console.error(` ✗ ${name} — ${result.stderr || result.stdout}`);
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
const result = await ssh.execCommand(
|
|
78
|
+
withNvm(`${args ? `${args} ` : ''}pm2 start ${server.src} --name "${name}" --log-date-format "YYYY-MM-DD HH:mm"`),
|
|
79
|
+
{ cwd: server.path }
|
|
80
|
+
);
|
|
81
|
+
if (result.code === 0) {
|
|
82
|
+
console.log(` ✓ ${name} (created)`);
|
|
83
|
+
} else {
|
|
84
|
+
console.error(` ✗ ${name} — ${result.stderr || result.stdout}`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
ssh.dispose();
|
|
91
|
+
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Start Restart
|
|
95
|
+
|
|
96
|
+
(async () => {
|
|
97
|
+
|
|
98
|
+
for (const server of targets) {
|
|
99
|
+
await restartOn(server);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
console.log('\nRestart complete!');
|
|
103
|
+
process.exit();
|
|
104
|
+
|
|
105
|
+
})();
|
package/template/.env.template
CHANGED
|
@@ -7,6 +7,13 @@ PROJECT_ID=
|
|
|
7
7
|
DEPLOY_1_IP=
|
|
8
8
|
DEPLOY_1_PASSWORD=
|
|
9
9
|
DEPLOY_1_PATH=
|
|
10
|
+
DEPLOY_1_PM2_NAME=
|
|
11
|
+
# EXAMPLE 1: DEPLOY_1_PM2_NAME=nm-bot
|
|
12
|
+
# EXAMPLE 2: DEPLOY_1_PM2_NAME=nm-tiktok-video-1;nm-tiktok-live-1
|
|
13
|
+
DEPLOY_1_PM2_SRC=src/index.js
|
|
14
|
+
DEPLOY_1_PM2_ARGS= # Optional
|
|
15
|
+
# EXAMPLE 1: DEPLOY_1_PM2_ARGS=PROJECT_INSTANCE_ID=1 PROJECT_INSTANCE_TYPE=video
|
|
16
|
+
# EXAMPLE 2: DEPLOY_1_PM2_ARGS=PROJECT_INSTANCE_ID=1 PROJECT_INSTANCE_TYPE=video;PROJECT_INSTANCE_ID=1 PROJECT_INSTANCE_TYPE=live
|
|
10
17
|
|
|
11
18
|
# Supabase
|
|
12
19
|
|
package/template/package.json
CHANGED