onedeploy-cli 0.1.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/dist/index.d.ts +1 -0
- package/dist/index.js +175 -0
- package/package.json +44 -0
- package/src/index.ts +196 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { execFile, spawn } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { Readable } from "node:stream";
|
|
6
|
+
import { promisify } from "node:util";
|
|
7
|
+
import prompts from "prompts";
|
|
8
|
+
import { y } from "yedra";
|
|
9
|
+
const nebulaUrl = `https://github.com/slackhq/nebula/releases/download/v1.9.6/nebula-linux-amd64.tar.gz`;
|
|
10
|
+
const configDir = `${homedir()}/.config/onedeploy`;
|
|
11
|
+
const tmpDir = `/tmp/onedeploy-${randomUUID()}`;
|
|
12
|
+
await mkdir(configDir, { recursive: true });
|
|
13
|
+
await mkdir(tmpDir, { recursive: true });
|
|
14
|
+
const Instance = y.object({
|
|
15
|
+
url: y.string(),
|
|
16
|
+
apiKey: y.string(),
|
|
17
|
+
});
|
|
18
|
+
const Config = y.object({
|
|
19
|
+
instances: Instance.array(),
|
|
20
|
+
});
|
|
21
|
+
const installNebula = async () => {
|
|
22
|
+
console.log("Downloading Nebula...");
|
|
23
|
+
const file = await fetch(nebulaUrl);
|
|
24
|
+
if (file.body === null) {
|
|
25
|
+
throw new Error("Could not download Nebula executable.");
|
|
26
|
+
}
|
|
27
|
+
await writeFile(`${tmpDir}/nebula.tar.gz`, Readable.from(file.body));
|
|
28
|
+
console.log("Extracting Nebula...");
|
|
29
|
+
await promisify(execFile)("tar", [
|
|
30
|
+
"-xf",
|
|
31
|
+
`${tmpDir}/nebula.tar.gz`,
|
|
32
|
+
"-C",
|
|
33
|
+
tmpDir,
|
|
34
|
+
]);
|
|
35
|
+
};
|
|
36
|
+
const loadConfig = async () => {
|
|
37
|
+
try {
|
|
38
|
+
const result = await readFile(`${configDir}/config.json`, "utf-8");
|
|
39
|
+
const config = JSON.parse(result);
|
|
40
|
+
return Config.parse(config);
|
|
41
|
+
}
|
|
42
|
+
catch (_error) {
|
|
43
|
+
// no config yet
|
|
44
|
+
return { instances: [] };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const saveConfig = async () => {
|
|
48
|
+
await writeFile(`${configDir}/config.json`, JSON.stringify(config, null, 2));
|
|
49
|
+
await chmod(`${configDir}/config.json`, 0o600);
|
|
50
|
+
};
|
|
51
|
+
const getNebulaCert = async (instance) => {
|
|
52
|
+
const response = await fetch(`${instance.url}/api/nebula/cert`, {
|
|
53
|
+
headers: {
|
|
54
|
+
authorization: `Bearer ${instance.apiKey}`,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
throw new Error(`Failed to fetch Nebula certificate: ${response.status}`);
|
|
59
|
+
}
|
|
60
|
+
const { nebulaConfig } = await response.json();
|
|
61
|
+
await writeFile(`${tmpDir}/nebula.yml`, nebulaConfig);
|
|
62
|
+
await chmod(`${tmpDir}/nebula.yml`, 0o600);
|
|
63
|
+
};
|
|
64
|
+
const connectNebula = async (instance) => {
|
|
65
|
+
await getNebulaCert(instance);
|
|
66
|
+
const nebula = spawn("sudo", [`${tmpDir}/nebula`, "-config", `${tmpDir}/nebula.yml`], {
|
|
67
|
+
stdio: "inherit",
|
|
68
|
+
});
|
|
69
|
+
const interval = setInterval(async () => {
|
|
70
|
+
// refresh Nebula cert every 10 minutes (lifetime is 15 minutes)
|
|
71
|
+
await getNebulaCert(instance);
|
|
72
|
+
nebula.kill("SIGHUP");
|
|
73
|
+
}, 10 * 60 * 1000);
|
|
74
|
+
await new Promise((resolve) => nebula.on("exit", resolve));
|
|
75
|
+
clearInterval(interval);
|
|
76
|
+
};
|
|
77
|
+
const setupConnection = async () => {
|
|
78
|
+
const result = await prompts([
|
|
79
|
+
{
|
|
80
|
+
type: "text",
|
|
81
|
+
name: "url",
|
|
82
|
+
message: "Enter OneDeploy URL",
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
type: "password",
|
|
86
|
+
name: "apiKey",
|
|
87
|
+
message: "Enter OneDeploy API key",
|
|
88
|
+
},
|
|
89
|
+
]);
|
|
90
|
+
if (result.apiKey === undefined || result.url === undefined) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
config.instances.push({ url: result.url, apiKey: result.apiKey });
|
|
94
|
+
await saveConfig();
|
|
95
|
+
};
|
|
96
|
+
const removeConnection = async () => {
|
|
97
|
+
const { connection } = await prompts([
|
|
98
|
+
{
|
|
99
|
+
type: "select",
|
|
100
|
+
name: "connection",
|
|
101
|
+
message: "Select connection to remove",
|
|
102
|
+
choices: config.instances.map((instance, index) => ({
|
|
103
|
+
title: instance.url,
|
|
104
|
+
value: index,
|
|
105
|
+
})),
|
|
106
|
+
},
|
|
107
|
+
]);
|
|
108
|
+
if (connection === undefined) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const { confirm } = await prompts([
|
|
112
|
+
{
|
|
113
|
+
type: "confirm",
|
|
114
|
+
name: "confirm",
|
|
115
|
+
message: `Are you sure you want to remove connection to ${config.instances[connection].url}?`,
|
|
116
|
+
initial: false,
|
|
117
|
+
},
|
|
118
|
+
]);
|
|
119
|
+
if (confirm === true) {
|
|
120
|
+
config.instances.splice(connection, 1);
|
|
121
|
+
await saveConfig();
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
await installNebula();
|
|
125
|
+
const config = await loadConfig();
|
|
126
|
+
while (true) {
|
|
127
|
+
const { action } = await prompts([
|
|
128
|
+
{
|
|
129
|
+
type: "select",
|
|
130
|
+
name: "action",
|
|
131
|
+
message: "What do you want to do?",
|
|
132
|
+
choices: [
|
|
133
|
+
{
|
|
134
|
+
title: "Connect",
|
|
135
|
+
value: "connect",
|
|
136
|
+
disabled: config.instances.length === 0,
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
title: "Setup new connection",
|
|
140
|
+
value: "setup",
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
title: "Remove connection",
|
|
144
|
+
value: "remove",
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
},
|
|
148
|
+
]);
|
|
149
|
+
if (action === "connect") {
|
|
150
|
+
const result = await prompts([
|
|
151
|
+
{
|
|
152
|
+
type: "select",
|
|
153
|
+
name: "connection",
|
|
154
|
+
message: "Select OneDeploy instance",
|
|
155
|
+
choices: config.instances.map((instance) => ({
|
|
156
|
+
title: instance.url,
|
|
157
|
+
value: instance,
|
|
158
|
+
})),
|
|
159
|
+
},
|
|
160
|
+
]);
|
|
161
|
+
if (result.connection !== undefined) {
|
|
162
|
+
await connectNebula(result.connection);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else if (action === "setup") {
|
|
166
|
+
await setupConnection();
|
|
167
|
+
}
|
|
168
|
+
else if (action === "remove") {
|
|
169
|
+
await removeConnection();
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
await rm(tmpDir, { recursive: true, force: true });
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "onedeploy-cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "CLI tool for connecting to OneDeploy via Nebula",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"onedeploy",
|
|
7
|
+
"nebula"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/0codekit/onedeploy-cli#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/0codekit/onedeploy-cli/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/0codekit/onedeploy-cli.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"files": [
|
|
19
|
+
"./dist/**"
|
|
20
|
+
],
|
|
21
|
+
"authors": [
|
|
22
|
+
"Justus Zorn <justus.zorn@relyon.de>"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "src/index.ts",
|
|
26
|
+
"bin": {
|
|
27
|
+
"onedeploy": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsc",
|
|
31
|
+
"check": "biome check src/*",
|
|
32
|
+
"fix": "biome check src/* --fix"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@biomejs/biome": "^2.2.3",
|
|
36
|
+
"@types/node": "^24.3.1",
|
|
37
|
+
"@types/prompts": "^2.4.9",
|
|
38
|
+
"typescript": "^5.9.2"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"prompts": "^2.4.2",
|
|
42
|
+
"yedra": "^0.17.9"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { execFile, spawn } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { Readable } from "node:stream";
|
|
6
|
+
import { promisify } from "node:util";
|
|
7
|
+
import prompts from "prompts";
|
|
8
|
+
import { y } from "yedra";
|
|
9
|
+
|
|
10
|
+
const nebulaUrl = `https://github.com/slackhq/nebula/releases/download/v1.9.6/nebula-linux-amd64.tar.gz`;
|
|
11
|
+
const configDir = `${homedir()}/.config/onedeploy`;
|
|
12
|
+
const tmpDir = `/tmp/onedeploy-${randomUUID()}`;
|
|
13
|
+
await mkdir(configDir, { recursive: true });
|
|
14
|
+
await mkdir(tmpDir, { recursive: true });
|
|
15
|
+
|
|
16
|
+
const Instance = y.object({
|
|
17
|
+
url: y.string(),
|
|
18
|
+
apiKey: y.string(),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const Config = y.object({
|
|
22
|
+
instances: Instance.array(),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
type Instance = y.Typeof<typeof Instance>;
|
|
26
|
+
type Config = y.Typeof<typeof Config>;
|
|
27
|
+
|
|
28
|
+
const installNebula = async (): Promise<void> => {
|
|
29
|
+
console.log("Downloading Nebula...");
|
|
30
|
+
const file = await fetch(nebulaUrl);
|
|
31
|
+
if (file.body === null) {
|
|
32
|
+
throw new Error("Could not download Nebula executable.");
|
|
33
|
+
}
|
|
34
|
+
await writeFile(`${tmpDir}/nebula.tar.gz`, Readable.from(file.body));
|
|
35
|
+
console.log("Extracting Nebula...");
|
|
36
|
+
await promisify(execFile)("tar", [
|
|
37
|
+
"-xf",
|
|
38
|
+
`${tmpDir}/nebula.tar.gz`,
|
|
39
|
+
"-C",
|
|
40
|
+
tmpDir,
|
|
41
|
+
]);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const loadConfig = async (): Promise<Config> => {
|
|
45
|
+
try {
|
|
46
|
+
const result = await readFile(`${configDir}/config.json`, "utf-8");
|
|
47
|
+
const config = JSON.parse(result);
|
|
48
|
+
return Config.parse(config);
|
|
49
|
+
} catch (_error) {
|
|
50
|
+
// no config yet
|
|
51
|
+
return { instances: [] };
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const saveConfig = async (): Promise<void> => {
|
|
56
|
+
await writeFile(`${configDir}/config.json`, JSON.stringify(config, null, 2));
|
|
57
|
+
await chmod(`${configDir}/config.json`, 0o600);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const getNebulaCert = async (instance: Instance) => {
|
|
61
|
+
const response = await fetch(`${instance.url}/api/nebula/cert`, {
|
|
62
|
+
headers: {
|
|
63
|
+
authorization: `Bearer ${instance.apiKey}`,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
throw new Error(`Failed to fetch Nebula certificate: ${response.status}`);
|
|
68
|
+
}
|
|
69
|
+
const { nebulaConfig } = (await response.json()) as { nebulaConfig: string };
|
|
70
|
+
await writeFile(`${tmpDir}/nebula.yml`, nebulaConfig);
|
|
71
|
+
await chmod(`${tmpDir}/nebula.yml`, 0o600);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const connectNebula = async (instance: {
|
|
75
|
+
url: string;
|
|
76
|
+
apiKey: string;
|
|
77
|
+
}): Promise<void> => {
|
|
78
|
+
await getNebulaCert(instance);
|
|
79
|
+
const nebula = spawn(
|
|
80
|
+
"sudo",
|
|
81
|
+
[`${tmpDir}/nebula`, "-config", `${tmpDir}/nebula.yml`],
|
|
82
|
+
{
|
|
83
|
+
stdio: "inherit",
|
|
84
|
+
},
|
|
85
|
+
);
|
|
86
|
+
const interval = setInterval(
|
|
87
|
+
async () => {
|
|
88
|
+
// refresh Nebula cert every 10 minutes (lifetime is 15 minutes)
|
|
89
|
+
await getNebulaCert(instance);
|
|
90
|
+
nebula.kill("SIGHUP");
|
|
91
|
+
},
|
|
92
|
+
10 * 60 * 1000,
|
|
93
|
+
);
|
|
94
|
+
await new Promise((resolve) => nebula.on("exit", resolve));
|
|
95
|
+
clearInterval(interval);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const setupConnection = async () => {
|
|
99
|
+
const result = await prompts([
|
|
100
|
+
{
|
|
101
|
+
type: "text",
|
|
102
|
+
name: "url",
|
|
103
|
+
message: "Enter OneDeploy URL",
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
type: "password",
|
|
107
|
+
name: "apiKey",
|
|
108
|
+
message: "Enter OneDeploy API key",
|
|
109
|
+
},
|
|
110
|
+
]);
|
|
111
|
+
if (result.apiKey === undefined || result.url === undefined) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
config.instances.push({ url: result.url, apiKey: result.apiKey });
|
|
115
|
+
await saveConfig();
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const removeConnection = async () => {
|
|
119
|
+
const { connection } = await prompts([
|
|
120
|
+
{
|
|
121
|
+
type: "select",
|
|
122
|
+
name: "connection",
|
|
123
|
+
message: "Select connection to remove",
|
|
124
|
+
choices: config.instances.map((instance, index) => ({
|
|
125
|
+
title: instance.url,
|
|
126
|
+
value: index,
|
|
127
|
+
})),
|
|
128
|
+
},
|
|
129
|
+
]);
|
|
130
|
+
if (connection === undefined) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const { confirm } = await prompts([
|
|
134
|
+
{
|
|
135
|
+
type: "confirm",
|
|
136
|
+
name: "confirm",
|
|
137
|
+
message: `Are you sure you want to remove connection to ${config.instances[connection].url}?`,
|
|
138
|
+
initial: false,
|
|
139
|
+
},
|
|
140
|
+
]);
|
|
141
|
+
if (confirm === true) {
|
|
142
|
+
config.instances.splice(connection, 1);
|
|
143
|
+
await saveConfig();
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
await installNebula();
|
|
148
|
+
const config = await loadConfig();
|
|
149
|
+
while (true) {
|
|
150
|
+
const { action } = await prompts([
|
|
151
|
+
{
|
|
152
|
+
type: "select",
|
|
153
|
+
name: "action",
|
|
154
|
+
message: "What do you want to do?",
|
|
155
|
+
choices: [
|
|
156
|
+
{
|
|
157
|
+
title: "Connect",
|
|
158
|
+
value: "connect",
|
|
159
|
+
disabled: config.instances.length === 0,
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
title: "Setup new connection",
|
|
163
|
+
value: "setup",
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
title: "Remove connection",
|
|
167
|
+
value: "remove",
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
},
|
|
171
|
+
]);
|
|
172
|
+
if (action === "connect") {
|
|
173
|
+
const result = await prompts([
|
|
174
|
+
{
|
|
175
|
+
type: "select",
|
|
176
|
+
name: "connection",
|
|
177
|
+
message: "Select OneDeploy instance",
|
|
178
|
+
choices: config.instances.map((instance) => ({
|
|
179
|
+
title: instance.url,
|
|
180
|
+
value: instance,
|
|
181
|
+
})),
|
|
182
|
+
},
|
|
183
|
+
]);
|
|
184
|
+
if (result.connection !== undefined) {
|
|
185
|
+
await connectNebula(result.connection);
|
|
186
|
+
}
|
|
187
|
+
} else if (action === "setup") {
|
|
188
|
+
await setupConnection();
|
|
189
|
+
} else if (action === "remove") {
|
|
190
|
+
await removeConnection();
|
|
191
|
+
} else {
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
await rm(tmpDir, { recursive: true, force: true });
|