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
-
#_cset(name, *args, &block) ⇒ Object
Sets a variable only if it doesn’t already exist.
-
#close_sessions ⇒ Object
Helper method to close all session connections to the remote servers.
-
#compressed_file?(filename) ⇒ Boolean
Checks to see if a filename has an extension which would imply that it is compressed.
-
#download_compressed(remote, local, options = {}) ⇒ Object
Compresses a specific remote file before transferring it to the local machine.
- #fetch_log(logs) ⇒ Object
- #find_all_logs(log_directory, log_filenames) ⇒ Object
- #log_directory(log_directories) ⇒ Object
-
#lookup_ip_for(hostname) ⇒ String
Uses nslookup locally to figure out the IP address of the provided hostname.
- #maintenance_filename ⇒ Object
- #remote_directory_exists?(directory, options = {}) ⇒ Boolean
-
#remote_file_exists?(file) ⇒ Boolean
Runs a command on the remote server to see if the file currently exists.
-
#require_if_exists(file) ⇒ nil
Will require a file but will not throw an error if that file does not exist.
-
#run_task(task_name, options = {}) ⇒ Object
Helper method to run tasks in different contexts.
-
#run_with_ruby_manager(ruby_env_string, command, options = {}) ⇒ Object
A stub method which simply passes through to Capistrano’s #run.
-
#set_user_to(username) ⇒ Object
Forces all connections to switch to the user passed into it.
- #tail_log(logs) ⇒ Object
-
#vc_log ⇒ nil
A stub method which simply returns nil.
-
#verify_variables(required_variables) ⇒ Object
Checks an array of items to see if they are currently set within the Capistrano scope.
Instance Method Details
#_cset(name, *args, &block) ⇒ Object
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_sessions ⇒ Object
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.
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.
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, = {}) 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, 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.
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_filename ⇒ Object
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
121 122 123 124 125 |
# File 'lib/chicken_soup/global.rb', line 121 def remote_directory_exists?(directory, = {}) with_files_check = [: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.
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.
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
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.
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, = {}) abort "#run_task must be passed an `:as` option so that it knows who to change the user to." unless [:as] original_username = exists?(:user) ? user : nil if [:now] set_user_to [:as] find_and_execute_task(task_name) set_user_to original_username else before task_name, "os:users:#{[: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.
212 213 214 |
# File 'lib/chicken_soup/global.rb', line 212 def run_with_ruby_manager(ruby_version, command, = {}) run command, 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.
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_log ⇒ nil
A stub method which simply returns nil. It is meant to be overridden when a version control capability (ie Git) is installed.
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.
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 |