public
Description: Phusion Passenger (mod_rails)
Homepage: http://www.modrails.com/
Clone URL: git://github.com/FooBarWidget/passenger.git
Click here to lend your support to: passenger and make a donation at www.pledgie.com !
passenger / bin / passenger-install-apache2-module
100755 300 lines (267 sloc) 7.415 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env ruby
# Phusion Passenger - http://www.modrails.com/
# Copyright (C) 2008 Phusion
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
PASSENGER_ROOT = File.expand_path(File.dirname(__FILE__) << "/..")
$LOAD_PATH.unshift("#{PASSENGER_ROOT}/lib")
 
# The Apache executable may be located in an 'sbin' folder. We add
# the 'sbin' folders to $PATH just in case. On some systems
# 'sbin' isn't in $PATH unless the user is logged in as root from
# the start (i.e. not via 'su' or 'sudo').
ENV["PATH"] += ":/usr/sbin:/sbin:/usr/local/sbin"
 
require 'passenger/platform_info'
require 'passenger/dependencies'
require 'passenger/console_text_template'
include PlatformInfo
 
class Installer
  include Passenger
  
  PASSENGER_WEBSITE = "http://www.modrails.com/"
  PHUSION_WEBSITE = "www.phusion.nl"
  
  REQUIRED_DEPENDENCIES = [
    Dependencies::GCC,
    Dependencies::Ruby_DevHeaders,
    Dependencies::Ruby_OpenSSL,
    Dependencies::RubyGems,
    Dependencies::Rake,
    Dependencies::Apache2,
    Dependencies::Apache2_DevHeaders,
    Dependencies::APR_DevHeaders,
    Dependencies::FastThread
  ]
  
  def start
    if natively_packaged?
      show_apache2_config_snippets
      show_deployment_example
      exit
    end
    
    Dir.chdir(PASSENGER_ROOT)
    show_welcome_screen
    check_dependencies || exit(1)
    check_whether_apache_uses_prefork_mpm
    check_write_permission_to_passenger_root || exit(1)
    if install_apache2_module
      show_apache2_config_snippets
      show_deployment_example
    else
      show_possible_solutions_for_compilation_and_installation_problems
      exit(1)
    end
  ensure
    reset_terminal_colors
  end
 
