Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Print all logs also on build success (#814)

Jakub Dobry2817a8510180
diff --git a/.changeset/shy-knives-shake.md b/.changeset/shy-knives-shake.md
new file mode 100644
index 000000000..b227638ed
--- /dev/null
+++ b/.changeset/shy-knives-shake.md
@@ -0,0 +1,5 @@
+---
+'@e2b/cli': patch
+---
+
+print all logs on template build success
diff --git a/packages/cli/src/commands/template/build.ts b/packages/cli/src/commands/template/build.ts
index f0a1f587c..4acad3226 100644
--- a/packages/cli/src/commands/template/build.ts
+++ b/packages/cli/src/commands/template/build.ts
@@ -20,10 +20,7 @@ import {
withDelimiter,
} from 'src/utils/format'
import { configOption, pathOption, teamOption } from 'src/options'
-import {
- defaultDockerfileName,
- fallbackDockerfileName,
-} from 'src/docker/constants'
+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'
@@ -33,10 +30,10 @@ import { buildWithProxy } from './buildWithProxy'
const templateCheckInterval = 500 // 0.5 sec
async function getTemplateBuildLogs({
- templateID,
- buildID,
- logsOffset,
-}: {
+ templateID,
+ buildID,
+ logsOffset,
+ }: {
templateID: string
buildID: string
logsOffset: number
@@ -55,7 +52,7 @@ async function getTemplateBuildLogs({
logsOffset,
},
},
- }
+ },
)
handleE2BRequestError(res, 'Error getting template build status')
@@ -63,7 +60,7 @@ async function getTemplateBuildLogs({
}
async function requestTemplateBuild(
- args: e2b.paths['/templates']['post']['requestBody']['content']['application/json']
+ args: e2b.paths['/templates']['post']['requestBody']['content']['application/json'],
) {
return await client.api.POST('/templates', {
body: args,
@@ -72,7 +69,7 @@ async function requestTemplateBuild(
async function requestTemplateRebuild(
templateID: string,
- args: e2b.paths['/templates/{templateID}']['post']['requestBody']['content']['application/json']
+ args: e2b.paths['/templates/{templateID}']['post']['requestBody']['content']['application/json'],
) {
return await client.api.POST('/templates/{templateID}', {
body: args,
@@ -123,57 +120,57 @@ async function triggerTemplateBuild(templateID: string, buildID: string) {
export const buildCommand = new commander.Command('build')
.description(
`build sandbox template defined by ${asLocalRelative(
- defaultDockerfileName
+ defaultDockerfileName,
)} or ${asLocalRelative(
- fallbackDockerfileName
+ fallbackDockerfileName,
)} in root directory. By default the root directory is the current working directory. This command also creates ${asLocal(
- configName
- )} config.`
+ configName,
+ )} config.`,
)
.argument(
'[template]',
`specify ${asBold(
- '[template]'
+ '[template]',
)} to rebuild it. If you dont's specify ${asBold(
- '[template]'
+ '[template]',
)} and there is no ${asLocal(
- 'e2b.toml'
- )} a new sandbox template will be created.`
+ 'e2b.toml',
+ )} a new sandbox template will be created.`,
)
.addOption(pathOption)
.option(
'-d, --dockerfile <file>',
`specify path to Dockerfile. By default E2B tries to find ${asLocal(
- defaultDockerfileName
- )} or ${asLocal(fallbackDockerfileName)} in root directory.`
+ defaultDockerfileName,
+ )} or ${asLocal(fallbackDockerfileName)} in root directory.`,
)
.option(
'-n, --name <template-name>',
- 'specify sandbox template name. You can use the template name to start the sandbox with SDK. The template name must be lowercase and contain only letters, numbers, dashes and underscores.'
+ 'specify sandbox template name. You can use the template name to start the sandbox with SDK. The template name must be lowercase and contain only letters, numbers, dashes and underscores.',
)
.option(
'-c, --cmd <start-command>',
- 'specify command that will be executed when the sandbox is started.'
+ '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.'
+ 'specify command that will need to exit 0 for the template to be ready.',
)
.addOption(teamOption)
.addOption(configOption)
.option(
'--cpu-count <cpu-count>',
'specify the number of CPUs that will be used to run the sandbox. The default value is 2.',
- parseInt
+ 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
+ parseInt,
)
.option(
'--build-arg <args...>',
- 'specify additional build arguments for the build command. The format should be <varname>=<value>.'
+ 'specify additional build arguments for the build command. The format should be <varname>=<value>.',
)
.option('--no-cache', 'skip cache when building the template.')
.alias('bd')
@@ -192,13 +189,13 @@ export const buildCommand = new commander.Command('build')
memoryMb?: number
buildArg?: [string]
noCache?: boolean
- }
+ },
) => {
try {
const dockerInstalled = commandExists.sync('docker')
if (!dockerInstalled) {
console.error(
- 'Docker is required to build and push the sandbox template. Please install Docker and try again.'
+ 'Docker is required to build and push the sandbox template. Please install Docker and try again.',
)
process.exit(1)
}
@@ -218,8 +215,8 @@ export const buildCommand = new commander.Command('build')
if (newName && !/[a-z0-9-_]+/.test(newName)) {
console.error(
`Name ${asLocal(
- newName
- )} is not valid. Name can only contain lowercase letters, numbers, dashes and underscores.`
+ newName,
+ )} is not valid. Name can only contain lowercase letters, numbers, dashes and underscores.`,
)
process.exit(1)
}
@@ -249,8 +246,8 @@ export const buildCommand = new commander.Command('build')
? [config.template_name]
: undefined,
},
- relativeConfigPath
- )}`
+ relativeConfigPath,
+ )}`,
)
templateID = config.template_id
dockerfile = opts.dockerfile || config.dockerfile
@@ -269,7 +266,7 @@ export const buildCommand = new commander.Command('build')
if (config && templateID && config.template_id !== templateID) {
// error: you can't specify different ID than the one in config
console.error(
- "You can't specify different ID than the one in config. If you want to build a new sandbox template remove the config file."
+ 'You can\'t specify different ID than the one in config. If you want to build a new sandbox template remove the config file.',
)
process.exit(1)
}
@@ -281,21 +278,21 @@ export const buildCommand = new commander.Command('build')
) {
console.log(
`The sandbox template name will be changed from ${asLocal(
- config.template_name
- )} to ${asLocal(newName)}.`
+ config.template_name,
+ )} to ${asLocal(newName)}.`,
)
}
const name = newName || config?.template_name
const { dockerfileContent, dockerfileRelativePath } = getDockerfile(
root,
- dockerfile
+ dockerfile,
)
console.log(
`Found ${asLocalRelative(
- dockerfileRelativePath
- )} that will be used to build the sandbox template.`
+ dockerfileRelativePath,
+ )} that will be used to build the sandbox template.`,
)
const body = {
@@ -312,8 +309,8 @@ export const buildCommand = new commander.Command('build')
if (opts.memoryMb % 2 !== 0) {
console.error(
`The memory in megabytes must be an even number. You provided ${asLocal(
- opts.memoryMb.toFixed(0)
- )}.`
+ opts.memoryMb.toFixed(0),
+ )}.`,
)
process.exit(1)
}
@@ -324,8 +321,8 @@ export const buildCommand = new commander.Command('build')
console.log(
`Requested build for the sandbox template ${asFormattedSandboxTemplate(
- template
- )} `
+ template,
+ )} `,
)
await saveConfig(
@@ -340,7 +337,7 @@ export const buildCommand = new commander.Command('build')
memory_mb: memoryMB,
team_id: teamID,
},
- true
+ true,
)
try {
@@ -349,11 +346,11 @@ export const buildCommand = new commander.Command('build')
{
stdio: 'inherit',
cwd: root,
- }
+ },
)
} catch (err: any) {
console.error(
- 'Docker login failed. Please try to log in with `e2b auth login` and try again.'
+ 'Docker login failed. Please try to log in with `e2b auth login` and try again.',
)
process.exit(1)
}
@@ -376,7 +373,7 @@ export const buildCommand = new commander.Command('build')
].join(' ')
console.log(
- `Building docker image with the following command:\n${asBold(cmd)}\n`
+ `Building docker image with the following command:\n${asBold(cmd)}\n`,
)
child_process.execSync(cmd, {
@@ -392,8 +389,8 @@ export const buildCommand = new commander.Command('build')
const pushCmd = `docker push docker.${connectionConfig.domain}/e2b/custom-envs/${templateID}:${template.buildID}`
console.log(
`Pushing docker image with the following command:\n${asBold(
- pushCmd
- )}\n`
+ pushCmd,
+ )}\n`,
)
try {
child_process.execSync(pushCmd, {
@@ -406,7 +403,7 @@ export const buildCommand = new commander.Command('build')
connectionConfig,
accessToken,
template,
- root
+ root,
)
}
console.log('> Docker image pushed.\n')
@@ -416,8 +413,8 @@ export const buildCommand = new commander.Command('build')
console.log(
`> Triggered build for the sandbox template ${asFormattedSandboxTemplate(
- template
- )} with build ID: ${template.buildID}`
+ template,
+ )} with build ID: ${template.buildID}`,
)
console.log('Waiting for build to finish...')
@@ -428,13 +425,13 @@ export const buildCommand = new commander.Command('build')
console.error(err)
process.exit(1)
}
- }
+ },
)
async function waitForBuildFinish(
templateID: string,
buildID: string,
- name?: string
+ name?: string,
) {
let logsOffset = 0
@@ -453,11 +450,12 @@ async function waitForBuildFinish(
logsOffset += template.logs.length
+ template.logs.forEach((line) =>
+ process.stdout.write(asBuildLogs(stripAnsi.default(line))),
+ )
+
switch (template.status) {
case 'building':
- template.logs.forEach((line) =>
- process.stdout.write(asBuildLogs(stripAnsi.default(line)))
- )
break
case 'ready': {
const pythonExample = asPython(`from e2b import Sandbox, AsyncSandbox
@@ -478,7 +476,7 @@ const sandbox = await Sandbox.create('${
}')`)
const examplesMessage = `You can now use the template to create custom sandboxes.\nLearn more on ${asPrimary(
- 'https://e2b.dev/docs'
+ 'https://e2b.dev/docs',
)}`
const exampleHeader = boxen.default(examplesMessage, {
@@ -502,28 +500,25 @@ const sandbox = await Sandbox.create('${
const exampleUsage = `${withDelimiter(
pythonExample,
- 'Python SDK'
+ 'Python SDK',
)}\n${withDelimiter(typescriptExample, 'JS SDK', true)}`
console.log(
`\n✅ Building sandbox template ${asFormattedSandboxTemplate({
aliases,
...template,
- })} finished.\n${exampleHeader}\n${exampleUsage}\n`
+ })} finished.\n${exampleHeader}\n${exampleUsage}\n`,
)
break
}
case 'error':
- template.logs.forEach((line) =>
- process.stdout.write(asBuildLogs(stripAnsi.default(line)))
- )
throw new Error(
`\n❌ Building sandbox template ${asFormattedSandboxTemplate({
aliases,
...template,
})} failed.\nCheck the logs above for more details or contact us ${asPrimary(
- '(https://e2b.dev/docs/support)'
- )} to get help.\n`
+ '(https://e2b.dev/docs/support)',
+ )} to get help.\n`,
)
}
} while (template.status === 'building')
@@ -547,8 +542,8 @@ function getDockerfile(root: string, file?: string) {
if (dockerfileContent === undefined) {
throw new Error(
`No ${asLocalRelative(
- dockerfileRelativePath
- )} found in the root directory.`
+ dockerfileRelativePath,
+ )} found in the root directory.`,
)
}
@@ -589,16 +584,16 @@ function getDockerfile(root: string, file?: string) {
throw new Error(
`No ${asLocalRelative(defaultDockerfileRelativePath)} or ${asLocalRelative(
- fallbackDockerfileRelativeName
+ fallbackDockerfileRelativeName,
)} found in the root directory (${root}). You can specify a custom Dockerfile with ${asBold(
- '--dockerfile <file>'
- )} option.`
+ '--dockerfile <file>',
+ )} option.`,
)
}
async function requestBuildTemplate(
args: e2b.paths['/templates']['post']['requestBody']['content']['application/json'],
- templateID?: string
+ templateID?: string,
): Promise<
Omit<
e2b.paths['/templates']['post']['responses']['202']['content']['application/json'],