class Chef::Provisioning::Machine::UnixMachine

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
# File lib/chef/provisioning/machine/unix_machine.rb, line 8
def initialize(machine_spec, transport, convergence_strategy)
  super

  @tmp_dir = '/tmp'
end

Public Instance Methods

create_dir(action_handler, path) click to toggle source
# File lib/chef/provisioning/machine/unix_machine.rb, line 69
def create_dir(action_handler, path)
  if !file_exists?(path)
    action_handler.perform_action "create directory #{path} on #{machine_spec.name}" do
      transport.execute("mkdir -p #{path}").error!
    end
  end
end
delete_file(action_handler, path) click to toggle source

Delete file

# File lib/chef/provisioning/machine/unix_machine.rb, line 20
def delete_file(action_handler, path)
  if file_exists?(path)
    action_handler.perform_action "delete file #{path} on #{machine_spec.name}" do
      transport.execute("rm -f #{path}").error!
    end
  end
end
dirname_on_machine(path) click to toggle source
# File lib/chef/provisioning/machine/unix_machine.rb, line 120
def dirname_on_machine(path)
  path.split('/')[0..-2].join('/')
end
file_exists?(path) click to toggle source

Return true or false depending on whether file exists

# File lib/chef/provisioning/machine/unix_machine.rb, line 35
def file_exists?(path)
  result = transport.execute("ls -d #{path}", :read_only => true)
  result.exitstatus == 0 && result.stdout != ''
end
files_different?(path, local_path, content=nil) click to toggle source
# File lib/chef/provisioning/machine/unix_machine.rb, line 40
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
  result = transport.execute("md5sum -b #{path}", :read_only => true)
  unless result.exitstatus == 0
    result = transport.execute("md5 -r #{path}", :read_only => true)
    unless result.exitstatus == 0
      result = transport.execute("digest -a md5 #{path}", :read_only => true)
    end
  end
  result.error!
  remote_sum = result.stdout.split(' ')[0]

  digest = Digest::MD5.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
get_attributes(path) click to toggle source

Get file attributes { :mode, :owner, :group }

# File lib/chef/provisioning/machine/unix_machine.rb, line 100
def get_attributes(path)
  os = transport.execute('uname -s')
  result = if os.stdout =~ /darwin/i # macOS
             transport.execute("stat -f '%Lp %Su %Sg %N' #{path}", read_only: true)
           else
             transport.execute("stat -c '%a %U %G %n' #{path}", read_only: true)
           end

  return nil if result.exitstatus != 0
  file_info = result.stdout.split(/\s+/)

  raise "#{path} does not exist in set_attributes()" if file_info.size <= 1

  {
      mode: file_info[0],
      owner: file_info[1],
      group: file_info[2]
  }
end
is_directory?(path) click to toggle source
# File lib/chef/provisioning/machine/unix_machine.rb, line 28
def is_directory?(path)
  result = transport.execute("stat -c '%F' #{path}", :read_only => true)
  return nil if result.exitstatus != 0
  result.stdout.chomp == 'directory'
end
set_attributes(action_handler, path, attributes) click to toggle source

Set file attributes { mode, :owner, :group }

# File lib/chef/provisioning/machine/unix_machine.rb, line 78
def set_attributes(action_handler, path, attributes)
  if attributes[:mode] || attributes[:owner] || attributes[:group]
    current_attributes = get_attributes(path)
    if attributes[:mode] && current_attributes[:mode].to_i != attributes[:mode].to_i
      action_handler.perform_action "change mode of #{path} on #{machine_spec.name} from #{current_attributes[:mode].to_i} to #{attributes[:mode].to_i}" do
        transport.execute("chmod #{attributes[:mode].to_i} #{path}").error!
      end
    end
    if attributes[:owner] && current_attributes[:owner] != attributes[:owner]
      action_handler.perform_action "change owner of #{path} on #{machine_spec.name} from #{current_attributes[:owner]} to #{attributes[:owner]}" do
        transport.execute("chown #{attributes[:owner]} #{path}").error!
      end
    end
    if attributes[:group] && current_attributes[:group] != attributes[:group]
      action_handler.perform_action "change group of #{path} on #{machine_spec.name} from #{current_attributes[:group]} to #{attributes[:group]}" do
        transport.execute("chgrp #{attributes[:group]} #{path}").error!
      end
    end
  end
end