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
Instance Attribute Summary collapse
-
#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 = {}) ⇒ 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 = {}) ⇒ WinRM
Returns a new instance of WinRM.
7 8 9 10 11 |
# File 'lib/chef_metal/transport/winrm.rb', line 7 def initialize(endpoint, type, = {}) @endpoint = endpoint @type = type @options = end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
13 14 15 |
# File 'lib/chef_metal/transport/winrm.rb', line 13 def endpoint @endpoint end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/chef_metal/transport/winrm.rb', line 15 def @options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
14 15 16 |
# File 'lib/chef_metal/transport/winrm.rb', line 14 def type @type end |
Instance Method Details
#disconnect ⇒ Object
55 56 57 |
# File 'lib/chef_metal/transport/winrm.rb', line 55 def disconnect # end |
#escape(string) ⇒ Object
59 60 61 |
# File 'lib/chef_metal/transport/winrm.rb', line 59 def escape(string) "'#{string.gsub("'", "''")}'" end |
#execute(command, execute_options = {}) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/chef_metal/transport/winrm.rb', line 17 def execute(command, = {}) output = session.run_powershell_script(command) do |stdout, stderr| stream_chunk(, stdout, stderr) end WinRMResult.new(command, , output) end |
#read_file(path) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/chef_metal/transport/winrm.rb', line 24 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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/chef_metal/transport/winrm.rb', line 33 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 |