class Chef::Provisioning::Machine::WindowsMachine
Attributes
options[R]
Options include:
command_prefix - prefix to put in front of any command, e.g. sudo
Public Class Methods
new(machine_spec, transport, convergence_strategy)
click to toggle source
Calls superclass method
Chef::Provisioning::Machine::BasicMachine::new
# File lib/chef/provisioning/machine/windows_machine.rb, line 7 def initialize(machine_spec, transport, convergence_strategy) super end
Public Instance Methods
create_dir(action_handler, path)
click to toggle source
# 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)
click to toggle source
Delete file
# 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)
click to toggle source
Get file attributes { :owner, :group, :rights }
def get_attributes(path) end
# File lib/chef/provisioning/machine/windows_machine.rb, line 90 def dirname_on_machine(path) path.split(/[\\\/]/)[0..-2].join('\\') end
escape(string)
click to toggle source
# File lib/chef/provisioning/machine/windows_machine.rb, line 94 def escape(string) transport.escape(string) end
file_exists?(path)
click to toggle source
Return true or false depending on whether file exists
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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_drive()
click to toggle source
# File lib/chef/provisioning/machine/windows_machine.rb, line 78 def system_drive transport.execute('$env:SystemDrive').stdout.strip end