Module: Utilities
- Defined in:
- lib/cap_recipes/tasks/utilities.rb
Instance Method Summary collapse
-
#_cset(name, *args, &block) ⇒ Object
set var.
-
#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
-
#render(file, binding) ⇒ Object
render a template.
-
#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?’).
-
#yum_install(packages) ⇒ Object
utilities.yum_install %w[package1 package2] utilities.yum_install “package1 package2”.
- #yum_update ⇒ Object
Instance Method Details
#_cset(name, *args, &block) ⇒ Object
set var
5 6 7 8 9 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 5 def _cset(name, *args, &block) unless exists?(name) set(name, *args, &block) end end |
#adduser(user, options = {}) ⇒ Object
utilities.adduser(‘deploy’)
80 81 82 83 84 85 86 87 88 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 80 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”
44 45 46 47 48 49 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 44 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
51 52 53 54 55 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 51 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’)
29 30 31 32 33 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 29 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”)
18 19 20 21 22 23 24 25 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 18 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
139 140 141 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 139 def invoke_with_input(shell_command, input_query=/^Password/, response=nil) handle_command_with_input(run_method, shell_command, input_query, response) end |
#render(file, binding) ⇒ Object
render a template
12 13 14 15 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 12 def render(file, binding) template = File.read("#{File.dirname(__FILE__)}/templates/#{file}.erb") result = ERB.new(template).result(binding) 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?/
125 126 127 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 125 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
114 115 116 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 114 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)
72 73 74 75 76 77 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 72 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
135 136 137 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 135 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’ }
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 102 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
91 92 93 94 95 96 97 98 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 91 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?’)
36 37 38 39 40 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 36 def yes?(question) question = "\n" + question.join("\n") if question.respond_to?(:uniq) question += ' (y/n)' ask(question).downcase.include? 'y' end |
#yum_install(packages) ⇒ Object
utilities.yum_install %w[package1 package2] utilities.yum_install “package1 package2”
58 59 60 61 62 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 58 def yum_install(packages) packages = packages.split(/\s+/) if packages.respond_to?(:split) packages = Array(packages) sudo "yum -y install #{packages.join(" ")}" end |
#yum_update ⇒ Object
64 65 66 67 |
# File 'lib/cap_recipes/tasks/utilities.rb', line 64 def yum_update apt_get="DEBCONF_TERSE='yes' DEBIAN_PRIORITY='critical' DEBIAN_FRONTEND=noninteractive apt-get" sudo "yum -y update" end |