-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.ts
More file actions
238 lines (214 loc) · 6.98 KB
/
Copy pathcreate.ts
File metadata and controls
238 lines (214 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* Create devbox command
*/
import { getClient } from "../../utils/client.js";
import { output, outputError } from "../../utils/output.js";
interface CreateOptions {
name?: string;
template?: string;
snapshot?: string;
blueprint?: string;
resources?: string;
architecture?: string;
entrypoint?: string;
launchCommands?: string[];
envVars?: string[];
secrets?: string[];
codeMounts?: string[];
idleTime?: string;
idleAction?: string;
availablePorts?: string[];
root?: boolean;
user?: string;
networkPolicy?: string;
gateways?: string[];
output?: string;
}
// Parse environment variables from KEY=value format
function parseEnvVars(envVars: string[]): Record<string, string> {
const result: Record<string, string> = {};
for (const envVar of envVars) {
const eqIndex = envVar.indexOf("=");
if (eqIndex === -1) {
throw new Error(
`Invalid environment variable format: ${envVar}. Expected KEY=value`,
);
}
const key = envVar.substring(0, eqIndex);
const value = envVar.substring(eqIndex + 1);
result[key] = value;
}
return result;
}
// Parse secrets from ENV_VAR=SECRET_NAME format
function parseSecrets(secrets: string[]): Record<string, string> {
const result: Record<string, string> = {};
for (const secret of secrets) {
const eqIndex = secret.indexOf("=");
if (eqIndex === -1) {
throw new Error(
`Invalid secret format: ${secret}. Expected ENV_VAR=SECRET_NAME`,
);
}
const envVarName = secret.substring(0, eqIndex);
const secretName = secret.substring(eqIndex + 1);
result[envVarName] = secretName;
}
return result;
}
// Parse code mounts from JSON format
function parseCodeMounts(codeMounts: string[]): unknown[] {
return codeMounts.map((mount) => {
try {
return JSON.parse(mount);
} catch {
throw new Error(`Invalid code mount JSON: ${mount}`);
}
});
}
// Parse gateways from ENV_PREFIX=gateway,secret format
function parseGateways(
gateways: string[],
): Record<string, { gateway: string; secret: string }> {
const result: Record<string, { gateway: string; secret: string }> = {};
for (const gateway of gateways) {
const eqIndex = gateway.indexOf("=");
if (eqIndex === -1) {
throw new Error(
`Invalid gateway format: ${gateway}. Expected ENV_PREFIX=gateway_id_or_name,secret_id_or_name`,
);
}
const envPrefix = gateway.substring(0, eqIndex);
const valueStr = gateway.substring(eqIndex + 1);
// Split by comma to get gateway and secret
const commaIndex = valueStr.indexOf(",");
if (commaIndex === -1) {
throw new Error(
`Invalid gateway format: ${gateway}. Expected ENV_PREFIX=gateway_id_or_name,secret_id_or_name`,
);
}
const gatewayIdOrName = valueStr.substring(0, commaIndex);
const secretIdOrName = valueStr.substring(commaIndex + 1);
if (!envPrefix || !gatewayIdOrName || !secretIdOrName) {
throw new Error(
`Invalid gateway format: ${gateway}. Expected ENV_PREFIX=gateway_id_or_name,secret_id_or_name`,
);
}
result[envPrefix] = {
gateway: gatewayIdOrName,
secret: secretIdOrName,
};
}
return result;
}
export async function createDevbox(options: CreateOptions = {}) {
try {
const client = getClient();
// Parse user parameters
let userParameters = undefined;
if (options.user && options.root) {
outputError("Only one of --user or --root can be specified");
} else if (options.user) {
const [username, uid] = options.user.split(":");
if (!username || !uid) {
outputError("User must be in format 'username:uid'");
}
userParameters = { username, uid: parseInt(uid) };
} else if (options.root) {
userParameters = { username: "root", uid: 0 };
}
// Validate idle options
if (
(options.idleTime && !options.idleAction) ||
(!options.idleTime && options.idleAction)
) {
outputError(
"Both --idle-time and --idle-action must be specified together",
);
}
// Build launch parameters
const launchParameters: Record<string, unknown> = {};
if (options.resources) {
launchParameters.resource_size_request = options.resources;
}
if (options.architecture) {
launchParameters.architecture = options.architecture;
}
if (options.launchCommands) {
launchParameters.launch_commands = options.launchCommands;
}
if (options.availablePorts) {
launchParameters.available_ports = options.availablePorts.map((p) =>
parseInt(p, 10),
);
}
if (userParameters) {
launchParameters.user_parameters = userParameters;
}
if (options.idleTime && options.idleAction) {
launchParameters.after_idle = {
idle_time_seconds: parseInt(options.idleTime, 10),
on_idle: options.idleAction,
};
}
if (options.networkPolicy) {
launchParameters.network_policy_id = options.networkPolicy;
}
// Build create request
const createRequest: Record<string, unknown> = {
name: options.name || `devbox-${Date.now()}`,
};
// Handle snapshot (--template and --snapshot are aliases)
const snapshotId = options.snapshot || options.template;
if (snapshotId) {
createRequest.snapshot_id = snapshotId;
}
// Handle blueprint - can be either ID or name
if (options.blueprint) {
// If it looks like an ID (starts with bp_ or similar pattern), use blueprint_id
// Otherwise, use blueprint_name
if (
options.blueprint.startsWith("bp_") ||
options.blueprint.startsWith("bpt_")
) {
createRequest.blueprint_id = options.blueprint;
} else {
createRequest.blueprint_name = options.blueprint;
}
}
// Handle entrypoint
if (options.entrypoint) {
createRequest.entrypoint = options.entrypoint;
}
// Handle environment variables
if (options.envVars && options.envVars.length > 0) {
createRequest.environment_variables = parseEnvVars(options.envVars);
}
// Handle code mounts
if (options.codeMounts && options.codeMounts.length > 0) {
createRequest.code_mounts = parseCodeMounts(options.codeMounts);
}
// Handle secrets
if (options.secrets && options.secrets.length > 0) {
createRequest.secrets = parseSecrets(options.secrets);
}
// Handle gateways
if (options.gateways && options.gateways.length > 0) {
createRequest.gateways = parseGateways(options.gateways);
}
if (Object.keys(launchParameters).length > 0) {
createRequest.launch_parameters = launchParameters;
}
const devbox = await client.devboxes.create(
createRequest as Parameters<typeof client.devboxes.create>[0],
);
// Default: just output the ID for easy scripting
if (!options.output || options.output === "text") {
console.log(devbox.id);
} else {
output(devbox, { format: options.output, defaultFormat: "json" });
}
} catch (error) {
outputError("Failed to create devbox", error);
}
}