Skip to content

Commit

Permalink
add shell-command and tty support to the LXD transport
Browse files Browse the repository at this point in the history
!feature

* **add shell-command and tty support to the LXD transport** ([#3262](#3262))

Previously the LXD transport would always execute commands on the
target with `sh -c`. The shell-command and tty options are added
to provide a more consistent experience between the docker, podman
and lxd transports.
  • Loading branch information
h0tw1r3 committed Jan 29, 2024
1 parent 43abac8 commit 8bbd7c1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/bolt/config/transport/lxd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class LXD < Base
cleanup
interpreters
remote
shell-command
tmpdir
tty
].concat(RUN_AS_OPTIONS).sort.freeze

DEFAULTS = {
Expand Down
2 changes: 1 addition & 1 deletion lib/bolt/config/transport/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ module Options
},
"shell-command" => {
type: String,
description: "A shell command to wrap any Docker exec commands in, such as `bash -lc`.",
description: "A shell command to wrap any exec commands in, such as `bash -lc`.",
_plugin: true,
_example: "bash -lc"
},
Expand Down
18 changes: 15 additions & 3 deletions lib/bolt/transport/lxd/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,24 @@ def add_env_vars(env_vars)
end

def execute(command)
lxc_command = %w[lxc exec]
lxc_command = %W[lxc exec #{container_id}]
lxc_command += ['--mode', target.options['tty'].to_s.empty? ? 'non-interactive' : 'interactive']
lxc_command += @env_vars if @env_vars
lxc_command += %W[#{container_id} -- sh -c #{Shellwords.shellescape(command)}]
lxc_command << '--'

if target.options['shell-command'].to_s.empty?
lxc_command += Shellwords.split(command)
else
lxc_command += Shellwords.split(target.options['shell-command'])
lxc_command << command
end

@logger.trace { "Executing: #{lxc_command.join(' ')}" }
Open3.popen3(lxc_command.join(' '))

Open3.popen3(*lxc_command)
rescue StandardError
@logger.trace { "Command aborted" }
raise
end

private def execute_local_command(command)
Expand Down
22 changes: 21 additions & 1 deletion schemas/bolt-defaults.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,16 @@
}
]
},
"shell-command": {
"oneOf": [
{
"$ref": "#/transport_definitions/shell-command"
},
{
"$ref": "#/definitions/_plugin"
}
]
},
"sudo-executable": {
"oneOf": [
{
Expand Down Expand Up @@ -914,6 +924,16 @@
"$ref": "#/definitions/_plugin"
}
]
},
"tty": {
"oneOf": [
{
"$ref": "#/transport_definitions/tty"
},
{
"$ref": "#/definitions/_plugin"
}
]
}
}
},
Expand Down Expand Up @@ -2118,7 +2138,7 @@
]
},
"shell-command": {
"description": "A shell command to wrap any Docker exec commands in, such as `bash -lc`.",
"description": "A shell command to wrap any exec commands in, such as `bash -lc`.",
"oneOf": [
{
"type": "string"
Expand Down
28 changes: 25 additions & 3 deletions schemas/bolt-inventory.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
]
},
"shell-command": {
"description": "A shell command to wrap any Docker exec commands in, such as `bash -lc`.",
"description": "A shell command to wrap any exec commands in, such as `bash -lc`.",
"oneOf": [
{
"type": "string"
Expand Down Expand Up @@ -292,7 +292,7 @@
]
},
"shell-command": {
"description": "A shell command to wrap any Docker exec commands in, such as `bash -lc`.",
"description": "A shell command to wrap any exec commands in, such as `bash -lc`.",
"oneOf": [
{
"type": "string"
Expand Down Expand Up @@ -561,6 +561,17 @@
}
]
},
"shell-command": {
"description": "A shell command to wrap any exec commands in, such as `bash -lc`.",
"oneOf": [
{
"type": "string"
},
{
"$ref": "#/definitions/_plugin"
}
]
},
"sudo-executable": {
"description": "The executable to use when escalating to the configured `run-as` user. This is useful when you want to escalate using the configured `sudo-password`, since `run-as-command` does not use `sudo-password` or support prompting. The command executed on the target is `<sudo-executable> -S -u <user> -p custom_bolt_prompt <command>`. **This option is experimental.**",
"oneOf": [
Expand Down Expand Up @@ -593,6 +604,17 @@
"$ref": "#/definitions/_plugin"
}
]
},
"tty": {
"description": "Whether to enable tty on exec commands.",
"oneOf": [
{
"type": "boolean"
},
{
"$ref": "#/definitions/_plugin"
}
]
}
}
},
Expand Down Expand Up @@ -787,7 +809,7 @@
]
},
"shell-command": {
"description": "A shell command to wrap any Docker exec commands in, such as `bash -lc`.",
"description": "A shell command to wrap any exec commands in, such as `bash -lc`.",
"oneOf": [
{
"type": "string"
Expand Down

0 comments on commit 8bbd7c1

Please sign in to comment.