Skip to content
Newer
Older
100644 114 lines (94 sloc) 3.26 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
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
16 def self.startApplications(apps)
94cd90a @alloy Write configs for multiple applications and reload the apache configs.
alloy authored
17 data = apps.to_ruby.map { |app| app.to_hash }.to_yaml
18 SharedPassengerBehaviour.p "Starting Rails applications (restarting Apache gracefully):\n#{data}"
19 SharedPassengerBehaviour.execute "/usr/bin/env ruby '#{CONFIG_INSTALLER}' '/etc/hosts' '#{data}' '/usr/sbin/apachectl graceful'"
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
20 end
21
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the confi…
alloy authored
22 kvc_accessor :host, :path, :dirty, :valid, :environment, :allow_mod_rewrite
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
23
24 def init
25 if super_init
3b57e1b @alloy Added stubs for environment and override_rewrite_rules.
alloy authored
26 @environment = DEVELOPMENT
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the confi…
alloy authored
27 @allow_mod_rewrite = false
3b57e1b @alloy Added stubs for environment and override_rewrite_rules.
alloy authored
28
86f33e9 @alloy Make sure an application is started if it's a new app and restarted i…
alloy authored
29 @new_app = true
e8c5b08 @alloy Don't automatically start/restart apps as this makes it much harder t…
alloy authored
30 @dirty = @valid = false
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
31 @host, @path = '', ''
32 self
33 end
34 end
35
36 def initWithFile(file)
37 if init
86f33e9 @alloy Make sure an application is started if it's a new app and restarted i…
alloy authored
38 @new_app = false
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the confi…
alloy authored
39 @valid = false
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
40 data = File.read(file)
41 @host = data.match(/ServerName\s+(.+)\n/)[1]
42 @path = data.match(/DocumentRoot\s+"(.+)\/public"\n/)[1]
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the confi…
alloy authored
43 @environment = (data.match(/RailsEnv\s+(development|production)\n/)[1] == 'development' ? DEVELOPMENT : PRODUCTION)
44 @allow_mod_rewrite = (data.match(/RailsAllowModRewrite\s+(off|on)\n/)[1] == 'on')
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
45 self
46 end
47 end
48
00b98a3 @alloy Refactored the default hostname into PassengerApplication. Marked cod…
alloy authored
49 def initWithPath(path)
4978e25 @alloy Small refactor.
alloy authored
50 if init
c5061e0 @alloy Working on updating the host textfield if a directory was chosen with…
alloy authored
51 @path = path
52 set_default_host_from_path(path)
4978e25 @alloy Small refactor.
alloy authored
53 self
54 end
55 end
56
e8c5b08 @alloy Don't automatically start/restart apps as this makes it much harder t…
alloy authored
57 def apply(sender = nil)
58 p "apply"
59 @new_app ? start : restart
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the confi…
alloy authored
60 # todo: check if it went ok before assumin so.
61 self.dirty = self.valid = false
e8c5b08 @alloy Don't automatically start/restart apps as this makes it much harder t…
alloy authored
62 end
63
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
64 def start
76c1df7 @alloy Verified that restarting the application works. Cleaned UI and code.
alloy authored
65 p "Starting Rails application (restarting Apache gracefully): #{@path}"
0729d2e @alloy Bring starting an application for the first time down to 1 request fo…
alloy authored
66 save_config! '/usr/sbin/apachectl graceful'
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
67 end
68
69 def restart(sender = nil)
70 p "Restarting Rails application: #{@path}"
71 save_config! if @dirty
76c1df7 @alloy Verified that restarting the application works. Cleaned UI and code.
alloy authored
72 Kernel.system("/usr/bin/touch '#{File.join(@path, 'tmp', 'restart.txt')}'")
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
73 end
86f33e9 @alloy Make sure an application is started if it's a new app and restarted i…
alloy authored
74
4aed8cd @alloy Added functionality to remove an application.
alloy authored
75 def remove
76 p "Removing application: #{path}"
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
77 execute "/usr/bin/env ruby '#{CONFIG_UNINSTALLER}' '/etc/hosts' '#{config_path}' '#{@host}'"
4aed8cd @alloy Added functionality to remove an application.
alloy authored
78 end
86f33e9 @alloy Make sure an application is started if it's a new app and restarted i…
alloy authored
79
0729d2e @alloy Bring starting an application for the first time down to 1 request fo…
alloy authored
80 def save_config!(extra_command = nil)
e79b2e2 @alloy Added code to actually start and restart an application.
alloy authored
81 p "Saving configuration: #{config_path}"
94cd90a @alloy Write configs for multiple applications and reload the apache configs.
alloy authored
82 command = "/usr/bin/env ruby '#{CONFIG_INSTALLER}' '/etc/hosts' '#{[to_hash].to_yaml}'"
0729d2e @alloy Bring starting an application for the first time down to 1 request fo…
alloy authored
83 command << " '#{extra_command}'" if extra_command
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the confi…
alloy authored
84 p command
0729d2e @alloy Bring starting an application for the first time down to 1 request fo…
alloy authored
85 execute command
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
86 end
87
88 def config_path
88f61cb @alloy Write out the config before starting an application.
alloy authored
89 @config_path ||= File.join(USERS_APACHE_PASSENGER_APPS_DIR, "#{@host}.vhost.conf")
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
90 end
91
86f33e9 @alloy Make sure an application is started if it's a new app and restarted i…
alloy authored
92 def rbSetValue_forKey(value, key)
93 super
e8c5b08 @alloy Don't automatically start/restart apps as this makes it much harder t…
alloy authored
94 self.dirty = true
95 set_default_host_from_path(@path) if key == 'path' && (@host.nil? || @host.empty?) && (!@path.nil? && !@path.empty?)
96 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
97 end
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
98
99 def to_hash
fd8cbad @alloy Actually set the environment and allow_mod_rewrite valus in the confi…
alloy authored
100 {
101 'config_path' => config_path,
102 'host' => @host.to_s,
103 'path' => @path.to_s,
104 'environment' => @environment == DEVELOPMENT ? 'development' : 'production',
105 'allow_mod_rewrite' => @allow_mod_rewrite == true || @allow_mod_rewrite == 1
106 }
b52f45e @alloy Started the work to create configs for multiple apps at once. WIP.
alloy authored
107 end
c5061e0 @alloy Working on updating the host textfield if a directory was chosen with…
alloy authored
108
109 private
110
111 def set_default_host_from_path(path)
e8c5b08 @alloy Don't automatically start/restart apps as this makes it much harder t…
alloy authored
112 self.host = "#{File.basename(path).downcase}.local"
c5061e0 @alloy Working on updating the host textfield if a directory was chosen with…
alloy authored
113 end
2e075ef @alloy Added tests for PassengerApplication.
alloy authored
114 end
Something went wrong with that request. Please try again.