Module: ChickenSoup

Defined in:
lib/chicken_soup/notifiers/git/git-tasks.rb,
lib/chicken_soup/global.rb,
lib/chicken_soup/version.rb,
lib/chicken_soup/notifiers/email/presenter.rb,
lib/chicken_soup/capabilities/rvm/rvm-tasks.rb,
lib/chicken_soup/capabilities/svn/svn-tasks.rb,
lib/chicken_soup/capabilities/rvm/rvm-defaults.rb,
lib/chicken_soup/capabilities/nginx/nginx-defaults.rb,
lib/chicken_soup/capabilities/apache/apache-defaults.rb,
lib/chicken_soup/capabilities/passenger/passenger-defaults.rb

Overview

PASSENGER DEFAULTS #

Defined Under Namespace

Modules: ApplicationServer, Email, WebServer

Constant Summary collapse

VERSION =
"0.9.0"
RVM_INFO_FORMAT =
/^rvm.+\s(([a-zA-Z0-9\-\._]+)(?:@([a-zA-Z0-9\-\._]+))?)/

Instance Method Summary collapse

Instance Method Details

#_cset(name, *args, &block) ⇒ Object

Note:

Taken directly from the Capistrano codebase.

Sets a variable only if it doesn’t already exist.



94
95
96
97
98
# File 'lib/chicken_soup/global.rb', line 94

def _cset(name, *args, &block)
  unless exists?(name)
    set(name, *args, &block)
  end
end

#close_sessionsObject

Helper method to close all session connections to the remote servers



5
6
7
8
# File 'lib/chicken_soup/global.rb', line 5

def close_sessions
  sessions.values.each { |session| session.close }
  sessions.clear
end

#compressed_file?(filename) ⇒ Boolean

Checks to see if a filename has an extension which would imply that it is compressed.

extension.

Examples:

compressed_file? 'file.bz2'

Parameters:

  • filename (String)

    The filename whose extension will be checked.

Returns:

  • (Boolean)

    the result of whether the file has a compression



191
192
193
# File 'lib/chicken_soup/global.rb', line 191

def compressed_file?(filename)
  filename =~ /.*\.bz2/
end

#download_compressed(remote, local, options = {}) ⇒ Object

Compresses a specific remote file before transferring it to the local machine. Once the transfer is completed, the file will be uncompressed and the compressed version will be deleted.

or directory that you would like to transfer.

or directory to be transferred.

#download method.

Examples:

download_compressed 'my/remote/file', 'my/local/file', :once => true

Parameters:

  • remote (String)

    The remote filename (optionally including path),

  • local (String)

    The location locally where you would like the file

  • options (String) (defaults to: {})

    Any options that can be passed to Capistrano’s



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/chicken_soup/global.rb', line 160

def download_compressed(remote, local, options = {})
  remote_basename              = File.basename(remote)

  unless compressed_file? remote
    remote_compressed_filename = "#{user_home}/#{remote_basename}.bz2"
    local_compressed_filename  = "#{local}.bz2"

    run "bzip2 -zvck9 #{remote} > #{remote_compressed_filename}"
  end

  remote_compressed_filename  ||= remote
  local_compressed_filename   ||= local

  download remote_compressed_filename, local_compressed_filename, options

  run "rm -f #{remote_compressed_filename}" unless remote_compressed_filename == remote
  `bunzip2 -f #{local_compressed_filename} && rm -f #{local_compressed_filename}`
end

#fetch_log(logs) ⇒ Object



265
266
267
268
269
270
271
272
# File 'lib/chicken_soup/global.rb', line 265

def fetch_log(logs)
  logs.each do |log|
    local_log_directory = "#{rails_root}/log/#{rails_env}/#{release_name}"

    `mkdir -p #{local_log_directory}`
    download log, "#{local_log_directory}/$CAPISTRANO:HOST$-#{File.basename(log)}"
  end
end

#find_all_logs(log_directory, log_filenames) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/chicken_soup/global.rb', line 247

