@magpiecloud/mags 1.8.13 → 1.8.15
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 +95 -378
- package/bin/mags.js +196 -104
- package/index.js +6 -52
- package/package.json +22 -4
- package/API.md +0 -388
- package/Mags-API.postman_collection.json +0 -374
- package/QUICKSTART.md +0 -295
- package/deploy-page.sh +0 -171
- package/mags +0 -0
- package/mags.sh +0 -270
- package/nodejs/README.md +0 -197
- package/nodejs/bin/mags.js +0 -1146
- package/nodejs/index.js +0 -642
- package/nodejs/package.json +0 -42
- package/python/INTEGRATION.md +0 -800
- package/python/README.md +0 -161
- package/python/dist/magpie_mags-1.3.5-py3-none-any.whl +0 -0
- package/python/dist/magpie_mags-1.3.5.tar.gz +0 -0
- package/python/examples/demo.py +0 -181
- package/python/pyproject.toml +0 -39
- package/python/src/magpie_mags.egg-info/PKG-INFO +0 -182
- package/python/src/magpie_mags.egg-info/SOURCES.txt +0 -9
- package/python/src/magpie_mags.egg-info/dependency_links.txt +0 -1
- package/python/src/magpie_mags.egg-info/requires.txt +0 -1
- package/python/src/magpie_mags.egg-info/top_level.txt +0 -1
- package/python/src/mags/__init__.py +0 -6
- package/python/src/mags/client.py +0 -573
- package/python/test_sdk.py +0 -78
- package/skill.md +0 -153
- package/website/api.html +0 -1095
- package/website/claude-skill.html +0 -481
- package/website/cookbook/hn-marketing.html +0 -410
- package/website/cookbook/hn-marketing.sh +0 -42
- package/website/cookbook.html +0 -282
- package/website/env.js +0 -4
- package/website/index.html +0 -801
- package/website/llms.txt +0 -334
- package/website/login.html +0 -108
- package/website/mags.md +0 -210
- package/website/script.js +0 -453
- package/website/styles.css +0 -908
- package/website/tokens.html +0 -169
- package/website/usage.html +0 -185
package/nodejs/index.js
DELETED
|
@@ -1,642 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Mags SDK - Execute scripts on Magpie's instant VM infrastructure
|
|
3
|
-
* @module @magpiecloud/mags
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const https = require('https');
|
|
7
|
-
const http = require('http');
|
|
8
|
-
const { URL } = require('url');
|
|
9
|
-
|
|
10
|
-
class MagsError extends Error {
|
|
11
|
-
constructor(message, statusCode) {
|
|
12
|
-
super(message);
|
|
13
|
-
this.name = 'MagsError';
|
|
14
|
-
this.statusCode = statusCode || null;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
class Mags {
|
|
19
|
-
/**
|
|
20
|
-
* Create a Mags client
|
|
21
|
-
* @param {object} options - Configuration options
|
|
22
|
-
* @param {string} options.apiUrl - API endpoint (default: https://api.magpiecloud.com)
|
|
23
|
-
* @param {string} options.apiToken - API token (required, or set MAGS_API_TOKEN env var)
|
|
24
|
-
* @param {number} options.timeout - Default request timeout in ms (default: 30000)
|
|
25
|
-
*/
|
|
26
|
-
constructor(options = {}) {
|
|
27
|
-
this.apiUrl = (options.apiUrl || process.env.MAGS_API_URL || 'https://api.magpiecloud.com').replace(/\/+$/, '');
|
|
28
|
-
this.apiToken = options.apiToken || process.env.MAGS_API_TOKEN || process.env.MAGS_TOKEN;
|
|
29
|
-
this.timeout = options.timeout || 30000;
|
|
30
|
-
|
|
31
|
-
if (!this.apiToken) {
|
|
32
|
-
throw new MagsError('API token required. Set MAGS_API_TOKEN or pass apiToken option.');
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
_request(method, path, body = null, params = null) {
|
|
37
|
-
return new Promise((resolve, reject) => {
|
|
38
|
-
const url = new URL(path, this.apiUrl);
|
|
39
|
-
if (params) {
|
|
40
|
-
for (const [k, v] of Object.entries(params)) {
|
|
41
|
-
url.searchParams.set(k, v);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
const isHttps = url.protocol === 'https:';
|
|
45
|
-
const lib = isHttps ? https : http;
|
|
46
|
-
|
|
47
|
-
const options = {
|
|
48
|
-
hostname: url.hostname,
|
|
49
|
-
port: url.port || (isHttps ? 443 : 80),
|
|
50
|
-
path: url.pathname + url.search,
|
|
51
|
-
method,
|
|
52
|
-
headers: {
|
|
53
|
-
'Authorization': `Bearer ${this.apiToken}`,
|
|
54
|
-
'Content-Type': 'application/json'
|
|
55
|
-
},
|
|
56
|
-
timeout: this.timeout
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const req = lib.request(options, (res) => {
|
|
60
|
-
let data = '';
|
|
61
|
-
res.on('data', chunk => data += chunk);
|
|
62
|
-
res.on('end', () => {
|
|
63
|
-
try {
|
|
64
|
-
const parsed = JSON.parse(data);
|
|
65
|
-
if (res.statusCode >= 400) {
|
|
66
|
-
reject(new MagsError(parsed.error || parsed.message || `HTTP ${res.statusCode}`, res.statusCode));
|
|
67
|
-
} else {
|
|
68
|
-
resolve(parsed);
|
|
69
|
-
}
|
|
70
|
-
} catch {
|
|
71
|
-
if (res.statusCode >= 400) {
|
|
72
|
-
reject(new MagsError(data || `HTTP ${res.statusCode}`, res.statusCode));
|
|
73
|
-
} else {
|
|
74
|
-
resolve(data);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
req.on('error', reject);
|
|
81
|
-
req.on('timeout', () => {
|
|
82
|
-
req.destroy();
|
|
83
|
-
reject(new MagsError('Request timed out'));
|
|
84
|
-
});
|
|
85
|
-
if (body) req.write(JSON.stringify(body));
|
|
86
|
-
req.end();
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// ── Jobs ──────────────────────────────────────────────────────────
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Submit a job for execution
|
|
94
|
-
* @param {string} script - Script to execute
|
|
95
|
-
* @param {object} options - Job options
|
|
96
|
-
* @param {string} options.name - Job name
|
|
97
|
-
* @param {string} options.workspaceId - Persistent workspace ID
|
|
98
|
-
* @param {string} options.baseWorkspaceId - Read-only base workspace to mount
|
|
99
|
-
* @param {boolean} options.persistent - Keep VM alive after script
|
|
100
|
-
* @param {boolean} options.noSleep - Never auto-sleep (requires persistent)
|
|
101
|
-
* @param {boolean} options.ephemeral - No workspace/S3 sync (fastest)
|
|
102
|
-
* @param {string} options.startupCommand - Command to run when waking from sleep
|
|
103
|
-
* @param {object} options.environment - Environment variables
|
|
104
|
-
* @param {string[]} options.fileIds - File IDs from uploadFiles()
|
|
105
|
-
* @param {number} options.diskGb - Custom disk size in GB (default 2)
|
|
106
|
-
* @returns {Promise<{request_id: string, status: string}>}
|
|
107
|
-
*/
|
|
108
|
-
async run(script, options = {}) {
|
|
109
|
-
if (options.ephemeral && options.workspaceId) {
|
|
110
|
-
throw new MagsError('Cannot use ephemeral with workspaceId');
|
|
111
|
-
}
|
|
112
|
-
if (options.ephemeral && options.persistent) {
|
|
113
|
-
throw new MagsError('Cannot use ephemeral with persistent');
|
|
114
|
-
}
|
|
115
|
-
if (options.noSleep && !options.persistent) {
|
|
116
|
-
throw new MagsError('noSleep requires persistent=true');
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const payload = {
|
|
120
|
-
script,
|
|
121
|
-
type: 'inline',
|
|
122
|
-
persistent: options.persistent || false,
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
if (options.noSleep) payload.no_sleep = true;
|
|
126
|
-
if (options.name) payload.name = options.name;
|
|
127
|
-
if (!options.ephemeral && options.workspaceId) payload.workspace_id = options.workspaceId;
|
|
128
|
-
if (options.baseWorkspaceId) payload.base_workspace_id = options.baseWorkspaceId;
|
|
129
|
-
if (options.startupCommand) payload.startup_command = options.startupCommand;
|
|
130
|
-
if (options.environment) payload.environment = options.environment;
|
|
131
|
-
if (options.fileIds && options.fileIds.length > 0) payload.file_ids = options.fileIds;
|
|
132
|
-
if (options.diskGb) payload.disk_gb = options.diskGb;
|
|
133
|
-
|
|
134
|
-
return this._request('POST', '/api/v1/mags-jobs', payload);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Run a job and wait for completion
|
|
139
|
-
* @param {string} script - Script to execute
|
|
140
|
-
* @param {object} options - Job options (same as run() plus timeout/pollInterval)
|
|
141
|
-
* @param {number} options.timeout - Timeout in ms (default: 60000)
|
|
142
|
-
* @param {number} options.pollInterval - Poll interval in ms (default: 1000)
|
|
143
|
-
* @returns {Promise<{requestId: string, status: string, exitCode: number, durationMs: number, logs: Array}>}
|
|
144
|
-
*/
|
|
145
|
-
async runAndWait(script, options = {}) {
|
|
146
|
-
const timeout = options.timeout || 60000;
|
|
147
|
-
const pollInterval = options.pollInterval || 1000;
|
|
148
|
-
const result = await this.run(script, options);
|
|
149
|
-
const requestId = result.request_id;
|
|
150
|
-
|
|
151
|
-
const startTime = Date.now();
|
|
152
|
-
while (Date.now() - startTime < timeout) {
|
|
153
|
-
const status = await this.status(requestId);
|
|
154
|
-
|
|
155
|
-
if (status.status === 'completed' || status.status === 'error') {
|
|
156
|
-
const logsResp = await this.logs(requestId);
|
|
157
|
-
return {
|
|
158
|
-
requestId,
|
|
159
|
-
status: status.status,
|
|
160
|
-
exitCode: status.exit_code,
|
|
161
|
-
durationMs: status.script_duration_ms,
|
|
162
|
-
logs: logsResp.logs || []
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
throw new MagsError(`Job ${requestId} timed out after ${timeout}ms`);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Get job status
|
|
174
|
-
* @param {string} requestId - Job request ID
|
|
175
|
-
* @returns {Promise<object>}
|
|
176
|
-
*/
|
|
177
|
-
async status(requestId) {
|
|
178
|
-
return this._request('GET', `/api/v1/mags-jobs/${requestId}/status`);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Get job logs
|
|
183
|
-
* @param {string} requestId - Job request ID
|
|
184
|
-
* @returns {Promise<{logs: Array}>}
|
|
185
|
-
*/
|
|
186
|
-
async logs(requestId) {
|
|
187
|
-
return this._request('GET', `/api/v1/mags-jobs/${requestId}/logs`);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* List recent jobs
|
|
192
|
-
* @param {object} options - Pagination options
|
|
193
|
-
* @param {number} options.page - Page number (default: 1)
|
|
194
|
-
* @param {number} options.pageSize - Page size (default: 20)
|
|
195
|
-
* @returns {Promise<{jobs: Array, total: number}>}
|
|
196
|
-
*/
|
|
197
|
-
async list(options = {}) {
|
|
198
|
-
const page = options.page || 1;
|
|
199
|
-
const pageSize = options.pageSize || 20;
|
|
200
|
-
return this._request('GET', `/api/v1/mags-jobs`, null, { page, page_size: pageSize });
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Update a job's settings
|
|
205
|
-
* @param {string} requestId - Job request ID
|
|
206
|
-
* @param {object} options - Settings to update
|
|
207
|
-
* @param {string} options.startupCommand - Command to run when VM wakes from sleep
|
|
208
|
-
* @param {boolean} options.noSleep - If true, VM never auto-sleeps. If false, re-enables auto-sleep.
|
|
209
|
-
* @returns {Promise<object>}
|
|
210
|
-
*/
|
|
211
|
-
async updateJob(requestId, options = {}) {
|
|
212
|
-
const payload = {};
|
|
213
|
-
if (options.startupCommand !== undefined) payload.startup_command = options.startupCommand;
|
|
214
|
-
if (options.noSleep !== undefined) payload.no_sleep = options.noSleep;
|
|
215
|
-
return this._request('PATCH', `/api/v1/mags-jobs/${requestId}`, payload);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Enable URL or SSH access for a job
|
|
220
|
-
* @param {string} requestId - Job request ID
|
|
221
|
-
* @param {number} port - Port to expose (default: 8080, use 22 for SSH)
|
|
222
|
-
* @returns {Promise<object>}
|
|
223
|
-
*/
|
|
224
|
-
async enableAccess(requestId, port = 8080) {
|
|
225
|
-
return this._request('POST', `/api/v1/mags-jobs/${requestId}/access`, { port });
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Enable public URL access for a job's VM
|
|
230
|
-
* @param {string} nameOrId - Job name, workspace ID, or request ID
|
|
231
|
-
* @param {number} port - Port to expose (default: 8080)
|
|
232
|
-
* @returns {Promise<object>} Object with url and access details
|
|
233
|
-
*/
|
|
234
|
-
async url(nameOrId, port = 8080) {
|
|
235
|
-
const requestId = await this._resolveJobId(nameOrId);
|
|
236
|
-
const st = await this.status(requestId);
|
|
237
|
-
const resp = await this.enableAccess(requestId, port);
|
|
238
|
-
const subdomain = st.subdomain || resp.subdomain;
|
|
239
|
-
if (subdomain) {
|
|
240
|
-
resp.url = `https://${subdomain}.apps.magpiecloud.com`;
|
|
241
|
-
}
|
|
242
|
-
return resp;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Stop a running job. Accepts a job ID, job name, or workspace ID.
|
|
247
|
-
* @param {string} nameOrId - Job name, workspace ID, or request ID
|
|
248
|
-
* @returns {Promise<object>}
|
|
249
|
-
*/
|
|
250
|
-
async stop(nameOrId) {
|
|
251
|
-
const requestId = await this._resolveJobId(nameOrId);
|
|
252
|
-
return this._request('POST', `/api/v1/mags-jobs/${requestId}/stop`);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Sync a running job's workspace to S3 without stopping the VM
|
|
257
|
-
* @param {string} requestId - Job request ID
|
|
258
|
-
* @returns {Promise<object>}
|
|
259
|
-
*/
|
|
260
|
-
async sync(requestId) {
|
|
261
|
-
return this._request('POST', `/api/v1/mags-jobs/${requestId}/sync`);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Create a new persistent VM workspace and wait until it's running
|
|
266
|
-
* @param {string} name - Workspace name
|
|
267
|
-
* @param {object} options - Options
|
|
268
|
-
* @param {string} options.baseWorkspaceId - Read-only base workspace to mount
|
|
269
|
-
* @param {number} options.diskGb - Custom disk size in GB
|
|
270
|
-
* @param {number} options.timeout - Timeout in ms (default: 30000)
|
|
271
|
-
* @param {number} options.pollInterval - Poll interval in ms (default: 1000)
|
|
272
|
-
* @returns {Promise<{request_id: string, status: string}>}
|
|
273
|
-
*/
|
|
274
|
-
async new(name, options = {}) {
|
|
275
|
-
const timeout = options.timeout || 30000;
|
|
276
|
-
const pollInterval = options.pollInterval || 1000;
|
|
277
|
-
|
|
278
|
-
const result = await this.run('sleep infinity', {
|
|
279
|
-
workspaceId: name,
|
|
280
|
-
persistent: true,
|
|
281
|
-
baseWorkspaceId: options.baseWorkspaceId,
|
|
282
|
-
diskGb: options.diskGb,
|
|
283
|
-
});
|
|
284
|
-
const requestId = result.request_id;
|
|
285
|
-
|
|
286
|
-
const startTime = Date.now();
|
|
287
|
-
while (Date.now() - startTime < timeout) {
|
|
288
|
-
const st = await this.status(requestId);
|
|
289
|
-
if (st.status === 'running' && st.vm_id) {
|
|
290
|
-
return { request_id: requestId, status: 'running' };
|
|
291
|
-
}
|
|
292
|
-
if (st.status === 'completed' || st.status === 'error') {
|
|
293
|
-
throw new MagsError(`Job ${requestId} ended unexpectedly: ${st.status}`);
|
|
294
|
-
}
|
|
295
|
-
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
throw new MagsError(`Job ${requestId} did not start within ${timeout}ms`);
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* Find a running or sleeping job by name, workspace ID, or job ID
|
|
303
|
-
* @param {string} nameOrId - Job name, workspace ID, or request ID
|
|
304
|
-
* @returns {Promise<object|null>} The job object, or null if not found
|
|
305
|
-
*/
|
|
306
|
-
async findJob(nameOrId) {
|
|
307
|
-
const resp = await this.list({ pageSize: 50 });
|
|
308
|
-
const jobs = resp.jobs || [];
|
|
309
|
-
|
|
310
|
-
// Priority 1: exact name match, running/sleeping
|
|
311
|
-
for (const j of jobs) {
|
|
312
|
-
if (j.name === nameOrId && (j.status === 'running' || j.status === 'sleeping')) return j;
|
|
313
|
-
}
|
|
314
|
-
// Priority 2: workspace_id match, running/sleeping
|
|
315
|
-
for (const j of jobs) {
|
|
316
|
-
if (j.workspace_id === nameOrId && (j.status === 'running' || j.status === 'sleeping')) return j;
|
|
317
|
-
}
|
|
318
|
-
// Priority 3: exact name match, any status
|
|
319
|
-
for (const j of jobs) {
|
|
320
|
-
if (j.name === nameOrId) return j;
|
|
321
|
-
}
|
|
322
|
-
// Priority 4: workspace_id match, any status
|
|
323
|
-
for (const j of jobs) {
|
|
324
|
-
if (j.workspace_id === nameOrId) return j;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
return null;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* Execute a command on an existing running/sleeping VM via SSH
|
|
332
|
-
* @param {string} nameOrId - Job name, workspace ID, or request ID
|
|
333
|
-
* @param {string} command - Command to execute
|
|
334
|
-
* @param {object} options - Options
|
|
335
|
-
* @param {number} options.timeout - Timeout in ms (default: 30000)
|
|
336
|
-
* @returns {Promise<{exitCode: number, output: string, stderr: string}>}
|
|
337
|
-
*/
|
|
338
|
-
async exec(nameOrId, command, options = {}) {
|
|
339
|
-
const timeout = options.timeout || 30000;
|
|
340
|
-
|
|
341
|
-
const job = await this.findJob(nameOrId);
|
|
342
|
-
if (!job) throw new MagsError(`No running or sleeping VM found for '${nameOrId}'`);
|
|
343
|
-
if (job.status !== 'running' && job.status !== 'sleeping') {
|
|
344
|
-
throw new MagsError(`VM for '${nameOrId}' is ${job.status}, needs to be running or sleeping`);
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
const requestId = job.request_id || job.id;
|
|
348
|
-
const access = await this.enableAccess(requestId, 22);
|
|
349
|
-
|
|
350
|
-
if (!access.success || !access.ssh_host) {
|
|
351
|
-
throw new MagsError(`Failed to enable SSH access: ${access.error || 'unknown error'}`);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
const { execFileSync, execFile } = require('child_process');
|
|
355
|
-
const fs = require('fs');
|
|
356
|
-
const os = require('os');
|
|
357
|
-
const path = require('path');
|
|
358
|
-
|
|
359
|
-
const escaped = command.replace(/'/g, "'\\''");
|
|
360
|
-
const wrapped =
|
|
361
|
-
`if [ -d /overlay/bin ]; then ` +
|
|
362
|
-
`chroot /overlay /bin/sh -l -c 'cd /root 2>/dev/null; ${escaped}'; ` +
|
|
363
|
-
`else cd /root 2>/dev/null; ${escaped}; fi`;
|
|
364
|
-
|
|
365
|
-
let keyFile = null;
|
|
366
|
-
try {
|
|
367
|
-
const sshArgs = [
|
|
368
|
-
'-o', 'StrictHostKeyChecking=no',
|
|
369
|
-
'-o', 'UserKnownHostsFile=/dev/null',
|
|
370
|
-
'-o', 'LogLevel=ERROR',
|
|
371
|
-
'-p', String(access.ssh_port),
|
|
372
|
-
];
|
|
373
|
-
|
|
374
|
-
if (access.ssh_private_key) {
|
|
375
|
-
keyFile = path.join(os.tmpdir(), `mags_ssh_${Date.now()}`);
|
|
376
|
-
fs.writeFileSync(keyFile, access.ssh_private_key, { mode: 0o600 });
|
|
377
|
-
sshArgs.push('-i', keyFile);
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
sshArgs.push(`root@${access.ssh_host}`, wrapped);
|
|
381
|
-
|
|
382
|
-
return new Promise((resolve, reject) => {
|
|
383
|
-
const proc = execFile('ssh', sshArgs, { timeout, maxBuffer: 10 * 1024 * 1024 }, (err, stdout, stderr) => {
|
|
384
|
-
if (keyFile) try { fs.unlinkSync(keyFile); } catch {}
|
|
385
|
-
if (err && err.killed) {
|
|
386
|
-
reject(new MagsError(`Command timed out after ${timeout}ms`));
|
|
387
|
-
} else {
|
|
388
|
-
resolve({
|
|
389
|
-
exitCode: err ? err.code || 1 : 0,
|
|
390
|
-
output: stdout,
|
|
391
|
-
stderr: stderr,
|
|
392
|
-
});
|
|
393
|
-
}
|
|
394
|
-
});
|
|
395
|
-
});
|
|
396
|
-
} catch (e) {
|
|
397
|
-
if (keyFile) try { require('fs').unlinkSync(keyFile); } catch {}
|
|
398
|
-
throw e;
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* Resize a workspace's disk. Stops the existing VM, then creates a new one.
|
|
404
|
-
* @param {string} workspace - Workspace name
|
|
405
|
-
* @param {number} diskGb - New disk size in GB
|
|
406
|
-
* @param {object} options - Options
|
|
407
|
-
* @param {number} options.timeout - Timeout in ms (default: 30000)
|
|
408
|
-
* @param {number} options.pollInterval - Poll interval in ms (default: 1000)
|
|
409
|
-
* @returns {Promise<{request_id: string, status: string}>}
|
|
410
|
-
*/
|
|
411
|
-
async resize(workspace, diskGb, options = {}) {
|
|
412
|
-
const existing = await this.findJob(workspace);
|
|
413
|
-
if (existing && existing.status === 'running') {
|
|
414
|
-
await this._request('POST', `/api/v1/mags-jobs/${existing.request_id}/sync`);
|
|
415
|
-
await this._request('POST', `/api/v1/mags-jobs/${existing.request_id}/stop`);
|
|
416
|
-
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
417
|
-
} else if (existing && existing.status === 'sleeping') {
|
|
418
|
-
await this._request('POST', `/api/v1/mags-jobs/${existing.request_id}/stop`);
|
|
419
|
-
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
return this.new(workspace, { diskGb, timeout: options.timeout, pollInterval: options.pollInterval });
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
* Get aggregated usage summary
|
|
427
|
-
* @param {object} options - Options
|
|
428
|
-
* @param {number} options.windowDays - Time window in days (default: 30)
|
|
429
|
-
* @returns {Promise<object>}
|
|
430
|
-
*/
|
|
431
|
-
async usage(options = {}) {
|
|
432
|
-
return this._request('GET', '/api/v1/mags-jobs/usage', null, { window_days: options.windowDays || 30 });
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
// ── Workspaces ────────────────────────────────────────────────────
|
|
436
|
-
|
|
437
|
-
/**
|
|
438
|
-
* List all workspaces
|
|
439
|
-
* @returns {Promise<{workspaces: Array, total: number}>}
|
|
440
|
-
*/
|
|
441
|
-
async listWorkspaces() {
|
|
442
|
-
return this._request('GET', '/api/v1/mags-workspaces');
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
/**
|
|
446
|
-
* Delete a workspace and all its stored data
|
|
447
|
-
* @param {string} workspaceId - Workspace ID to delete
|
|
448
|
-
* @returns {Promise<object>}
|
|
449
|
-
*/
|
|
450
|
-
async deleteWorkspace(workspaceId) {
|
|
451
|
-
return this._request('DELETE', `/api/v1/mags-workspaces/${workspaceId}`);
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
// ── File uploads ──────────────────────────────────────────────────
|
|
455
|
-
|
|
456
|
-
/**
|
|
457
|
-
* Upload files for use in a job
|
|
458
|
-
* @param {string[]} filePaths - Array of local file paths
|
|
459
|
-
* @returns {Promise<string[]>} Array of file IDs
|
|
460
|
-
*/
|
|
461
|
-
async uploadFiles(filePaths) {
|
|
462
|
-
const fs = require('fs');
|
|
463
|
-
const path = require('path');
|
|
464
|
-
const fileIds = [];
|
|
465
|
-
|
|
466
|
-
for (const filePath of filePaths) {
|
|
467
|
-
const fileName = path.basename(filePath);
|
|
468
|
-
const fileData = fs.readFileSync(filePath);
|
|
469
|
-
const boundary = '----MagsBoundary' + Date.now().toString(16);
|
|
470
|
-
|
|
471
|
-
const parts = [];
|
|
472
|
-
parts.push(`--${boundary}\r\n`);
|
|
473
|
-
parts.push(`Content-Disposition: form-data; name="file"; filename="${fileName}"\r\n`);
|
|
474
|
-
parts.push(`Content-Type: application/octet-stream\r\n\r\n`);
|
|
475
|
-
const header = Buffer.from(parts.join(''));
|
|
476
|
-
const footer = Buffer.from(`\r\n--${boundary}--\r\n`);
|
|
477
|
-
const body = Buffer.concat([header, fileData, footer]);
|
|
478
|
-
|
|
479
|
-
const response = await this._multipartRequest('/api/v1/mags-files', body, boundary);
|
|
480
|
-
if (response.file_id) {
|
|
481
|
-
fileIds.push(response.file_id);
|
|
482
|
-
} else {
|
|
483
|
-
throw new MagsError(`Failed to upload file: ${fileName}`);
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
return fileIds;
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
_multipartRequest(apiPath, body, boundary) {
|
|
491
|
-
return new Promise((resolve, reject) => {
|
|
492
|
-
const url = new URL(apiPath, this.apiUrl);
|
|
493
|
-
const isHttps = url.protocol === 'https:';
|
|
494
|
-
const lib = isHttps ? https : http;
|
|
495
|
-
|
|
496
|
-
const options = {
|
|
497
|
-
hostname: url.hostname,
|
|
498
|
-
port: url.port || (isHttps ? 443 : 80),
|
|
499
|
-
path: url.pathname,
|
|
500
|
-
method: 'POST',
|
|
501
|
-
headers: {
|
|
502
|
-
'Authorization': `Bearer ${this.apiToken}`,
|
|
503
|
-
'Content-Type': `multipart/form-data; boundary=${boundary}`,
|
|
504
|
-
'Content-Length': body.length
|
|
505
|
-
}
|
|
506
|
-
};
|
|
507
|
-
|
|
508
|
-
const req = lib.request(options, (res) => {
|
|
509
|
-
let data = '';
|
|
510
|
-
res.on('data', chunk => data += chunk);
|
|
511
|
-
res.on('end', () => {
|
|
512
|
-
try {
|
|
513
|
-
resolve(JSON.parse(data));
|
|
514
|
-
} catch {
|
|
515
|
-
resolve(data);
|
|
516
|
-
}
|
|
517
|
-
});
|
|
518
|
-
});
|
|
519
|
-
|
|
520
|
-
req.on('error', reject);
|
|
521
|
-
req.write(body);
|
|
522
|
-
req.end();
|
|
523
|
-
});
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
// ── Cron jobs ─────────────────────────────────────────────────────
|
|
527
|
-
|
|
528
|
-
/**
|
|
529
|
-
* Create a cron job
|
|
530
|
-
* @param {object} options - Cron job options
|
|
531
|
-
* @param {string} options.name - Cron job name
|
|
532
|
-
* @param {string} options.cronExpression - Cron expression (e.g., "0 * * * *")
|
|
533
|
-
* @param {string} options.script - Script to execute
|
|
534
|
-
* @param {string} options.workspaceId - Workspace ID
|
|
535
|
-
* @param {object} options.environment - Environment variables
|
|
536
|
-
* @param {boolean} options.persistent - Keep VM alive
|
|
537
|
-
* @returns {Promise<object>}
|
|
538
|
-
*/
|
|
539
|
-
async cronCreate(options) {
|
|
540
|
-
const payload = {
|
|
541
|
-
name: options.name,
|
|
542
|
-
cron_expression: options.cronExpression,
|
|
543
|
-
script: options.script,
|
|
544
|
-
persistent: options.persistent || false
|
|
545
|
-
};
|
|
546
|
-
if (options.workspaceId) payload.workspace_id = options.workspaceId;
|
|
547
|
-
if (options.environment) payload.environment = options.environment;
|
|
548
|
-
return this._request('POST', '/api/v1/mags-cron', payload);
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
/**
|
|
552
|
-
* List cron jobs
|
|
553
|
-
* @returns {Promise<{cron_jobs: Array}>}
|
|
554
|
-
*/
|
|
555
|
-
async cronList() {
|
|
556
|
-
return this._request('GET', '/api/v1/mags-cron');
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
/**
|
|
560
|
-
* Get a cron job
|
|
561
|
-
* @param {string} id - Cron job ID
|
|
562
|
-
* @returns {Promise<object>}
|
|
563
|
-
*/
|
|
564
|
-
async cronGet(id) {
|
|
565
|
-
return this._request('GET', `/api/v1/mags-cron/${id}`);
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
/**
|
|
569
|
-
* Update a cron job
|
|
570
|
-
* @param {string} id - Cron job ID
|
|
571
|
-
* @param {object} updates - Fields to update
|
|
572
|
-
* @returns {Promise<object>}
|
|
573
|
-
*/
|
|
574
|
-
async cronUpdate(id, updates) {
|
|
575
|
-
return this._request('PATCH', `/api/v1/mags-cron/${id}`, updates);
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
/**
|
|
579
|
-
* Delete a cron job
|
|
580
|
-
* @param {string} id - Cron job ID
|
|
581
|
-
* @returns {Promise<object>}
|
|
582
|
-
*/
|
|
583
|
-
async cronDelete(id) {
|
|
584
|
-
return this._request('DELETE', `/api/v1/mags-cron/${id}`);
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
// ── URL aliases ───────────────────────────────────────────────────
|
|
588
|
-
|
|
589
|
-
/**
|
|
590
|
-
* Create a stable URL alias for a workspace
|
|
591
|
-
* @param {string} subdomain - Subdomain for the alias
|
|
592
|
-
* @param {string} workspaceId - Workspace to point to
|
|
593
|
-
* @param {string} domain - Domain (default: apps.magpiecloud.com)
|
|
594
|
-
* @returns {Promise<{id: string, subdomain: string, url: string}>}
|
|
595
|
-
*/
|
|
596
|
-
async urlAliasCreate(subdomain, workspaceId, domain = 'apps.magpiecloud.com') {
|
|
597
|
-
return this._request('POST', '/api/v1/mags-url-aliases', {
|
|
598
|
-
subdomain,
|
|
599
|
-
workspace_id: workspaceId,
|
|
600
|
-
domain,
|
|
601
|
-
});
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
/**
|
|
605
|
-
* List all URL aliases
|
|
606
|
-
* @returns {Promise<{aliases: Array, total: number}>}
|
|
607
|
-
*/
|
|
608
|
-
async urlAliasList() {
|
|
609
|
-
return this._request('GET', '/api/v1/mags-url-aliases');
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
/**
|
|
613
|
-
* Delete a URL alias by subdomain
|
|
614
|
-
* @param {string} subdomain - Subdomain to delete
|
|
615
|
-
* @returns {Promise<object>}
|
|
616
|
-
*/
|
|
617
|
-
async urlAliasDelete(subdomain) {
|
|
618
|
-
return this._request('DELETE', `/api/v1/mags-url-aliases/${subdomain}`);
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
// ── Internal helpers ──────────────────────────────────────────────
|
|
622
|
-
|
|
623
|
-
/**
|
|
624
|
-
* Resolve a job name, workspace ID, or UUID to a request_id
|
|
625
|
-
* @param {string} nameOrId - Name, workspace ID, or request ID
|
|
626
|
-
* @returns {Promise<string>} The resolved request_id
|
|
627
|
-
*/
|
|
628
|
-
async _resolveJobId(nameOrId) {
|
|
629
|
-
// If it looks like a UUID, use directly
|
|
630
|
-
if (nameOrId.length >= 32 && nameOrId.includes('-')) {
|
|
631
|
-
return nameOrId;
|
|
632
|
-
}
|
|
633
|
-
const job = await this.findJob(nameOrId);
|
|
634
|
-
if (!job) throw new MagsError(`No job found for '${nameOrId}'`);
|
|
635
|
-
return job.request_id || job.id;
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
module.exports = Mags;
|
|
640
|
-
module.exports.Mags = Mags;
|
|
641
|
-
module.exports.MagsError = MagsError;
|
|
642
|
-
module.exports.default = Mags;
|
package/nodejs/package.json
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@magpiecloud/mags",
|
|
3
|
-
"version": "1.5.1",
|
|
4
|
-
"description": "Mags CLI - Execute scripts on Magpie's instant VM infrastructure",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"bin": {
|
|
7
|
-
"mags": "./bin/mags.js"
|
|
8
|
-
},
|
|
9
|
-
"scripts": {
|
|
10
|
-
"test": "node bin/mags.js --help"
|
|
11
|
-
},
|
|
12
|
-
"keywords": [
|
|
13
|
-
"magpie",
|
|
14
|
-
"mags",
|
|
15
|
-
"vm",
|
|
16
|
-
"microvm",
|
|
17
|
-
"cloud",
|
|
18
|
-
"serverless",
|
|
19
|
-
"execution",
|
|
20
|
-
"cli",
|
|
21
|
-
"claude",
|
|
22
|
-
"claude-code"
|
|
23
|
-
],
|
|
24
|
-
"author": "Magpie Cloud",
|
|
25
|
-
"license": "MIT",
|
|
26
|
-
"repository": {
|
|
27
|
-
"type": "git",
|
|
28
|
-
"url": "https://github.com/magpiecloud/mags"
|
|
29
|
-
},
|
|
30
|
-
"homepage": "https://mags.run",
|
|
31
|
-
"bugs": {
|
|
32
|
-
"url": "https://github.com/magpiecloud/mags/issues"
|
|
33
|
-
},
|
|
34
|
-
"engines": {
|
|
35
|
-
"node": ">=14.0.0"
|
|
36
|
-
},
|
|
37
|
-
"files": [
|
|
38
|
-
"index.js",
|
|
39
|
-
"bin/mags.js",
|
|
40
|
-
"README.md"
|
|
41
|
-
]
|
|
42
|
-
}
|