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



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


86
87
88
# File 'lib/chef/provisioning/machine/windows_machine.rb', line 86

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

#escape(string) ⇒ Object



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

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

#file_exists?(path) ⇒ Boolean

Return true or false depending on whether file exists



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



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("$md5 = [System.Security.Cryptography.MD5]::Create(\"MD5\")\n$fd = [System.IO.File]::OpenRead(\#{path.inspect})\n$buf = new-object byte[] (1024*1024*8) # 8mb buffer\nwhile (($read_len = $fd.Read($buf,0,$buf.length)) -eq $buf.length){\n    $total += $buf.length\n    $md5.TransformBlock($buf,$offset,$buf.length,$buf,$offset)\n}\n# finalize the last read\n$md5.TransformFinalBlock($buf,0,$read_len)\n$hash = $md5.Hash\n# convert hash bytes to hex formatted string\n$hash | foreach { $hash_txt += $_.ToString(\"x2\") }\n$hash_txt\n", :read_only => true)
  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



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



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

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