We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Fork of alloy/passengerpane
Clone URL: git://github.com/bryanl/passengerpane.git
passengerpane / config_installer.rb
100755 91 lines (76 sloc) 2.391 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
#!/usr/bin/env ruby
 
require 'osx/cocoa'
require 'yaml'
require 'fileutils'
 
class String
  def bypass_safe_level_1
    str = dup
    str.untaint
    str
  end
end
 
class ConfigInstaller
  attr_reader :data
  
  def initialize(yaml_data, extra_command = nil)
    @data = YAML.load(yaml_data)
    @extra_command = extra_command
  end
  
  def add_to_hosts(index)
    host = @data[index]['host']
    OSX::NSLog("Will add host: #{host}")
    system "/usr/bin/dscl localhost -create /Local/Default/Hosts/#{host.bypass_safe_level_1} IPAddress 127.0.0.1"
  end
  
  VHOSTS_DIR = "/private/etc/apache2/passenger_pane_vhosts"
  def verify_vhost_conf
    unless File.exist? VHOSTS_DIR
      OSX::NSLog("Will create directory: #{VHOSTS_DIR}")
      FileUtils.mkdir_p VHOSTS_DIR
    end
  end
  
  CONF = "/private/etc/apache2/httpd.conf"
  def verify_httpd_conf
    unless File.read(CONF).include? 'Include /private/etc/apache2/passenger_pane_vhosts/*.conf'
      OSX::NSLog("Will try to append passenger pane vhosts conf to: #{CONF}")
      File.open(CONF, 'a') do |f|
        f << %{
 
# Added by the Passenger preference pane
# Make sure to include the Passenger configuration (the LoadModule,
# PassengerRoot, and PassengerRuby directives) before this section.
<IfModule passenger_module>
NameVirtualHost *:80
Include /private/etc/apache2/passenger_pane_vhosts/*.conf
</IfModule>}
      end
    end
  end
  
  def create_vhost_conf(index)
    app = @data[index]
    public_dir = File.join(app['path'], 'public')
    vhost = %{
<VirtualHost #{app['vhostname']}>
ServerName #{app['host']}
DocumentRoot "#{public_dir}"
RailsEnv #{app['environment']}
RailsAllowModRewrite #{app['allow_mod_rewrite'] ? 'on' : 'off'}
#{ "#{app['user_defined_data']}\n" unless app['user_defined_data'].empty? }</VirtualHost>
}.sub(/^\n/, '')
    
    OSX::NSLog("Will write vhost file: #{app['config_path']}\nData: #{vhost}")
    File.open(app['config_path'].bypass_safe_level_1, 'w') { |f| f << vhost }
  end
  
  def restart_apache!
    system "sudo /bin/launchctl stop org.apache.httpd"
  end
  
  def install!
    verify_vhost_conf
    verify_httpd_conf
    
    (0..(@data.length - 1)).each do |index|
      add_to_hosts index
      create_vhost_conf index
    end
    
    restart_apache!
  end
end
 
if $0 == __FILE__
  OSX::NSLog("Will try to write config(s).")
  ConfigInstaller.new(*ARGV).install!
end