Skip to content
Newer
Older
100644 209 lines (163 sloc) 5.28 KB
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
1 require 'osx/cocoa'
2 include OSX
3
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
4 require 'yaml'
e90332d @alloy If the user wants it, we'll add the configuration needed by passenger…
alloy authored
5 require File.expand_path('../shared_passenger_behaviour', __FILE__)
6
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
7 class PassengerApplication < NSObject
e90332d @alloy If the user wants it, we'll add the configuration needed by passenger…
alloy authored
8 include SharedPassengerBehaviour
9
4aed8cd @alloy Added functionality to remove an application.
alloy authored
10 CONFIG_UNINSTALLER = File.expand_path('../config_uninstaller.rb', __FILE__)
11 CONFIG_INSTALLER = File.expand_path('../config_installer.rb', __FILE__)
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
12
3b57e1b @alloy Added stubs for environment and override_rewrite_rules.
alloy authored
13 DEVELOPMENT = 0
14 PRODUCTION = 1
15
ccdd90c @alloy When removing applications do it in one transaction so we don't resta…
alloy authored
16 class << self
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
17 include SharedPassengerBehaviour
18
19 def existingApplications
20 Dir.glob(File.join(PASSENGER_APPS_DIR, '*.vhost.conf')).map do |app|
21 PassengerApplication.alloc.initWithFile(app)
22 end
23 end
24
ccdd90c @alloy When removing applications do it in one transaction so we don't resta…
alloy authored
25 def startApplications(apps)
26 data = serializedApplicationsData(apps)
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
27 p "Starting Rails applications:\n#{data}"
28 execute '/usr/bin/ruby', CONFIG_INSTALLER, data
29 apps.each { |app| app.apply(false) }
ccdd90c @alloy When removing applications do it in one transaction so we don't resta…
alloy authored
30 end
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
31
ccdd90c @alloy When removing applications do it in one transaction so we don't resta…
alloy authored
32 def removeApplications(apps)
33 data = serializedApplicationsData(apps)
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
34 p "Removing applications: #{data}"
35 execute '/usr/bin/ruby', CONFIG_UNINSTALLER, data
ccdd90c @alloy When removing applications do it in one transaction so we don't resta…
alloy authored
36 end
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
37
ccdd90c @alloy When removing applications do it in one transaction so we don't resta…
alloy authored
38 def serializedApplicationsData(apps)
39 apps.to_ruby.map { |app| app.to_hash }.to_yaml
40 end
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
41 end
42
bf40737 @alloy Added support for RailsBaseURI.
alloy authored
43 kvc_accessor :host, :path, :dirty, :valid, :environment, :allow_mod_rewrite, :base_uri
d7a2659 @alloy When a PassengerApplication is a new app then set @user_defined_data …
alloy authored
44 attr_accessor :user_defined_data, :vhostname
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
45
46 def init
47 if super_init
3b57e1b @alloy Added stubs for environment and override_rewrite_rules.
alloy authored
48 @environment = DEVELOPMENT
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the confi…
alloy authored
49 @allow_mod_rewrite = false
3b57e1b @alloy Added stubs for environment and override_rewrite_rules.
alloy authored
50
86f33e9 @alloy Make sure an application is started if it's a new app and restarted i…
alloy authored
51 @new_app = true
e8c5b08 @alloy Don't automatically start/restart apps as this makes it much harder t…
alloy authored
52 @dirty = @valid = false
d7d939a @alloy Allow the user to modify the vhost conf by hand. WIP.
alloy authored
53 @host, @path, @base_uri, @user_defined_data = '', '', '', ''
40c603c @alloy Treat the vhostname "<VirtualHost (.+?)>" as user defined data as wel…
alloy authored
54 @vhostname = '*:80'
d30ba95 @alloy Made revert work.
alloy authored
55
bf40737 @alloy Added support for RailsBaseURI.
alloy authored
56 set_original_values!
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
57 self
58 end
59 end
60
61 def initWithFile(file)
62 if init
86f33e9 @alloy Make sure an application is started if it's a new app and restarted i…
alloy authored
63 @new_app = false
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the confi…
alloy authored
64 @valid = false
d7d939a @alloy Allow the user to modify the vhost conf by hand. WIP.
alloy authored
65
7084e6f @alloy Fixed bug in regexp that ate an extra line becuase the matcher was to…
alloy authored
66 data = File.read(file).strip
d30ba95 @alloy Made revert work.
alloy authored
67
d7d939a @alloy Allow the user to modify the vhost conf by hand. WIP.
alloy authored
68 data.gsub!(/\s*ServerName\s+(.+)\n/, '')
69 @host = $1
70
71 data.gsub!(/\s*DocumentRoot\s+"(.+)\/public"\n/, '')
72 @path = $1
73
74 data.gsub!(/\s*RailsEnv\s+(development|production)\n/, '')
75 @environment = ($1 == 'development' ? DEVELOPMENT : PRODUCTION)
76
77 data.gsub!(/\s*RailsAllowModRewrite\s+(off|on)\n/, '')
78 @allow_mod_rewrite = ($1 == 'on')
79
80 data.gsub!(/\s*RailsBaseURI\s+(.+)\n/, '')
81 @base_uri = $1 unless $1.nil?
82
7084e6f @alloy Fixed bug in regexp that ate an extra line becuase the matcher was to…
alloy authored
83 data.gsub!(/<VirtualHost\s(.+?)>/, '')
40c603c @alloy Treat the vhostname "<VirtualHost (.+?)>" as user defined data as wel…
alloy authored
84 @vhostname = $1
85
d7d939a @alloy Allow the user to modify the vhost conf by hand. WIP.
alloy authored
86 data.gsub!(/\s*<\/VirtualHost>\n*/, '')
87 @user_defined_data = data
bf40737 @alloy Added support for RailsBaseURI.
alloy authored
88
89 set_original_values!
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
90 self
91 end
92 end
93
00b98a3 @alloy Refactored the default hostname into PassengerApplication. Marked cod…
alloy authored
94 def initWithPath(path)
4978e25 @alloy Small refactor.
alloy authored
95 if init
aa2080f @alloy Made Apply in the UI work for all unsaved apps. When unselecting the …
alloy authored
96 mark_dirty!
7a87314 @alloy Mark an application as valid when initialized with a path.
alloy authored
97
c5061e0 @alloy Working on updating the host textfield if a directory was chosen with…
alloy authored
98 @path = path
99 set_default_host_from_path(path)
d30ba95 @alloy Made revert work.
alloy authored
100
7a87314 @alloy Mark an application as valid when initialized with a path.
alloy authored
101 @valid = true
bf40737 @alloy Added support for RailsBaseURI.
alloy authored
102 set_original_values!
4978e25 @alloy Small refactor.
alloy authored
103 self
104 end
105 end
106
82e62e9 @alloy Open the browse panel once a new app is added.
alloy authored
107 def new_app?
108 @new_app
109 end
110
fcb0389 @alloy Started on the delegate methdos for the ]]tableview which check if an…
alloy authored
111 def dirty?
112 @dirty
113 end
114
d30ba95 @alloy Made revert work.
alloy authored
115 def valid?
116 @valid
117 end
118
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
119 def apply(save_config = nil)
00a8f36 @alloy Should not save/start/restart a invalid app.
alloy authored
120 unless @valid
121 p "Not applying changes to invalid Rails application: #{@path}"
7a87314 @alloy Mark an application as valid when initialized with a path.
alloy authored
122 return false
00a8f36 @alloy Should not save/start/restart a invalid app.
alloy authored
123 end
124
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
125 p "Applying changes to Rails application: #{@path}"
126 (@new_app ? start : restart) unless save_config == false
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the confi…
alloy authored
127 # todo: check if it went ok before assumin so.
eb0351c @alloy Refactored/cleaned code from yesterday evening.
alloy authored
128 @new_app = self.dirty = self.valid = false
7a87314 @alloy Mark an application as valid when initialized with a path.
alloy authored
129
130 true
e8c5b08 @alloy Don't automatically start/restart apps as this makes it much harder t…
alloy authored
131 end
132
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
133 def start
4b6595e @alloy Small refactors.
alloy authored
134 p "Starting Rails application: #{@path}"
7edae98 @alloy Changed the vhosts to be in /private/etc/apache2/passenger_vhosts and…
alloy authored
135 save_config!
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
136 end
137
138 def restart(sender = nil)
139 p "Restarting Rails application: #{@path}"
0532edb @alloy When a host has been changed first remove the old app before writing …
alloy authored
140 execute('/usr/bin/ruby', CONFIG_UNINSTALLER, [@original_values].to_yaml) unless @host == @original_values['host']
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
141 save_config! if @dirty
76c1df7 @alloy Verified that restarting the application works. Cleaned UI and code.
alloy authored
142 Kernel.system("/usr/bin/touch '#{File.join(@path, 'tmp', 'restart.txt')}'")
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
143 end
86f33e9 @alloy Make sure an application is started if it's a new app and restarted i…
alloy authored
144
7edae98 @alloy Changed the vhosts to be in /private/etc/apache2/passenger_vhosts and…
alloy authored
145 def save_config!
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
146 p "Saving configuration: #{config_path}"
7edae98 @alloy Changed the vhosts to be in /private/etc/apache2/passenger_vhosts and…
alloy authored
147 execute '/usr/bin/ruby', CONFIG_INSTALLER, [to_hash].to_yaml
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
148 end
149
150 def config_path
0532edb @alloy When a host has been changed first remove the old app before writing …
alloy authored
151 File.join(PASSENGER_APPS_DIR, "#{@host}.vhost.conf")
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
152 end
153
bf40737 @alloy Added support for RailsBaseURI.
alloy authored
154 def rbValueForKey(key)
155 key == 'host' ? "#{@host}#{@base_uri}" : super
156 end
157
86f33e9 @alloy Make sure an application is started if it's a new app and restarted i…
alloy authored
158 def rbSetValue_forKey(value, key)
159 super
aa2080f @alloy Made Apply in the UI work for all unsaved apps. When unselecting the …
alloy authored
160 mark_dirty!
bf40737 @alloy Added support for RailsBaseURI.
alloy authored
161
162 if key == 'host'
163 if value.to_s =~ /^(.+?)(\/.+)$/
164 @host, @base_uri = $1, $2
165 else
166 @base_uri = ''
167 end
168 end
169
e8c5b08 @alloy Don't automatically start/restart apps as this makes it much harder t…
alloy authored
170 set_default_host_from_path(@path) if key == 'path' && (@host.nil? || @host.empty?) && (!@path.nil? && !@path.empty?)
171 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 i…
alloy authored
172 end
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
173
aa2080f @alloy Made Apply in the UI work for all unsaved apps. When unselecting the …
alloy authored
174 def mark_dirty!
175 self.dirty = true
176 PrefPanePassenger.sharedInstance.applicationMarkedDirty self
177 end
178
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
179 def to_hash
d7a2659 @alloy When a PassengerApplication is a new app then set @user_defined_data …
alloy authored
180 @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 confi…
alloy authored
181 {
182 'config_path' => config_path,
183 'host' => @host.to_s,
184 'path' => @path.to_s,
4b6595e @alloy Small refactors.
alloy authored
185 'environment' => (@environment == DEVELOPMENT ? 'development' : 'production'),
bf40737 @alloy Added support for RailsBaseURI.
alloy authored
186 'allow_mod_rewrite' => (@allow_mod_rewrite == true || @allow_mod_rewrite == 1),
34dee4b @alloy Preserve and extra data the user might have added to the vhost.
alloy authored
187 'base_uri' => @base_uri,
40c603c @alloy Treat the vhostname "<VirtualHost (.+?)>" as user defined data as wel…
alloy authored
188 'vhostname' => @vhostname,
34dee4b @alloy Preserve and extra data the user might have added to the vhost.
alloy authored
189 'user_defined_data' => @user_defined_data
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the confi…
alloy authored
190 }
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
191 end
c5061e0 @alloy Working on updating the host textfield if a directory was chosen with…
alloy authored
192
d30ba95 @alloy Made revert work.
alloy authored
193 def revert(sender = nil)
194 @original_values.each do |key, value|
195 send "#{key}=", value
196 end
197 self.valid = self.dirty = false
198 end
199
c5061e0 @alloy Working on updating the host textfield if a directory was chosen with…
alloy authored
200 private
201
bf40737 @alloy Added support for RailsBaseURI.
alloy authored
202 def set_original_values!
203 @original_values = { 'host' => @host, 'path' => @path, 'environment' => @environment, 'allow_mod_rewrite' => @allow_mod_rewrite, 'base_uri' => @base_uri }
204 end
205
c5061e0 @alloy Working on updating the host textfield if a directory was chosen with…
alloy authored
206 def set_default_host_from_path(path)
e8c5b08 @alloy Don't automatically start/restart apps as this makes it much harder t…
alloy authored
207 self.host = "#{File.basename(path).downcase}.local"
c5061e0 @alloy Working on updating the host textfield if a directory was chosen with…
alloy authored
208 end
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
209 end
Something went wrong with that request. Please try again.