Module: Utilities
- Defined in:
- lib/generators/templates/config/recipes/utilities.rb
Instance Method Summary collapse
-
#adduser(user, options = {}) ⇒ Object
utilities.adduser(‘deploy’).
-
#apt_install(packages) ⇒ Object
utilities.apt_install %w[package1 package2] utilities.apt_install “package1 package2”.
- #apt_upgrade ⇒ Object
-
#ask(question, default = '') ⇒ Object
utilities.ask(‘What is your name?’, ‘John’).
-
#config_gsub(file, find, replace) ⇒ Object
utilities.config_gsub(‘/etc/example’, /(.*)/im, “\1”).
- #invoke_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object
-
#run_with_input(shell_command, input_query = /^Password/, response = nil) ⇒ Object
Run a command and ask for input when input_query is seen.
- #space(str) ⇒ Object
-
#sudo_upload(from, to, options = {}, &block) ⇒ Object
utilities.sudo_upload(‘/local/path/to/file’, ‘/remote/path/to/destination’, options).
-
#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.
-
#with_credentials(options = {}, &block) ⇒ Object
utilities.with_credentials(:user => ‘xxxx’, :password => ‘secret’) options = { :user => ‘xxxxx’, :password => ‘xxxxx’ }.
-
#with_role(role, &block) ⇒ Object
role = :app.
-
#yes?(question) ⇒ Boolean
utilities.yes?(‘Proceed with install?’).
Instance Method Details
#adduser(user, options = {}) ⇒ Object
utilities.adduser(‘deploy’)
52 53 54 55 56 57 58 59 60 |
# File 'lib/generators/templates/config/recipes/utilities.rb', line 52 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 |
#apt_install(packages) ⇒ Object
utilities.apt_install %w[package1 package2] utilities.apt_install “package1 package2”
30 31 32 33 34 35 |
# File 'lib/generators/templates/config/recipes/utilities.rb', line 30 def apt_install(packages) packages = packages.split(/\s+/) if packages.respond_to?(:split) packages = Array(packages) apt_get="DEBCONF_TERSE='yes' DEBIAN_PRIORITY='critical' DEBIAN_FRONTEND=noninteractive apt-get" sudo "#{apt_get} -qyu --force-yes install #{packages.join(" ")}" end |
#apt_upgrade ⇒ Object
37 38 39 40 41 |
# File 'lib/generators/templates/config/recipes/utilities.rb', line 37 def apt_upgrade apt_get="DEBCONF_TERSE='yes' DEBIAN_PRIORITY='critical' DEBIAN_FRONTEND=noninteractive apt-get" sudo "#{apt_get} -qy update" sudo "#{apt_get} -qyu --force-yes upgrade" end |
#ask(question, default = '') ⇒ Object
utilities.ask(‘What is your name?’, ‘John’)
15 16 17 18 19 |
# File 'lib/generators/templates/config/recipes/utilities.rb', line 15 def ask(question, default='') question = "\n" + question.join("\n") if question.respond_to?(:uniq) answer = Capistrano::CLI.ui.ask(space(question)).strip answer.empty? ? default : answer end |
#config_gsub(file, find, replace) ⇒ Object
utilities.config_gsub(‘/etc/example’, /(.*)/im, “\1”)
5 6 7 8 9 10 11 12 |
# File 'lib/generators/templates/config/recipes/utilities.rb', line 5 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
111 112 113 |
# File 'lib/generators/templates/config/recipes/utilities.rb', line 111 def invoke_with_input(shell_command, input_query=/^Password/, response=nil) handle_command_with_input(run_method, shell_command, input_query, response) 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?/
97 98 99 |
# File 'lib/generators/templates/config/recipes/utilities.rb', line 97 def run_with_input(shell_command, input_query=/^Password/, response=nil) handle_command_with_input(:run, shell_command, input_query, response) end |
#space(str) ⇒ Object
86 87 88 |
# File 'lib/generators/templates/config/recipes/utilities.rb', line 86 def space(str) "\n#{'=' * 80}\n#{str}" end |
#sudo_upload(from, to, options = {}, &block) ⇒ Object
utilities.sudo_upload(‘/local/path/to/file’, ‘/remote/path/to/destination’, options)
44 45 46 47 48 49 |
# File 'lib/generators/templates/config/recipes/utilities.rb', line 44 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
107 108 109 |
# File 'lib/generators/templates/config/recipes/utilities.rb', line 107 def sudo_with_input(shell_command, input_query=/^Password/, response=nil) handle_command_with_input(:sudo, shell_command, input_query, response) end |
#with_credentials(options = {}, &block) ⇒ Object
utilities.with_credentials(:user => ‘xxxx’, :password => ‘secret’) options = { :user => ‘xxxxx’, :password => ‘xxxxx’ }
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/generators/templates/config/recipes/utilities.rb', line 74 def with_credentials(={}, &block) original_username, original_password = user, password begin set :user, [:user] || original_username set :password, [:password] || original_password yield ensure set :user, original_username set :password, original_password end end |
#with_role(role, &block) ⇒ Object
role = :app
63 64 65 66 67 68 69 70 |
# File 'lib/generators/templates/config/recipes/utilities.rb', line 63 def with_role(role, &block) original, ENV['HOSTS'] = ENV['HOSTS'], find_servers(:roles => role).map{|d| d.host}.join(",") begin yield ensure ENV['HOSTS'] = original end end |
#yes?(question) ⇒ Boolean
utilities.yes?(‘Proceed with install?’)
22 23 24 25 26 |
# File 'lib/generators/templates/config/recipes/utilities.rb', line 22 def yes?(question) question = "\n" + question.join("\n") if question.respond_to?(:uniq) question += ' (y/n)' ask(question).downcase.include? 'y' end |