onedeploy-cli 0.1.3 → 0.1.5

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/dist/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  import prompts from "prompts";
3
3
  import { loadConfig, saveConfig } from "./config.js";
4
- import { cleanupNebula, connectNebula, installNebula } from "./nebula.js";
4
+ import { cleanupNebula, connectNebula, installNebula, isNebulaRunning, } from "./nebula.js";
5
+ if (process.argv.includes("--version")) {
6
+ console.log("onedeploy-cli version 0.1.4");
7
+ process.exit();
8
+ }
5
9
  const config = await loadConfig();
6
10
  const setupConnection = async () => {
7
11
  const result = await prompts([
@@ -100,6 +104,12 @@ const mainMenu = async () => {
100
104
  }
101
105
  return true;
102
106
  };
107
+ process.on("SIGINT", () => {
108
+ if (!isNebulaRunning) {
109
+ process.exit();
110
+ }
111
+ // ignore SIGINTs to continue after Nebula is disconnected
112
+ });
103
113
  await installNebula();
104
114
  try {
105
115
  while (true) {
package/dist/nebula.js CHANGED
@@ -5,6 +5,7 @@ import { tmpdir } from "node:os";
5
5
  import path from "node:path";
6
6
  import { Readable } from "node:stream";
7
7
  import { promisify } from "node:util";
8
+ import { parse } from "yaml";
8
9
  const getNebulaPackageName = () => {
9
10
  switch (process.platform) {
10
11
  case "linux":
@@ -29,13 +30,13 @@ const nebulaUrl = `https://github.com/slackhq/nebula/releases/download/${nebulaV
29
30
  const tmpDir = path.join(tmpdir(), `onedeploy-${randomUUID()}`);
30
31
  await mkdir(tmpDir, { recursive: true });
31
32
  export const installNebula = async () => {
32
- console.log("Downloading Nebula...");
33
+ console.log(`Downloading Nebula to ${tmpDir}/${packageName}...`);
33
34
  const file = await fetch(nebulaUrl);
34
35
  if (file.body === null) {
35
36
  throw new Error("Could not download Nebula executable.");
36
37
  }
37
38
  await writeFile(`${tmpDir}/${packageName}`, Readable.from(file.body));
38
- console.log("Extracting Nebula...");
39
+ console.log(`Extracting Nebula to ${tmpDir}/nebula...`);
39
40
  await promisify(execFile)("tar", [
40
41
  "-xf",
41
42
  `${tmpDir}/${packageName}`,
@@ -58,9 +59,31 @@ export const getNebulaCert = async (instance) => {
58
59
  const { nebulaConfig } = (await response.json());
59
60
  await writeFile(`${tmpDir}/nebula.yml`, nebulaConfig);
60
61
  await chmod(`${tmpDir}/nebula.yml`, 0o600);
62
+ console.log(`Wrote Nebula configuration to ${tmpDir}/nebula.yml`);
63
+ const parsedConfig = parse(nebulaConfig);
64
+ await writeFile(`${tmpDir}/nebula-debug.crt`, parsedConfig.pki.cert);
65
+ const { stdout } = await promisify(execFile)(`${tmpDir}/nebula-cert`, [
66
+ "print",
67
+ "-path",
68
+ `${tmpDir}/nebula-debug.crt`,
69
+ ]);
70
+ // in case our local time isn't correct, wait for the certificate to become valid
71
+ const validFrom = stdout.match(/Not before: ([\d-]+ [\d:]+ \+\d+)/);
72
+ if (validFrom === null) {
73
+ throw new Error("Invalid Nebula certificate: Missing 'Not before' field.");
74
+ }
75
+ const validFromDate = new Date(validFrom[1]);
76
+ if (validFromDate > new Date()) {
77
+ const waitTime = validFromDate.getTime() - Date.now();
78
+ console.log(`Your system clock is running slow! Waiting ${Math.ceil(waitTime / 1000)} seconds for Nebula certificate to become valid...`);
79
+ await new Promise((resolve) => setTimeout(resolve, waitTime));
80
+ }
81
+ process.stdout.write(stdout);
61
82
  };
83
+ export let isNebulaRunning = false;
62
84
  export const connectNebula = async (instance) => {
63
85
  await getNebulaCert(instance);
86
+ isNebulaRunning = true;
64
87
  const nebula = spawn(`${tmpDir}/nebula`, ["-config", `${tmpDir}/nebula.yml`], { stdio: "inherit" });
65
88
  const interval = setInterval(async () => {
66
89
  // refresh Nebula cert every 10 minutes (lifetime is 15 minutes)
@@ -68,5 +91,6 @@ export const connectNebula = async (instance) => {
68
91
  nebula.kill("SIGHUP");
69
92
  }, 10 * 60 * 1000);
70
93
  await new Promise((resolve) => nebula.on("exit", resolve));
94
+ isNebulaRunning = false;
71
95
  clearInterval(interval);
72
96
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onedeploy-cli",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "CLI tool for connecting to OneDeploy via Nebula",
5
5
  "keywords": [
6
6
  "onedeploy",
@@ -39,6 +39,7 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "prompts": "^2.4.2",
42
+ "yaml": "^2.8.1",
42
43
  "yedra": "^0.17.9"
43
44
  }
44
45
  }
package/src/index.ts DELETED
@@ -1,115 +0,0 @@
1
- #!/usr/bin/env node
2
- import prompts from "prompts";
3
- import { loadConfig, saveConfig } from "./config.js";
4
- import { cleanupNebula, connectNebula, installNebula } from "./nebula.js";
5
-
6
- const config = await loadConfig();
7
-
8
- const setupConnection = async () => {
9
- const result = await prompts([
10
- {
11
- type: "text",
12
- name: "url",
13
- message: "Enter OneDeploy URL",
14
- },
15
- {
16
- type: "password",
17
- name: "apiKey",
18
- message: "Enter OneDeploy API key",
19
- },
20
- ]);
21
- if (result.apiKey === undefined || result.url === undefined) {
22
- return;
23
- }
24
- config.instances.push({ url: result.url, apiKey: result.apiKey });
25
- await saveConfig(config);
26
- };
27
-
28
- const removeConnection = async () => {
29
- const { connection } = await prompts([
30
- {
31
- type: "select",
32
- name: "connection",
33
- message: "Select connection to remove",
34
- choices: config.instances.map((instance, index) => ({
35
- title: instance.url,
36
- value: index,
37
- })),
38
- },
39
- ]);
40
- if (connection === undefined) {
41
- return;
42
- }
43
- const { confirm } = await prompts([
44
- {
45
- type: "confirm",
46
- name: "confirm",
47
- message: `Are you sure you want to remove connection to ${config.instances[connection].url}?`,
48
- initial: false,
49
- },
50
- ]);
51
- if (confirm === true) {
52
- config.instances.splice(connection, 1);
53
- await saveConfig(config);
54
- }
55
- };
56
-
57
- const mainMenu = async (): Promise<boolean> => {
58
- const { action } = await prompts([
59
- {
60
- type: "select",
61
- name: "action",
62
- message: "What do you want to do?",
63
- choices: [
64
- {
65
- title: "Connect",
66
- value: "connect",
67
- disabled: config.instances.length === 0,
68
- },
69
- {
70
- title: "Setup new connection",
71
- value: "setup",
72
- },
73
- {
74
- title: "Remove connection",
75
- value: "remove",
76
- },
77
- ],
78
- },
79
- ]);
80
- if (action === "connect") {
81
- const result = await prompts([
82
- {
83
- type: "select",
84
- name: "connection",
85
- message: "Select OneDeploy instance",
86
- choices: config.instances.map((instance) => ({
87
- title: instance.url,
88
- value: instance,
89
- })),
90
- },
91
- ]);
92
- if (result.connection !== undefined) {
93
- await connectNebula(result.connection);
94
- }
95
- } else if (action === "setup") {
96
- await setupConnection();
97
- } else if (action === "remove") {
98
- await removeConnection();
99
- } else {
100
- return false;
101
- }
102
- return true;
103
- };
104
-
105
- await installNebula();
106
- try {
107
- while (true) {
108
- const shouldContinue = await mainMenu();
109
- if (!shouldContinue) {
110
- break;
111
- }
112
- }
113
- } finally {
114
- await cleanupNebula();
115
- }