Skip to content

Commit a069e60

Browse files
authored
Merge pull request #3262 from h0tw1r3/lxd-transport-options
add shell-command and tty support to the LXD transport
2 parents 84520a0 + 1084bfd commit a069e60

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

lib/bolt/config/transport/lxd.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class LXD < Base
1111
cleanup
1212
interpreters
1313
remote
14+
shell-command
1415
tmpdir
16+
tty
1517
].concat(RUN_AS_OPTIONS).sort.freeze
1618

1719
DEFAULTS = {

lib/bolt/config/transport/options.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ module Options
332332
},
333333
"shell-command" => {
334334
type: String,
335-
description: "A shell command to wrap any Docker exec commands in, such as `bash -lc`.",
335+
description: "A shell command to wrap any exec commands in, such as `bash -lc`.",
336336
_plugin: true,
337337
_example: "bash -lc"
338338
},

lib/bolt/transport/lxd/connection.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,24 @@ def add_env_vars(env_vars)
5757
end
5858

5959
def execute(command)
60-
lxc_command = %w[lxc exec]
60+
lxc_command = %W[lxc exec #{container_id}]
61+
lxc_command += ['--mode', target.options['tty'].to_s.empty? ? 'non-interactive' : 'interactive']
6162
lxc_command += @env_vars if @env_vars
62-
lxc_command += %W[#{container_id} -- sh -c #{Shellwords.shellescape(command)}]
63+
lxc_command << '--'
64+
65+
if target.options['shell-command'].to_s.empty?
66+
lxc_command += Shellwords.split(command)
67+
else
68+
lxc_command += Shellwords.split(target.options['shell-command'])
69+
lxc_command << command
70+
end
6371

6472
@logger.trace { "Executing: #{lxc_command.join(' ')}" }
65-
Open3.popen3(lxc_command.join(' '))
73+
74+
Open3.popen3(*lxc_command)
75+
rescue StandardError
76+
@logger.trace { "Command aborted" }
77+
raise
6678
end
6779

6880
private def execute_local_command(command)

schemas/bolt-defaults.schema.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,16 @@
885885
}
886886
]
887887
},
888+
"shell-command": {
889+
"oneOf": [
890+
{
891+
"$ref": "#/transport_definitions/shell-command"
892+
},
893+
{
894+
"$ref": "#/definitions/_plugin"
895+
}
896+
]
897+
},
888898
"sudo-executable": {
889899
"oneOf": [
890900
{
@@ -914,6 +924,16 @@
914924
"$ref": "#/definitions/_plugin"
915925
}
916926
]
927+
},
928+
"tty": {
929+
"oneOf": [
930+
{
931+
"$ref": "#/transport_definitions/tty"
932+
},
933+
{
934+
"$ref": "#/definitions/_plugin"
935+
}
936+
]
917937
}
918938
}
919939
},
@@ -2118,7 +2138,7 @@
21182138
]
21192139
},
21202140
"shell-command": {
2121-
"description": "A shell command to wrap any Docker exec commands in, such as `bash -lc`.",
2141+
"description": "A shell command to wrap any exec commands in, such as `bash -lc`.",
21222142
"oneOf": [
21232143
{
21242144
"type": "string"

schemas/bolt-inventory.schema.json

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
]
151151
},
152152
"shell-command": {
153-
"description": "A shell command to wrap any Docker exec commands in, such as `bash -lc`.",
153+
"description": "A shell command to wrap any exec commands in, such as `bash -lc`.",
154154
"oneOf": [
155155
{
156156
"type": "string"
@@ -292,7 +292,7 @@
292292
]
293293
},
294294
"shell-command": {
295-
"description": "A shell command to wrap any Docker exec commands in, such as `bash -lc`.",
295+
"description": "A shell command to wrap any exec commands in, such as `bash -lc`.",
296296
"oneOf": [
297297
{
298298
"type": "string"
@@ -561,6 +561,17 @@
561561
}
562562
]
563563
},
564+
"shell-command": {
565+
"description": "A shell command to wrap any exec commands in, such as `bash -lc`.",
566+
"oneOf": [
567+
{
568+
"type": "string"
569+
},
570+
{
571+
"$ref": "#/definitions/_plugin"
572+
}
573+
]
574+
},
564575
"sudo-executable": {
565576
"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.**",
566577
"oneOf": [
@@ -593,6 +604,17 @@
593604
"$ref": "#/definitions/_plugin"
594605
}
595606
]
607+
},
608+
"tty": {
609+
"description": "Whether to enable tty on exec commands.",
610+
"oneOf": [
611+
{
612+
"type": "boolean"
613+
},
614+
{
615+
"$ref": "#/definitions/_plugin"
616+
}
617+
]
596618
}
597619
}
598620
},
@@ -787,7 +809,7 @@
787809
]
788810
},
789811
"shell-command": {
790-
"description": "A shell command to wrap any Docker exec commands in, such as `bash -lc`.",
812+
"description": "A shell command to wrap any exec commands in, such as `bash -lc`.",
791813
"oneOf": [
792814
{
793815
"type": "string"

0 commit comments

Comments
 (0)