Top Level Namespace

Defined Under Namespace

Modules: Capistrano, TeknobingoRecipes

Instance Method Summary collapse

Instance Method Details

#copy_file(from, to, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/teknobingo_recipes/helpers.rb', line 5

def copy_file(from,to,options={})
  tmpname = '/tmp/knutervrang'
  fname = ["../config/recipes/#{stage}/#{from}", "../config/recipes/#{from}", "#{Gem.loaded_specs['teknobingo-recipes'].full_gem_path}/lib/recipes/#{from}"].keep_if do |path|
    fname = File.expand_path(path, ENV['BUNDLE_GEMFILE'])
    fname if File.exists? fname
  end.first
  put File.read(File.expand_path(fname, ENV['BUNDLE_GEMFILE'])), tmpname, options
  run "#{sudo} mv #{tmpname} #{to}"
end

#enrich_file(filename, content, search_expr = nil) ⇒ Object



42
43
44
45
46
47
# File 'lib/teknobingo_recipes/helpers.rb', line 42

def enrich_file(filename, content, search_expr = nil)
  content = content.gsub("'", "'\\\\''").gsub("\n", '\n')
  search_expr ||= content
  run "[ -e '#{filename}' ] || touch '#{filename}'"
  run "if grep -q '#{search_expr}' #{filename}; then echo ok; else echo '#{content}' >> #{filename}; fi"
end

#has_servers?(options) ⇒ Boolean Also known as: has_server?

Returns:

  • (Boolean)


29
30
31
# File 'lib/teknobingo_recipes/helpers.rb', line 29

def has_servers?(options)
  find_servers(options).size > 0
end

#remote_file_exists?(full_path) ⇒ Boolean

Check if remote file exists

Returns:

  • (Boolean)


8
9
10
# File 'lib/teknobingo_recipes/recipes/rainbows.rb', line 8

def remote_file_exists?(full_path)
  'true' ==  capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip
end

#remote_process_exists?(pid_file) ⇒ Boolean

Check if process is running

Returns:

  • (Boolean)


13
14
15
# File 'lib/teknobingo_recipes/recipes/rainbows.rb', line 13

def remote_process_exists?(pid_file)
  capture("ps -p $(cat #{pid_file}) ; true").strip.split("\n").size == 2
end

#replace_line_in_file(filename, search_expr, new_line) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/teknobingo_recipes/helpers.rb', line 49

def replace_line_in_file(filename, search_expr, new_line)
  search_expr = search_expr.gsub("'", "'\\\\''").gsub("\n", '\n')
  new_line = new_line.gsub("'", "'\\\\''").gsub("\n", '\n')
  run "#{sudo} cp '#{filename}' '#{filename}.bak'" # keep permissions
  run "#{sudo} sh -c \"sed 's/^#{search_expr}$/#{new_line}/' < '#{filename}.bak' > '#{filename}'\""
  run "#{sudo} rm '#{filename}.bak'"
end

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



1
2
3
# File 'lib/teknobingo_recipes/helpers.rb', line 1

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

#sudo_enrich_file(filename, content, search_expr = nil) ⇒ Object

Add text to fil unless it is already there



36
37
38
39
40
41
# File 'lib/teknobingo_recipes/helpers.rb', line 36

def sudo_enrich_file(filename, content, search_expr = nil)
  content = content.gsub("'", "'\\\\''").gsub("\n", '\n')
  search_expr ||= content
  run "#{sudo} sh -c \"[ -e '#{filename}' ] || touch '#{filename}'\""
  run "#{sudo} sh -c \"if grep -q '#{search_expr}' #{filename}; then echo ok; else echo '#{content}' >> #{filename}; fi\""
end

#template(from, to) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/teknobingo_recipes/helpers.rb', line 19

def template(from, to)
  fname = ["../config/recipes/templates/#{stage}/#{from}", "../config/recipes/templates/#{from}", "#{Gem.loaded_specs['teknobingo-recipes'].full_gem_path}/templates/#{from}"].keep_if do |path|
    fname = File.expand_path(path, ENV['BUNDLE_GEMFILE'])
    fname if File.exists? fname
  end.first
  fname = File.expand_path(fname, ENV['BUNDLE_GEMFILE'])
  erb = File.read(fname)
  put ERB.new(erb).result(binding), to
end