Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 239 lines (194 sloc) 6.555 kb
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
1 require 'osx/cocoa'
2 include OSX
3
cdba86e @alloy Create a tmp dir before touching restart.txt if none exists. [#9 state:r...
alloy authored
4 require 'fileutils'
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
5 require 'yaml'
8b6387c @alloy Refactored all paths to Apache dirs into a central config module Passeng...
alloy authored
6 require File.expand_path('../passenger_pane_config', __FILE__)
e90332d @alloy If the user wants it, we'll add the configuration needed by passenger to...
alloy authored
7 require File.expand_path('../shared_passenger_behaviour', __FILE__)
8
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
9 class PassengerApplication < NSObject
e90332d @alloy If the user wants it, we'll add the configuration needed by passenger to...
alloy authored
10 include SharedPassengerBehaviour
11
4aed8cd @alloy Added functionality to remove an application.
alloy authored
12 CONFIG_UNINSTALLER = File.expand_path('../config_uninstaller.rb', __FILE__)
13 CONFIG_INSTALLER = File.expand_path('../config_installer.rb', __FILE__)
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
14
e52b6b9 @alloy PassengerApplication#application_type checks if a directory contains a R...
alloy authored
15 RAILS = 'rails'
16 RACK = 'rack'
17
3b57e1b @alloy Added stubs for environment and override_rewrite_rules.
alloy authored
18 DEVELOPMENT = 0
19 PRODUCTION = 1
20
ccdd90c @alloy When removing applications do it in one transaction so we don't restart ...
alloy authored
21 class << self
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
22 include SharedPassengerBehaviour
23
24 def existingApplications
09dfc88 @felipemathies Passenger app extension
felipemathies authored
25 Dir.glob(File.join(PassengerPaneConfig::PASSENGER_APPS_DIR, "*.#{PassengerPaneConfig::PASSENGER_APPS_EXTENSION}")).map do |app|
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
26 PassengerApplication.alloc.initWithFile(app)
27 end
28 end
29
ccdd90c @alloy When removing applications do it in one transaction so we don't restart ...
alloy authored
30 def startApplications(apps)
31 data = serializedApplicationsData(apps)
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
32 p "Starting Rails applications:\n#{data}"
33 execute '/usr/bin/ruby', CONFIG_INSTALLER, data
34 apps.each { |app| app.apply(false) }
ccdd90c @alloy When removing applications do it in one transaction so we don't restart ...
alloy authored
35 end
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
36
ccdd90c @alloy When removing applications do it in one transaction so we don't restart ...
alloy authored
37 def removeApplications(apps)
38 data = serializedApplicationsData(apps)
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
39 p "Removing applications: #{data}"
40 execute '/usr/bin/ruby', CONFIG_UNINSTALLER, data
ccdd90c @alloy When removing applications do it in one transaction so we don't restart ...
alloy authored
41 end
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
42
ccdd90c @alloy When removing applications do it in one transaction so we don't restart ...
alloy authored
43 def serializedApplicationsData(apps)
44 apps.to_ruby.map { |app| app.to_hash }.to_yaml
45 end
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
46 end
47
0aaf713 @alloy Removed the "Allow mod rewrite rules" from the UI because it's not an as...
alloy authored
48 kvc_accessor :host, :path, :aliases, :dirty, :valid, :revertable, :environment
d7a2659 @alloy When a PassengerApplication is a new app then set @user_defined_data to ...
alloy authored
49 attr_accessor :user_defined_data, :vhostname
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
50
51 def init
52 if super_init
3b57e1b @alloy Added stubs for environment and override_rewrite_rules.
alloy authored
53 @environment = DEVELOPMENT
54
86f33e9 @alloy Make sure an application is started if it's a new app and restarted if t...
alloy authored
55 @new_app = true
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
56 @dirty = @valid = @revertable = false
e89416a @alloy Added support for ServerAlias and add those entries to the hosts db. [#5...
alloy authored
57 @host, @path, @aliases, @user_defined_data = '', '', '', ''
40c603c @alloy Treat the vhostname "<VirtualHost (.+?)>" as user defined data as well. ...
alloy authored
58 @vhostname = '*:80'
d30ba95 @alloy Made revert work.
alloy authored
59
bf40737 @alloy Added support for RailsBaseURI.
alloy authored
60 set_original_values!
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
61 self
62 end
63 end
64
65 def initWithFile(file)
66 if init
86f33e9 @alloy Make sure an application is started if it's a new app and restarted if t...
alloy authored
67 @new_app = false
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the config a...
alloy authored
68 @valid = false
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
69 load_data_from_vhost_file(file)
bf40737 @alloy Added support for RailsBaseURI.
alloy authored
70 set_original_values!
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
71 self
72 end
73 end
74
00b98a3 @alloy Refactored the default hostname into PassengerApplication. Marked code t...
alloy authored
75 def initWithPath(path)
4978e25 @alloy Small refactor.
alloy authored
76 if init
aa2080f @alloy Made Apply in the UI work for all unsaved apps. When unselecting the pan...
alloy authored
77 mark_dirty!
7a87314 @alloy Mark an application as valid when initialized with a path.
alloy authored
78
c5061e0 @alloy Working on updating the host textfield if a directory was chosen with th...
alloy authored
79 @path = path
80 set_default_host_from_path(path)
d30ba95 @alloy Made revert work.
alloy authored
81
7a87314 @alloy Mark an application as valid when initialized with a path.
alloy authored
82 @valid = true
bf40737 @alloy Added support for RailsBaseURI.
alloy authored
83 set_original_values!
4978e25 @alloy Small refactor.
alloy authored
84 self
85 end
86 end
87
e52b6b9 @alloy PassengerApplication#application_type checks if a directory contains a R...
alloy authored
88 def application_type
89 @application_type ||= check_application_type
90 end
91
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
92 def new_app?; @new_app; end
93 def dirty?; @dirty; end
94 def valid?; @valid; end
95 def revertable?; @revertable; end
d30ba95 @alloy Made revert work.
alloy authored
96
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
97 def apply(save_config = nil)
00a8f36 @alloy Should not save/start/restart a invalid app.
alloy authored
98 unless @valid
99 p "Not applying changes to invalid Rails application: #{@path}"
7a87314 @alloy Mark an application as valid when initialized with a path.
alloy authored
100 return false
00a8f36 @alloy Should not save/start/restart a invalid app.
alloy authored
101 end
102
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
103 p "Applying changes to Rails application: #{@path}"
104 (@new_app ? start : restart) unless save_config == false
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
105 # todo: check if it went ok before assuming so.
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
106 @new_app = self.dirty = self.valid = false
7a87314 @alloy Mark an application as valid when initialized with a path.
alloy authored
107
108 true
e8c5b08 @alloy Don't automatically start/restart apps as this makes it much harder to e...
alloy authored
109 end
110
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
111 def start
4b6595e @alloy Small refactors.
alloy authored
112 p "Starting Rails application: #{@path}"
7edae98 @alloy Changed the vhosts to be in /private/etc/apache2/passenger_vhosts and th...
alloy authored
113 save_config!
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
114 end
115
116 def restart(sender = nil)
117 p "Restarting Rails application: #{@path}"
e89416a @alloy Added support for ServerAlias and add those entries to the hosts db. [#5...
alloy authored
118 if @host != @original_values['host'] || @aliases != @original_values['aliases']
119 execute('/usr/bin/ruby', CONFIG_UNINSTALLER, [@original_values].to_yaml)
120 end
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
121 save_config! if @dirty
cdba86e @alloy Create a tmp dir before touching restart.txt if none exists. [#9 state:r...
alloy authored
122
123 tmp_dir = File.join(@path, 'tmp')
124 FileUtils.mkdir(tmp_dir) unless File.exist?(tmp_dir)
125 Kernel.system("/usr/bin/touch '#{File.join(tmp_dir, 'restart.txt')}'")
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
126 end
86f33e9 @alloy Make sure an application is started if it's a new app and restarted if t...
alloy authored
127
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
128 def revert(sender = nil)
129 @original_values.each do |key, value|
130 send "#{key}=", value
131 end
132 self.valid = self.dirty = self.revertable = false
133 end
134
135 def reload!
136 return if new_app?
137 load_data_from_vhost_file
b32d5a6 @alloy Small cleanup.
alloy authored
138 mark_dirty! if values_changed_after_load?
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
139 set_original_values!
140 self.valid = true
141 end
142
7edae98 @alloy Changed the vhosts to be in /private/etc/apache2/passenger_vhosts and th...
alloy authored
143 def save_config!
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
144 p "Saving configuration: #{config_path}"
7edae98 @alloy Changed the vhosts to be in /private/etc/apache2/passenger_vhosts and th...
alloy authored
145 execute '/usr/bin/ruby', CONFIG_INSTALLER, [to_hash].to_yaml
8ae26eb @alloy Respect custom environments that a user might have set. [#8 state:resolv...
alloy authored
146 set_original_values!
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
147 end
148
149 def config_path
09dfc88 @felipemathies Passenger app extension
felipemathies authored
150 File.join(PassengerPaneConfig::PASSENGER_APPS_DIR, "#{@host}.#{PassengerPaneConfig::PASSENGER_APPS_EXTENSION}")
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
151 end
152
86f33e9 @alloy Make sure an application is started if it's a new app and restarted if t...
alloy authored
153 def rbSetValue_forKey(value, key)
154 super
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
155 self.revertable = true
aa2080f @alloy Made Apply in the UI work for all unsaved apps. When unselecting the pan...
alloy authored
156 mark_dirty!
8ae26eb @alloy Respect custom environments that a user might have set. [#8 state:resolv...
alloy authored
157 @custom_environment = nil if key == 'environment'
e8c5b08 @alloy Don't automatically start/restart apps as this makes it much harder to e...
alloy authored
158 set_default_host_from_path(@path) if key == 'path' && (@host.nil? || @host.empty?) && (!@path.nil? && !@path.empty?)
159 self.valid = (!@host.nil? && !@host.empty? && !@path.nil? && !@path.empty?)
86f33e9 @alloy Make sure an application is started if it's a new app and restarted if t...
alloy authored
160 end
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
161
aa2080f @alloy Made Apply in the UI work for all unsaved apps. When unselecting the pan...
alloy authored
162 def mark_dirty!
163 self.dirty = true
164 PrefPanePassenger.sharedInstance.applicationMarkedDirty self
165 end
166
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
167 def to_hash
d7a2659 @alloy When a PassengerApplication is a new app then set @user_defined_data to ...
alloy authored
168 @user_defined_data = " <directory \"#{File.join(@path.to_s, 'public')}\">\n Order allow,deny\n Allow from all\n </directory>" if @new_app
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the config a...
alloy authored
169 {
e52b6b9 @alloy PassengerApplication#application_type checks if a directory contains a R...
alloy authored
170 'app_type' => application_type,
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the config a...
alloy authored
171 'config_path' => config_path,
172 'host' => @host.to_s,
e89416a @alloy Added support for ServerAlias and add those entries to the hosts db. [#5...
alloy authored
173 'aliases' => @aliases.to_s,
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the config a...
alloy authored
174 'path' => @path.to_s,
8ae26eb @alloy Respect custom environments that a user might have set. [#8 state:resolv...
alloy authored
175 'environment' => (@environment.nil? ? @custom_environment : (@environment == DEVELOPMENT ? 'development' : 'production')),
40c603c @alloy Treat the vhostname "<VirtualHost (.+?)>" as user defined data as well. ...
alloy authored
176 'vhostname' => @vhostname,
34dee4b @alloy Preserve and extra data the user might have added to the vhost.
alloy authored
177 'user_defined_data' => @user_defined_data
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the config a...
alloy authored
178 }
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
179 end
c5061e0 @alloy Working on updating the host textfield if a directory was chosen with th...
alloy authored
180
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
181 private
182
e52b6b9 @alloy PassengerApplication#application_type checks if a directory contains a R...
alloy authored
183 def check_application_type
184 env_file = File.join(@path, 'config', 'environment.rb')
185 (File.exist?(env_file) and File.read(env_file) =~ /Rails::Initializer/) ? RAILS : RACK
186 end
187
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
188 def load_data_from_vhost_file(file = config_path)
189 data = File.read(file).strip
190
66748bc @alloy Fixed bugs in parsing user defined data.
alloy authored
191 data.gsub!(/\n\s*ServerName\s+(.+)/, '')
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
192 self.host = $1
193
e89416a @alloy Added support for ServerAlias and add those entries to the hosts db. [#5...
alloy authored
194 data.gsub!(/\n\s*ServerAlias\s+(.+)/, '')
195 self.aliases = $1 || ''
196
66748bc @alloy Fixed bugs in parsing user defined data.
alloy authored
197 data.gsub!(/\n\s*DocumentRoot\s+"(.+)\/public"/, '')
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
198 self.path = $1
199
8f81b78 @alloy Parse the correct environment from either a Rails or a Rack app vhost fi...
alloy authored
200 data.gsub!(/\n\s*(Rails|Rack)Env\s+(\w+)/, '')
201 if %w{ development production }.include?($2)
202 self.environment = ($2 == 'development' ? DEVELOPMENT : PRODUCTION)
8ae26eb @alloy Respect custom environments that a user might have set. [#8 state:resolv...
alloy authored
203 else
204 self.environment = nil
8f81b78 @alloy Parse the correct environment from either a Rails or a Rack app vhost fi...
alloy authored
205 @custom_environment = $2
8ae26eb @alloy Respect custom environments that a user might have set. [#8 state:resolv...
alloy authored
206 end
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
207
208 data.gsub!(/<VirtualHost\s(.+?)>/, '')
209 self.vhostname = $1
210
66748bc @alloy Fixed bugs in parsing user defined data.
alloy authored
211 data.gsub!(/\s*<\/VirtualHost>\n*/, '').gsub!(/^\n*/, '')
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
212 @user_defined_data = data
d30ba95 @alloy Made revert work.
alloy authored
213 end
214
b32d5a6 @alloy Small cleanup.
alloy authored
215 def values_changed_after_load?
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
216 @original_values.any? do |key, value|
e89416a @alloy Added support for ServerAlias and add those entries to the hosts db. [#5...
alloy authored
217 # user_defined_data and aliases can be empty
218 if %{ user_defined_data aliases }.include?(key) && (value.nil? || value.empty?)
b698809 @alloy Reload the applications from disk when the preference pane is brought to...
alloy authored
219 false
220 else
221 send(key) != value
222 end
223 end
224 end
c5061e0 @alloy Working on updating the host textfield if a directory was chosen with th...
alloy authored
225
bf40737 @alloy Added support for RailsBaseURI.
alloy authored
226 def set_original_values!
e89416a @alloy Added support for ServerAlias and add those entries to the hosts db. [#5...
alloy authored
227 @original_values = {
228 'host' => @host,
229 'aliases' => @aliases,
230 'path' => @path,
8ae26eb @alloy Respect custom environments that a user might have set. [#8 state:resolv...
alloy authored
231 'environment' => @custom_environment || @environment,
e89416a @alloy Added support for ServerAlias and add those entries to the hosts db. [#5...
alloy authored
232 'user_defined_data' => @user_defined_data
233 }
bf40737 @alloy Added support for RailsBaseURI.
alloy authored
234 end
235
c5061e0 @alloy Working on updating the host textfield if a directory was chosen with th...
alloy authored
236 def set_default_host_from_path(path)
1818e3b _ in hostnames in invalid
bryan authored
237 self.host = "#{File.basename(path).downcase.gsub('_','-')}.local"
c5061e0 @alloy Working on updating the host textfield if a directory was chosen with th...
alloy authored
238 end
8ae26eb @alloy Respect custom environments that a user might have set. [#8 state:resolv...
alloy authored
239 end
Something went wrong with that request. Please try again.