def find_all_logs(log_directory, log_filenames)
  existing_files = []

  log_filenames.each do |standard_file|
    existing_files << "#{log_directory}/#{application}.#{standard_file}" if remote_file_exists?("#{log_directory}/#{application}.#{standard_file}")
    existing_files << "#{log_directory}/#{application}-#{standard_file}" if remote_file_exists?("#{log_directory}/#{application}-#{standard_file}")
    existing_files << "#{log_directory}/#{standard_file}"                if remote_file_exists?("#{log_directory}/#{standard_file}")
  end

  existing_files
end

#log_directory(log_directories) ⇒ Object



259
260
261
262
263
# File 'lib/chicken_soup/global.rb', line 259

def log_directory(log_directories)
  log_directories.detect do |directory|
    remote_directory_exists? directory, :with_files => true
  end
end

#lookup_ip_for(hostname) ⇒ String

Uses nslookup locally to figure out the IP address of the provided hostname.

IP for.

IP can be retrieved, nil will be returned.

Examples:

lookup_ip_for 'google.com'

Parameters:

  • hostname (String)

    The hostname you would like to retrieve the

Returns:

  • (String)

    The IP address of the provided hostname. If no



242
243
244
245
# File 'lib/chicken_soup/global.rb', line 242

def lookup_ip_for(hostname)
  ip = `nslookup #{hostname} | tail -n 2 | head -n 1 | cut -d ' ' -f 2`.chomp
  ip != '' ? ip : nil
end

#maintenance_filenameObject



285
286
287
288
289
290
# File 'lib/chicken_soup/global.rb', line 285

def maintenance_filename
  custom_maintenance_path   = File.join(rails_root, maintenance_page_path, "#{maintenance_basename}.html.erb")
  template_maintenance_path = File.join(File.dirname(__FILE__), "templates", "maintenance.html.erb")

  File.exist?(custom_maintenance_path) ? custom_maintenance_path : template_maintenance_path
end

#remote_directory_exists?(directory, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
# File 'lib/chicken_soup/global.rb', line 121

def remote_directory_exists?(directory, options = {})
  with_files_check = options[:with_files] ? "&& $(ls -A #{directory})" : ''

  capture("if [[ -d #{directory} #{with_files_check} ]]; then echo -n 'exists'; fi") == 'exists'
end

#remote_file_exists?(file) ⇒ Boolean

Runs a command on the remote server to see if the file currently exists.

Examples:

File without path:

remote_file_exists? 'server.log'

File with path:

remote_file_exists? '/var/www/myappdir/log/production.log'

Directory:

remote_file_exists? '/var/www/myappdir/log'

Symbolic Link:

remote_file_exists? '/var/www/myappdir/current'

Parameters:

  • file (String)

    The filename (optionally including path), directory, or symlink that is may or may not exist.

Returns:

  • (Boolean)

    Whether or not the file, directory or symlink exists.



117
118
119
# File 'lib/chicken_soup/global.rb', line 117

def remote_file_exists?(file)
  capture("if [[ -d #{file} ]] || [[ -h #{file} ]] || [[ -f #{file} ]]; then echo -n 'exists'; fi;") == 'exists'
end

#require_if_exists(file) ⇒ nil

Will require a file but will not throw an error if that file does not exist.

Examples:

require_if_exists 'my_library'

Parameters:

  • file (String)

    The filename (optionally including path), directory, or symlink that is may or may not exist.

Returns:

  • (nil)

    nil will be returned if the file was loaded successfully.



139
140
141
# File 'lib/chicken_soup/global.rb', line 139

def require_if_exists(file)
  require file if File.exists?(File.join(File.dirname(__FILE__), '..', "#{file}.rb"))
end

#run_task(task_name, options = {}) ⇒ Object

TODO:

Remove all previous hooks prior to adding new ones. Also disable hooks when running “now”

Helper method to run tasks in different contexts.

The workflow is as follows:

  • Current user is saved

  • User is switched to the desired username passed in via the :as option

  • The task is run

  • The user is switched back to the original user

By default the task hooks are prepared but the task itself is not executed. You can change this by passing :now as an option.

Running a task ‘now’ does not create hooks. Standard calls to the task will be executed via the current user.

