Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#ask(message, default = true) ⇒ Object

Prompts the user for a message to agree/decline



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

def ask(message, default=true)
  Capistrano::CLI.ui.agree(message)
end

#docs_pathObject



44
45
46
# File 'lib/helpers.rb', line 44

def docs_path
  expanded_path_for('../doc')
end

#environmentObject

automatically sets the environment based on presence of :stage (multistage gem), :rails_env, or RAILS_ENV variable; otherwise defaults to ‘production’



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

def environment
  if exists?(:stage)
    stage
  elsif exists?(:rails_env)
    rails_env
  elsif(ENV['RAILS_ENV'])
    ENV['RAILS_ENV']
  else
    "production"
  end
end

#expanded_path_for(path) ⇒ Object



48
49
50
51
# File 'lib/helpers.rb', line 48

def expanded_path_for(path)
  e = File.join(File.dirname(__FILE__),path)
  File.expand_path(e)
end

#generate_config(local_file, remote_file) ⇒ Object

Generates a configuration file parsing through ERB Fetches local file and uploads it to remote_file Make sure your user has the right permissions.



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

def generate_config(local_file,remote_file)
  temp_file = '/tmp/' + File.basename(local_file)
  buffer    = parse_config(local_file)
  File.open(temp_file, 'w+') { |f| f << buffer }
  upload temp_file, remote_file, :via => :scp
  `rm #{temp_file}`
end

#install_gems(ssh) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'bin/ricodigo-install-server', line 93

def install_gems(ssh)
  puts ">> Installing required gems..."

  command = %@
echo "gem: --no-ri --no-rdoc" >> /etc/gemrc
echo "update: --no-ri --no-rdoc" >> /etc/gemrc
echo "install: --no-ri --no-rdoc" >> /etc/gemrc
@
  ssh.exec!("sudo bash -c '#{command}'") {|c,s,d| puts d }

  gems = %w[
    rails mongoid whenever capistrano unicorn mongoid_ext magent
    bson_ext sanitize uuidtools ruby-stemmer mini_magick magic
    haml sass compass mechanize nokogiri rdiscount
  ]

  ssh.exec! "rvmsudo gem install #{gems.join(" ")} --no-ri --no-rdoc" do |channel, stream, data|
    puts data
  end
end

#install_packages(ssh) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'bin/ricodigo-install-server', line 30

def install_packages(ssh)
  puts ">> Installing packages..."
  packages = %w[
    bind9 build-essential ufw libreadline6-dev git-core
    libxslt1-dev libxml2-dev libssl-dev nginx mongodb-10gen curl
    libcurl4-openssl-dev graphicsmagick openjdk-6-jre-headless libsasl2-dev
  ]

  ssh.exec! "sudo apt-get install -y --force-yes #{packages.join(" ")}" do |channel, stream, data|
    puts data
  end
end

#install_postfix(ssh) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'bin/ricodigo-install-server', line 43

def install_postfix(ssh)
  ssh.exec!("sudo apt-get install postfix -y --force-yes") do |channel, stream, data|
    puts data
    if data =~ /General type of mail configuration:\s*$/
      answer = $stdin.gets
      channel.send_data(answer)
    elsif data =~ /System mail name:\s*$/
      answer = $stdin.gets
      channel.send_data(answer)
    elsif data =~ /Root and postmaster mail recipient:\s*$/
      answer = $stdin.gets
      channel.send_data(answer)
    elsif data =~ /Other destinations to accept mail for \(blank for none\):\s*$/
      answer = $stdin.gets
      channel.send_data(answer)
    elsif data =~ /Force synchronous updates on mail queue\?\s*$/
      channel.send_data("no\n")
    elsif data =~ /Local networks:\s*$/
      channel.send_data("\n")
    elsif data =~ /Mailbox size limit \(bytes\):\s*$/
      channel.send_data("0\n")
    elsif data =~ /Local address extension character:\s*$/
      channel.send_data("\n")
    elsif data =~ /Internet protocols to use:\s*$/
      channel.send_data("1\n")
    end
  end
end

#install_rvm(ssh) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'bin/ricodigo-install-server', line 72

def install_rvm(ssh)
  puts "Installing RVM..."

  ssh.exec!("sudo bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)") {|c,s,d| puts d }

  command = %@
mv /etc/bash.bashrc /tmp/rd_bash.bashrc -f
echo ". /etc/rvmrc" >> /etc/bash.bashrc
echo ". /usr/local/rvm/scripts/rvm" >> /etc/bash.bashrc
cat /tmp/rd_bash.bashrc >> /etc/bash.bashrc
source /etc/rvmrc
source /usr/local/rvm/scripts/rvm

rvm install 1.9.2

rvm use 1.9.2 --default
@

  ssh.exec!("sudo bash -c '#{command}'") {|c,s,d| puts d }
end

#is_app_monitored?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/helpers.rb', line 31

def is_app_monitored?
  is_using('bluepill', :monitorer)
end

#is_using(something, with_some_var) ⇒ Object



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

def is_using(something, with_some_var)
 exists?(with_some_var.to_sym) && fetch(with_some_var.to_sym).to_s.downcase == something
end

#is_using_nginxObject



19
20
21
# File 'lib/helpers.rb', line 19

def is_using_nginx
  is_using('nginx',:web_server)
end

#is_using_passengerObject



23
24
25
# File 'lib/helpers.rb', line 23

def is_using_passenger
  is_using('passenger',:app_server)
end

#is_using_unicornObject



27
28
29
# File 'lib/helpers.rb', line 27

def is_using_unicorn
  is_using('unicorn',:app_server)
end

#parse_config(file) ⇒ Object



53
54
55
56
57
# File 'lib/helpers.rb', line 53

def parse_config(file)
  require 'erb'  #render not available in Capistrano 2
  template=File.read(file)          # read it
  return ERB.new(template).result(binding)   # parse it
end

#run_rake(task) ⇒ Object

Executes a basic rake task. Example: run_rake log:clear



81
82
83
# File 'lib/helpers.rb', line 81

def run_rake(task)
  run "cd #{current_path} && rake #{task} RAILS_ENV=#{environment}"
end

#rvmsudo(task) ⇒ Object



86
87
88
# File 'lib/helpers.rb', line 86

def rvmsudo(task)
  run "cd #{current_path} && rvmsudo #{task}"
end

#setup_repos(ssh) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'bin/ricodigo-install-server', line 14

def setup_repos(ssh)
  puts ">> Adding extra repos.."
  cmd = 'echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" > /etc/apt/sources.list.d/mongo.list'
  ssh.exec! %@sudo bash -c '#{cmd}'@ do |c,s,d|
    puts d
  end

  ssh.exec! 'sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10' do |c,s,d|
    puts d
  end

  ssh.exec! "sudo apt-get update" do |c,s,d|
    puts d
  end
end

#templates_pathObject

Path to where the generators live



40
41
42
# File 'lib/helpers.rb', line 40

def templates_path
  expanded_path_for('../generators')
end