|
| 1 | +/** |
| 2 | + * Create blueprint command |
| 3 | + */ |
| 4 | + |
| 5 | +import { readFile } from "fs/promises"; |
| 6 | +import { getClient } from "../../utils/client.js"; |
| 7 | +import { output, outputError } from "../../utils/output.js"; |
| 8 | + |
| 9 | +interface CreateBlueprintOptions { |
| 10 | + name: string; |
| 11 | + dockerfile?: string; |
| 12 | + dockerfilePath?: string; |
| 13 | + systemSetupCommands?: string[]; |
| 14 | + resources?: string; |
| 15 | + architecture?: string; |
| 16 | + availablePorts?: string[]; |
| 17 | + root?: boolean; |
| 18 | + user?: string; |
| 19 | + output?: string; |
| 20 | +} |
| 21 | + |
| 22 | +export async function createBlueprint(options: CreateBlueprintOptions) { |
| 23 | + try { |
| 24 | + const client = getClient(); |
| 25 | + |
| 26 | + // Read dockerfile from file if path is provided |
| 27 | + let dockerfileContents = options.dockerfile; |
| 28 | + if (options.dockerfilePath) { |
| 29 | + dockerfileContents = await readFile(options.dockerfilePath, "utf-8"); |
| 30 | + } |
| 31 | + |
| 32 | + // Parse user parameters |
| 33 | + let userParameters = undefined; |
| 34 | + if (options.user && options.root) { |
| 35 | + outputError("Only one of --user or --root can be specified"); |
| 36 | + } else if (options.user) { |
| 37 | + const [username, uid] = options.user.split(":"); |
| 38 | + if (!username || !uid) { |
| 39 | + outputError("User must be in format 'username:uid'"); |
| 40 | + } |
| 41 | + userParameters = { username, uid: parseInt(uid) }; |
| 42 | + } else if (options.root) { |
| 43 | + userParameters = { username: "root", uid: 0 }; |
| 44 | + } |
| 45 | + |
| 46 | + // Build launch parameters |
| 47 | + const launchParameters: Record<string, unknown> = {}; |
| 48 | + if (options.resources) { |
| 49 | + launchParameters.resource_size_request = options.resources; |
| 50 | + } |
| 51 | + if (options.architecture) { |
| 52 | + launchParameters.architecture = options.architecture; |
| 53 | + } |
| 54 | + if (options.availablePorts) { |
| 55 | + launchParameters.available_ports = options.availablePorts.map((port) => parseInt(port, 10)); |
| 56 | + } |
| 57 | + if (userParameters) { |
| 58 | + launchParameters.user_parameters = userParameters; |
| 59 | + } |
| 60 | + |
| 61 | + const blueprint = await client.blueprints.create({ |
| 62 | + name: options.name, |
| 63 | + dockerfile: dockerfileContents, |
| 64 | + system_setup_commands: options.systemSetupCommands, |
| 65 | + launch_parameters: launchParameters as Parameters<typeof client.blueprints.create>[0]["launch_parameters"], |
| 66 | + }); |
| 67 | + |
| 68 | + // Default: just output the ID for easy scripting |
| 69 | + if (!options.output || options.output === "text") { |
| 70 | + console.log(blueprint.id); |
| 71 | + } else { |
| 72 | + output(blueprint, { format: options.output, defaultFormat: "json" }); |
| 73 | + } |
| 74 | + } catch (error) { |
| 75 | + outputError("Failed to create blueprint", error); |
| 76 | + } |
| 77 | +} |
0 commit comments