@rainyun/cli 0.0.0-stage → 1.0.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/README.md +132 -2
- package/bin/postinstall.mjs +13 -0
- package/bin/rainyun-cli.mjs +13 -0
- package/catalog/human-names.json +340 -0
- package/catalog/list.openapi.json +354 -0
- package/catalog/openapi.json +34531 -0
- package/catalog/product-commands.json +332 -0
- package/catalog/public.openapi.json +1895 -0
- package/docs/AGENT_GUIDE.md +64 -0
- package/docs/install.md +44 -0
- package/docs/product-lists.md +48 -0
- package/docs/rgp.md +77 -0
- package/docs/terminal-exec.md +111 -0
- package/examples/public-panel.json +1 -0
- package/examples/public-rcs-price.json +1 -0
- package/examples/rca-terminal-input.json +1 -0
- package/examples/rcs-get.json +1 -0
- package/examples/rcs-list.json +1 -0
- package/examples/rcs-reboot.json +1 -0
- package/examples/terminal-controls.ndjson +3 -0
- package/examples/terminal-input.json +1 -0
- package/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +539 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +22 -0
- package/node_modules/ws/lib/buffer-util.js +142 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +530 -0
- package/node_modules/ws/lib/receiver.js +751 -0
- package/node_modules/ws/lib/sender.js +610 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +565 -0
- package/node_modules/ws/lib/websocket.js +1471 -0
- package/node_modules/ws/package.json +74 -0
- package/node_modules/ws/wrapper.mjs +21 -0
- package/package.json +29 -4
- package/skills/rainyun-cli/SKILL.md +46 -0
- package/src/api/catalog.mjs +230 -0
- package/src/api/http.mjs +222 -0
- package/src/api/request.mjs +178 -0
- package/src/api/titles.mjs +9 -0
- package/src/auth/commands.mjs +192 -0
- package/src/auth/store.mjs +162 -0
- package/src/cli.mjs +154 -0
- package/src/commands/dispatch.mjs +128 -0
- package/src/commands/fields.mjs +77 -0
- package/src/commands/products.mjs +209 -0
- package/src/commands/rgs.mjs +44 -0
- package/src/json.mjs +81 -0
- package/src/output/business.mjs +188 -0
- package/src/output/discovery.mjs +197 -0
- package/src/output/examples.mjs +71 -0
- package/src/output/format.mjs +164 -0
- package/src/output/help.json +181 -0
- package/src/output/help.mjs +334 -0
- package/src/rgp/commands.mjs +251 -0
- package/src/rgp/input.mjs +144 -0
- package/src/rgp/operations.json +625 -0
- package/src/rgp/result.mjs +103 -0
- package/src/rgp/transfer.mjs +213 -0
- package/src/terminal/commands.mjs +155 -0
- package/src/terminal/session.mjs +233 -0
- package/src/terminal/target.mjs +155 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { bounded, object, fail } from './input.mjs';
|
|
2
|
+
|
|
3
|
+
const privateKeys = new Set([
|
|
4
|
+
'app-token',
|
|
5
|
+
'project-token',
|
|
6
|
+
'token',
|
|
7
|
+
'authorization',
|
|
8
|
+
'api-key',
|
|
9
|
+
'apikey',
|
|
10
|
+
'download-url',
|
|
11
|
+
'download-path',
|
|
12
|
+
'k8s-panel-helper-token',
|
|
13
|
+
]);
|
|
14
|
+
export function sanitizer(secrets) {
|
|
15
|
+
const variants = new Set();
|
|
16
|
+
for (const s of secrets)
|
|
17
|
+
if (typeof s === 'string' && s) {
|
|
18
|
+
variants.add(s);
|
|
19
|
+
variants.add(encodeURIComponent(s));
|
|
20
|
+
variants.add(new URLSearchParams({ v: s }).toString().slice(2));
|
|
21
|
+
}
|
|
22
|
+
const ordered = [...variants].sort((a, b) => b.length - a.length);
|
|
23
|
+
const text = (s) => ordered.reduce((v, secret) => v.replaceAll(secret, '[REDACTED]'), s);
|
|
24
|
+
function clean(value) {
|
|
25
|
+
if (typeof value === 'string') return text(value);
|
|
26
|
+
if (Array.isArray(value)) return value.map(clean);
|
|
27
|
+
if (object(value) && !JSON.isRawJSON?.(value))
|
|
28
|
+
return Object.fromEntries(
|
|
29
|
+
Object.entries(value).map(([k, v]) => [
|
|
30
|
+
text(k),
|
|
31
|
+
privateKeys.has(k.toLowerCase().replaceAll('_', '-')) ? '[REDACTED]' : clean(v),
|
|
32
|
+
])
|
|
33
|
+
);
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
return { text, clean };
|
|
37
|
+
}
|
|
38
|
+
function taskID(id, clean) {
|
|
39
|
+
if ((Number.isSafeInteger(id) || typeof id === 'bigint') && id > 0) id = String(id);
|
|
40
|
+
return bounded(id, 1024) && clean.text(id) === id ? id : null;
|
|
41
|
+
}
|
|
42
|
+
export function interpret(op, response, query, clean) {
|
|
43
|
+
const id = op.operation_id,
|
|
44
|
+
data = response.data;
|
|
45
|
+
const result = { ok: true, operation: id, response: clean.clean(response) };
|
|
46
|
+
if (id === 'cron.run') result.outcome = 'accepted';
|
|
47
|
+
if (op.async || id.startsWith('task.')) {
|
|
48
|
+
if (op.async && data?.id == null) return { ...result, outcome: 'accepted_unknown' };
|
|
49
|
+
const tasks = id === 'task.list' && Array.isArray(data) ? data : [data];
|
|
50
|
+
for (const task of tasks) {
|
|
51
|
+
const key = taskID(task?.id, clean);
|
|
52
|
+
if (op.async && key && !Object.hasOwn(task, 'status'))
|
|
53
|
+
return { ...result, outcome: 'accepted', task_id: key };
|
|
54
|
+
if (
|
|
55
|
+
!key ||
|
|
56
|
+
(query.id && query.id !== key) ||
|
|
57
|
+
!['pending', 'running', 'failed', 'succeed'].includes(task?.status)
|
|
58
|
+
)
|
|
59
|
+
fail(
|
|
60
|
+
'protocol',
|
|
61
|
+
'helper task identity or state is invalid',
|
|
62
|
+
id === 'task.list'
|
|
63
|
+
? {}
|
|
64
|
+
: { outcome: 'unknown', details: { task_id: clean.text(query.id ?? '') } }
|
|
65
|
+
);
|
|
66
|
+
if (id === 'task.list') continue;
|
|
67
|
+
if (task.status === 'failed')
|
|
68
|
+
fail('api', 'helper task failed', {
|
|
69
|
+
outcome: 'failed',
|
|
70
|
+
details: { task_id: key, response: result.response },
|
|
71
|
+
});
|
|
72
|
+
result.task_id = key;
|
|
73
|
+
result.outcome = task.status === 'succeed' ? 'succeeded' : 'accepted';
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (id === 'status') {
|
|
77
|
+
if (!Array.isArray(data)) fail('protocol', 'container status response is not an array');
|
|
78
|
+
let crashing = false,
|
|
79
|
+
known = false;
|
|
80
|
+
for (const pod of data) {
|
|
81
|
+
if (!object(pod) || (pod.containerStatuses != null && !Array.isArray(pod.containerStatuses)))
|
|
82
|
+
fail('protocol', 'invalid container status');
|
|
83
|
+
for (const container of pod.containerStatuses ?? []) {
|
|
84
|
+
if (container !== null && !object(container)) fail('protocol', 'invalid container status');
|
|
85
|
+
known = true;
|
|
86
|
+
const state = container?.state;
|
|
87
|
+
const waiting = state?.waiting;
|
|
88
|
+
const reason = waiting?.reason;
|
|
89
|
+
if (
|
|
90
|
+
(state != null && (!object(state) || JSON.isRawJSON(state))) ||
|
|
91
|
+
(waiting != null && (!object(waiting) || JSON.isRawJSON(waiting))) ||
|
|
92
|
+
(reason != null && typeof reason !== 'string')
|
|
93
|
+
)
|
|
94
|
+
fail('protocol', 'invalid nested container status');
|
|
95
|
+
if (reason === 'CrashLoopBackOff') crashing = true;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (crashing)
|
|
99
|
+
result.diagnostics = ['CrashLoopBackOff:容器反复退出,系统正在延迟重启;查看日志定位原因'];
|
|
100
|
+
if (!known) result.diagnostics = ['unknown:没有可用的容器状态,无法判断容器是否正常'];
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { lstat, stat, open, link, unlink } from 'node:fs/promises';
|
|
2
|
+
import { resolve, dirname, join } from 'node:path';
|
|
3
|
+
import { randomUUID, createHash } from 'node:crypto';
|
|
4
|
+
import { check, fail, collect } from './input.mjs';
|
|
5
|
+
|
|
6
|
+
async function source(path, limit) {
|
|
7
|
+
let handle;
|
|
8
|
+
try {
|
|
9
|
+
const prior = await lstat(path, { bigint: true });
|
|
10
|
+
check(prior.isFile(), 'source must be a regular file');
|
|
11
|
+
handle = await open(path, 'r');
|
|
12
|
+
const info = await handle.stat({ bigint: true });
|
|
13
|
+
// Windows path stat can report device 0 while fstat reports the volume serial.
|
|
14
|
+
const sameDevice = prior.dev === info.dev || (process.platform === 'win32' && prior.dev === 0n);
|
|
15
|
+
check(info.isFile() && sameDevice && prior.ino === info.ino, 'source changed while opening');
|
|
16
|
+
check(info.size <= BigInt(limit), 'source exceeds file size limit');
|
|
17
|
+
return { handle, info };
|
|
18
|
+
} catch (e) {
|
|
19
|
+
await handle?.close();
|
|
20
|
+
if (e.kind) throw e;
|
|
21
|
+
fail('usage', 'cannot open source file');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export async function readSource(path, limit) {
|
|
25
|
+
const { handle } = await source(path, limit);
|
|
26
|
+
try {
|
|
27
|
+
const bytes = await collect(
|
|
28
|
+
handle.createReadStream({ autoClose: false }),
|
|
29
|
+
limit,
|
|
30
|
+
'usage',
|
|
31
|
+
'source too large'
|
|
32
|
+
);
|
|
33
|
+
return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
34
|
+
} catch {
|
|
35
|
+
fail('usage', 'source must be readable UTF-8 within the size limit');
|
|
36
|
+
} finally {
|
|
37
|
+
await handle.close();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// Inspect the original spelling before URL normalization can remove dot segments.
|
|
41
|
+
function safePath(raw) {
|
|
42
|
+
if (typeof raw !== 'string' || !raw || raw.trim() !== raw || /[\\#\x00-\x1f\x7f]/.test(raw))
|
|
43
|
+
return false;
|
|
44
|
+
const path = raw.split('?')[0];
|
|
45
|
+
if (/%(?:2f|5c|25)/i.test(path)) return false;
|
|
46
|
+
try {
|
|
47
|
+
return !decodeURIComponent(path)
|
|
48
|
+
.split('/')
|
|
49
|
+
.some((s) => s === '.' || s === '..' || /[\\\x00-\x1f]/.test(s));
|
|
50
|
+
} catch {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async function request(url, options, write) {
|
|
55
|
+
try {
|
|
56
|
+
return await fetch(url, { ...options, redirect: 'manual' });
|
|
57
|
+
} catch {
|
|
58
|
+
fail('network', 'RGP transfer request failed', write ? { outcome: 'unknown' } : {});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function tusResponse(res) {
|
|
62
|
+
await res.body?.cancel();
|
|
63
|
+
if (!res.ok)
|
|
64
|
+
fail([401, 403].includes(res.status) ? 'auth' : 'protocol', 'tus request failed', {
|
|
65
|
+
http_status: res.status,
|
|
66
|
+
outcome: 'unknown',
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
export async function upload(endpoint, token, remote, path, limit, signal) {
|
|
70
|
+
const { handle, info } = await source(path, limit);
|
|
71
|
+
const headers = { 'Tus-Resumable': '1.0.0', 'app-token': token };
|
|
72
|
+
try {
|
|
73
|
+
const created = await request(
|
|
74
|
+
endpoint,
|
|
75
|
+
{
|
|
76
|
+
method: 'POST',
|
|
77
|
+
signal,
|
|
78
|
+
headers: {
|
|
79
|
+
...headers,
|
|
80
|
+
'Upload-Length': String(info.size),
|
|
81
|
+
'Upload-Metadata': 'path_with_file_name ' + Buffer.from(remote).toString('base64'),
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
true
|
|
85
|
+
);
|
|
86
|
+
await tusResponse(created);
|
|
87
|
+
const loc = created.headers.get('location');
|
|
88
|
+
let target;
|
|
89
|
+
try {
|
|
90
|
+
target = new URL(loc, endpoint);
|
|
91
|
+
} catch {}
|
|
92
|
+
if (
|
|
93
|
+
!safePath(loc) ||
|
|
94
|
+
loc.includes('?') ||
|
|
95
|
+
!target ||
|
|
96
|
+
target.username ||
|
|
97
|
+
target.password ||
|
|
98
|
+
target.origin !== endpoint.origin ||
|
|
99
|
+
!target.pathname.startsWith(endpoint.pathname) ||
|
|
100
|
+
target.pathname === endpoint.pathname
|
|
101
|
+
)
|
|
102
|
+
fail('protocol', 'invalid tus upload location', { outcome: 'unknown' });
|
|
103
|
+
const hash = createHash('sha256');
|
|
104
|
+
let offset = 0;
|
|
105
|
+
const buf = Buffer.alloc(1 << 20);
|
|
106
|
+
while (offset < Number(info.size)) {
|
|
107
|
+
const length = Math.min(buf.length, Number(info.size) - offset);
|
|
108
|
+
const { bytesRead } = await handle.read(buf, 0, length, offset);
|
|
109
|
+
if (!bytesRead) fail('usage', 'source changed during upload', { outcome: 'unknown' });
|
|
110
|
+
const chunk = buf.subarray(0, bytesRead);
|
|
111
|
+
const res = await request(
|
|
112
|
+
target,
|
|
113
|
+
{
|
|
114
|
+
method: 'PATCH',
|
|
115
|
+
signal,
|
|
116
|
+
body: chunk,
|
|
117
|
+
headers: {
|
|
118
|
+
...headers,
|
|
119
|
+
'Content-Type': 'application/offset+octet-stream',
|
|
120
|
+
'Upload-Offset': String(offset),
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
true
|
|
124
|
+
);
|
|
125
|
+
await tusResponse(res);
|
|
126
|
+
offset += bytesRead;
|
|
127
|
+
if (res.headers.get('upload-offset') !== String(offset))
|
|
128
|
+
fail('protocol', 'unexpected tus upload offset', { outcome: 'unknown' });
|
|
129
|
+
hash.update(chunk);
|
|
130
|
+
}
|
|
131
|
+
const current = await handle.stat({ bigint: true });
|
|
132
|
+
if (current.size !== info.size || current.mtimeNs !== info.mtimeNs)
|
|
133
|
+
fail('usage', 'source changed during upload', { outcome: 'unknown' });
|
|
134
|
+
return {
|
|
135
|
+
ok: true,
|
|
136
|
+
operation: 'file.upload',
|
|
137
|
+
outcome: 'completed',
|
|
138
|
+
...(offset ? { bytes: offset } : {}),
|
|
139
|
+
sha256: hash.digest('hex'),
|
|
140
|
+
};
|
|
141
|
+
} catch (e) {
|
|
142
|
+
if (e.kind) throw e;
|
|
143
|
+
fail('usage', 'upload source could not be read', { outcome: 'unknown' });
|
|
144
|
+
} finally {
|
|
145
|
+
await handle.close();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
export async function download(base, op, authorize, path, limit, signal) {
|
|
149
|
+
const output = resolve(path),
|
|
150
|
+
parent = dirname(output);
|
|
151
|
+
try {
|
|
152
|
+
await lstat(output);
|
|
153
|
+
fail('usage', 'output already exists');
|
|
154
|
+
} catch (e) {
|
|
155
|
+
if (e.code !== 'ENOENT') fail('usage', 'output exists or is inaccessible');
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
check((await stat(parent)).isDirectory(), 'invalid output directory');
|
|
159
|
+
} catch {
|
|
160
|
+
fail('usage', 'invalid output directory');
|
|
161
|
+
}
|
|
162
|
+
const { data: suffix } = await authorize();
|
|
163
|
+
if (!safePath(suffix) || !suffix.startsWith('/') || suffix.startsWith('//'))
|
|
164
|
+
fail('protocol', 'invalid download path');
|
|
165
|
+
const response = await request(
|
|
166
|
+
base + suffix,
|
|
167
|
+
{ signal, headers: { 'Accept-Encoding': 'identity' } },
|
|
168
|
+
false
|
|
169
|
+
);
|
|
170
|
+
const length = response.headers.get('content-length');
|
|
171
|
+
if (response.status !== 200 || Number(length) > limit) {
|
|
172
|
+
await response.body?.cancel();
|
|
173
|
+
fail('protocol', 'download returned an invalid response', { http_status: response.status });
|
|
174
|
+
}
|
|
175
|
+
const temp = join(parent, '.rain-download-' + randomUUID());
|
|
176
|
+
let handle,
|
|
177
|
+
owned = false;
|
|
178
|
+
try {
|
|
179
|
+
handle = await open(temp, 'wx', 0o600);
|
|
180
|
+
owned = true;
|
|
181
|
+
const hash = createHash('sha256');
|
|
182
|
+
let count = 0;
|
|
183
|
+
for await (const chunk of response.body) {
|
|
184
|
+
count += chunk.length;
|
|
185
|
+
if (count > limit) fail('protocol', 'download exceeds file limit');
|
|
186
|
+
await handle.writeFile(chunk);
|
|
187
|
+
hash.update(chunk);
|
|
188
|
+
}
|
|
189
|
+
if (length !== null && count !== Number(length)) fail('protocol', 'incomplete download');
|
|
190
|
+
signal.throwIfAborted();
|
|
191
|
+
await handle.sync();
|
|
192
|
+
await handle.close();
|
|
193
|
+
handle = null;
|
|
194
|
+
try {
|
|
195
|
+
await link(temp, output);
|
|
196
|
+
} catch {
|
|
197
|
+
fail('usage', 'cannot publish download without replacing an existing file');
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
ok: true,
|
|
201
|
+
operation: op,
|
|
202
|
+
local_path: output,
|
|
203
|
+
...(count ? { bytes: count } : {}),
|
|
204
|
+
sha256: hash.digest('hex'),
|
|
205
|
+
};
|
|
206
|
+
} catch (e) {
|
|
207
|
+
if (e.kind) throw e;
|
|
208
|
+
fail('network', 'download was not completely saved');
|
|
209
|
+
} finally {
|
|
210
|
+
await handle?.close();
|
|
211
|
+
if (owned) await unlink(temp).catch(() => {});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { error, importRCA, buildEndpoint, resolveRGS } from './target.mjs';
|
|
2
|
+
import { session } from './session.mjs';
|
|
3
|
+
import { check, allow } from '../api/catalog.mjs';
|
|
4
|
+
import { duration, baseAddress, defaultBase } from '../api/http.mjs';
|
|
5
|
+
import { inputFile } from '../api/request.mjs';
|
|
6
|
+
|
|
7
|
+
const int = (v, max = Number.MAX_SAFE_INTEGER) => {
|
|
8
|
+
const n = Number(v);
|
|
9
|
+
check(
|
|
10
|
+
/^\d+$/.test(String(v)) && Number.isSafeInteger(n) && n >= 1 && n <= max,
|
|
11
|
+
'Expected a positive integer up to ' + max
|
|
12
|
+
);
|
|
13
|
+
return n;
|
|
14
|
+
};
|
|
15
|
+
async function readJSON(file, ctx) {
|
|
16
|
+
check(typeof file === 'string' && file.length, '--input requires a file or -');
|
|
17
|
+
const text = await inputFile(file, ctx);
|
|
18
|
+
try {
|
|
19
|
+
return ctx.jsonParse(text);
|
|
20
|
+
} catch {
|
|
21
|
+
throw error('usage', 'Invalid UTF-8 JSON input or duplicate field');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async function loadProfile(store, id, cid, ctx) {
|
|
25
|
+
const s = await store.get('rca-' + id + '-' + cid);
|
|
26
|
+
if (s == null) return null;
|
|
27
|
+
try {
|
|
28
|
+
const json = s.trimStart().startsWith('{') ? s : Buffer.from(s, 'base64').toString('utf8');
|
|
29
|
+
const target = ctx.jsonParse(json);
|
|
30
|
+
const keys = [
|
|
31
|
+
'product',
|
|
32
|
+
'id',
|
|
33
|
+
'container_id',
|
|
34
|
+
'helper_domain',
|
|
35
|
+
'namespace',
|
|
36
|
+
'token',
|
|
37
|
+
'install_name',
|
|
38
|
+
'region_id',
|
|
39
|
+
'region_name',
|
|
40
|
+
];
|
|
41
|
+
if (
|
|
42
|
+
!Object.keys(target).every((k) => keys.includes(k)) ||
|
|
43
|
+
target.product !== 'rca' ||
|
|
44
|
+
target.id !== id ||
|
|
45
|
+
target.container_id !== cid
|
|
46
|
+
)
|
|
47
|
+
throw Error();
|
|
48
|
+
buildEndpoint(target, { mode: 'auto', cols: 100, rows: 60 });
|
|
49
|
+
return target;
|
|
50
|
+
} catch {
|
|
51
|
+
throw error('auth', 'Invalid local RCA profile; import it again');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async function profile(args, flags, ctx) {
|
|
55
|
+
const [action, product, app, container] = args;
|
|
56
|
+
check(
|
|
57
|
+
args.length === 4 && product === 'rca',
|
|
58
|
+
'Expected terminal import|status|forget rca APP_ID CONTAINER_ID'
|
|
59
|
+
);
|
|
60
|
+
allow(flags, action === 'import' ? ['input'] : []);
|
|
61
|
+
const id = int(app),
|
|
62
|
+
cid = int(container),
|
|
63
|
+
key = 'rca-' + id + '-' + cid;
|
|
64
|
+
const data = { product, id, container_id: cid, verified: false };
|
|
65
|
+
if (action === 'import') {
|
|
66
|
+
const target = importRCA(await readJSON(flags.input, ctx), id, cid);
|
|
67
|
+
await ctx.store.set(key, JSON.stringify(target));
|
|
68
|
+
Object.assign(data, { configured: true, saved: true });
|
|
69
|
+
} else if (action === 'status') data.configured = !!(await loadProfile(ctx.store, id, cid, ctx));
|
|
70
|
+
else {
|
|
71
|
+
data.removed = await ctx.store.remove(key);
|
|
72
|
+
data.remote_revoked = false;
|
|
73
|
+
}
|
|
74
|
+
return { ok: true, data };
|
|
75
|
+
}
|
|
76
|
+
export async function terminal(args, flags, ctx) {
|
|
77
|
+
const [product, instance, container] = args;
|
|
78
|
+
if (['import', 'status', 'forget'].includes(product)) return profile(args, flags, ctx);
|
|
79
|
+
const isRGS = product === 'rgs';
|
|
80
|
+
check(isRGS || product === 'rca', 'Expected terminal rgs ID or rca APP_ID CONTAINER_ID');
|
|
81
|
+
check(args.length === (isRGS ? 2 : 3), 'Incorrect number of terminal resource IDs');
|
|
82
|
+
allow(flags, [
|
|
83
|
+
'mode',
|
|
84
|
+
'input',
|
|
85
|
+
'duration',
|
|
86
|
+
'max-output-bytes',
|
|
87
|
+
'cols',
|
|
88
|
+
'rows',
|
|
89
|
+
'yes',
|
|
90
|
+
'dry-run',
|
|
91
|
+
'stream',
|
|
92
|
+
...(isRGS ? ['base-url', 'timeout'] : ['logs', 'tail-lines', 'since-seconds']),
|
|
93
|
+
]);
|
|
94
|
+
const id = int(instance),
|
|
95
|
+
cid = isRGS ? 0 : int(container);
|
|
96
|
+
const mode = flags.mode ?? (isRGS ? 'attach' : 'auto');
|
|
97
|
+
check((isRGS ? ['attach'] : ['auto', 'exec', 'attach']).includes(mode), 'Unsupported terminal mode');
|
|
98
|
+
const stream = !!flags.stream,
|
|
99
|
+
logs = !!flags.logs;
|
|
100
|
+
check(
|
|
101
|
+
!logs || !('mode' in flags || 'input' in flags || stream),
|
|
102
|
+
'--logs conflicts with --mode, --input or --stream'
|
|
103
|
+
);
|
|
104
|
+
check(logs || !('tail-lines' in flags || 'since-seconds' in flags), 'Log filters require --logs');
|
|
105
|
+
check(
|
|
106
|
+
!('input' in flags) ||
|
|
107
|
+
(typeof flags.input === 'string' && flags.input.length && !(stream && flags.input === '-')),
|
|
108
|
+
'Invalid --input or stdin conflict'
|
|
109
|
+
);
|
|
110
|
+
const o = {
|
|
111
|
+
product,
|
|
112
|
+
id,
|
|
113
|
+
container_id: cid,
|
|
114
|
+
mode,
|
|
115
|
+
stream,
|
|
116
|
+
logs,
|
|
117
|
+
duration_ms: duration(flags.duration ?? (stream ? '5m' : '5s')),
|
|
118
|
+
max_output_bytes: int(flags['max-output-bytes'] ?? 262144, 1048576),
|
|
119
|
+
cols: int(flags.cols ?? (isRGS ? 240 : 100), 1000),
|
|
120
|
+
rows: int(flags.rows ?? 60, 1000),
|
|
121
|
+
tail_lines: int(flags['tail-lines'] ?? 200, 10000),
|
|
122
|
+
since_seconds: int(flags['since-seconds'] ?? 86400, 604800),
|
|
123
|
+
};
|
|
124
|
+
check(o.duration_ms <= (stream ? 3600000 : 120000), 'Terminal duration exceeds limit');
|
|
125
|
+
if (isRGS) {
|
|
126
|
+
baseAddress(flags['base-url'] ?? (process.env.RAINYUN_API_URL || defaultBase));
|
|
127
|
+
if (flags.timeout !== undefined) duration(flags.timeout);
|
|
128
|
+
}
|
|
129
|
+
if (flags['dry-run'])
|
|
130
|
+
return {
|
|
131
|
+
ok: true,
|
|
132
|
+
dry_run: true,
|
|
133
|
+
...o,
|
|
134
|
+
metadata_source: isRGS ? 'rgs_api_detail' : 'local_rca_profile',
|
|
135
|
+
initial_input_requested: 'input' in flags,
|
|
136
|
+
is_write: !logs && ('input' in flags || stream || mode !== 'attach'),
|
|
137
|
+
requires_yes: false,
|
|
138
|
+
exit_code: null,
|
|
139
|
+
command_outcome: 'unknown',
|
|
140
|
+
};
|
|
141
|
+
if (flags.input) {
|
|
142
|
+
const body = await readJSON(flags.input, ctx);
|
|
143
|
+
check(
|
|
144
|
+
body &&
|
|
145
|
+
Object.keys(body).length === 1 &&
|
|
146
|
+
typeof body.text === 'string' &&
|
|
147
|
+
Buffer.byteLength(body.text) <= 65536,
|
|
148
|
+
'Expected a terminal input object containing only text (up to 65536 bytes)'
|
|
149
|
+
);
|
|
150
|
+
o.input = body.text;
|
|
151
|
+
}
|
|
152
|
+
const target = isRGS ? await resolveRGS(id, flags, ctx) : await loadProfile(ctx.store, id, cid, ctx);
|
|
153
|
+
if (!target) throw error('auth', 'First run terminal import rca for this app and container');
|
|
154
|
+
return session(target, o, ctx);
|
|
155
|
+
}
|