Module: Capistrano::Baptize::Helpers
- Defined in:
- lib/baptize/helpers.rb
Instance Method Summary collapse
- #asset_path(asset) ⇒ Object
- #current_role ⇒ Object
- #escape_sed_arg(s) ⇒ Object
- #for_roles(*roles, &block) ⇒ Object
- #load_configuration(environment) ⇒ Object
- #md5_of_file(path, md5) ⇒ Object
- #remote_assert(command) ⇒ Object
- #render(path, locals = {}) ⇒ Object
- #replace_text(pattern, replacement, path) ⇒ Object
-
#run_locally(cmd) ⇒ Object
logs the command then executes it locally.
Instance Method Details
#asset_path(asset) ⇒ Object
15 16 17 |
# File 'lib/baptize/helpers.rb', line 15 def asset_path(asset) File.join("#{capistrano_path}/assets", asset) end |
#current_role ⇒ Object
46 47 48 |
# File 'lib/baptize/helpers.rb', line 46 def current_role ENV['ROLES'].to_sym end |
#escape_sed_arg(s) ⇒ Object
71 72 73 |
# File 'lib/baptize/helpers.rb', line 71 def escape_sed_arg(s) s.gsub("'", "'\\\\''").gsub("\n", '\n').gsub("/", "\\\\/").gsub('&', '\\\&') end |
#for_roles(*roles, &block) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/baptize/helpers.rb', line 6 def for_roles(*roles, &block) old_env_roles = ENV['ROLES'] ENV['ROLES'] = Array(roles).flatten.map(&:to_s).join(",") logger.info "Invoking for roles: #{ENV['ROLES']}" block.call ensure ENV['ROLES'] = old_env_roles end |
#load_configuration(environment) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/baptize/helpers.rb', line 50 def load_configuration(environment) top.instance_eval do if environment == :all Dir.glob("#{capistrano_path}/config/**/*.rb").each do |conf| load(conf) end else Dir.glob("#{capistrano_path}/config/#{environment}.rb").each do |conf| load(conf) end Dir.glob("#{capistrano_path}/config/#{environment}/**/*.rb").each do |conf| load(conf) end end end end |
#md5_of_file(path, md5) ⇒ Object
67 68 69 |
# File 'lib/baptize/helpers.rb', line 67 def md5_of_file(path, md5) remote_assert "test $(md5sum #{path.shellescape} | cut -f1 -d' ') = #{md5.shellescape}" end |
#remote_assert(command) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/baptize/helpers.rb', line 19 def remote_assert(command) results = [] pipeline = Array(command).flatten.map {|c| "#{c} > /dev/null 2> /dev/null ; if [ $? -eq 0 ] ; then echo -n 'true' ; else echo -n 'false' ; fi" }.join(" ; ") invoke_command(pipeline) do |ch, stream, out| results << (out == 'true') end results.all? end |
#render(path, locals = {}) ⇒ Object
79 80 81 82 83 |
# File 'lib/baptize/helpers.rb', line 79 def render(path, locals = {}) require 'erb' require 'ostruct' ERB.new(File.read(path)).result(OpenStruct.new(locals).instance_eval { binding }) end |
#replace_text(pattern, replacement, path) ⇒ Object
75 76 77 |
# File 'lib/baptize/helpers.rb', line 75 def replace_text(pattern, replacement, path) run "sed -i 's/#{escape_sed_arg(pattern)}/#{escape_sed_arg(replacement)}/g' #{path.shellescape}" end |
#run_locally(cmd) ⇒ Object
logs the command then executes it locally. returns the command output as a string
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/baptize/helpers.rb', line 30 def run_locally(cmd) if dry_run return logger.debug "executing locally: #{cmd.inspect}" end logger.trace "executing locally: #{cmd.inspect}" if logger output_on_stdout = nil elapsed = Benchmark.realtime do output_on_stdout = `#{cmd}` end if $?.to_i > 0 # $? is command exit code (posix style) raise Capistrano::LocalArgumentError, "Command #{cmd} returned status code #{$?}" end logger.trace "command finished in #{(elapsed * 1000).round}ms" if logger output_on_stdout end |