Skip to content
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

add shell-command and tty support to the LXD transport #3262

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading