Module: Capchef

Extended by:
Capchef
Included in:
Capchef
Defined in:
lib/capchef.rb

Instance Method Summary collapse

Instance Method Details

#all_nodes(filter = nil) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/capchef.rb', line 99

def all_nodes(filter=nil)
  return [] if @config_pass && @config_pass > 1
  # Select only those nodes with an actual run_list, assume the others are just setting up vars
  nodes = nodes_config.keys.select { |node| nodes_config[node]['run_list'] }
  nodes = nodes.grep(Regexp.new(filter)) if filter
  return nodes
end

#nodes_configObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/capchef.rb', line 85

def nodes_config
  # The config file might want access to information that is contained within it (all_nodes for instance).
  # If so, make sure the 2nd pass doesn't try to acquire the same info or we will have an endless loop
  @config_pass ||= 0
  @nodes_config ||= begin
    nodes_file = ENV['NODES_FILE'] || 'nodes.yml'
    raise "No file #{nodes_file}" unless File.exist?(nodes_file)
    @config_pass += 1
    config = YAML.load(ERB.new(File.read(nodes_file), nil, '-').result(binding))
    @config_pass -= 1
    config
  end
end

#pathObject



18
19
20
# File 'lib/capchef.rb', line 18

def path
  @path ||= '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
end

#path=(path) ⇒ Object



22
23
24
# File 'lib/capchef.rb', line 22

def path=(path)
  @path = path
end

#prepend_path(dir) ⇒ Object



26
27
28
# File 'lib/capchef.rb', line 26

def prepend_path(dir)
  @path = "#{dir}:#{path}"
end

#sudo_optionsObject



42
43
44
# File 'lib/capchef.rb', line 42

def sudo_options
  @sudo_options ||= ''
end

#sudo_options=(val) ⇒ Object



46
47
48
# File 'lib/capchef.rb', line 46

def sudo_options=(val)
  @sudo_options = val
end

#surun(cap, command, options = {}, &block) ⇒ Object

Runs command as root invoking the command with ‘su -c’ and handling the root password prompt.

surun cap, "/etc/init.d/apache reload"
# Executes
# su - -c '/etc/init.d/apache reload'

surun cap, 'my_install' do |channel, stream, output|
  channel.send_data("\n") if output && output =~ /to continue/
  channel.send_data("y\n") if output && output =~ /replace/
end


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/capchef.rb', line 60

def surun(cap, command, options={}, &block)
  if use_sudo?
    if command.kind_of?(Array)
      my_surun_script(cap, 'surun', command, nil, options, &block)
    else
      sucmd = "#{cap.sudo} #{sudo_options} PATH=#{path} #{command}"
      cap.run(sucmd, options, &block)
    end
  else
    @root_password ||= cap.fetch(:root_password, Capistrano::CLI.password_prompt("root password: "))
    command = command.join(';') if command.kind_of?(Array)
    sucmd = "su -c 'cd; PATH=#{path}; #{command}'"
    cap.run(sucmd, options) do |channel, stream, output|
      puts "[#{channel[:host]}] #{output}" if output
      channel.send_data("#{@root_password}\n") if output && output =~ /^Password:/
      yield channel, stream, output if block_given?
    end
  end
end

#surun_script(cap, script, args = nil, options = {}, &block) ⇒ Object



80
81
82
83
# File 'lib/capchef.rb', line 80

def surun_script(cap, script, args=nil, options={}, &block)
  raise "No such file: #{script}" unless File.exist?(script)
  my_surun_script(cap, script, nil, args, options, &block)
end

#tmpdirObject

Default to /tmp but allow change in case it is noexec



10
11
12
# File 'lib/capchef.rb', line 10

def tmpdir
  @tmpdir ||= '/tmp'
end

#tmpdir=(tmpdir) ⇒ Object



14
15
16
# File 'lib/capchef.rb', line 14

def tmpdir=(tmpdir)
  @tmpdir = tmpdir
end

#use_sudo=(val) ⇒ Object



30
31
32
# File 'lib/capchef.rb', line 30

def use_sudo=(val)
  @use_sudo = val
end

#use_sudo?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/capchef.rb', line 34

def use_sudo?
  return true  if ENV['CAPCHEF_SUDO'] == 'true'
  return false if ENV['CAPCHEF_SUDO'] == 'false'
  # Default to true if unset
  @use_sudo = true if @use_sudo.nil?
  return @use_sudo
end