Class: ChefMetal::Machine::UnixMachine

Inherits:
BasicMachine show all
Defined in:
lib/chef_metal/machine/unix_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, #make_url_available_to_remote, #read_file, #setup_convergence, #upload_file, #write_file

Constructor Details

#initialize(node, transport, convergence_strategy) ⇒ UnixMachine

Returns a new instance of UnixMachine.



7
8
9
10
11
# File 'lib/chef_metal/machine/unix_machine.rb', line 7

def initialize(node, transport, convergence_strategy)
  super

  @tmp_dir = '/tmp'
end

Instance Attribute Details

#optionsObject (readonly)

Options include:

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



16
17
18
# File 'lib/chef_metal/machine/unix_machine.rb', line 16

def options
  @options
end

Instance Method Details

#create_dir(action_handler, path) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/chef_metal/machine/unix_machine.rb', line 56

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

#delete_file(action_handler, path) ⇒ Object

Delete file



19
20
21
22
23
24
25
# File 'lib/chef_metal/machine/unix_machine.rb', line 19

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

#dirname_on_machine(path) ⇒ Object



99
100
101
# File 'lib/chef_metal/machine/unix_machine.rb', line 99

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

#file_exists?(path) ⇒ Boolean

Return true or false depending on whether file exists

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/chef_metal/machine/unix_machine.rb', line 28

def file_exists?(path)
  result = transport.execute("ls -d #{path}")
  result.exitstatus == 0 && result.stdout != ''
end

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

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef_metal/machine/unix_machine.rb', line 33

def files_different?(path, local_path, content=nil)
  if !file_exists?(path)
    return true
  end

  # Get remote checksum of file
  result = transport.execute("md5sum -b #{path}")
  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) ⇒ Object

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



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/chef_metal/machine/unix_machine.rb', line 87

def get_attributes(path)
  file_info = transport.execute("stat -c '%a %U %G %n' #{path}").stdout.split(/\s+/)
  if file_info.size <= 1
    raise "#{path} does not exist in set_attributes()"
  end
  result = {
    :mode => file_info[0],
    :owner => file_info[1],
    :group => file_info[2]
  }
end

#set_attributes(action_handler, path, attributes) ⇒ Object

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chef_metal/machine/unix_machine.rb', line 65

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.converge_by "change mode of #{path} on #{node['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.converge_by "change group of #{path} on #{node['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.converge_by "change group of #{path} on #{node['name']} from #{current_attributes[:group]} to #{attributes[:group]}" do
        transport.execute("chgrp #{attributes[:group]} #{path}").error!
      end
    end
  end
end