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
-
#remote_rake(rake_command, options = {}) ⇒ Object
Runs a rake command remotely.
- #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
63 64 65 |
# File 'lib/baptize/helpers.rb', line 63 def current_role ENV['ROLES'].to_sym end |
#escape_sed_arg(s) ⇒ Object
93 94 95 |
# File 'lib/baptize/helpers.rb', line 93 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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/baptize/helpers.rb', line 67 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 if fetch(:use_sudo, true) [:shell] = 'sudo bash' else [:shell].gsub!(/^sudo /, "") end end end |
#md5_of_file(path, md5) ⇒ Object
89 90 91 |
# File 'lib/baptize/helpers.rb', line 89 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 |
#remote_rake(rake_command, options = {}) ⇒ Object
Runs a rake command remotely
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/baptize/helpers.rb', line 29 def remote_rake(rake_command, = {}) = {:bundle_cmd => fetch(:bundle_cmd, "bundle"), :current_path => current_path, :rails_env => rails_env, :env => {}, :pty => false}.merge() command = "" command << "cd #{[:current_path]} && " if [:current_path] command << "RAILS_ENV=#{[:rails_env]} " if [:rails_env] [:env].each do |k,v| command << "#{k}=#{v.shellescape} " end if [:bundle_cmd] command << "#{[:bundle_cmd]} exec rake #{rake_command}" else command << "rake #{rake_command}" end [:norun] ? command : run(command, :pty => [:pty]) end |
#render(path, locals = {}) ⇒ Object
101 102 103 104 105 |
# File 'lib/baptize/helpers.rb', line 101 def render(path, locals = {}) require 'erb' require 'ostruct' ERB.new(File.read(path)).result(locals.kind_of?(Binding) ? locals : OpenStruct.new(locals).instance_eval { binding }) end |
#replace_text(pattern, replacement, path) ⇒ Object
97 98 99 |
# File 'lib/baptize/helpers.rb', line 97 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
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/baptize/helpers.rb', line 47 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 |