Class: Chef::Provisioning::Machine::WindowsMachine

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

Instance Attribute Summary collapse

Attributes inherited from BasicMachine

#convergence_strategy, #transport

Attributes inherited from Chef::Provisioning::Machine

#machine_spec

Instance Method Summary collapse

Methods inherited from BasicMachine

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

Methods inherited from Chef::Provisioning::Machine

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

Constructor Details

#initialize(machine_spec, transport, convergence_strategy) ⇒ WindowsMachine

Returns a new instance of WindowsMachine.



7
8
9
# File 'lib/chef/provisioning/machine/windows_machine.rb', line 7

def initialize(machine_spec, 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



14
15
16
# File 'lib/chef/provisioning/machine/windows_machine.rb', line 14

def options
  @options
end

Instance Method Details

#create_dir(action_handler, path) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/chef/provisioning/machine/windows_machine.rb', line 70

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

#delete_file(action_handler, path) ⇒ Object

Delete file



17
18
19
20
21
22
23
# File 'lib/chef/provisioning/machine/windows_machine.rb', line 17

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

#dirname_on_machine(path) ⇒ Object

def get_attributes(path)

end


90
91
92
# File 'lib/chef/provisioning/machine/windows_machine.rb', line 90

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

#escape(string) ⇒ Object



94
95
96
# File 'lib/chef/provisioning/machine/windows_machine.rb', line 94

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

#file_exists?(path) ⇒ Boolean

Return true or false depending on whether file exists

Returns:

  • (Boolean)


30
31
32
# File 'lib/chef/provisioning/machine/windows_machine.rb', line 30

def file_exists?(path)
  parse_boolean(transport.execute("Test-Path #{escape(path)}", :read_only => true).stdout)
end

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

Returns:

  • (Boolean)


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
60
61
62
63
64
65
66
67
68
# File 'lib/chef/provisioning/machine/windows_machine.rb', line 34

def files_different?(path, local_path, content=nil)
  if !file_exists?(path) || (local_path && !File.exists?(local_path))
    return true
  end

  # Get remote checksum of file (from http://stackoverflow.com/a/13926809)
  result = transport.execute(<<-EOM, :read_only => true)
$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

#is_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_directory?(path)
  parse_boolean(transport.execute("Test-Path #{escape(path)} -pathtype container", :read_only => true).stdout)
end

#parse_boolean(string) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/chef/provisioning/machine/windows_machine.rb', line 98

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

#system_driveObject



78
79
80
# File 'lib/chef/provisioning/machine/windows_machine.rb', line 78

def system_drive
  transport.execute('$env:SystemDrive').stdout.strip
end