@rpcbase/cli 0.91.0 → 0.93.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/package.json +1 -1
- package/src/cmd-deploy.js +57 -55
- package/src/cmd-ssh.js +33 -31
- package/src/cmd-wait-for/index.js +2 -2
- package/src/index.js +4 -1
package/package.json
CHANGED
package/src/cmd-deploy.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { execSync } from "child_process"
|
|
2
|
-
import fs from "fs"
|
|
3
|
-
import path from "path"
|
|
4
|
-
import os from "os"
|
|
5
|
-
|
|
1
|
+
import { execSync } from "child_process"
|
|
2
|
+
import fs from "fs"
|
|
3
|
+
import path from "path"
|
|
4
|
+
import os from "os"
|
|
5
|
+
|
|
6
|
+
import validator from "validator"
|
|
7
|
+
|
|
6
8
|
|
|
7
9
|
export const deploy = async (argv) => {
|
|
8
10
|
const log = (message) => {
|
|
9
|
-
if (argv.verbose) console.log(message)
|
|
11
|
+
if (argv.verbose) console.log(message)
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
const {
|
|
@@ -14,7 +16,7 @@ export const deploy = async (argv) => {
|
|
|
14
16
|
RB_SSH_USER,
|
|
15
17
|
RB_SSH_KEY,
|
|
16
18
|
RB_DEPLOY_DIR,
|
|
17
|
-
} = argv.env || {}
|
|
19
|
+
} = argv.env || {}
|
|
18
20
|
|
|
19
21
|
if (
|
|
20
22
|
!RB_SSH_HOST ||
|
|
@@ -22,66 +24,66 @@ export const deploy = async (argv) => {
|
|
|
22
24
|
) {
|
|
23
25
|
throw new Error(
|
|
24
26
|
"Missing or invalid required environment variable: RB_SSH_HOST",
|
|
25
|
-
)
|
|
27
|
+
)
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
if (!RB_SSH_USER || !validator.isLength(RB_SSH_USER, { min: 1 })) {
|
|
29
31
|
throw new Error(
|
|
30
32
|
"Missing or invalid required environment variable: RB_SSH_USER",
|
|
31
|
-
)
|
|
33
|
+
)
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
if (!RB_SSH_KEY || !validator.isLength(RB_SSH_KEY, { min: 1 })) {
|
|
35
37
|
throw new Error(
|
|
36
38
|
"Missing or invalid required environment variable: RB_SSH_KEY",
|
|
37
|
-
)
|
|
39
|
+
)
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
if (!RB_DEPLOY_DIR || !validator.isLength(RB_DEPLOY_DIR, { min: 1 })) {
|
|
41
43
|
throw new Error(
|
|
42
44
|
"Missing or invalid required environment variable: RB_DEPLOY_DIR",
|
|
43
|
-
)
|
|
45
|
+
)
|
|
44
46
|
}
|
|
45
47
|
|
|
46
|
-
const host = RB_SSH_HOST
|
|
47
|
-
const user = RB_SSH_USER
|
|
48
|
-
const deployDir = RB_DEPLOY_DIR
|
|
49
|
-
let keyPath = RB_SSH_KEY
|
|
48
|
+
const host = RB_SSH_HOST
|
|
49
|
+
const user = RB_SSH_USER
|
|
50
|
+
const deployDir = RB_DEPLOY_DIR
|
|
51
|
+
let keyPath = RB_SSH_KEY
|
|
50
52
|
|
|
51
|
-
let tempKeyPath = null
|
|
53
|
+
let tempKeyPath = null
|
|
52
54
|
|
|
53
55
|
// Create an SSH function that can be reused
|
|
54
56
|
const ssh = async (command, stdioInherit = false) => {
|
|
55
|
-
const sshCommand = `ssh -i "${keyPath}" -o StrictHostKeyChecking=no ${user}@${host} "${command}"
|
|
56
|
-
log(`${command}`)
|
|
57
|
+
const sshCommand = `ssh -i "${keyPath}" -o StrictHostKeyChecking=no ${user}@${host} "${command}"`
|
|
58
|
+
log(`${command}`)
|
|
57
59
|
const out = execSync(sshCommand, {
|
|
58
60
|
stdio: (argv.verbose || stdioInherit) ? "inherit" : "pipe",
|
|
59
61
|
})
|
|
60
62
|
const res = out?.toString()?.trim() || ""
|
|
61
63
|
return res
|
|
62
|
-
}
|
|
64
|
+
}
|
|
63
65
|
|
|
64
66
|
try {
|
|
65
|
-
if (keyPath.startsWith("~")) keyPath = keyPath.replace("~", os.homedir())
|
|
67
|
+
if (keyPath.startsWith("~")) keyPath = keyPath.replace("~", os.homedir())
|
|
66
68
|
|
|
67
69
|
if (!fs.existsSync(keyPath)) {
|
|
68
70
|
// try to treat RB_SSH_KEY as base64-encoded key content
|
|
69
|
-
const keyContent = Buffer.from(RB_SSH_KEY, "base64").toString("utf-8")
|
|
71
|
+
const keyContent = Buffer.from(RB_SSH_KEY, "base64").toString("utf-8")
|
|
70
72
|
if (!keyContent.includes("-----BEGIN")) {
|
|
71
73
|
throw new Error(
|
|
72
74
|
"Decoded content does not appear to be a valid SSH key",
|
|
73
|
-
)
|
|
75
|
+
)
|
|
74
76
|
}
|
|
75
|
-
tempKeyPath = path.join(os.tmpdir(), `deploy-key-${Date.now()}`)
|
|
76
|
-
fs.writeFileSync(tempKeyPath, keyContent)
|
|
77
|
-
fs.chmodSync(tempKeyPath, "400")
|
|
78
|
-
keyPath = tempKeyPath
|
|
77
|
+
tempKeyPath = path.join(os.tmpdir(), `deploy-key-${Date.now()}`)
|
|
78
|
+
fs.writeFileSync(tempKeyPath, keyContent)
|
|
79
|
+
fs.chmodSync(tempKeyPath, "400")
|
|
80
|
+
keyPath = tempKeyPath
|
|
79
81
|
}
|
|
80
82
|
|
|
81
|
-
log(`Connecting to ${host} as ${user} using key at ${keyPath}`)
|
|
82
|
-
const res = await ssh("echo connection-ok")
|
|
83
|
+
log(`Connecting to ${host} as ${user} using key at ${keyPath}`)
|
|
84
|
+
const res = await ssh("echo connection-ok")
|
|
83
85
|
console.log(`${user}@${host}`, res)
|
|
84
|
-
await ssh(`mkdir -p ~/apps/${deployDir}`)
|
|
86
|
+
await ssh(`mkdir -p ~/apps/${deployDir}`)
|
|
85
87
|
|
|
86
88
|
const excludeList = [
|
|
87
89
|
".aider*",
|
|
@@ -100,7 +102,7 @@ export const deploy = async (argv) => {
|
|
|
100
102
|
].concat(argv.ignore)
|
|
101
103
|
|
|
102
104
|
|
|
103
|
-
const excludeArgs = excludeList.map((p) => `--exclude='${p}'`).join(" ")
|
|
105
|
+
const excludeArgs = excludeList.map((p) => `--exclude='${p}'`).join(" ")
|
|
104
106
|
const rsyncCmd = [
|
|
105
107
|
"rsync -avz -C",
|
|
106
108
|
excludeArgs,
|
|
@@ -108,37 +110,37 @@ export const deploy = async (argv) => {
|
|
|
108
110
|
`-e "ssh -i '${keyPath}' -o StrictHostKeyChecking=no"`,
|
|
109
111
|
".",
|
|
110
112
|
`${user}@${host}:~/apps/${deployDir}/`,
|
|
111
|
-
].join(" ")
|
|
113
|
+
].join(" ")
|
|
112
114
|
|
|
113
|
-
log(`Running: ${rsyncCmd}`)
|
|
114
|
-
execSync(rsyncCmd, { stdio: argv.verbose ? "inherit" : "ignore" })
|
|
115
|
+
log(`Running: ${rsyncCmd}`)
|
|
116
|
+
execSync(rsyncCmd, { stdio: argv.verbose ? "inherit" : "ignore" })
|
|
115
117
|
|
|
116
|
-
await ssh(`cd ~/apps/${deployDir}/infrastructure && [ -f package.json ] && yarn || true`)
|
|
118
|
+
await ssh(`cd ~/apps/${deployDir}/infrastructure && [ -f package.json ] && yarn || true`)
|
|
117
119
|
|
|
118
|
-
log("Restarting application...")
|
|
119
|
-
await ssh(`cd ~/apps/${deployDir}/infrastructure && node ctrl.js down prod`, true)
|
|
120
|
-
log("Application stopped. Starting it up again...")
|
|
121
|
-
await ssh(`cd ~/apps/${deployDir}/infrastructure && node ctrl.js up prod`, true)
|
|
120
|
+
log("Restarting application...")
|
|
121
|
+
await ssh(`cd ~/apps/${deployDir}/infrastructure && node ctrl.js down prod`, true)
|
|
122
|
+
log("Application stopped. Starting it up again...")
|
|
123
|
+
await ssh(`cd ~/apps/${deployDir}/infrastructure && node ctrl.js up prod`, true)
|
|
122
124
|
|
|
123
|
-
log("Deployment complete ✅")
|
|
125
|
+
log("Deployment complete ✅")
|
|
124
126
|
} catch (error) {
|
|
125
127
|
console.error(
|
|
126
128
|
`Deployment failed: ${error instanceof Error ? error.message : error}`,
|
|
127
|
-
)
|
|
128
|
-
throw error
|
|
129
|
+
)
|
|
130
|
+
throw error
|
|
129
131
|
} finally {
|
|
130
132
|
if (tempKeyPath) {
|
|
131
133
|
try {
|
|
132
|
-
fs.unlinkSync(tempKeyPath)
|
|
133
|
-
log(`Removed temporary key file: ${tempKeyPath}`)
|
|
134
|
+
fs.unlinkSync(tempKeyPath)
|
|
135
|
+
log(`Removed temporary key file: ${tempKeyPath}`)
|
|
134
136
|
} catch (cleanupError) {
|
|
135
|
-
console.warn(`Failed to remove temporary key file: ${cleanupError}`)
|
|
137
|
+
console.warn(`Failed to remove temporary key file: ${cleanupError}`)
|
|
136
138
|
}
|
|
137
139
|
}
|
|
138
140
|
}
|
|
139
141
|
|
|
140
142
|
// Purge Cloudflare Cache if CLOUDFLARE_ZONE and CLOUDFLARE_TOKEN are set
|
|
141
|
-
const { CLOUDFLARE_ZONE, CLOUDFLARE_TOKEN } = process.env
|
|
143
|
+
const { CLOUDFLARE_ZONE, CLOUDFLARE_TOKEN } = process.env
|
|
142
144
|
|
|
143
145
|
console.log("Cloudflare env", {
|
|
144
146
|
CLOUDFLARE_ZONE: JSON.stringify(CLOUDFLARE_ZONE),
|
|
@@ -146,33 +148,33 @@ export const deploy = async (argv) => {
|
|
|
146
148
|
})
|
|
147
149
|
|
|
148
150
|
if (!CLOUDFLARE_ZONE || !CLOUDFLARE_TOKEN) {
|
|
149
|
-
console.warn("Cloudflare cache purge will not run: missing CLOUDFLARE_ZONE or CLOUDFLARE_TOKEN environment variables")
|
|
151
|
+
console.warn("Cloudflare cache purge will not run: missing CLOUDFLARE_ZONE or CLOUDFLARE_TOKEN environment variables")
|
|
150
152
|
} else {
|
|
151
|
-
console.log("Purging Cloudflare cache...")
|
|
153
|
+
console.log("Purging Cloudflare cache...")
|
|
152
154
|
|
|
153
155
|
try {
|
|
154
|
-
const axios = (await import(
|
|
156
|
+
const axios = (await import("axios")).default
|
|
155
157
|
const response = await axios.post(
|
|
156
158
|
`https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE}/purge_cache`,
|
|
157
159
|
{ purge_everything: true },
|
|
158
160
|
{
|
|
159
161
|
headers: {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
+
"Authorization": `Bearer ${CLOUDFLARE_TOKEN}`,
|
|
163
|
+
"Content-Type": "application/json"
|
|
162
164
|
}
|
|
163
165
|
}
|
|
164
|
-
)
|
|
166
|
+
)
|
|
165
167
|
|
|
166
168
|
if (response.data.success) {
|
|
167
|
-
console.log("Successfully purged Cloudflare cache for zone:", CLOUDFLARE_ZONE)
|
|
169
|
+
console.log("Successfully purged Cloudflare cache for zone:", CLOUDFLARE_ZONE)
|
|
168
170
|
} else {
|
|
169
|
-
console.warn("Failed to purge Cloudflare cache:", response.data.errors)
|
|
171
|
+
console.warn("Failed to purge Cloudflare cache:", response.data.errors)
|
|
170
172
|
}
|
|
171
173
|
} catch (error) {
|
|
172
|
-
console.error("Error purging Cloudflare cache:", error.message)
|
|
174
|
+
console.error("Error purging Cloudflare cache:", error.message)
|
|
173
175
|
process.exit(1)
|
|
174
176
|
}
|
|
175
177
|
}
|
|
176
178
|
|
|
177
179
|
console.log("Done.")
|
|
178
|
-
}
|
|
180
|
+
}
|
package/src/cmd-ssh.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
|
-
import { execSync } from "child_process"
|
|
2
|
-
import fs from "fs"
|
|
3
|
-
import path from "path"
|
|
4
|
-
import os from "os"
|
|
5
|
-
|
|
1
|
+
import { execSync } from "child_process"
|
|
2
|
+
import fs from "fs"
|
|
3
|
+
import path from "path"
|
|
4
|
+
import os from "os"
|
|
5
|
+
|
|
6
|
+
import validator from "validator"
|
|
7
|
+
|
|
6
8
|
|
|
7
9
|
export const ssh = async (argv) => {
|
|
8
10
|
const log = (message) => {
|
|
9
|
-
if (argv.verbose) console.log(message)
|
|
11
|
+
if (argv.verbose) console.log(message)
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
const {
|
|
13
15
|
RB_SSH_HOST,
|
|
14
16
|
RB_SSH_USER,
|
|
15
17
|
RB_SSH_KEY,
|
|
16
|
-
} = argv.env || {}
|
|
18
|
+
} = argv.env || {}
|
|
17
19
|
|
|
18
20
|
if (
|
|
19
21
|
!RB_SSH_HOST ||
|
|
@@ -21,71 +23,71 @@ export const ssh = async (argv) => {
|
|
|
21
23
|
) {
|
|
22
24
|
throw new Error(
|
|
23
25
|
"Missing or invalid required environment variable: RB_SSH_HOST",
|
|
24
|
-
)
|
|
26
|
+
)
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
if (!RB_SSH_USER || !validator.isLength(RB_SSH_USER, { min: 1 })) {
|
|
28
30
|
throw new Error(
|
|
29
31
|
"Missing or invalid required environment variable: RB_SSH_USER",
|
|
30
|
-
)
|
|
32
|
+
)
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
if (!RB_SSH_KEY || !validator.isLength(RB_SSH_KEY, { min: 1 })) {
|
|
34
36
|
throw new Error(
|
|
35
37
|
"Missing or invalid required environment variable: RB_SSH_KEY",
|
|
36
|
-
)
|
|
38
|
+
)
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
const host = RB_SSH_HOST
|
|
40
|
-
const user = RB_SSH_USER
|
|
41
|
-
let keyPath = RB_SSH_KEY
|
|
41
|
+
const host = RB_SSH_HOST
|
|
42
|
+
const user = RB_SSH_USER
|
|
43
|
+
let keyPath = RB_SSH_KEY
|
|
42
44
|
|
|
43
|
-
let tempKeyPath = null
|
|
45
|
+
let tempKeyPath = null
|
|
44
46
|
|
|
45
47
|
const execSsh = async (command, stdioInherit = true) => {
|
|
46
|
-
const sshCommand = `ssh -i "${keyPath}" -o StrictHostKeyChecking=no ${user}@${host} "${command}"
|
|
47
|
-
log(`${command}`)
|
|
48
|
+
const sshCommand = `ssh -i "${keyPath}" -o StrictHostKeyChecking=no ${user}@${host} "${command}"`
|
|
49
|
+
log(`${command}`)
|
|
48
50
|
const out = execSync(sshCommand, {
|
|
49
51
|
stdio: (argv.verbose || stdioInherit) ? "inherit" : "pipe",
|
|
50
52
|
})
|
|
51
53
|
const res = out?.toString()?.trim() || ""
|
|
52
54
|
return res
|
|
53
|
-
}
|
|
55
|
+
}
|
|
54
56
|
|
|
55
57
|
try {
|
|
56
|
-
if (keyPath.startsWith("~")) keyPath = keyPath.replace("~", os.homedir())
|
|
58
|
+
if (keyPath.startsWith("~")) keyPath = keyPath.replace("~", os.homedir())
|
|
57
59
|
|
|
58
60
|
if (!fs.existsSync(keyPath)) {
|
|
59
61
|
// try to treat RB_SSH_KEY as base64-encoded key content
|
|
60
|
-
const keyContent = Buffer.from(RB_SSH_KEY, "base64").toString("utf-8")
|
|
62
|
+
const keyContent = Buffer.from(RB_SSH_KEY, "base64").toString("utf-8")
|
|
61
63
|
if (!keyContent.includes("-----BEGIN")) {
|
|
62
64
|
throw new Error(
|
|
63
65
|
"Decoded content does not appear to be a valid SSH key",
|
|
64
|
-
)
|
|
66
|
+
)
|
|
65
67
|
}
|
|
66
|
-
tempKeyPath = path.join(os.tmpdir(), `deploy-key-${Date.now()}`)
|
|
67
|
-
fs.writeFileSync(tempKeyPath, keyContent)
|
|
68
|
-
fs.chmodSync(tempKeyPath, "400")
|
|
69
|
-
keyPath = tempKeyPath
|
|
68
|
+
tempKeyPath = path.join(os.tmpdir(), `deploy-key-${Date.now()}`)
|
|
69
|
+
fs.writeFileSync(tempKeyPath, keyContent)
|
|
70
|
+
fs.chmodSync(tempKeyPath, "400")
|
|
71
|
+
keyPath = tempKeyPath
|
|
70
72
|
}
|
|
71
73
|
|
|
72
|
-
await execSsh("echo connection-ok", false)
|
|
74
|
+
await execSsh("echo connection-ok", false)
|
|
73
75
|
|
|
74
76
|
await execSsh(argv.command, true)
|
|
75
77
|
|
|
76
78
|
} catch (error) {
|
|
77
79
|
console.error(
|
|
78
80
|
`Command failed: ${error instanceof Error ? error.message : error}`,
|
|
79
|
-
)
|
|
80
|
-
throw error
|
|
81
|
+
)
|
|
82
|
+
throw error
|
|
81
83
|
} finally {
|
|
82
84
|
if (tempKeyPath) {
|
|
83
85
|
try {
|
|
84
|
-
fs.unlinkSync(tempKeyPath)
|
|
85
|
-
log(`Removed temporary key file: ${tempKeyPath}`)
|
|
86
|
+
fs.unlinkSync(tempKeyPath)
|
|
87
|
+
log(`Removed temporary key file: ${tempKeyPath}`)
|
|
86
88
|
} catch (cleanupError) {
|
|
87
|
-
console.warn(`Failed to remove temporary key file: ${cleanupError}`)
|
|
89
|
+
console.warn(`Failed to remove temporary key file: ${cleanupError}`)
|
|
88
90
|
}
|
|
89
91
|
}
|
|
90
92
|
}
|
|
91
|
-
}
|
|
93
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
import fs from "fs"
|
|
4
|
+
|
|
3
5
|
import yargs from "yargs/yargs"
|
|
4
6
|
import { hideBin } from "yargs/helpers"
|
|
5
7
|
import dotenv from "dotenv"
|
|
6
8
|
import { expand } from "dotenv-expand"
|
|
7
|
-
|
|
9
|
+
|
|
8
10
|
import { deploy } from "./cmd-deploy.js"
|
|
9
11
|
import { ssh } from "./cmd-ssh.js"
|
|
10
12
|
import { waitFor } from "./cmd-wait-for/index.js"
|
|
11
13
|
|
|
14
|
+
|
|
12
15
|
yargs(hideBin(process.argv))
|
|
13
16
|
.option("env", {
|
|
14
17
|
describe: "Path to environment file",
|