-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9cbae64
Add aio runtime sandbox exec command
995b0f2
Fix constant naming and duplicated logging
a5aeebe
Fix sandbox exec non-interactively
4d1d377
Replace end of options delimiter to quoted string argument
d88b4f4
Add suggested changes
ffefa23
read stdin before flag parse to avoid missing piped input on Windows …
bb65b0a
clarify stdinPromise.catch comment that errors still surface via await
4218008
accept command as quoted arg + --file list; drop -- and stdin
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,197 @@ | ||
| /* | ||
|
riddhi2910 marked this conversation as resolved.
|
||
| 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 fs = require('node:fs') | ||
| const { Sandbox } = require('@adobe/aio-lib-sandbox') | ||
| const { Args, Flags } = require('@oclif/core') | ||
| const RuntimeBaseCommand = require('../../../RuntimeBaseCommand') | ||
| const { | ||
| buildNetworkPolicy, | ||
| parsePortFlags, | ||
| parseEgressFlags, | ||
| buildCommandList, | ||
| logPolicy, | ||
| logPreviewUrls | ||
| } = require('../../../sandbox-helpers') | ||
|
|
||
| const DEFAULT_COMMAND_TIMEOUT_MS = 30000 | ||
|
|
||
| class SandboxExec extends RuntimeBaseCommand { | ||
| async run () { | ||
| const { args, flags } = await this.parse(SandboxExec) | ||
|
|
||
| let listText = '' | ||
| if (flags.file) { | ||
| try { | ||
| listText = this._readFile(flags.file) | ||
| } catch (err) { | ||
| this._failUsage(`Cannot read --file "${flags.file}": ${err.message || err}`) | ||
| return | ||
| } | ||
| } | ||
| const commands = buildCommandList(args.command, listText) | ||
|
|
||
| if (commands.length === 0) { | ||
| this._failUsage('No commands to run. Pass a quoted command (e.g. \'node --version\') and/or a command list with --file. 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}`) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| _readFile (filePath) { | ||
| return fs.readFileSync(filePath, 'utf8') | ||
| } | ||
|
|
||
| _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 as a quoted argument and/or a newline-separated list | ||
| of commands via --file. When both are given, the one-shot command runs first, | ||
| followed by the list 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, | ||
| // non-greedy so a trailing positional command is not swallowed as an egress value | ||
| multipleNonGreedy: true | ||
| }), | ||
| port: Flags.string({ | ||
| char: 'p', | ||
| description: 'Port to expose via a preview URL (repeatable)', | ||
| multiple: true, | ||
| // non-greedy so a trailing positional command is not swallowed as a port value | ||
| multipleNonGreedy: 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 | ||
| }), | ||
| file: Flags.string({ | ||
| char: 'f', | ||
| description: 'path to a file with a newline-separated list of commands to run' | ||
| }) | ||
| } | ||
|
|
||
| SandboxExec.args = { | ||
| command: Args.string({ | ||
| description: 'command to run in the sandbox (quote multi-word commands)', | ||
| required: false | ||
| }) | ||
| } | ||
|
|
||
| SandboxExec.examples = [ | ||
| '<%= config.bin %> <%= command.id %> "node --version"', | ||
| '<%= config.bin %> <%= command.id %> --file commands.txt', | ||
| '<%= config.bin %> <%= command.id %> "node --version" --file commands.txt', | ||
| '<%= config.bin %> <%= command.id %> -e allow-all -p 5173 --file commands.txt', | ||
| '<%= config.bin %> <%= command.id %> --fail-fast --command-timeout 120000 --file commands.txt' | ||
| ] | ||
|
|
||
| SandboxExec.aliases = ['rt:sandbox:exec'] | ||
|
|
||
| // exposed for testing | ||
| SandboxExec.parseEgressFlags = parseEgressFlags | ||
| SandboxExec.parsePortFlags = parsePortFlags | ||
| SandboxExec.buildNetworkPolicy = buildNetworkPolicy | ||
| SandboxExec.buildCommandList = buildCommandList | ||
|
|
||
| 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.