private
  def init_terminal_colors
    STDOUT.write("\e[0m\e[37m\e[40m")
    STDOUT.flush
  end
  
  def reset_terminal_colors
    STDOUT.write("\e[0m")
    STDOUT.flush
  end
  
  def show_welcome_screen
    render_template 'welcome', :version => PASSENGER_VERSION
    wait
  end
 
  def check_dependencies
    missing_dependencies = []
    color_puts "<banner>Checking for required software...</banner>"
    puts
    REQUIRED_DEPENDENCIES.each do |dep|
      color_print " * #{dep.name}... "
      result = dep.check
      if result.found?
        if result.found_at
          color_puts "<green>found at #{result.found_at}</green>"
        else
          color_puts "<green>found</green>"
        end
      else
        color_puts "<red>not found</red>"
        missing_dependencies << dep
      end
    end
    
    if missing_dependencies.empty?
      return true
    else
      puts
      color_puts "<red>Some required software is not installed.</red>"
      color_puts "But don't worry, this installer will tell you how to install them.\n"
      color_puts "<b>Press Enter to continue, or Ctrl-C to abort.</b>"
      wait
      
      line
      color_puts "<banner>Installation instructions for required software</banner>"
      puts
      missing_dependencies.each do |dep|
        print_dependency_installation_instructions(dep)
        puts
      end
      return false
    end
  end
  
  def check_whether_apache_uses_prefork_mpm
    line
    # 'httpd -V' output is in the form of:
    #
    # Server MPM: Prefork # <--- this line is not always available!
    # ...
    # Server compiled with....
    # -D APACHE_MPM_DIR="server/mpm/prefork"
    output = `#{HTTPD} -V`
    output =~ /^Server MPM: +(.*)$/
    if $1
      mpm = $1.downcase
    else
      output =~ /APACHE_MPM_DIR="server\/mpm\/(.*)"/
      if $1
        mpm = $1.downcase
      else
        mpm = nil
      end
    end
    if mpm != "prefork"
      render_template 'apache_must_be_compiled_with_prefork_mpm',
        :current_mpm => mpm
      wait
    end
  end
  
  def check_write_permission_to_passenger_root
    File.new("__test__.txt", "w").close
    return true
  rescue
    puts
    line
    if Process.uid == 0
      render_template 'no_write_permission_to_passenger_root'
    else
      render_template 'run_installer_as_root'
    end
    return false
  ensure
    File.unlink("__test__.txt") rescue nil
  end
  
  def install_apache2_module
    puts
    line
    color_puts '<banner>Compiling and installing Apache 2 module...</banner>'
    puts "cd #{PASSENGER_ROOT}"
    puts "rake clean apache2"
    return system("rake", "clean", "apache2")
  end
  
  def show_apache2_config_snippets
    puts
    line
    if natively_packaged?
      module_location = "#{PASSENGER_ROOT}/libexec/passenger/mod_passenger.so"
    else
      module_location = "#{PASSENGER_ROOT}/ext/apache2/mod_passenger.so"
    end
    render_template 'apache2_config_snippets',
      :module_location => module_location,
      :spawn_server_location => "#{PASSENGER_ROOT}/bin/passenger-spawn-server",
      :ruby => RUBY
    if natively_packaged?
      wait(10)
    else
      wait
    end
  end
  
  def show_deployment_example
    puts
    line
    render_template 'deployment_example',
      :users_guide => USERS_GUIDE,
      :phusion_website => PHUSION_WEBSITE,
      :passenger_website => PASSENGER_WEBSITE
  end
  
  def show_possible_solutions_for_compilation_and_installation_problems
    puts
    line
    render_template 'possible_solutions_for_compilation_and_installation_problems',
      :users_guide => USERS_GUIDE,
      :passenger_website => PASSENGER_WEBSITE
  end
 
private
  def color_print(text)
    STDOUT.write(ConsoleTextTemplate.new(:text => text).result)
    STDOUT.flush
  end
  
  def color_puts(text)
    color_print("#{text}\n")
  end
  
  def render_template(name, options = {})
    puts ConsoleTextTemplate.new({ :file => name }, options).result
  end
  
  def line
    puts "--------------------------------------------"
  end
  
  def wait(timeout = nil)
    begin
      if timeout
        require 'timeout' unless defined?(Timeout)
        begin
          Timeout.timeout(timeout) do
            STDIN.readline
          end
        rescue Timeout::Error
          # Do nothing.
        end
      else
        STDIN.readline
      end
    rescue Interrupt
      exit 2
    end
  end
  
  def print_dependency_installation_instructions(dep)
    color_puts " * To install <yellow>#{dep.name}</yellow>:"
    if !dep.install_command.nil?
      color_puts " Please run <b>#{dep.install_command}</b> as root."
    elsif !dep.install_instructions.nil?
      color_puts " " << dep.install_instructions
    elsif !dep.website.nil?
      color_puts " Please download it from <b>#{dep.website}</b>"
      if !dep.website_comments.nil?
        color_puts " (#{dep.website_comments})"
      end
    else
      color_puts " Search Google."
    end
  end
  
  def natively_packaged?
    return self.class.natively_packaged?
  end
  
  def self.natively_packaged?
    return File.expand_path(File.dirname(__FILE__)) == "/usr/bin"
  end
  
  def self.determine_version
    if natively_packaged?
      require 'rbconfig'
      return File.read("/etc/passenger_version.txt")
    else
      File.read("#{PASSENGER_ROOT}/Rakefile") =~ /^PACKAGE_VERSION = "(.*)"$/
      return $1
    end
  end
  
  def self.determine_users_guide
    if natively_packaged?
      return "/usr/share/doc/passenger/Users guide.html"
    else
      return "#{PASSENGER_ROOT}/doc/Users guide.html"
    end
  end
  
  PASSENGER_VERSION = determine_version
  USERS_GUIDE = determine_users_guide
end
 
Installer.new.start