Module: Dwell1

Defined in:
lib/dwell/cap_extensions.rb

Instance Method Summary collapse

Instance Method Details

#adduser(user, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/dwell/cap_extensions.rb', line 37

def adduser(user, options={})
  options[:shell] ||= '/bin/bash' # new accounts on ubuntu 6.06.1 have been getting /bin/sh
  switches = '--disabled-password --gecos ""'
  switches += " --shell=#{options[:shell]} " if options[:shell]
  switches += ' --no-create-home ' if options[:nohome]
  switches += " --ingroup #{options[:group]} " unless options[:group].nil?
  invoke_command "grep '^#{user}:' /etc/passwd || sudo /usr/sbin/adduser #{user} #{switches}", 
  :via => run_method
end

#append_to_file_if_missing(filename, value, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/dwell/cap_extensions.rb', line 19

def append_to_file_if_missing(filename, value, options={})
  # XXX sort out single quotes in 'value' - they'l break command!
  # XXX if options[:requires_sudo] and :use_sudo then use sudo
  sudo <<-END
  sh -c '
  grep -F "#{value}" #{filename} > /dev/null 2>&1 || 
  echo "#{value}" >> #{filename}
  '
  END
end

#config_gsub(file, find, replace) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/dwell/cap_extensions.rb', line 10

def config_gsub(file, find, replace)
  tmp="/tmp/#{File.basename(file)}"
  get file, tmp
  content=File.open(tmp).read
  content.gsub!(find,replace)
  put content, tmp
  sudo "mv #{tmp} #{file}"
end

#invoke_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object



87
88
89
# File 'lib/dwell/cap_extensions.rb', line 87

def invoke_with_input(shell_command, input_query=/^Password/, response=nil)
  handle_command_with_input(run_method, shell_command, input_query, response)
end

#mkdir(path, options = {}) ⇒ Object

create directory if it doesn’t already exist set permissions and ownership XXX move mode, path and



50
51
52
53
54
55
56
57
58
59
# File 'lib/dwell/cap_extensions.rb', line 50

def mkdir(path, options={})
  via = options.delete(:via) || :run
  # XXX need to make sudo commands wrap the whole command (sh -c ?)
  # XXX removed the extra 'sudo' from after the '||' - need something else
  invoke_command "test -d #{path} || #{sudo if via == :sudo} mkdir -p #{path}"
  invoke_command "chmod #{sprintf("%3o",options[:mode]||0755)} #{path}", :via => via if options[:mode]
  invoke_command "chown -R #{options[:owner]} #{path}", :via => via if options[:owner]
#    groupadd(options[:group], :via => via) if options[:group]
  invoke_command "chgrp -R #{options[:group]} #{path}", :via => via if options[:group]
end

#record_install(package) ⇒ Object



6
7
8
# File 'lib/dwell/cap_extensions.rb', line 6

def record_install(package)
  dwell1.append_to_file_if_missing "/etc/dwell/install_log", package
end

#run_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object

Run a command and ask for input when input_query is seen. Sends the response back to the server.

input_query is a regular expression that defaults to /^Password/.

Can be used where run would otherwise be used.

run_with_input 'ssh-keygen ...', /^Are you sure you want to overwrite\?/


71
72
73
# File 'lib/dwell/cap_extensions.rb', line 71

def run_with_input(shell_command, input_query=/^Password/, response=nil)
  handle_command_with_input(:run, shell_command, input_query, response)
end

#sudo_upload(from, to, options = {}, &block) ⇒ Object



30
31
32
33
34
35
# File 'lib/dwell/cap_extensions.rb', line 30

def sudo_upload(from, to, options={}, &block)
  top.upload from, "/tmp/#{File.basename(to)}", options, &block
  sudo "mv /tmp/#{File.basename(to)} #{to}"
  sudo "chmod #{options[:mode]} #{to}" if options[:mode]
  sudo "chown #{options[:owner]} #{to}" if options[:owner]
end

#sudo_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object

Run a command using sudo and ask for input when a regular expression is seen. Sends the response back to the server.

See also run_with_input

input_query is a regular expression



83
84
85
# File 'lib/dwell/cap_extensions.rb', line 83

def sudo_with_input(shell_command, input_query=/^Password/, response=nil)
  handle_command_with_input(:sudo, shell_command, input_query, response)
end