@rpcbase/cli 0.79.0 → 0.81.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 +16 -16
- package/src/cmd-ssh.js +91 -0
- package/src/index.js +21 -0
package/package.json
CHANGED
package/src/cmd-deploy.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { execSync } from "child_process";
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import path from "path";
|
|
4
4
|
import os from "os";
|
|
@@ -10,30 +10,30 @@ export const deploy = async (argv) => {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
const {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
RB_SSH_HOST,
|
|
14
|
+
RB_SSH_USER,
|
|
15
|
+
RB_SSH_KEY,
|
|
16
16
|
RB_DEPLOY_DIR,
|
|
17
17
|
} = argv.env || {};
|
|
18
18
|
|
|
19
19
|
if (
|
|
20
|
-
!
|
|
21
|
-
!validator.isURL(
|
|
20
|
+
!RB_SSH_HOST ||
|
|
21
|
+
!validator.isURL(RB_SSH_HOST, { require_tld: false })
|
|
22
22
|
) {
|
|
23
23
|
throw new Error(
|
|
24
|
-
"Missing or invalid required environment variable:
|
|
24
|
+
"Missing or invalid required environment variable: RB_SSH_HOST",
|
|
25
25
|
);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
if (!
|
|
28
|
+
if (!RB_SSH_USER || !validator.isLength(RB_SSH_USER, { min: 1 })) {
|
|
29
29
|
throw new Error(
|
|
30
|
-
"Missing or invalid required environment variable:
|
|
30
|
+
"Missing or invalid required environment variable: RB_SSH_USER",
|
|
31
31
|
);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
if (!
|
|
34
|
+
if (!RB_SSH_KEY || !validator.isLength(RB_SSH_KEY, { min: 1 })) {
|
|
35
35
|
throw new Error(
|
|
36
|
-
"Missing or invalid required environment variable:
|
|
36
|
+
"Missing or invalid required environment variable: RB_SSH_KEY",
|
|
37
37
|
);
|
|
38
38
|
}
|
|
39
39
|
|
|
@@ -43,10 +43,10 @@ export const deploy = async (argv) => {
|
|
|
43
43
|
);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
const host =
|
|
47
|
-
const user =
|
|
46
|
+
const host = RB_SSH_HOST;
|
|
47
|
+
const user = RB_SSH_USER;
|
|
48
48
|
const deployDir = RB_DEPLOY_DIR;
|
|
49
|
-
let keyPath =
|
|
49
|
+
let keyPath = RB_SSH_KEY;
|
|
50
50
|
|
|
51
51
|
let tempKeyPath = null;
|
|
52
52
|
|
|
@@ -65,8 +65,8 @@ export const deploy = async (argv) => {
|
|
|
65
65
|
if (keyPath.startsWith("~")) keyPath = keyPath.replace("~", os.homedir());
|
|
66
66
|
|
|
67
67
|
if (!fs.existsSync(keyPath)) {
|
|
68
|
-
// try to treat
|
|
69
|
-
const keyContent = Buffer.from(
|
|
68
|
+
// try to treat RB_SSH_KEY as base64-encoded key content
|
|
69
|
+
const keyContent = Buffer.from(RB_SSH_KEY, "base64").toString("utf-8");
|
|
70
70
|
if (!keyContent.includes("-----BEGIN")) {
|
|
71
71
|
throw new Error(
|
|
72
72
|
"Decoded content does not appear to be a valid SSH key",
|
package/src/cmd-ssh.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { execSync } from "child_process";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import os from "os";
|
|
5
|
+
import validator from "validator";
|
|
6
|
+
|
|
7
|
+
export const ssh = async (argv) => {
|
|
8
|
+
const log = (message) => {
|
|
9
|
+
if (argv.verbose) console.log(message);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const {
|
|
13
|
+
RB_SSH_HOST,
|
|
14
|
+
RB_SSH_USER,
|
|
15
|
+
RB_SSH_KEY,
|
|
16
|
+
} = argv.env || {};
|
|
17
|
+
|
|
18
|
+
if (
|
|
19
|
+
!RB_SSH_HOST ||
|
|
20
|
+
!validator.isURL(RB_SSH_HOST, { require_tld: false })
|
|
21
|
+
) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
"Missing or invalid required environment variable: RB_SSH_HOST",
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!RB_SSH_USER || !validator.isLength(RB_SSH_USER, { min: 1 })) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
"Missing or invalid required environment variable: RB_SSH_USER",
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!RB_SSH_KEY || !validator.isLength(RB_SSH_KEY, { min: 1 })) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
"Missing or invalid required environment variable: RB_SSH_KEY",
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const host = RB_SSH_HOST;
|
|
40
|
+
const user = RB_SSH_USER;
|
|
41
|
+
let keyPath = RB_SSH_KEY;
|
|
42
|
+
|
|
43
|
+
let tempKeyPath = null;
|
|
44
|
+
|
|
45
|
+
const execSsh = async (command, stdioInherit = true) => {
|
|
46
|
+
const sshCommand = `ssh -i "${keyPath}" -o StrictHostKeyChecking=no ${user}@${host} "${command}"`;
|
|
47
|
+
log(`${command}`);
|
|
48
|
+
const out = execSync(sshCommand, {
|
|
49
|
+
stdio: (argv.verbose || stdioInherit) ? "inherit" : "pipe",
|
|
50
|
+
})
|
|
51
|
+
const res = out?.toString()?.trim() || ""
|
|
52
|
+
return res
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
if (keyPath.startsWith("~")) keyPath = keyPath.replace("~", os.homedir());
|
|
57
|
+
|
|
58
|
+
if (!fs.existsSync(keyPath)) {
|
|
59
|
+
// try to treat RB_SSH_KEY as base64-encoded key content
|
|
60
|
+
const keyContent = Buffer.from(RB_SSH_KEY, "base64").toString("utf-8");
|
|
61
|
+
if (!keyContent.includes("-----BEGIN")) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
"Decoded content does not appear to be a valid SSH key",
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
tempKeyPath = path.join(os.tmpdir(), `deploy-key-${Date.now()}`);
|
|
67
|
+
fs.writeFileSync(tempKeyPath, keyContent);
|
|
68
|
+
fs.chmodSync(tempKeyPath, "400");
|
|
69
|
+
keyPath = tempKeyPath;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
await execSsh("echo connection-ok", false);
|
|
73
|
+
|
|
74
|
+
await execSsh(argv.command, true)
|
|
75
|
+
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error(
|
|
78
|
+
`Command failed: ${error instanceof Error ? error.message : error}`,
|
|
79
|
+
);
|
|
80
|
+
throw error;
|
|
81
|
+
} finally {
|
|
82
|
+
if (tempKeyPath) {
|
|
83
|
+
try {
|
|
84
|
+
fs.unlinkSync(tempKeyPath);
|
|
85
|
+
log(`Removed temporary key file: ${tempKeyPath}`);
|
|
86
|
+
} catch (cleanupError) {
|
|
87
|
+
console.warn(`Failed to remove temporary key file: ${cleanupError}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
package/src/index.js
CHANGED
|
@@ -5,12 +5,14 @@ import { hideBin } from 'yargs/helpers'
|
|
|
5
5
|
import dotenv from 'dotenv'
|
|
6
6
|
import fs from 'fs'
|
|
7
7
|
import {deploy} from "./cmd-deploy.js"
|
|
8
|
+
import {ssh} from "./cmd-ssh.js"
|
|
8
9
|
import {waitFor} from "./cmd-wait-for/index.js"
|
|
9
10
|
|
|
10
11
|
yargs(hideBin(process.argv))
|
|
11
12
|
.option("env", {
|
|
12
13
|
describe: "Path to environment file",
|
|
13
14
|
type: "array",
|
|
15
|
+
nargs: 1,
|
|
14
16
|
global: true,
|
|
15
17
|
default: ["process.env"],
|
|
16
18
|
coerce: (envFiles) => {
|
|
@@ -69,6 +71,25 @@ yargs(hideBin(process.argv))
|
|
|
69
71
|
await deploy(argv)
|
|
70
72
|
},
|
|
71
73
|
)
|
|
74
|
+
.command(
|
|
75
|
+
"ssh <command>",
|
|
76
|
+
"Run ssh command",
|
|
77
|
+
(yargs) => {
|
|
78
|
+
return yargs
|
|
79
|
+
.positional("command", {
|
|
80
|
+
describe: "Command to run on the remote server",
|
|
81
|
+
type: "string",
|
|
82
|
+
})
|
|
83
|
+
.option("verbose", {
|
|
84
|
+
alias: "v",
|
|
85
|
+
describe: "Run with verbose logging",
|
|
86
|
+
type: "boolean",
|
|
87
|
+
})
|
|
88
|
+
},
|
|
89
|
+
async(argv) => {
|
|
90
|
+
await ssh(argv)
|
|
91
|
+
},
|
|
92
|
+
)
|
|
72
93
|
.command("wait-for", "Waits for the specified service to become available", () => {}, (argv) => {
|
|
73
94
|
waitFor()
|
|
74
95
|
})
|