@magpiecloud/mags 1.5.0 → 1.5.2
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 +10 -4
- package/bin/mags.js +1 -1
- package/index.js +324 -54
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,10 +18,11 @@ mags login
|
|
|
18
18
|
|
|
19
19
|
This will open your browser to create an API token. Paste the token when prompted, and it will be saved for future use.
|
|
20
20
|
|
|
21
|
-
### 2. Create a
|
|
21
|
+
### 2. Create a sandbox
|
|
22
22
|
|
|
23
23
|
```bash
|
|
24
|
-
mags new myproject
|
|
24
|
+
mags new myproject # Local disk only
|
|
25
|
+
mags new myproject -p # With S3 persistence
|
|
25
26
|
mags ssh myproject
|
|
26
27
|
```
|
|
27
28
|
|
|
@@ -59,9 +60,12 @@ export MAGS_API_TOKEN="your-token-here"
|
|
|
59
60
|
## CLI Commands
|
|
60
61
|
|
|
61
62
|
```bash
|
|
62
|
-
# Create a
|
|
63
|
+
# Create a sandbox (local disk)
|
|
63
64
|
mags new myproject
|
|
64
65
|
|
|
66
|
+
# Create with S3 persistence
|
|
67
|
+
mags new myproject -p
|
|
68
|
+
|
|
65
69
|
# SSH into it
|
|
66
70
|
mags ssh myproject
|
|
67
71
|
|
|
@@ -135,11 +139,13 @@ Features:
|
|
|
135
139
|
|
|
136
140
|
## Workspaces & Persistence
|
|
137
141
|
|
|
138
|
-
|
|
142
|
+
When using persistent mode (`-p`), your `/root` directory syncs to S3:
|
|
139
143
|
- **Auto-sync**: Every 30 seconds while running
|
|
140
144
|
- **On stop**: Full sync before VM terminates
|
|
141
145
|
- **On wake**: Previous state restored
|
|
142
146
|
|
|
147
|
+
Without `-p`, data lives on local disk only and is cleaned up when the VM is destroyed.
|
|
148
|
+
|
|
143
149
|
## Node.js SDK
|
|
144
150
|
|
|
145
151
|
```javascript
|
package/bin/mags.js
CHANGED
package/index.js
CHANGED
|
@@ -7,25 +7,40 @@ const https = require('https');
|
|
|
7
7
|
const http = require('http');
|
|
8
8
|
const { URL } = require('url');
|
|
9
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
|
+
|
|
10
18
|
class Mags {
|
|
11
19
|
/**
|
|
12
20
|
* Create a Mags client
|
|
13
21
|
* @param {object} options - Configuration options
|
|
14
22
|
* @param {string} options.apiUrl - API endpoint (default: https://api.magpiecloud.com)
|
|
15
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)
|
|
16
25
|
*/
|
|
17
26
|
constructor(options = {}) {
|
|
18
|
-
this.apiUrl = options.apiUrl || process.env.MAGS_API_URL || 'https://api.magpiecloud.com';
|
|
19
|
-
this.apiToken = options.apiToken || process.env.MAGS_API_TOKEN;
|
|
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;
|
|
20
30
|
|
|
21
31
|
if (!this.apiToken) {
|
|
22
|
-
throw new
|
|
32
|
+
throw new MagsError('API token required. Set MAGS_API_TOKEN or pass apiToken option.');
|
|
23
33
|
}
|
|
24
34
|
}
|
|
25
35
|
|
|
26
|
-
_request(method, path, body = null) {
|
|
36
|
+
_request(method, path, body = null, params = null) {
|
|
27
37
|
return new Promise((resolve, reject) => {
|
|
28
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
|
+
}
|
|
29
44
|
const isHttps = url.protocol === 'https:';
|
|
30
45
|
const lib = isHttps ? https : http;
|
|
31
46
|
|
|
@@ -37,7 +52,8 @@ class Mags {
|
|
|
37
52
|
headers: {
|
|
38
53
|
'Authorization': `Bearer ${this.apiToken}`,
|
|
39
54
|
'Content-Type': 'application/json'
|
|
40
|
-
}
|
|
55
|
+
},
|
|
56
|
+
timeout: this.timeout
|
|
41
57
|
};
|
|
42
58
|
|
|
43
59
|
const req = lib.request(options, (res) => {
|
|
@@ -47,66 +63,110 @@ class Mags {
|
|
|
47
63
|
try {
|
|
48
64
|
const parsed = JSON.parse(data);
|
|
49
65
|
if (res.statusCode >= 400) {
|
|
50
|
-
reject(new
|
|
66
|
+
reject(new MagsError(parsed.error || parsed.message || `HTTP ${res.statusCode}`, res.statusCode));
|
|
51
67
|
} else {
|
|
52
68
|
resolve(parsed);
|
|
53
69
|
}
|
|
54
70
|
} catch {
|
|
55
|
-
|
|
71
|
+
if (res.statusCode >= 400) {
|
|
72
|
+
reject(new MagsError(data || `HTTP ${res.statusCode}`, res.statusCode));
|
|
73
|
+
} else {
|
|
74
|
+
resolve(data);
|
|
75
|
+
}
|
|
56
76
|
}
|
|
57
77
|
});
|
|
58
78
|
});
|
|
59
79
|
|
|
60
80
|
req.on('error', reject);
|
|
81
|
+
req.on('timeout', () => {
|
|
82
|
+
req.destroy();
|
|
83
|
+
reject(new MagsError('Request timed out'));
|
|
84
|
+
});
|
|
61
85
|
if (body) req.write(JSON.stringify(body));
|
|
62
86
|
req.end();
|
|
63
87
|
});
|
|
64
88
|
}
|
|
65
89
|
|
|
90
|
+
// ── Jobs ──────────────────────────────────────────────────────────
|
|
91
|
+
|
|
66
92
|
/**
|
|
67
93
|
* Submit a job for execution
|
|
68
94
|
* @param {string} script - Script to execute
|
|
69
95
|
* @param {object} options - Job options
|
|
70
96
|
* @param {string} options.name - Job name
|
|
71
97
|
* @param {string} options.workspaceId - Persistent workspace ID
|
|
98
|
+
* @param {string} options.baseWorkspaceId - Read-only base workspace to mount
|
|
72
99
|
* @param {boolean} options.persistent - Keep VM alive after script
|
|
100
|
+
* @param {boolean} options.noSleep - Never auto-sleep (requires persistent)
|
|
73
101
|
* @param {boolean} options.ephemeral - No workspace/S3 sync (fastest)
|
|
74
102
|
* @param {string} options.startupCommand - Command to run when waking from sleep
|
|
75
103
|
* @param {object} options.environment - Environment variables
|
|
76
104
|
* @param {string[]} options.fileIds - File IDs from uploadFiles()
|
|
77
|
-
* @
|
|
105
|
+
* @param {number} options.diskGb - Custom disk size in GB (default 2)
|
|
106
|
+
* @returns {Promise<{request_id: string, status: string}>}
|
|
78
107
|
*/
|
|
79
108
|
async run(script, options = {}) {
|
|
80
109
|
if (options.ephemeral && options.workspaceId) {
|
|
81
|
-
throw new
|
|
110
|
+
throw new MagsError('Cannot use ephemeral with workspaceId');
|
|
82
111
|
}
|
|
83
112
|
if (options.ephemeral && options.persistent) {
|
|
84
|
-
throw new
|
|
113
|
+
throw new MagsError('Cannot use ephemeral with persistent');
|
|
114
|
+
}
|
|
115
|
+
if (options.noSleep && !options.persistent) {
|
|
116
|
+
throw new MagsError('noSleep requires persistent=true');
|
|
85
117
|
}
|
|
86
118
|
|
|
87
119
|
const payload = {
|
|
88
120
|
script,
|
|
89
121
|
type: 'inline',
|
|
90
|
-
name: options.name,
|
|
91
122
|
persistent: options.persistent || false,
|
|
92
|
-
startup_command: options.startupCommand,
|
|
93
|
-
environment: options.environment
|
|
94
123
|
};
|
|
95
124
|
|
|
96
|
-
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
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
|
+
}
|
|
100
165
|
|
|
101
|
-
|
|
102
|
-
payload.file_ids = options.fileIds;
|
|
166
|
+
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
103
167
|
}
|
|
104
168
|
|
|
105
|
-
|
|
106
|
-
return {
|
|
107
|
-
requestId: response.request_id,
|
|
108
|
-
status: response.status
|
|
109
|
-
};
|
|
169
|
+
throw new MagsError(`Job ${requestId} timed out after ${timeout}ms`);
|
|
110
170
|
}
|
|
111
171
|
|
|
112
172
|
/**
|
|
@@ -137,60 +197,216 @@ class Mags {
|
|
|
137
197
|
async list(options = {}) {
|
|
138
198
|
const page = options.page || 1;
|
|
139
199
|
const pageSize = options.pageSize || 20;
|
|
140
|
-
return this._request('GET', `/api/v1/mags-jobs
|
|
200
|
+
return this._request('GET', `/api/v1/mags-jobs`, null, { page, page_size: pageSize });
|
|
141
201
|
}
|
|
142
202
|
|
|
143
203
|
/**
|
|
144
|
-
*
|
|
204
|
+
* Update a job's settings
|
|
145
205
|
* @param {string} requestId - Job request ID
|
|
146
|
-
* @param {
|
|
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.
|
|
147
209
|
* @returns {Promise<object>}
|
|
148
210
|
*/
|
|
149
|
-
async
|
|
150
|
-
|
|
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);
|
|
151
216
|
}
|
|
152
217
|
|
|
153
218
|
/**
|
|
154
|
-
*
|
|
219
|
+
* Enable URL or SSH access for a job
|
|
155
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
|
|
156
248
|
* @returns {Promise<object>}
|
|
157
249
|
*/
|
|
158
|
-
async stop(
|
|
250
|
+
async stop(nameOrId) {
|
|
251
|
+
const requestId = await this._resolveJobId(nameOrId);
|
|
159
252
|
return this._request('POST', `/api/v1/mags-jobs/${requestId}/stop`);
|
|
160
253
|
}
|
|
161
254
|
|
|
162
255
|
/**
|
|
163
|
-
*
|
|
164
|
-
* @param {string}
|
|
165
|
-
* @
|
|
166
|
-
* @param {number} options.timeout - Timeout in ms (default: 60000)
|
|
167
|
-
* @returns {Promise<{status: string, exitCode: number, logs: Array}>}
|
|
256
|
+
* Sync a running job's workspace to S3 without stopping the VM
|
|
257
|
+
* @param {string} requestId - Job request ID
|
|
258
|
+
* @returns {Promise<object>}
|
|
168
259
|
*/
|
|
169
|
-
async
|
|
170
|
-
|
|
171
|
-
|
|
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;
|
|
172
285
|
|
|
173
286
|
const startTime = Date.now();
|
|
174
287
|
while (Date.now() - startTime < timeout) {
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const logsResp = await this.logs(requestId);
|
|
179
|
-
return {
|
|
180
|
-
requestId,
|
|
181
|
-
status: status.status,
|
|
182
|
-
exitCode: status.exit_code,
|
|
183
|
-
durationMs: status.script_duration_ms,
|
|
184
|
-
logs: logsResp.logs || []
|
|
185
|
-
};
|
|
288
|
+
const st = await this.status(requestId);
|
|
289
|
+
if (st.status === 'running' && st.vm_id) {
|
|
290
|
+
return { request_id: requestId, status: 'running' };
|
|
186
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 HTTP exec endpoint
|
|
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 resp = await this._request('POST', `/api/v1/mags-jobs/${requestId}/exec`, {
|
|
349
|
+
command,
|
|
350
|
+
timeout: Math.ceil(timeout / 1000),
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
return { exitCode: resp.exit_code, output: resp.stdout, stderr: resp.stderr };
|
|
354
|
+
}
|
|
187
355
|
|
|
356
|
+
/**
|
|
357
|
+
* Resize a workspace's disk. Stops the existing VM, then creates a new one.
|
|
358
|
+
* @param {string} workspace - Workspace name
|
|
359
|
+
* @param {number} diskGb - New disk size in GB
|
|
360
|
+
* @param {object} options - Options
|
|
361
|
+
* @param {number} options.timeout - Timeout in ms (default: 30000)
|
|
362
|
+
* @param {number} options.pollInterval - Poll interval in ms (default: 1000)
|
|
363
|
+
* @returns {Promise<{request_id: string, status: string}>}
|
|
364
|
+
*/
|
|
365
|
+
async resize(workspace, diskGb, options = {}) {
|
|
366
|
+
const existing = await this.findJob(workspace);
|
|
367
|
+
if (existing && existing.status === 'running') {
|
|
368
|
+
await this._request('POST', `/api/v1/mags-jobs/${existing.request_id}/sync`);
|
|
369
|
+
await this._request('POST', `/api/v1/mags-jobs/${existing.request_id}/stop`);
|
|
370
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
371
|
+
} else if (existing && existing.status === 'sleeping') {
|
|
372
|
+
await this._request('POST', `/api/v1/mags-jobs/${existing.request_id}/stop`);
|
|
188
373
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
189
374
|
}
|
|
190
375
|
|
|
191
|
-
|
|
376
|
+
return this.new(workspace, { diskGb, timeout: options.timeout, pollInterval: options.pollInterval });
|
|
192
377
|
}
|
|
193
378
|
|
|
379
|
+
/**
|
|
380
|
+
* Get aggregated usage summary
|
|
381
|
+
* @param {object} options - Options
|
|
382
|
+
* @param {number} options.windowDays - Time window in days (default: 30)
|
|
383
|
+
* @returns {Promise<object>}
|
|
384
|
+
*/
|
|
385
|
+
async usage(options = {}) {
|
|
386
|
+
return this._request('GET', '/api/v1/mags-jobs/usage', null, { window_days: options.windowDays || 30 });
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// ── Workspaces ────────────────────────────────────────────────────
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* List all workspaces
|
|
393
|
+
* @returns {Promise<{workspaces: Array, total: number}>}
|
|
394
|
+
*/
|
|
395
|
+
async listWorkspaces() {
|
|
396
|
+
return this._request('GET', '/api/v1/mags-workspaces');
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Delete a workspace and all its stored data
|
|
401
|
+
* @param {string} workspaceId - Workspace ID to delete
|
|
402
|
+
* @returns {Promise<object>}
|
|
403
|
+
*/
|
|
404
|
+
async deleteWorkspace(workspaceId) {
|
|
405
|
+
return this._request('DELETE', `/api/v1/mags-workspaces/${workspaceId}`);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// ── File uploads ──────────────────────────────────────────────────
|
|
409
|
+
|
|
194
410
|
/**
|
|
195
411
|
* Upload files for use in a job
|
|
196
412
|
* @param {string[]} filePaths - Array of local file paths
|
|
@@ -218,7 +434,7 @@ class Mags {
|
|
|
218
434
|
if (response.file_id) {
|
|
219
435
|
fileIds.push(response.file_id);
|
|
220
436
|
} else {
|
|
221
|
-
throw new
|
|
437
|
+
throw new MagsError(`Failed to upload file: ${fileName}`);
|
|
222
438
|
}
|
|
223
439
|
}
|
|
224
440
|
|
|
@@ -261,7 +477,7 @@ class Mags {
|
|
|
261
477
|
});
|
|
262
478
|
}
|
|
263
479
|
|
|
264
|
-
// Cron
|
|
480
|
+
// ── Cron jobs ─────────────────────────────────────────────────────
|
|
265
481
|
|
|
266
482
|
/**
|
|
267
483
|
* Create a cron job
|
|
@@ -270,6 +486,7 @@ class Mags {
|
|
|
270
486
|
* @param {string} options.cronExpression - Cron expression (e.g., "0 * * * *")
|
|
271
487
|
* @param {string} options.script - Script to execute
|
|
272
488
|
* @param {string} options.workspaceId - Workspace ID
|
|
489
|
+
* @param {object} options.environment - Environment variables
|
|
273
490
|
* @param {boolean} options.persistent - Keep VM alive
|
|
274
491
|
* @returns {Promise<object>}
|
|
275
492
|
*/
|
|
@@ -278,9 +495,10 @@ class Mags {
|
|
|
278
495
|
name: options.name,
|
|
279
496
|
cron_expression: options.cronExpression,
|
|
280
497
|
script: options.script,
|
|
281
|
-
workspace_id: options.workspaceId,
|
|
282
498
|
persistent: options.persistent || false
|
|
283
499
|
};
|
|
500
|
+
if (options.workspaceId) payload.workspace_id = options.workspaceId;
|
|
501
|
+
if (options.environment) payload.environment = options.environment;
|
|
284
502
|
return this._request('POST', '/api/v1/mags-cron', payload);
|
|
285
503
|
}
|
|
286
504
|
|
|
@@ -319,8 +537,60 @@ class Mags {
|
|
|
319
537
|
async cronDelete(id) {
|
|
320
538
|
return this._request('DELETE', `/api/v1/mags-cron/${id}`);
|
|
321
539
|
}
|
|
540
|
+
|
|
541
|
+
// ── URL aliases ───────────────────────────────────────────────────
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Create a stable URL alias for a workspace
|
|
545
|
+
* @param {string} subdomain - Subdomain for the alias
|
|
546
|
+
* @param {string} workspaceId - Workspace to point to
|
|
547
|
+
* @param {string} domain - Domain (default: apps.magpiecloud.com)
|
|
548
|
+
* @returns {Promise<{id: string, subdomain: string, url: string}>}
|
|
549
|
+
*/
|
|
550
|
+
async urlAliasCreate(subdomain, workspaceId, domain = 'apps.magpiecloud.com') {
|
|
551
|
+
return this._request('POST', '/api/v1/mags-url-aliases', {
|
|
552
|
+
subdomain,
|
|
553
|
+
workspace_id: workspaceId,
|
|
554
|
+
domain,
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* List all URL aliases
|
|
560
|
+
* @returns {Promise<{aliases: Array, total: number}>}
|
|
561
|
+
*/
|
|
562
|
+
async urlAliasList() {
|
|
563
|
+
return this._request('GET', '/api/v1/mags-url-aliases');
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Delete a URL alias by subdomain
|
|
568
|
+
* @param {string} subdomain - Subdomain to delete
|
|
569
|
+
* @returns {Promise<object>}
|
|
570
|
+
*/
|
|
571
|
+
async urlAliasDelete(subdomain) {
|
|
572
|
+
return this._request('DELETE', `/api/v1/mags-url-aliases/${subdomain}`);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// ── Internal helpers ──────────────────────────────────────────────
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Resolve a job name, workspace ID, or UUID to a request_id
|
|
579
|
+
* @param {string} nameOrId - Name, workspace ID, or request ID
|
|
580
|
+
* @returns {Promise<string>} The resolved request_id
|
|
581
|
+
*/
|
|
582
|
+
async _resolveJobId(nameOrId) {
|
|
583
|
+
// If it looks like a UUID, use directly
|
|
584
|
+
if (nameOrId.length >= 32 && nameOrId.includes('-')) {
|
|
585
|
+
return nameOrId;
|
|
586
|
+
}
|
|
587
|
+
const job = await this.findJob(nameOrId);
|
|
588
|
+
if (!job) throw new MagsError(`No job found for '${nameOrId}'`);
|
|
589
|
+
return job.request_id || job.id;
|
|
590
|
+
}
|
|
322
591
|
}
|
|
323
592
|
|
|
324
593
|
module.exports = Mags;
|
|
325
594
|
module.exports.Mags = Mags;
|
|
595
|
+
module.exports.MagsError = MagsError;
|
|
326
596
|
module.exports.default = Mags;
|