Add --run flag to console command for running a specific command
--run CMD wraps the command in the shell: /bin/sh -c CMD, keeping TTY mode so interactive commands like 'rails c' or 'bash' work correctly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -32,6 +32,7 @@ module PortainerCli
|
|||||||
portainer-cli create container --endpoint ID --image IMAGE [--name NAME] [--port h:c] [--env K=V]
|
portainer-cli create container --endpoint ID --image IMAGE [--name NAME] [--port h:c] [--env K=V]
|
||||||
portainer-cli console <stack> [container] Open interactive shell (resolves endpoint automatically)
|
portainer-cli console <stack> [container] Open interactive shell (resolves endpoint automatically)
|
||||||
--shell SHELL Shell to use (default: /bin/sh)
|
--shell SHELL Shell to use (default: /bin/sh)
|
||||||
|
--run CMD Run command instead of a bare shell
|
||||||
portainer-cli exec <endpoint-id> <container> Open interactive shell (manual endpoint)
|
portainer-cli exec <endpoint-id> <container> Open interactive shell (manual endpoint)
|
||||||
--shell SHELL Shell to use (default: /bin/sh)
|
--shell SHELL Shell to use (default: /bin/sh)
|
||||||
|
|
||||||
|
|||||||
@@ -23,9 +23,10 @@ module PortainerCli
|
|||||||
opts = { shell: '/bin/sh' }
|
opts = { shell: '/bin/sh' }
|
||||||
OptionParser.new do |o|
|
OptionParser.new do |o|
|
||||||
o.on('--shell SHELL', 'Shell to use (default: /bin/sh)') { |v| opts[:shell] = v }
|
o.on('--shell SHELL', 'Shell to use (default: /bin/sh)') { |v| opts[:shell] = v }
|
||||||
|
o.on('--run CMD', 'Run command instead of a bare shell') { |v| opts[:run] = v }
|
||||||
end.parse!(args)
|
end.parse!(args)
|
||||||
|
|
||||||
stack_name = args.shift or error("Usage: portainer-cli console <stack> [container] [--shell /bin/bash]")
|
stack_name = args.shift or error("Usage: portainer-cli console <stack> [container] [--run CMD] [--shell /bin/bash]")
|
||||||
container_name = args.shift || stack_name
|
container_name = args.shift || stack_name
|
||||||
|
|
||||||
stack = resolve_stack(stack_name)
|
stack = resolve_stack(stack_name)
|
||||||
@@ -37,7 +38,7 @@ module PortainerCli
|
|||||||
|
|
||||||
$stderr.puts dim("Connecting to #{container_name} (#{container_id[0, 12]})...")
|
$stderr.puts dim("Connecting to #{container_name} (#{container_id[0, 12]})...")
|
||||||
|
|
||||||
exec_id = create_exec(endpoint_id, container_id, opts[:shell])
|
exec_id = create_exec(endpoint_id, container_id, opts[:shell], opts[:run])
|
||||||
ws_url = exec_websocket_url(exec_id, endpoint_id)
|
ws_url = exec_websocket_url(exec_id, endpoint_id)
|
||||||
|
|
||||||
send_resize(endpoint_id, exec_id) if $stdout.tty?
|
send_resize(endpoint_id, exec_id) if $stdout.tty?
|
||||||
@@ -123,7 +124,8 @@ module PortainerCli
|
|||||||
error("Container '#{name_or_id}' not found. Available:\n#{available}")
|
error("Container '#{name_or_id}' not found. Available:\n#{available}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_exec(endpoint_id, container_id, shell)
|
def create_exec(endpoint_id, container_id, shell, run_cmd = nil)
|
||||||
|
cmd = run_cmd ? [shell, '-c', run_cmd] : [shell]
|
||||||
result = @client.post(
|
result = @client.post(
|
||||||
"/api/endpoints/#{endpoint_id}/docker/containers/#{container_id}/exec",
|
"/api/endpoints/#{endpoint_id}/docker/containers/#{container_id}/exec",
|
||||||
{
|
{
|
||||||
@@ -131,7 +133,7 @@ module PortainerCli
|
|||||||
'AttachStdout' => true,
|
'AttachStdout' => true,
|
||||||
'AttachStderr' => true,
|
'AttachStderr' => true,
|
||||||
'Tty' => true,
|
'Tty' => true,
|
||||||
'Cmd' => [shell]
|
'Cmd' => cmd
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
result['Id']
|
result['Id']
|
||||||
|
|||||||
Reference in New Issue
Block a user