Class: ChefMetal::Transport::WinRM
- Inherits:
-
ChefMetal::Transport
- Object
- ChefMetal::Transport
- ChefMetal::Transport::WinRM
- Defined in:
- lib/chef_metal/transport/winrm.rb
Defined Under Namespace
Classes: WinRMResult
Constant Summary
Constants inherited from ChefMetal::Transport
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #disconnect ⇒ Object
- #escape(string) ⇒ Object
- #execute(command, execute_options = {}) ⇒ Object
-
#initialize(endpoint, type, options, global_config) ⇒ WinRM
constructor
A new instance of WinRM.
- #read_file(path) ⇒ Object
- #write_file(path, content) ⇒ Object
Methods inherited from ChefMetal::Transport
#available?, #download_file, #make_url_available_to_remote, #upload_file
Constructor Details
#initialize(endpoint, type, options, global_config) ⇒ WinRM
Returns a new instance of WinRM.
8 9 10 11 12 13 |
# File 'lib/chef_metal/transport/winrm.rb', line 8 def initialize(endpoint, type, , global_config) @endpoint = endpoint @type = type @options = @config = global_config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
18 19 20 |
# File 'lib/chef_metal/transport/winrm.rb', line 18 def config @config end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
15 16 17 |
# File 'lib/chef_metal/transport/winrm.rb', line 15 def endpoint @endpoint end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/chef_metal/transport/winrm.rb', line 17 def @options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
16 17 18 |
# File 'lib/chef_metal/transport/winrm.rb', line 16 def type @type end |
Instance Method Details
#disconnect ⇒ Object
60 61 62 |
# File 'lib/chef_metal/transport/winrm.rb', line 60 def disconnect # end |
#escape(string) ⇒ Object
64 65 66 |
# File 'lib/chef_metal/transport/winrm.rb', line 64 def escape(string) "'#{string.gsub("'", "''")}'" end |
#execute(command, execute_options = {}) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/chef_metal/transport/winrm.rb', line 20 def execute(command, = {}) output = with_execute_timeout() do session.run_powershell_script(command) do |stdout, stderr| stream_chunk(, stdout, stderr) end end WinRMResult.new(command, , config, output) end |
#read_file(path) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/chef_metal/transport/winrm.rb', line 29 def read_file(path) result = execute("[Convert]::ToBase64String((Get-Content #{escape(path)} -Encoding byte -ReadCount 0))") if result.exitstatus == 0 Base64.decode64(result.stdout) else nil end end |
#write_file(path, content) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/chef_metal/transport/winrm.rb', line 38 def write_file(path, content) chunk_size = [:chunk_size] || 1024 # TODO if we could marshal this data directly, we wouldn't have to base64 or do this godawful slow stuff :( index = 0 execute(" $ByteArray = [System.Convert]::FromBase64String(#{escape(Base64.encode64(content[index..index+chunk_size-1]))}) $file = [System.IO.File]::Open(#{escape(path)}, 'Create', 'Write') $file.Write($ByteArray, 0, $ByteArray.Length) $file.Close ").error! index += chunk_size while index < content.length execute(" $ByteArray = [System.Convert]::FromBase64String(#{escape(Base64.encode64(content[index..index+chunk_size-1]))}) $file = [System.IO.File]::Open(#{escape(path)}, 'Append', 'Write') $file.Write($ByteArray, 0, $ByteArray.Length) $file.Close ").error! index += chunk_size end end |