Module: Capistrano::Baptize::Helpers

Defined in:
lib/baptize/helpers.rb

Instance Method Summary collapse

Instance Method Details

#asset_path(asset) ⇒ Object



5
6
7
# File 'lib/baptize/helpers.rb', line 5

def asset_path(asset)
  File.join(fetch(:assets_path), asset)
end

#current_roleObject



36
37
38
# File 'lib/baptize/helpers.rb', line 36

def current_role
  ENV['ROLES'].to_sym
end

#escape_sed_arg(s) ⇒ Object



61
62
63
# File 'lib/baptize/helpers.rb', line 61

def escape_sed_arg(s)
  s.gsub("'", "'\\\\''").gsub("\n", '\n').gsub("/", "\\\\/").gsub('&', '\\\&')
end

#load_configuration(environment) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/baptize/helpers.rb', line 40

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



57
58
59
# File 'lib/baptize/helpers.rb', line 57

def md5_of_file(path, md5)
  remote_assert "test $(md5sum #{path.shellescape} | cut -f1 -d' ') = #{md5.shellescape}"
end

#remote_assert(command) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/baptize/helpers.rb', line 9

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



69
70
71
72
73
# File 'lib/baptize/helpers.rb', line 69

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



65
66
67
# File 'lib/baptize/helpers.rb', line 65

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/baptize/helpers.rb', line 20

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