Skip to content

armgit / E2B

Public Imported from GitHub
0

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

CLI: Refactored sandbox kill command (#904)

Mish Ushakova116679b872c
diff --git a/.changeset/unlucky-eyes-sit.md b/.changeset/unlucky-eyes-sit.md
new file mode 100644
index 000000000..60caa124b
--- /dev/null
+++ b/.changeset/unlucky-eyes-sit.md
@@ -0,0 +1,5 @@
+---
+'@e2b/cli': minor
+---
+
+added --state --metadata filters to kill as well as the ability to pass multiple sandbox ids
diff --git a/apps/web/src/app/(docs)/docs/cli/shutdown-sandboxes/page.mdx b/apps/web/src/app/(docs)/docs/cli/shutdown-sandboxes/page.mdx
index 2c33d0d2f..d8686500f 100644
--- a/apps/web/src/app/(docs)/docs/cli/shutdown-sandboxes/page.mdx
+++ b/apps/web/src/app/(docs)/docs/cli/shutdown-sandboxes/page.mdx
@@ -2,13 +2,13 @@
You can shutdown single or all running sandboxes with the E2B CLI.
-## Shutdown single sandbox
+## Shutdown single or multiple sandboxes
-To shutdown a single sandbox, run the following command:
+To shutdown a single or multiple sandboxes, run the following command:
<CodeGroup isTerminalCommand>
```bash
-e2b sandbox kill <sandbox-id>
+e2b sandbox kill <sandbox-id1> <sandbox-id2> <sandbox-id3>
```
</CodeGroup>
@@ -21,3 +21,23 @@ To shutdown all running sandboxes, run the following command:
e2b sandbox kill --all
```
</CodeGroup>
+
+Further, you can filter the sandboxes to be shutdown by state, metadata or both.
+
+<CodeGroup isTerminalCommand>
+```bash
+e2b sandbox kill --all --state=running,paused
+```
+</CodeGroup>
+
+<CodeGroup isTerminalCommand>
+```bash
+e2b sandbox kill --all --metadata=key=value
+```
+</CodeGroup>
+
+<CodeGroup isTerminalCommand>
+```bash
+e2b sandbox kill --all --state=running,paused --metadata=key=value
+```
+</CodeGroup>
diff --git a/packages/cli/src/commands/sandbox/kill.ts b/packages/cli/src/commands/sandbox/kill.ts
index 5b475782c..978b2f143 100644
--- a/packages/cli/src/commands/sandbox/kill.ts
+++ b/packages/cli/src/commands/sandbox/kill.ts
@@ -3,7 +3,8 @@ import * as commander from 'commander'
import { ensureAPIKey } from 'src/api'
import { asBold } from 'src/utils/format'
import * as e2b from 'e2b'
-import { Sandbox } from 'e2b'
+import { Sandbox, components } from 'e2b'
+import { parseMetadata } from './utils'
async function killSandbox(sandboxID: string, apiKey: string) {
const killed = await e2b.Sandbox.kill(sandboxID, { apiKey })
@@ -17,63 +18,90 @@ async function killSandbox(sandboxID: string, apiKey: string) {
export const killCommand = new commander.Command('kill')
.description('kill sandbox')
.argument(
- '[sandboxID]',
- `kill the sandbox specified by ${asBold('[sandboxID]')}`
+ '[sandboxIDs...]',
+ `kill the sandboxes specified by ${asBold('[sandboxIDs...]')}`
)
.alias('kl')
- .option('-a, --all', 'kill all running sandboxes')
- .action(async (sandboxID: string, { all }: { all: boolean }) => {
- try {
- const apiKey = ensureAPIKey()
-
- if (!sandboxID && !all) {
- console.error(
- `You need to specify ${asBold('[sandboxID]')} or use ${asBold(
- '-a/--all'
- )} flag`
- )
- process.exit(1)
- }
-
- if (all && sandboxID) {
- console.error(
- `You cannot use ${asBold('-a/--all')} flag while specifying ${asBold(
- '[sandboxID]'
- )}`
- )
- process.exit(1)
+ .option('-a, --all', 'kill all sandboxes')
+ .option(
+ '-s, --state <state>',
+ 'when used with -a/--all flag, filter by state, eg. running, paused. Defaults to running',
+ (value) => value.split(',')
+ )
+ .option(
+ '-m, --metadata <metadata>',
+ 'when used with -a/--all flag, filter by metadata, eg. key1=value1'
+ )
+ .action(
+ async (
+ sandboxIDs: string[],
+ {
+ all,
+ state,
+ metadata,
+ }: {
+ all: boolean
+ state: components['schemas']['SandboxState'][]
+ metadata: string
}
+ ) => {
+ try {
+ const apiKey = ensureAPIKey()
+ const sandboxesState = state || ['running']
+ const sandboxesMetadata = parseMetadata(metadata)
- if (all) {
- let total = 0
- const iterator = Sandbox.list({
- apiKey,
- query: {
- state: ['running'],
- },
- })
-
- while (iterator.hasNext) {
- const sandboxes = await iterator.nextItems()
- total += sandboxes.length
+ if ((!sandboxIDs || sandboxIDs.length === 0) && !all) {
+ console.error(
+ `You need to specify ${asBold('[sandboxIDs...]')} or use ${asBold(
+ '-a/--all'
+ )} flag`
+ )
+ process.exit(1)
+ }
- await Promise.all(
- sandboxes.map((sandbox) => killSandbox(sandbox.sandboxId, apiKey))
+ if (all && sandboxIDs && sandboxIDs.length > 0) {
+ console.error(
+ `You cannot use ${asBold(
+ '-a/--all'
+ )} flag while specifying ${asBold('[sandboxIDs...]')}`
)
+ process.exit(1)
}
- if (total === 0) {
- console.log('No running sandboxes')
+ if (all) {
+ let total = 0
+ const iterator = Sandbox.list({
+ apiKey,
+ query: {
+ state: sandboxesState,
+ metadata: sandboxesMetadata,
+ },
+ })
+
+ while (iterator.hasNext) {
+ const sandboxes = await iterator.nextItems()
+ total += sandboxes.length
+
+ await Promise.all(
+ sandboxes.map((sandbox) => killSandbox(sandbox.sandboxId, apiKey))
+ )
+ }
+
+ if (total === 0) {
+ console.log('No running sandboxes')
+ } else {
+ console.log(`Killed ${total} running sandboxes`)
+ }
+
+ process.exit(0)
} else {
- console.log(`Killed ${total} running sandboxes`)
+ await Promise.all(
+ sandboxIDs.map((sandboxID) => killSandbox(sandboxID, apiKey))
+ )
}
-
- process.exit(0)
- } else {
- await killSandbox(sandboxID, apiKey)
+ } catch (err: any) {
+ console.error(err)
+ process.exit(1)
}
- } catch (err: any) {
- console.error(err)
- process.exit(1)
}
- })
+ )
diff --git a/packages/cli/src/commands/sandbox/list.ts b/packages/cli/src/commands/sandbox/list.ts
index 8371a3ead..205e2f2e9 100644
--- a/packages/cli/src/commands/sandbox/list.ts
+++ b/packages/cli/src/commands/sandbox/list.ts
@@ -3,6 +3,7 @@ import * as commander from 'commander'
import { components, Sandbox, SandboxInfo } from 'e2b'
import { ensureAPIKey } from 'src/api'
+import { parseMetadata } from './utils'
export const listCommand = new commander.Command('list')
.description('list all sandboxes, by default it list only running ones')
@@ -12,11 +13,7 @@ export const listCommand = new commander.Command('list')
'filter by state, eg. running, paused. Defaults to running',
(value) => value.split(',')
)
- .option(
- '-m, --metadata <metadata>',
- 'filter by metadata, eg. key1=value1',
- (value) => value.replace(/,/g, '&')
- )
+ .option('-m, --metadata <metadata>', 'filter by metadata, eg. key1=value1')
.option(
'-l, --limit <limit>',
'limit the number of sandboxes returned',
@@ -115,19 +112,7 @@ export async function listSandboxes({
metadataRaw,
}: ListSandboxesOptions = {}): Promise<SandboxInfo[]> {
const apiKey = ensureAPIKey()
-
- let metadata: Record<string, string> | undefined = undefined
- if (metadataRaw && metadataRaw.length > 0) {
- const parsedMetadata: Record<string, string> = {}
- metadataRaw.split(',').map((pair: string) => {
- const [key, value] = pair.split('=')
- if (key && value) {
- parsedMetadata[key.trim()] = value.trim()
- }
- })
-
- metadata = parsedMetadata
- }
+ const metadata = parseMetadata(metadataRaw)
let pageLimit = limit
if (!limit || limit > 100) {
diff --git a/packages/cli/src/commands/sandbox/utils.ts b/packages/cli/src/commands/sandbox/utils.ts
index 8a26f20a8..8bc65bcee 100644
--- a/packages/cli/src/commands/sandbox/utils.ts
+++ b/packages/cli/src/commands/sandbox/utils.ts
@@ -57,3 +57,20 @@ export async function isRunning(sandboxID: string) {
return false
}
}
+
+export function parseMetadata(metadataRaw?: string) {
+ let metadata: Record<string, string> | undefined = undefined
+ if (metadataRaw && metadataRaw.length > 0) {
+ const parsedMetadata: Record<string, string> = {}
+ metadataRaw.split(',').map((pair: string) => {
+ const [key, value] = pair.split('=')
+ if (key && value) {
+ parsedMetadata[key.trim()] = value.trim()
+ }
+ })
+
+ metadata = parsedMetadata
+ }
+
+ return metadata
+}