Module: Utilities

Defined in:
lib/generators/templates/config/recipes/utilities.rb

Instance Method Summary collapse

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, 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

#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_upgradeObject



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, 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



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(options={}, &block)
  original_username, original_password = user, password
  begin
    set :user,     options[:user] || original_username
    set :password, options[: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?’)

Returns:

  • (Boolean)


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