public
Description: My set of personal Thor scripts.
Homepage:
Clone URL: git://github.com/crnixon/thor_tasks.git
David Eisinger (author)
Thu Jan 22 12:47:08 -0800 2009
crnixon (committer)
Fri Jan 23 07:53:16 -0800 2009
commit  1c552b331ef1a694d23edce83ef82b0943ecd467
tree    2006d63a09a615dae483e1c918efde1dd07848f3
parent  98485c0651211a1351e634695e768411a846a9c3
thor_tasks / provision_ubuntu.thor
100644 122 lines (100 sloc) 3.561 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# module: provision_ubuntu
 
# This script is meant to be used to provision new Ubuntu servers for Rails
# and Rack apps using Phusion Passenger.
#
# Things this does:
# - installs Ruby and Rubygems
# - installs Apache 2
# - installs MySQL
# - installs Passenger
# - adds a Passenger Apache conf and enables the module
# - installs logrotate, git, and git-svn
# - installs RMagick
# - installs Rails
# Optional:
# - installs god, and creates an init file for it
 
require 'open-uri'
require 'hpricot'
 
class Provision < Thor
  def initialize(*args)
    raise "You must have provision_base installed." unless defined?(BASE_LOADED)
    super(*args)
  end
  
  desc "ubuntu SERVER", "Remotely provision an Ubuntu server for " +
    "running Rails and Rack apps with Phusion Passenger. You must be in " +
    "the sudoers file on the remote server."
  method_options :user => :optional, :god => :boolean
  def ubuntu(server)
    get_user_and_password(options)
    get_ubuntu_cap(server).provision
  end
  
  private
  
  def get_ubuntu_cap(server)
    cap = Capistrano::Configuration.new
    cap.logger.level = Capistrano::Logger::TRACE
    cap.set :user, @user
    cap.set :password, @password
    
    cap.role :app, server
    
    cap.task :provision do
      install_ubuntu_env
      install_ruby
      install_apache
      install_mysql
      install_passenger
    end
    
    cap.task :install_ubuntu_env do
      pkgs = %w(build-essential libssl-dev libreadline5-dev zlib1g-dev
        curl git-core git-svn)
      sudo 'aptitude update'
      sudo 'aptitude -y -q full-upgrade'
      sudo "aptitude -y -q install #{pkgs.join(' ')}"
    end
    
    cap.task :install_ruby do
      # get latest Ruby Enterprise Edition
      doc = open('http://rubyforge.org/frs/?group_id=5833') { |f| Hpricot(f) }
      link = (doc/'a').detect { |link| link['href'] =~ /\.tar\.gz$/ }['href']
 
      run "wget -nv http://rubyforge.org#{link}"
      run "tar xfz ruby-enterprise-*.tar.gz"
 
      sudo("./ruby-enterprise-*/installer", :pty => true) do |ch,stream,out|
        next if out.chomp == ''
        print out
        ch.send_data(input = "\n") if out =~ /enter/i
      end
 
      sudo "ln -s /opt/ruby-enterprise-* /opt/ruby-enterprise"
      sudo "ln -s /opt/ruby-enterprise/bin/* /usr/local/bin/"
    end
    
    cap.task :install_apache do
      pkgs = %w(apache2-mpm-prefork apache2-prefork-dev)
      sudo "aptitude -y -q install #{pkgs.join(' ')}"
      sudo "a2enmod rewrite"
    end
    
    cap.task :install_mysql do
      pkgs = %w(libmysql++-dev mysql-server)
      sudo "aptitude -y -q install #{pkgs.join(' ')}"
    end
        
    cap.task :install_passenger do
      sudo("/opt/ruby-enterprise/bin/passenger-install-apache2-module",
        :pty => true) do |ch,stream,out|
        next if out.chomp == ''
        print out
        ch.send_data(input = "\n") if out =~ /enter/i
      end
      passenger_root = capture('passenger-config --root').chomp
      sudo "rm -f /tmp/passenger.*"
      put PASSENGER_LOAD.gsub(/ROOT/, passenger_root), '/tmp/passenger.load'
      put PASSENGER_CONF.gsub(/ROOT/, passenger_root), '/tmp/passenger.conf'
      sudo "mv /tmp/passenger.* /etc/apache2/mods-available"
      sudo "a2enmod passenger"
      sudo "/etc/init.d/apache2 force-reload"
    end
        
    cap
  end
  
  PASSENGER_LOAD = <<EOF
LoadModule passenger_module ROOT/ext/apache2/mod_passenger.so
EOF
 
  PASSENGER_CONF = <<EOF
PassengerRoot ROOT
PassengerRuby /opt/ruby-enterprise/bin/ruby
PassengerDefaultUser www-data
EOF
end