Class: Bolt::Transport::WinRM
- Inherits:
-
Base
- Object
- Base
- Bolt::Transport::WinRM
show all
- Defined in:
- lib/bolt/transport/winrm.rb,
lib/bolt/transport/winrm/connection.rb
Defined Under Namespace
Classes: Connection
Constant Summary
collapse
- PS_ARGS =
%w[
-NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass
].freeze
- PROVIDED_FEATURES =
['powershell'].freeze
Constants inherited
from Base
Base::ENVIRONMENT_METHODS, Base::STDIN_METHODS
Instance Attribute Summary
Attributes inherited from Base
#logger
Class Method Summary
collapse
Instance Method Summary
collapse
-
#escape_arguments(arguments) ⇒ Object
-
#initialize ⇒ WinRM
constructor
-
#powershell_file?(path) ⇒ Boolean
-
#process_from_extension(path) ⇒ Object
-
#run_command(target, command, _options = {}) ⇒ Object
-
#run_script(target, script, arguments, _options = {}) ⇒ Object
-
#run_task(target, task, arguments, _options = {}) ⇒ Object
-
#upload(target, source, destination, _options = {}) ⇒ Object
-
#with_connection(target) ⇒ Object
Methods inherited from Base
#assert_batch_size_one, #batch_command, #batch_script, #batch_task, #batch_upload, #batches, #filter_options, #with_events
Constructor Details
#initialize ⇒ WinRM
Returns a new instance of WinRM.
36
37
38
39
40
|
# File 'lib/bolt/transport/winrm.rb', line 36
def initialize
super
require 'winrm'
require 'winrm-fs'
end
|
Class Method Details
.options ⇒ Object
12
13
14
|
# File 'lib/bolt/transport/winrm.rb', line 12
def self.options
%w[port user password connect-timeout ssl ssl-verify tmpdir cacert extensions]
end
|
.validate(options) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/bolt/transport/winrm.rb', line 18
def self.validate(options)
ssl_flag = options['ssl']
unless !!ssl_flag == ssl_flag
raise Bolt::ValidationError, 'ssl option must be a Boolean true or false'
end
ssl_verify_flag = options['ssl-verify']
unless !!ssl_verify_flag == ssl_verify_flag
raise Bolt::ValidationError, 'ssl-verify option must be a Boolean true or false'
end
timeout_value = options['connect-timeout']
unless timeout_value.is_a?(Integer) || timeout_value.nil?
error_msg = "connect-timeout value must be an Integer, received #{timeout_value}:#{timeout_value.class}"
raise Bolt::ValidationError, error_msg
end
end
|
Instance Method Details
#escape_arguments(arguments) ⇒ Object
184
185
186
187
188
189
190
191
192
|
# File 'lib/bolt/transport/winrm.rb', line 184
def escape_arguments(arguments)
arguments.map do |arg|
if arg =~ / /
"\"#{arg}\""
else
arg
end
end
end
|
#powershell_file?(path) ⇒ Boolean
154
155
156
|
# File 'lib/bolt/transport/winrm.rb', line 154
def powershell_file?(path)
Pathname(path).extname.casecmp('.ps1').zero?
end
|
#process_from_extension(path) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/bolt/transport/winrm.rb', line 158
def process_from_extension(path)
case Pathname(path).extname.downcase
when '.rb'
[
'ruby.exe',
['-S', "\"#{path}\""]
]
when '.ps1'
[
'powershell.exe',
[*PS_ARGS, '-File', "\"#{path}\""]
]
when '.pp'
[
'puppet.bat',
['apply', "\"#{path}\""]
]
else
[
'cmd.exe',
['/c', "\"#{path}\""]
]
end
end
|
#run_command(target, command, _options = {}) ⇒ Object
61
62
63
64
65
66
|
# File 'lib/bolt/transport/winrm.rb', line 61
def run_command(target, command, _options = {})
with_connection(target) do |conn|
output = conn.execute(command)
Bolt::Result.for_command(target, output.stdout.string, output.stderr.string, output.exit_code)
end
end
|
#run_script(target, script, arguments, _options = {}) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/bolt/transport/winrm.rb', line 68
def run_script(target, script, arguments, _options = {})
with_connection(target) do |conn|
conn.with_remote_file(script) do |remote_path|
if powershell_file?(remote_path)
mapped_args = arguments.map do |a|
"$invokeArgs.ArgumentList += @'\n#{a}\n'@"
end.join("\n")
output = conn.execute(<<-PS)
$invokeArgs = @{
ScriptBlock = (Get-Command "#{remote_path}").ScriptBlock
ArgumentList = @()
}
#{mapped_args}
try
{
Invoke-Command @invokeArgs
}
catch
{
Write-Error $_.Exception
exit 1
}
PS
else
path, args = *process_from_extension(remote_path)
args += escape_arguments(arguments)
output = conn.execute_process(path, args)
end
Bolt::Result.for_command(target, output.stdout.string, output.stderr.string, output.exit_code)
end
end
end
|
#run_task(target, task, arguments, _options = {}) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/bolt/transport/winrm.rb', line 102
def run_task(target, task, arguments, _options = {})
executable = target.select_impl(task, PROVIDED_FEATURES)
raise "No suitable implementation of #{task.name} for #{target.name}" unless executable
input_method = task.input_method
input_method ||= powershell_file?(executable) ? 'powershell' : 'both'
with_connection(target) do |conn|
if STDIN_METHODS.include?(input_method)
stdin = JSON.dump(arguments)
end
if ENVIRONMENT_METHODS.include?(input_method)
arguments.each do |(arg, val)|
val = val.to_json unless val.is_a?(String)
cmd = "[Environment]::SetEnvironmentVariable('PT_#{arg}', @'\n#{val}\n'@)"
result = conn.execute(cmd)
if result.exit_code != 0
raise EnvironmentVarError(var, value)
end
end
end
conn.with_remote_file(executable) do |remote_path|
output =
if powershell_file?(remote_path) && stdin.nil?
if input_method == 'powershell'
conn.execute(<<-PS)
$private:tempArgs = Get-ContentAsJson (
$utf8.GetString([System.Convert]::FromBase64String('#{Base64.encode64(JSON.dump(arguments))}'))
)
$allowedArgs = (Get-Command "#{remote_path}").Parameters.Keys
$private:taskArgs = @{}
$private:tempArgs.Keys | ? { $allowedArgs -contains $_ } | % { $private:taskArgs[$_] = $private:tempArgs[$_] }
try { & "#{remote_path}" @taskArgs } catch { Write-Error $_.Exception; exit 1 }
PS
else
conn.execute(%(try { & "#{remote_path}" } catch { Write-Error $_.Exception; exit 1 }))
end
else
path, args = *process_from_extension(remote_path)
conn.execute_process(path, args, stdin)
end
Bolt::Result.for_task(target, output.stdout.string,
output.stderr.string,
output.exit_code)
end
end
end
|
#upload(target, source, destination, _options = {}) ⇒ Object
54
55
56
57
58
59
|
# File 'lib/bolt/transport/winrm.rb', line 54
def upload(target, source, destination, _options = {})
with_connection(target) do |conn|
conn.write_remote_file(source, destination)
Bolt::Result.for_upload(target, source, destination)
end
end
|
#with_connection(target) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/bolt/transport/winrm.rb', line 42
def with_connection(target)
conn = Connection.new(target)
conn.connect
yield conn
ensure
begin
conn&.disconnect
rescue StandardError => ex
logger.info("Failed to close connection to #{target.uri} : #{ex.message}")
end
end
|