Class: ChefMetal::Machine::WindowsMachine

Inherits:
BasicMachine show all
Defined in:
lib/chef_metal/machine/windows_machine.rb

Instance Attribute Summary collapse

Attributes inherited from BasicMachine

#convergence_strategy, #transport

Attributes inherited from ChefMetal::Machine

#node

Instance Method Summary collapse

Methods inherited from BasicMachine

#converge, #disconnect, #download_file, #execute, #execute_always, #make_url_available_to_remote, #read_file, #setup_convergence, #upload_file, #write_file

Methods inherited from ChefMetal::Machine

#converge, #detect_os, #disconnect, #download_file, #execute, #execute_always, #get_attributes, #make_url_available_to_remote, #read_file, #set_attributes, #setup_convergence, #upload_file, #write_file

Constructor Details

#initialize(node, transport, convergence_strategy) ⇒ WindowsMachine

Returns a new instance of WindowsMachine.



6
7
8
# File 'lib/chef_metal/machine/windows_machine.rb', line 6

def initialize(node, transport, convergence_strategy)
  super
end

Instance Attribute Details

#optionsObject (readonly)

Options include:

command_prefix - prefix to put in front of any command, e.g. sudo



13
14
15
# File 'lib/chef_metal/machine/windows_machine.rb', line 13

def options
  @options
end

Instance Method Details

#create_dir(action_handler, path) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/chef_metal/machine/windows_machine.rb', line 61

def create_dir(action_handler, path)
  if !file_exists?(path)
    action_handler.converge_by "create directory #{path} on #{node['name']}" do
      transport.execute("New-Item #{escape(path)} -Type directory")
    end
  end
end

#delete_file(action_handler, path) ⇒ Object

Delete file



16
17
18
19
20
21
22
# File 'lib/chef_metal/machine/windows_machine.rb', line 16

def delete_file(action_handler, path)
  if file_exists?(path)
    action_handler.converge_by "delete file #{escape(path)} on #{node['name']}" do
      transport.execute("Remove-Item #{escape(path)}").error!
    end
  end
end

#dirname_on_machine(path) ⇒ Object

def get_attributes(path)

end


77
78
79
# File 'lib/chef_metal/machine/windows_machine.rb', line 77

def dirname_on_machine(path)
  path.split(/[\\\/]/)[0..-2].join('\\')
end

#escape(string) ⇒ Object



81
82
83
# File 'lib/chef_metal/machine/windows_machine.rb', line 81

def escape(string)
  transport.escape(string)
end

#file_exists?(path) ⇒ Boolean

Return true or false depending on whether file exists

Returns:

  • (Boolean)


25
26
27
# File 'lib/chef_metal/machine/windows_machine.rb', line 25

def file_exists?(path)
  parse_boolean(transport.execute("Test-Path #{escape(path)}").stdout)
end

#files_different?(path, local_path, content = nil) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
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/machine/windows_machine.rb', line 29

def files_different?(path, local_path, content=nil)
  # Get remote checksum of file (from http://stackoverflow.com/a/13926809)
  result = transport.execute <<-EOM
$md5 = [System.Security.Cryptography.MD5]::Create("MD5")
$fd = [System.IO.File]::OpenRead(#{path.inspect})
$buf = new-object byte[] (1024*1024*8) # 8mb buffer
while (($read_len = $fd.Read($buf,0,$buf.length)) -eq $buf.length){
    $total += $buf.length
    $md5.TransformBlock($buf,$offset,$buf.length,$buf,$offset)
}
# finalize the last read
$md5.TransformFinalBlock($buf,0,$read_len)
$hash = $md5.Hash
# convert hash bytes to hex formatted string
$hash | foreach { $hash_txt += $_.ToString("x2") }
$hash_txt
EOM
  result.error!
  remote_sum = result.stdout.split(' ')[0]
  digest = Digest::SHA256.new
  if content
    digest.update(content)
  else
    File.open(local_path, 'rb') do |io|
      while (buf = io.read(4096)) && buf.length > 0
        digest.update(buf)
      end
    end
  end
  remote_sum != digest.hexdigest
end

#parse_boolean(string) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/chef_metal/machine/windows_machine.rb', line 85

def parse_boolean(string)
  if string =~ /^\s*true\s*$/mi
    true
  else
    false
  end
end