-
Notifications
You must be signed in to change notification settings - Fork 32
Add aio runtime sandbox exec command #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
riddhi2910
wants to merge
3
commits into
master
Choose a base branch
from
acna-4665
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| /* | ||
| Copyright 2026 Adobe Inc. All rights reserved. | ||
| This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. You may obtain a copy | ||
| of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under | ||
| the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| OF ANY KIND, either express or implied. See the License for the specific language | ||
| governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| const { Sandbox } = require('@adobe/aio-lib-sandbox') | ||
| const { Flags } = require('@oclif/core') | ||
| const RuntimeBaseCommand = require('../../../RuntimeBaseCommand') | ||
| const { | ||
| buildNetworkPolicy, | ||
| parsePortFlags, | ||
| parseEgressFlags, | ||
| splitArgvAtDoubleDash, | ||
| buildCommandList, | ||
| shellQuote, | ||
| logPolicy, | ||
| logPreviewUrls | ||
| } = require('../../../sandbox-helpers') | ||
|
|
||
| const DEFAULT_COMMAND_TIMEOUT_MS = 30000 | ||
|
|
||
| class SandboxExec extends RuntimeBaseCommand { | ||
| async init () { | ||
| const rawArgv = [...this.argv] | ||
| const { cliArgs } = splitArgvAtDoubleDash(rawArgv) | ||
|
|
||
| await this.parse(SandboxExec, cliArgs) | ||
| this.argv = rawArgv | ||
| } | ||
|
|
||
| async run () { | ||
| const { cliArgs, commandArgs } = splitArgvAtDoubleDash(this.argv) | ||
| const { flags } = await this.parse(SandboxExec, cliArgs) | ||
|
|
||
| const stdinText = process.stdin.isTTY === true ? '' : await this._readStdin() | ||
| const commands = buildCommandList(commandArgs, stdinText) | ||
|
|
||
| if (commands.length === 0) { | ||
| this._failUsage('No commands to run. Pass a command after "--" and/or pipe a newline-separated list on stdin. For an interactive session use "aio runtime sandbox run".') | ||
| return | ||
| } | ||
|
|
||
| let sandbox | ||
| try { | ||
| const policy = buildNetworkPolicy(flags.egress) | ||
| const ports = parsePortFlags(flags.port) | ||
| const options = await this.getOptions() | ||
|
|
||
| this.log('\nCreating sandbox...') | ||
| sandbox = await Sandbox.create({ | ||
| apiHost: options.apihost, | ||
| namespace: options.namespace, | ||
| auth: options.api_key, | ||
| name: flags.name, | ||
| maxLifetime: flags['max-lifetime'], | ||
| envs: {}, | ||
| ...(ports && { ports }), | ||
| ...(policy && { policy }) | ||
| }) | ||
| this.log(`Created: ${sandbox.id}`) | ||
|
|
||
| logPolicy(policy, msg => this.log(msg)) | ||
| await logPreviewUrls(sandbox, ports, msg => this.log(msg)) | ||
|
|
||
| await this._runCommands(sandbox, commands, flags) | ||
| } catch (err) { | ||
| await this.handleError('failed to exec in sandbox', err) | ||
| } finally { | ||
| if (sandbox) { | ||
| try { | ||
| await sandbox.destroy() | ||
| this.log('Sandbox destroyed.') | ||
| } catch (destroyErr) { | ||
| this.log(`failed to destroy sandbox: ${destroyErr.message || destroyErr}`) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| _readStdin () { | ||
| return new Promise((resolve, reject) => { | ||
| const chunks = [] | ||
| process.stdin.on('data', chunk => chunks.push(chunk)) | ||
| process.stdin.on('end', () => resolve(Buffer.concat(chunks).toString())) | ||
| process.stdin.on('error', reject) | ||
| }) | ||
| } | ||
|
|
||
| _failUsage (message) { | ||
| process.stderr.write(`${message}\n`) | ||
| process.exitCode = 2 | ||
| } | ||
|
|
||
| async _runCommands (sandbox, commands, flags) { | ||
| const timeout = flags['command-timeout'] | ||
| for (const cmd of commands) { | ||
| this.log(`\n$ ${cmd}`) | ||
| const result = await sandbox.exec(cmd, { timeout }) | ||
| if (result.stdout) process.stdout.write(result.stdout) | ||
| if (result.stderr) process.stderr.write(result.stderr) | ||
| this.log(`[exit: ${result.exitCode}]`) | ||
|
|
||
| if (result.exitCode !== 0) { | ||
| process.exitCode = result.exitCode | ||
| if (flags['fail-fast']) { | ||
| this.log('Stopping: command exited non-zero (--fail-fast).') | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| SandboxExec.description = ` | ||
| [Alpha] Sandboxes are in a closed alpha. Your namespace must have | ||
| sandboxes enabled before you can use this command; contact Adobe to request | ||
| access. | ||
|
|
||
| Create a sandbox and run one or more commands non-interactively, then destroy it. | ||
|
|
||
| Provide a one-shot command after "--" and/or pipe a newline-separated list of | ||
| commands on stdin. When both are given, the one-shot command runs first, | ||
| followed by the piped commands in order. | ||
|
|
||
| Each command runs in a fresh process. Shell state (working directory, env | ||
| exports) does not persist between commands. Chain commands to work around | ||
| this: cd mydir && npm install | ||
|
|
||
| By default every command runs and the process exits with the last non-zero | ||
| exit code. Use --fail-fast to stop at the first failure. Each command is | ||
| capped at --command-timeout milliseconds (default 30000). | ||
|
|
||
| For an interactive session, use "aio runtime sandbox run" instead.` | ||
|
|
||
| SandboxExec.flags = { | ||
| ...RuntimeBaseCommand.flags, | ||
| name: Flags.string({ | ||
| char: 'n', | ||
| description: 'sandbox name', | ||
| default: 'aio-sandbox' | ||
| }), | ||
| egress: Flags.string({ | ||
| char: 'e', | ||
| description: 'egress rule in host:port[:protocol][|METHOD:path] format, or "allow-all" (repeatable)', | ||
| multiple: true | ||
| }), | ||
| port: Flags.string({ | ||
| char: 'p', | ||
| description: 'Port to expose via a preview URL (repeatable)', | ||
| multiple: true | ||
| }), | ||
| 'max-lifetime': Flags.integer({ | ||
| description: 'maximum sandbox lifetime in seconds', | ||
| default: 3600 | ||
| }), | ||
| 'command-timeout': Flags.integer({ | ||
| description: 'per-command timeout in milliseconds', | ||
| default: DEFAULT_COMMAND_TIMEOUT_MS | ||
| }), | ||
| 'fail-fast': Flags.boolean({ | ||
| description: 'stop execution when a command returns a non-zero exit code', | ||
| default: false | ||
| }) | ||
| } | ||
|
|
||
| SandboxExec.examples = [ | ||
| '<%= config.bin %> <%= command.id %> -- node --version', | ||
| '<%= config.bin %> <%= command.id %> < commands.txt', | ||
| '<%= config.bin %> <%= command.id %> -- node --version < commands.txt', | ||
| '<%= config.bin %> <%= command.id %> -e allow-all -p 5173 < commands.txt', | ||
| '<%= config.bin %> <%= command.id %> --fail-fast --command-timeout 120000 < commands.txt' | ||
| ] | ||
|
|
||
| SandboxExec.aliases = ['rt:sandbox:exec'] | ||
|
|
||
| // exposed for testing | ||
| SandboxExec.parseEgressFlags = parseEgressFlags | ||
| SandboxExec.parsePortFlags = parsePortFlags | ||
| SandboxExec.buildNetworkPolicy = buildNetworkPolicy | ||
| SandboxExec.splitArgvAtDoubleDash = splitArgvAtDoubleDash | ||
| SandboxExec.buildCommandList = buildCommandList | ||
| SandboxExec.shellQuote = shellQuote | ||
|
|
||
| module.exports = SandboxExec | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.