Examples:

Always run the task to install gems as the ‘manage’ user:

run_task 'gems:install', :as => 'manage'

Run the db migration task now as the ‘deploy’ user:

run_task 'db:migrate', :as => 'deploy', :now => true

Parameters:

  • task_name (String)

    The name of the task to run.

  • options (Hash) (defaults to: {})

    Options to customize how the task is run. Valid options are:

Options Hash (options):

  • :now (Boolean)
    • If present, the task will be executed immediately.

  • :as (String)
    • The name of the user you wish the task to be executed as.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chicken_soup/global.rb', line 54

def run_task(task_name, options = {})
  abort "#run_task must be passed an `:as` option so that it knows who to change the user to." unless options[:as]

  original_username = exists?(:user) ? user : nil

  if options[:now]
    set_user_to options[:as]
    find_and_execute_task(task_name)
    set_user_to original_username
  else
    before task_name,     "os:users:#{options[:as]}:use"
    after  task_name,     "os:users:#{original_username}:use"
  end
end

#run_with_ruby_manager(ruby_env_string, command, options = {}) ⇒ Object

A stub method which simply passes through to Capistrano’s #run. This method is meant to be overridden when a Ruby manager capability (ie RVM) is installed.

is not used. It is instead intended to be used with the Ruby manager that is installed.

#run method.

Examples:

run_with_ruby_manager 'foo', 'gem list', :pty => false

Parameters:

  • ruby_version (String)

    This is simply a noop on this method. It

  • command (String)

    The command that is passed to #run

  • options (String) (defaults to: {})

    Any options that can be passed to Capistrano’s



212
213
214
# File 'lib/chicken_soup/global.rb', line 212

def run_with_ruby_manager(ruby_version, command, options = {})
  run command, options
end

#set_user_to(username) ⇒ Object

Forces all connections to switch to the user passed into it.

It will forcibly terminate all open connections in order to accomplish this.

Examples:

Switch to the ‘deploy’ user:

set_user_to 'deploy'

Parameters:

  • username (String)

    The username you would like to begin using.



20
21
22
23
24
25
# File 'lib/chicken_soup/global.rb', line 20

def set_user_to(username)
  close_sessions
  set :user,        username
  set(:password)    {Capistrano::CLI.password_prompt("#{username.capitalize}'s Password: ")}
  set(:user_home)   { user == "root" ? "/root" : "/home/#{username}" }
end

#tail_log(logs) ⇒ Object



274
275
276
277
278
279
280
281
282
283
# File 'lib/chicken_soup/global.rb', line 274

def tail_log(logs)
  run "tail -n #{ENV['lines'] || 20} -f #{logs.join ' '}" do |channel, stream, data|
    trap("INT") { puts 'Log tailing aborted...'; exit 0; }

    puts  # for an extra line break before the host name
    puts "#{channel[:host]}: #{data}"

    break if stream == :err
  end
end

#vc_lognil

A stub method which simply returns nil. It is meant to be overridden when a version control capability (ie Git) is installed.

Examples:

run_with_ruby_manager 'foo', 'gem list', :pty => false

Returns:

  • (nil)

    Always returns nil



225
226
227
# File 'lib/chicken_soup/global.rb', line 225

def vc_log
  nil
end

#verify_variables(required_variables) ⇒ Object

Checks an array of items to see if they are currently set within the Capistrano scope. If any of them fail, Capistrano execution will terminate.

Examples:

Using an array:

verify_variables [:user, :deploy_base_dir, :app_server]

Parameters:

  • required_variables (Array, #each)

    An iterable list of items which represent the names of Capistrano environment variables. Each item in this list is expected to be set.

Raises:

  • (CapistranoGoBoom)

    Calls #abort on the Capistrano execution if any of the variables are not set.



83
84
85
86
87
# File 'lib/chicken_soup/global.rb', line 83

def verify_variables(required_variables)
  required_variables.each do |expected_variable|
    abort( "You have not defined '#{expected_variable}' which is necessary for deployment." ) unless exists?(expected_variable)
  end
end