Class: PassengerPane::Application
- Inherits:
-
Object
- Object
- PassengerPane::Application
- Defined in:
- lib/passenger_pane/application.rb
Constant Summary collapse
- RAILS_APP_REGEXP =
/::Initializer\.run|Application\.initialize!/- ATTRIBUTES =
[:config_filename, :host, :aliases, :path, :framework, :environment, :vhost_address, :user_defined_data]
Instance Attribute Summary collapse
-
#contents ⇒ Object
– Virtual Host reading and writing.
Class Method Summary collapse
- .all(configuration) ⇒ Object
- .find(configuration, conditions = {}) ⇒ Object
- .glob(configuration) ⇒ Object
Instance Method Summary collapse
- #_config_filename ⇒ Object
- #_derive ⇒ Object
- #_directory_defaults ⇒ Object
- #_document_root ⇒ Object
- #_framework ⇒ Object
- #_parse ⇒ Object
- #added_hosts ⇒ Object
- #changed? ⇒ Boolean
- #delete ⇒ Object
-
#initialize(configuration, options = {}) ⇒ Application
constructor
A new instance of Application.
- #new? ⇒ Boolean
- #rack? ⇒ Boolean
- #rails? ⇒ Boolean
-
#register ⇒ Object
– Directory services.
- #removed_hosts ⇒ Object
-
#restart ⇒ Object
– Operational.
-
#save ⇒ Object
– Persisting.
- #set(options) ⇒ Object
- #sync_host_registration ⇒ Object
-
#to_hash ⇒ Object
– Dirty checking.
- #unregister ⇒ Object
- #vhost_snippet ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(configuration, options = {}) ⇒ Application
Returns a new instance of Application.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/passenger_pane/application.rb', line 32 def initialize(configuration, ={}) @configuration = configuration set() if [:config_filename] @new = false _parse elsif [:path] @new = true _derive else raise ArgumentError, "Please specify either a :config_filename or :path" end @before_changes = to_hash end |
Instance Attribute Details
#contents ⇒ Object
– Virtual Host reading and writing
60 61 62 |
# File 'lib/passenger_pane/application.rb', line 60 def contents @contents ||= File.read(@config_filename) end |
Class Method Details
.all(configuration) ⇒ Object
16 17 18 19 20 |
# File 'lib/passenger_pane/application.rb', line 16 def self.all(configuration) Dir.glob(glob(configuration)).map do |config_filename| new(configuration, :config_filename => config_filename) end end |
.find(configuration, conditions = {}) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/passenger_pane/application.rb', line 22 def self.find(configuration, conditions={}) if conditions[:host] all(configuration).detect do |app| app.host == conditions[:host] end end end |
.glob(configuration) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/passenger_pane/application.rb', line 8 def self.glob(configuration) File.join( configuration.apache_directory, configuration.passenger_vhosts, "*.#{configuration.passenger_vhosts_ext}" ) end |
Instance Method Details
#_config_filename ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/passenger_pane/application.rb', line 102 def _config_filename File.join( @configuration.apache_directory, @configuration.passenger_vhosts, "#{@host}.#{@configuration.passenger_vhosts_ext}" ) end |
#_derive ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/passenger_pane/application.rb', line 119 def _derive @host ||= "#{File.basename(path).downcase.gsub('_','-')}.local" @aliases ||= '' @environment ||= 'development' @vhost_address ||= '*:80' @user_defined_data ||= _directory_defaults @config_filename ||= _config_filename @framework ||= _framework end |
#_directory_defaults ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/passenger_pane/application.rb', line 93 def _directory_defaults %{ <Directory "#{_document_root}"> Order allow,deny Allow from all </Directory> }.strip end |
#_document_root ⇒ Object
89 90 91 |
# File 'lib/passenger_pane/application.rb', line 89 def _document_root File.join(@path, 'public') end |
#_framework ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/passenger_pane/application.rb', line 110 def _framework environment_file = File.join(@path, 'config', 'environment.rb') if File.exist?(environment_file) and File.read(environment_file) =~ RAILS_APP_REGEXP 'rails' else 'rack' end end |
#_parse ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/passenger_pane/application.rb', line 66 def _parse data = contents.dup data.gsub!(/\n\s*ServerName\s+(.+)/, '') @host = $1 data.gsub!(/\n\s*ServerAlias\s+(.+)/, '') @aliases = $1 || '' data.gsub!(/\n\s*DocumentRoot\s+"(.+)\/public"/, '') @path = $1 data.gsub!(/\n\s*(Rails|Rack)Env\s+(\w+)/, '') @framework = $1.downcase @environment = $2 data.gsub!(/<VirtualHost\s(.+?)>/, '') @vhost_address = $1 data.gsub!(/\s*<\/VirtualHost>\n*/, '').gsub!(/^\n*/, '') @user_defined_data = data.strip end |
#added_hosts ⇒ Object
164 165 166 |
# File 'lib/passenger_pane/application.rb', line 164 def added_hosts to_hash['hosts'] - @before_changes['hosts'] end |
#changed? ⇒ Boolean
160 161 162 |
# File 'lib/passenger_pane/application.rb', line 160 def changed? @before_changes != to_hash end |
#delete ⇒ Object
197 198 199 200 201 |
# File 'lib/passenger_pane/application.rb', line 197 def delete FileUtils.rm_rf(config_filename) unregister true end |
#new? ⇒ Boolean
54 55 56 |
# File 'lib/passenger_pane/application.rb', line 54 def new? @new end |
#rack? ⇒ Boolean
130 |
# File 'lib/passenger_pane/application.rb', line 130 def rack?; @framework == 'rack' end |
#rails? ⇒ Boolean
129 |
# File 'lib/passenger_pane/application.rb', line 129 def rails?; @framework == 'rails' end |
#register ⇒ Object
– Directory services
174 175 176 |
# File 'lib/passenger_pane/application.rb', line 174 def register PassengerPane::DirectoryServices.register(to_hash['hosts']) end |
#removed_hosts ⇒ Object
168 169 170 |
# File 'lib/passenger_pane/application.rb', line 168 def removed_hosts @before_changes['hosts'] - to_hash['hosts'] end |
#restart ⇒ Object
– Operational
205 206 207 208 209 210 |
# File 'lib/passenger_pane/application.rb', line 205 def restart if File.exist?(@path) FileUtils.mkdir_p(File.join(File.join(@path, 'tmp'))) FileUtils.touch(File.join(@path, 'tmp', 'restart.txt')) end end |
#save ⇒ Object
– Persisting
193 194 195 |
# File 'lib/passenger_pane/application.rb', line 193 def save write and sync_host_registration end |
#set(options) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/passenger_pane/application.rb', line 47 def set() .each do |key, value| setter = "#{key}=" send(setter, value) if respond_to?(setter) end end |
#sync_host_registration ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'lib/passenger_pane/application.rb', line 182 def sync_host_registration if new? register else PassengerPane::DirectoryServices.register(added_hosts) and PassengerPane::DirectoryServices.unregister(removed_hosts) end end |
#to_hash ⇒ Object
– Dirty checking
153 154 155 156 157 158 |
# File 'lib/passenger_pane/application.rb', line 153 def to_hash hash = { 'hosts' => [host, *aliases.split] } ATTRIBUTES.each do |key| hash[key.to_s] = instance_variable_get("@#{key}") end; hash end |
#unregister ⇒ Object
178 179 180 |
# File 'lib/passenger_pane/application.rb', line 178 def unregister PassengerPane::DirectoryServices.unregister(to_hash['hosts']) end |
#vhost_snippet ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/passenger_pane/application.rb', line 132 def vhost_snippet lines = [] lines << "<VirtualHost #{vhost_address}>" lines << " ServerName #{host}" lines << " ServerAlias #{aliases}" unless aliases == '' lines << " DocumentRoot \"#{_document_root}\"" lines << " #{rails? ? 'RailsEnv' : 'RackEnv'} #{environment}" lines << " #{user_defined_data}" unless user_defined_data.strip == '' lines << "</VirtualHost>" lines.join("\n") end |
#write ⇒ Object
144 145 146 147 148 149 |
# File 'lib/passenger_pane/application.rb', line 144 def write FileUtils.mkdir_p(File.dirname(@config_filename)) File.open(@config_filename, 'w') do |file| file.write(vhost_snippet) end; true end |