Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
feat: add build v2 command that uses template SDK (#926)
Jakub Dobrya0b610432024
diff --git a/.changeset/selfish-bags-refuse.md b/.changeset/selfish-bags-refuse.mdnew file mode 100644index 000000000..994c7388c--- /dev/null+++ b/.changeset/selfish-bags-refuse.md@@ -0,0 +1,5 @@+---+'@e2b/cli': patch+---++add build-v2 command that uses template SDKdiff --git a/packages/cli/src/commands/template/build-v2.ts b/packages/cli/src/commands/template/build-v2.tsnew file mode 100644index 000000000..c43df7b06--- /dev/null+++ b/packages/cli/src/commands/template/build-v2.ts@@ -0,0 +1,211 @@+import * as boxen from 'boxen'+import * as commander from 'commander'+import { Template, TemplateClass } from 'e2b'+import { connectionConfig, ensureAccessToken, ensureAPIKey } from 'src/api'+import {+ defaultDockerfileName,+ fallbackDockerfileName,+} from 'src/docker/constants'+import { pathOption } from 'src/options'+import { getRoot } from 'src/utils/filesystem'+import {+ asFormattedSandboxTemplate,+ asLocal,+ asLocalRelative,+ asPrimary,+ asPython,+ asTypescript,+ withDelimiter,+} from '../../utils/format'+import { getDockerfile } from './build'++export const buildV2Command = new commander.Command('build-v2')+ .description(+ 'build Dockerfile as a Sandbox template. This command reads a Dockerfile and builds it directly.'+ )+ .argument(+ '<template-name>',+ 'template name to create or rebuild. The template name must be lowercase and contain only letters, numbers, dashes and underscores.'+ )+ .addOption(pathOption)+ .option(+ '-d, --dockerfile <file>',+ `specify path to Dockerfile. By default E2B tries to find ${asLocal(+ defaultDockerfileName+ )} or ${asLocal(fallbackDockerfileName)} in root directory.`+ )+ .option(+ '-c, --cmd <start-command>',+ 'specify command that will be executed when the sandbox is started.'+ )+ .option(+ '--ready-cmd <ready-command>',+ 'specify command that will need to exit 0 for the template to be ready.'+ )+ .option(+ '--cpu-count <cpu-count>',+ 'specify the number of CPUs that will be used to run the sandbox. The default value is 2.',+ parseInt+ )+ .option(+ '--memory-mb <memory-mb>',+ 'specify the amount of memory in megabytes that will be used to run the sandbox. Must be an even number. The default value is 512.',+ parseInt+ )+ .option('--no-cache', 'skip cache when building the template.')+ .alias('bd-v2')+ .action(+ async (+ templateName: string,+ opts: {+ path?: string+ dockerfile?: string+ cmd?: string+ readyCmd?: string+ cpuCount?: number+ memoryMb?: number+ noCache?: boolean+ }+ ) => {+ try {+ // Ensure we have access token+ ensureAccessToken()+ process.stdout.write('\n')++ // Validate template name+ if (!/^[a-z0-9-_]+$/.test(templateName)) {+ console.error(+ `Template name ${asLocal(+ templateName+ )} is not valid. Template name can only contain lowercase letters, numbers, dashes and underscores.`+ )+ process.exit(1)+ }++ // Validate memory+ if (opts.memoryMb && opts.memoryMb % 2 !== 0) {+ console.error(+ `The memory in megabytes must be an even number. You provided ${asLocal(+ opts.memoryMb.toFixed(0)+ )}.`+ )+ process.exit(1)+ }++ const root = getRoot(opts.path)++ // Use options directly+ const dockerfile = opts.dockerfile+ const startCmd = opts.cmd+ const readyCmd = opts.readyCmd+ const cpuCount = opts.cpuCount+ const memoryMB = opts.memoryMb++ // Get Dockerfile content+ const { dockerfileContent, dockerfileRelativePath } = getDockerfile(+ root,+ dockerfile+ )++ console.log(+ `Found ${asLocalRelative(+ dockerfileRelativePath+ )} that will be used to build the sandbox template.`+ )++ // Initialize template builder with file context and parse Dockerfile+ const baseTemplate = Template({+ fileContextPath: root,+ }).fromDockerfile(dockerfileContent)++ // Apply start/ready commands if provided+ let finalTemplate: TemplateClass = baseTemplate+ if (startCmd && readyCmd) {+ finalTemplate = baseTemplate.setStartCmd(startCmd, readyCmd)+ } else if (readyCmd) {+ finalTemplate = baseTemplate.setReadyCmd(readyCmd)+ } else if (startCmd) {+ console.error('Both start and ready commands must be provided.')+ process.exit(1)+ }++ console.log('\nBuilding sandbox template...\n')++ // Prepare API credentials for SDK+ const apiKey = ensureAPIKey()+ const domain = connectionConfig.domain++ // Build the template using SDK+ try {+ await Template.build(finalTemplate, {+ alias: templateName,+ cpuCount: cpuCount,+ memoryMB: memoryMB,+ skipCache: opts.noCache,+ apiKey: apiKey,+ domain: domain,+ onBuildLogs: (logEntry) => console.log(logEntry.toString()),+ })+ } catch (error) {+ console.error('\n❌ Template build failed.')+ if (error instanceof Error) {+ console.error('Error:', error.message)+ }+ process.exit(1)+ }++ // Display success message with examples+ const pythonExample = asPython(`from e2b import Sandbox, AsyncSandbox++# Create sync sandbox+sandbox = Sandbox.create("${templateName}")++# Create async sandbox+sandbox = await AsyncSandbox.create("${templateName}")`)++ const typescriptExample = asTypescript(`import { Sandbox } from 'e2b'++// Create sandbox+const sandbox = await Sandbox.create('${templateName}')`)++ const examplesMessage = `You can now use the template to create custom sandboxes.\nLearn more on ${asPrimary(+ 'https://e2b.dev/docs'+ )}`++ const exampleHeader = boxen.default(examplesMessage, {+ padding: {+ bottom: 1,+ top: 1,+ left: 2,+ right: 2,+ },+ margin: {+ top: 1,+ bottom: 1,+ left: 0,+ right: 0,+ },+ fullscreen(width) {+ return [width, 0]+ },+ float: 'left',+ })++ const exampleUsage = `${withDelimiter(+ pythonExample,+ 'Python SDK'+ )}\n${withDelimiter(typescriptExample, 'JS SDK', true)}`++ console.log(+ `\n✅ Building sandbox template ${asFormattedSandboxTemplate({+ templateID: templateName,+ })} finished.\n${exampleHeader}\n${exampleUsage}\n`+ )++ process.exit(0)+ } catch (err: any) {+ console.error(err)+ process.exit(1)+ }+ }+ )diff --git a/packages/cli/src/commands/template/build.ts b/packages/cli/src/commands/template/build.tsindex 2917d0126..41de1765e 100644--- a/packages/cli/src/commands/template/build.ts+++ b/packages/cli/src/commands/template/build.ts@@ -1,13 +1,22 @@+import * as boxen from 'boxen'+import * as child_process from 'child_process'+import commandExists from 'command-exists'import * as commander from 'commander'+import * as e2b from 'e2b'import * as fs from 'fs'import * as path from 'path'-import * as e2b from 'e2b'-import * as stripAnsi from 'strip-ansi'-import * as boxen from 'boxen'-import commandExists from 'command-exists'-import { wait } from 'src/utils/wait'import { client, connectionConfig, ensureAccessToken } from 'src/api'+import { configName, getConfigPath, loadConfig, saveConfig } from 'src/config'+import {+ defaultDockerfileName,+ fallbackDockerfileName,+} from 'src/docker/constants'+import { configOption, pathOption, teamOption } from 'src/options'+import { getUserConfig } from 'src/user'import { getRoot } from 'src/utils/filesystem'+import { wait } from 'src/utils/wait'+import * as stripAnsi from 'strip-ansi'+import { handleE2BRequestError } from '../../utils/errors'import {asBold,asBuildLogs,@@ -18,16 +27,7 @@ import {asPython,asTypescript,withDelimiter,-} from 'src/utils/format'-import { configOption, pathOption, teamOption } from 'src/options'-import {- defaultDockerfileName,- fallbackDockerfileName,-} from 'src/docker/constants'-import { configName, getConfigPath, loadConfig, saveConfig } from 'src/config'-import * as child_process from 'child_process'-import { handleE2BRequestError } from '../../utils/errors'-import { getUserConfig } from 'src/user'+} from '../../utils/format'import { buildWithProxy } from './buildWithProxy'const templateCheckInterval = 500 // 0.5 secdiff --git a/packages/cli/src/commands/template/index.ts b/packages/cli/src/commands/template/index.tsindex f303a82b6..9568a0070 100644--- a/packages/cli/src/commands/template/index.ts+++ b/packages/cli/src/commands/template/index.ts@@ -7,11 +7,13 @@ import { deleteCommand } from './delete'import { publishCommand, unPublishCommand } from './publish'import { migrateCommand } from './migrate'import { initV2Command } from './init-v2'+import { buildV2Command } from './build-v2'export const templateCommand = new commander.Command('template').description('manage sandbox templates').alias('tpl').addCommand(buildCommand)+ .addCommand(buildV2Command).addCommand(listCommand).addCommand(initCommand).addCommand(initV2Command)