Module: Dwell1
- Defined in:
- lib/dwell/cap_extensions.rb
Instance Method Summary collapse
- #adduser(user, options = {}) ⇒ Object
- #append_to_file_if_missing(filename, value, options = {}) ⇒ Object
- #config_gsub(file, find, replace) ⇒ Object
- #invoke_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object
-
#mkdir(path, options = {}) ⇒ Object
create directory if it doesn’t already exist set permissions and ownership XXX move mode, path and.
- #record_install(package) ⇒ Object
-
#run_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object
Run a command and ask for input when input_query is seen.
- #sudo_upload(from, to, options = {}, &block) ⇒ Object
-
#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.
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, ={}) [:shell] ||= '/bin/bash' # new accounts on ubuntu 6.06.1 have been getting /bin/sh switches = '--disabled-password --gecos ""' switches += " --shell=#{[:shell]} " if [:shell] switches += ' --no-create-home ' if [:nohome] switches += " --ingroup #{[:group]} " unless [: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, ={}) # 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, ={}) via = .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",[:mode]||0755)} #{path}", :via => via if [:mode] invoke_command "chown -R #{[:owner]} #{path}", :via => via if [:owner] # groupadd(options[:group], :via => via) if options[:group] invoke_command "chgrp -R #{[:group]} #{path}", :via => via if [: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, ={}, &block) top.upload from, "/tmp/#{File.basename(to)}", , &block sudo "mv /tmp/#{File.basename(to)} #{to}" sudo "chmod #{[:mode]} #{to}" if [:mode] sudo "chown #{[:owner]} #{to}" if [: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 |