Class: ChefMetal::Transport::WinRM

Inherits:
ChefMetal::Transport show all
Defined in:
lib/chef_metal/transport/winrm.rb

Defined Under Namespace

Classes: WinRMResult

Constant Summary

Constants inherited from ChefMetal::Transport

DEFAULT_TIMEOUT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ChefMetal::Transport

#download_file, #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, options, global_config)
  @endpoint = endpoint
  @type = type
  @options = options
  @config = global_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/chef_metal/transport/winrm.rb', line 18

def config
  @config
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



15
16
17
# File 'lib/chef_metal/transport/winrm.rb', line 15

def endpoint
  @endpoint
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/chef_metal/transport/winrm.rb', line 17

def options
  @options
end

#typeObject (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

#available?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chef_metal/transport/winrm.rb', line 69

def available?
  # If you can't pwd within 10 seconds, you can't pwd
  execute('pwd', :timeout => 10)
  true
rescue Timeout::Error, Errno::EHOSTUNREACH, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET, ::WinRM::WinRMHTTPTransportError, ::WinRM::WinRMWebServiceError, ::WinRM::WinRMWSManFault
  Chef::Log.debug("unavailable: network connection failed or broke: #{$!.inspect}")
  disconnect
  false
rescue ::WinRM::WinRMAuthorizationError
  Chef::Log.debug("unavailable: winrm authentication error: #{$!.inspect} ")
  disconnect
  false
end

#disconnectObject



61
62
63
# File 'lib/chef_metal/transport/winrm.rb', line 61

def disconnect
  #
end

#escape(string) ⇒ Object



65
66
67
# File 'lib/chef_metal/transport/winrm.rb', line 65

def escape(string)
  "\"#{string.gsub("\"", "`\"")}\""
end

#execute(command, execute_options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/chef_metal/transport/winrm.rb', line 20

def execute(command, execute_options = {})
  output = with_execute_timeout(execute_options) do
    session.set_timeout(execute_timeout(execute_options))
    session.run_powershell_script(command) do |stdout, stderr|
      stream_chunk(execute_options, stdout, stderr)
    end
  end
  WinRMResult.new(command, execute_options, config, output)
end

#make_url_available_to_remote(local_url) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/chef_metal/transport/winrm.rb', line 83

def make_url_available_to_remote(local_url)
  uri = URI(local_url)
  host = Socket.getaddrinfo(uri.host, uri.scheme, nil, :STREAM)[0][3]
  if host == '127.0.0.1' || host == '::1'
    raise 'Unable to converge locally via winrm. Local converge is currently only supported with SSH. You may only converge with winrm against a chef-server.'
  end
  local_url
end

#read_file(path) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/chef_metal/transport/winrm.rb', line 30

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chef_metal/transport/winrm.rb', line 39

def write_file(path, content)
  chunk_size = options[: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