Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Connect into running container (#301)

Jakub Novák0e9a22f76a93
diff --git a/.changeset/plenty-bikes-wave.md b/.changeset/plenty-bikes-wave.md
new file mode 100644
index 000000000..708b725c1
--- /dev/null
+++ b/.changeset/plenty-bikes-wave.md
@@ -0,0 +1,5 @@
+---
+"@e2b/cli": patch
+---
+
+Add connect command
diff --git a/packages/cli/src/commands/connect.ts b/packages/cli/src/commands/connect.ts
new file mode 100644
index 000000000..af0329e36
--- /dev/null
+++ b/packages/cli/src/commands/connect.ts
@@ -0,0 +1,57 @@
+import * as e2b from 'e2b'
+import * as commander from 'commander'
+
+import { spawnConnectedTerminal } from 'src/terminal'
+import { asBold, asPrimary } from 'src/utils/format'
+import { ensureAPIKey } from '../api'
+
+export const connectCommand = new commander.Command('connect')
+ .description('Connect terminal to already running sandbox')
+ .argument('[sandboxID]', `Connect to sandbox with ${asBold('[sandboxID]')}`)
+ .alias('c')
+ .action(async (sandboxID: string) => {
+ try {
+ const apiKey = ensureAPIKey()
+
+ if (!sandboxID) {
+ console.error(
+ `You need to specify sandbox ID`,
+ )
+ process.exit(1)
+ }
+
+ await connectToSandbox({ apiKey, sandboxID })
+ // We explicitly call exit because the sandbox is keeping the program alive.
+ // We also don't want to call sandbox.close because that would disconnect other users from the edit session.
+ process.exit(0)
+ } catch (err: any) {
+ console.error(err)
+ process.exit(1)
+ }
+ })
+
+
+async function connectToSandbox({ apiKey, sandboxID} : { apiKey: string, sandboxID: string }) {
+ const sandbox = await e2b.Sandbox.reconnect({sandboxID, apiKey})
+
+ if (sandbox.terminal) {
+ const { exited } = await spawnConnectedTerminal(
+ sandbox.terminal,
+ `Terminal connected to sandbox ${asPrimary(
+ sandboxID,
+ )}\nwith sandbox URL ${asBold(`${sandbox.getProtocol()}://${sandbox.getHostname()}`)}`,
+ `Disconnecting terminal from sandbox ${asPrimary(
+ sandboxID,
+ )}`,
+ )
+
+ await exited
+ console.log(
+ `Closing terminal connection to sandbox ${asPrimary(
+ sandboxID,
+ )}`,
+ )
+ } else {
+ throw new Error('Cannot start terminal - no sandbox')
+ }
+}
diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts
index 52716ff98..8bff97d62 100644
--- a/packages/cli/src/commands/index.ts
+++ b/packages/cli/src/commands/index.ts
@@ -9,6 +9,7 @@ import { shellCommand } from './shell'
import { initCommand } from './init'
import { deleteCommand } from './delete'
import { runningSandboxesCommand } from './sandboxes'
+import { connectCommand } from './connect'
export const program = new commander.Command()
.description(`Create sandbox templates from Dockerfiles by running ${asPrimary('e2b build')} then use our SDKs to create sandboxes from these templates.
@@ -23,3 +24,4 @@ Visit ${asPrimary('E2B docs (https://e2b.dev/docs)')} to learn how to create san
.addCommand(initCommand)
.addCommand(deleteCommand)
.addCommand(runningSandboxesCommand)
+ .addCommand(connectCommand)
\ No newline at end of file