ppane 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright © 2008-2010, Fingertips, http://www.fngtps.com,
2
+ Eloy Duran <eloy@fngtps.com>,
3
+ Thijs van der Vossen <thijs@fngtps.com>,
4
+ Manfred Stienstra <manfred@fngtps.com>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ if $0 == __FILE__
4
+ $:.unshift(File.expand_path('../../lib', __FILE__))
5
+ end
6
+
7
+ require 'passenger_pane'
8
+ require 'option_parser'
9
+
10
+ PassengerPane::Runner.run(*OptionParser.parse(ARGV))
@@ -0,0 +1,28 @@
1
+ class OptionParser
2
+ def self.parse(argv)
3
+ return [{},[]] if argv.empty?
4
+
5
+ options = {}
6
+ rest = []
7
+ switch = nil
8
+
9
+ for value in argv
10
+ # values is a switch
11
+ if value[0] == 45
12
+ switch = value.slice((value[1] == 45 ? 2 : 1)..-1)
13
+ options[switch] = nil
14
+ else
15
+ if switch
16
+ # we encountered a switch so this
17
+ # value belongs to that switch
18
+ options[switch] = value
19
+ switch = nil
20
+ else
21
+ rest << value
22
+ end
23
+ end
24
+ end
25
+
26
+ [options, rest]
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ module PassengerPane
2
+ autoload :Application, 'passenger_pane/application'
3
+ autoload :Configuration, 'passenger_pane/configuration'
4
+ autoload :DirectoryServices, 'passenger_pane/directory_services'
5
+ autoload :HttpdConf, 'passenger_pane/httpd_conf'
6
+ autoload :Runner, 'passenger_pane/runner'
7
+ end
@@ -0,0 +1,212 @@
1
+ require 'fileutils'
2
+
3
+ module PassengerPane
4
+ class Application
5
+ RAILS_APP_REGEXP = /::Initializer\.run|Application\.initialize!/
6
+ ATTRIBUTES = [:config_filename, :host, :aliases, :path, :framework, :environment, :vhost_address, :user_defined_data]
7
+
8
+ def self.glob(configuration)
9
+ File.join(
10
+ configuration.apache_directory,
11
+ configuration.passenger_vhosts,
12
+ "*.#{configuration.passenger_vhosts_ext}"
13
+ )
14
+ end
15
+
16
+ def self.all(configuration)
17
+ Dir.glob(glob(configuration)).map do |config_filename|
18
+ new(configuration, :config_filename => config_filename)
19
+ end
20
+ end
21
+
22
+ def self.find(configuration, conditions={})
23
+ if conditions[:host]
24
+ all(configuration).detect do |app|
25
+ app.host == conditions[:host]
26
+ end
27
+ end
28
+ end
29
+
30
+ attr_accessor *ATTRIBUTES
31
+
32
+ def initialize(configuration, options={})
33
+ @configuration = configuration
34
+ set(options)
35
+ if options[:config_filename]
36
+ @new = false
37
+ _parse
38
+ elsif options[:path]
39
+ @new = true
40
+ _derive
41
+ else
42
+ raise ArgumentError, "Please specify either a :config_filename or :path"
43
+ end
44
+ @before_changes = to_hash
45
+ end
46
+
47
+ def set(options)
48
+ options.each do |key, value|
49
+ setter = "#{key}="
50
+ send(setter, value) if respond_to?(setter)
51
+ end
52
+ end
53
+
54
+ def new?
55
+ @new
56
+ end
57
+
58
+ # -- Virtual Host reading and writing
59
+
60
+ def contents
61
+ @contents ||= File.read(@config_filename)
62
+ end
63
+
64
+ attr_writer :contents
65
+
66
+ def _parse
67
+ data = contents.dup
68
+
69
+ data.gsub!(/\n\s*ServerName\s+(.+)/, '')
70
+ @host = $1
71
+
72
+ data.gsub!(/\n\s*ServerAlias\s+(.+)/, '')
73
+ @aliases = $1 || ''
74
+
75
+ data.gsub!(/\n\s*DocumentRoot\s+"(.+)\/public"/, '')
76
+ @path = $1
77
+
78
+ data.gsub!(/\n\s*(Rails|Rack)Env\s+(\w+)/, '')
79
+ @framework = $1.downcase
80
+ @environment = $2
81
+
82
+ data.gsub!(/<VirtualHost\s(.+?)>/, '')
83
+ @vhost_address = $1
84
+
85
+ data.gsub!(/\s*<\/VirtualHost>\n*/, '').gsub!(/^\n*/, '')
86
+ @user_defined_data = data.strip
87
+ end
88
+
89
+ def _document_root
90
+ File.join(@path, 'public')
91
+ end
92
+
93
+ def _directory_defaults
94
+ %{
95
+ <Directory "#{_document_root}">
96
+ Order allow,deny
97
+ Allow from all
98
+ </Directory>
99
+ }.strip
100
+ end
101
+
102
+ def _config_filename
103
+ File.join(
104
+ @configuration.apache_directory,
105
+ @configuration.passenger_vhosts,
106
+ "#{@host}.#{@configuration.passenger_vhosts_ext}"
107
+ )
108
+ end
109
+
110
+ def _framework
111
+ environment_file = File.join(@path, 'config', 'environment.rb')
112
+ if File.exist?(environment_file) and File.read(environment_file) =~ RAILS_APP_REGEXP
113
+ 'rails'
114
+ else
115
+ 'rack'
116
+ end
117
+ end
118
+
119
+ def _derive
120
+ @host ||= "#{File.basename(path).downcase.gsub('_','-')}.local"
121
+ @aliases ||= ''
122
+ @environment ||= 'development'
123
+ @vhost_address ||= '*:80'
124
+ @user_defined_data ||= _directory_defaults
125
+ @config_filename ||= _config_filename
126
+ @framework ||= _framework
127
+ end
128
+
129
+ def rails?; @framework == 'rails' end
130
+ def rack?; @framework == 'rack' end
131
+
132
+ def vhost_snippet
133
+ lines = []
134
+ lines << "<VirtualHost #{vhost_address}>"
135
+ lines << " ServerName #{host}"
136
+ lines << " ServerAlias #{aliases}" unless aliases == ''
137
+ lines << " DocumentRoot \"#{_document_root}\""
138
+ lines << " #{rails? ? 'RailsEnv' : 'RackEnv'} #{environment}"
139
+ lines << " #{user_defined_data}" unless user_defined_data.strip == ''
140
+ lines << "</VirtualHost>"
141
+ lines.join("\n")
142
+ end
143
+
144
+ def write
145
+ FileUtils.mkdir_p(File.dirname(@config_filename))
146
+ File.open(@config_filename, 'w') do |file|
147
+ file.write(vhost_snippet)
148
+ end; true
149
+ end
150
+
151
+ # -- Dirty checking
152
+
153
+ def to_hash
154
+ hash = { 'hosts' => [host, *aliases.split] }
155
+ ATTRIBUTES.each do |key|
156
+ hash[key.to_s] = instance_variable_get("@#{key}")
157
+ end; hash
158
+ end
159
+
160
+ def changed?
161
+ @before_changes != to_hash
162
+ end
163
+
164
+ def added_hosts
165
+ to_hash['hosts'] - @before_changes['hosts']
166
+ end
167
+
168
+ def removed_hosts
169
+ @before_changes['hosts'] - to_hash['hosts']
170
+ end
171
+
172
+ # -- Directory services
173
+
174
+ def register
175
+ PassengerPane::DirectoryServices.register(to_hash['hosts'])
176
+ end
177
+
178
+ def unregister
179
+ PassengerPane::DirectoryServices.unregister(to_hash['hosts'])
180
+ end
181
+
182
+ def sync_host_registration
183
+ if new?
184
+ register
185
+ else
186
+ PassengerPane::DirectoryServices.register(added_hosts) and
187
+ PassengerPane::DirectoryServices.unregister(removed_hosts)
188
+ end
189
+ end
190
+
191
+ # -- Persisting
192
+
193
+ def save
194
+ write and sync_host_registration
195
+ end
196
+
197
+ def delete
198
+ FileUtils.rm_rf(config_filename)
199
+ unregister
200
+ true
201
+ end
202
+
203
+ # -- Operational
204
+
205
+ def restart
206
+ if File.exist?(@path)
207
+ FileUtils.mkdir_p(File.join(File.join(@path, 'tmp')))
208
+ FileUtils.touch(File.join(@path, 'tmp', 'restart.txt'))
209
+ end
210
+ end
211
+ end
212
+ end
@@ -0,0 +1,54 @@
1
+ module PassengerPane
2
+ class Configuration
3
+ CONFIG_FILENAME = '~/.passenger_pane.yml'
4
+ APACHE_DIRECTORY = '/private/etc/apache2'
5
+
6
+ def self.defaults
7
+ {
8
+ :ruby_binary => "/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby",
9
+ :httpd_binary => "/usr/sbin/httpd",
10
+ :httpd_conf => "httpd.conf",
11
+ :passenger_vhosts => "passenger_pane_vhosts",
12
+ :passenger_vhosts_ext => "vhost.conf",
13
+ :apache_restart_command => "/bin/launchctl stop org.apache.httpd"
14
+ }
15
+ end
16
+
17
+ def self.config_filename
18
+ File.expand_path(CONFIG_FILENAME)
19
+ end
20
+
21
+ def self.auto
22
+ configuration = new
23
+ if File.exist?(config_filename)
24
+ configuration.set(YAML.load_file(config_filename))
25
+ end
26
+ configuration
27
+ end
28
+
29
+ attr_accessor :apache_directory, *defaults.keys
30
+
31
+ def initialize(apache_directory=APACHE_DIRECTORY)
32
+ self.apache_directory = apache_directory
33
+ set(self.class.defaults)
34
+ end
35
+
36
+ def set(options)
37
+ options.each do |key, value|
38
+ begin
39
+ send("#{key}=", value)
40
+ rescue NoMethodError
41
+ raise ArgumentError, "There is no configuration named `#{key}', valid options are: #{self.class.defaults.keys.join(', ')} and apache_directory."
42
+ end
43
+ end
44
+ end
45
+
46
+ def httpd
47
+ @httpd ||= PassengerPane::HttpdConf.new(self)
48
+ end
49
+
50
+ def applications
51
+ PassengerPane::Application.all(self)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,19 @@
1
+ module PassengerPane
2
+ class DirectoryServices
3
+ def self.registered_hosts
4
+ `/usr/bin/dscl localhost -list /Local/Default/Hosts`.split("\n")
5
+ end
6
+
7
+ def self.register(hosts)
8
+ hosts.each do |host|
9
+ system "/usr/bin/dscl localhost -create /Local/Default/Hosts/#{host} IPAddress 127.0.0.1"
10
+ end
11
+ end
12
+
13
+ def self.unregister(hosts)
14
+ hosts.each do |host|
15
+ system "/usr/bin/dscl localhost -delete /Local/Default/Hosts/#{host}"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,66 @@
1
+ module PassengerPane
2
+ class HttpdConf
3
+ attr_accessor :filename
4
+
5
+ def initialize(configuration)
6
+ @configuration = configuration
7
+ @filename = File.expand_path(configuration.httpd_conf, configuration.apache_directory)
8
+ end
9
+
10
+ def contents
11
+ @contents ||= File.read(@filename)
12
+ end
13
+
14
+ attr_writer :contents
15
+
16
+ def passenger_vhost_include
17
+ "Include #{@configuration.apache_directory}/#{@configuration.passenger_vhosts}/*.#{@configuration.passenger_vhosts_ext}"
18
+ end
19
+
20
+ def passenger_configuration_snippet
21
+ %{
22
+ # Added by the Passenger preference pane
23
+ # Make sure to include the Passenger configuration (the LoadModule,
24
+ # PassengerRoot, and PassengerRuby directives) before this section.
25
+ <IfModule passenger_module>
26
+ NameVirtualHost *:80
27
+ <VirtualHost *:80>
28
+ ServerName _default_
29
+ </VirtualHost>
30
+ #{passenger_vhost_include}
31
+ </IfModule>
32
+ }.strip
33
+ end
34
+
35
+ def valid?
36
+ `#{@configuration.httpd_binary} -t 2>&1`.strip == 'Syntax OK'
37
+ end
38
+
39
+ def restart
40
+ if valid?
41
+ system @configuration.apache_restart_command
42
+ else
43
+ puts "[!] Apache configuration is not valid, skipping Apache restart"
44
+ end
45
+ end
46
+
47
+ def passenger_module_installed?
48
+ `#{@configuration.httpd_binary} -D DUMP_MODULES 2>&1`.include? 'passenger_module'
49
+ end
50
+
51
+ def passenger_configured?
52
+ !!(contents =~ /Include.*#{@configuration.passenger_vhosts}/)
53
+ end
54
+
55
+ def configure_passenger
56
+ self.contents << "\n\n"
57
+ self.contents << passenger_configuration_snippet
58
+ end
59
+
60
+ def write
61
+ File.open(@filename, 'w') do |file|
62
+ file.write(contents)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,191 @@
1
+ require 'yaml'
2
+
3
+ module PassengerPane
4
+ class Runner
5
+ def initialize(options)
6
+ @options = options
7
+ @configuration = PassengerPane::Configuration.auto
8
+ end
9
+
10
+ def machine_readable?
11
+ @options['machine_readable']
12
+ end
13
+
14
+ def info
15
+ if machine_readable?
16
+ puts YAML.dump({
17
+ 'passenger_module_installed' => @configuration.http.passenger_module_installed?,
18
+ 'passenger_configured' => @configuration.httpd.passenger_configured?
19
+ })
20
+ else
21
+ puts "Apache directory: #{@configuration.apache_directory}"
22
+ puts "Passenger installed: #{@configuration.httpd.passenger_module_installed? ? 'yes' : 'no'}"
23
+ puts "Passenger configured: #{@configuration.httpd.passenger_configured? ? 'yes' : 'no'}"
24
+ puts "Registered hostnames:"
25
+ PassengerPane::DirectoryServices.registered_hosts.each do |host|
26
+ puts " #{host}"
27
+ end
28
+ end
29
+ end
30
+
31
+ def configure
32
+ unless @configuration.httpd.passenger_configured?
33
+ @configuration.httpd.configure_passenger
34
+ @configuration.httpd.write
35
+ @configuration.httpd.restart
36
+ end
37
+ end
38
+
39
+ def add(directory)
40
+ options = @options.dup
41
+ options[:path] = File.expand_path(directory)
42
+ application = PassengerPane::Application.new(@configuration, options)
43
+ if application.save
44
+ @configuration.httpd.restart
45
+ end
46
+ if machine_readable?
47
+ puts YAML.dump(application.to_hash)
48
+ end
49
+ end
50
+
51
+ def update(host)
52
+ if application = PassengerPane::Application.find(@configuration, :host => host)
53
+ application.set(@options)
54
+ if application.save
55
+ @configuration.httpd.restart
56
+ end
57
+ if machine_readable?
58
+ puts YAML.dump(application.to_hash)
59
+ end
60
+ end
61
+ end
62
+
63
+ def delete(host)
64
+ if application = PassengerPane::Application.find(@configuration, :host => host)
65
+ application.delete
66
+ @configuration.httpd.restart
67
+ end
68
+ end
69
+
70
+ def list
71
+ if machine_readable?
72
+ puts YAML.dump(@configuration.applications.map do |app|
73
+ app.to_hash
74
+ end)
75
+ else
76
+ @configuration.applications.each_with_index do |app, index|
77
+ puts unless index == 0
78
+ puts "#{app.host}"
79
+ puts " Aliases: #{app.aliases}"
80
+ puts " Folder: #{app.path}"
81
+ puts " Environment: #{app.environment}"
82
+ end
83
+ end
84
+ end
85
+
86
+ def restart(host)
87
+ if host and application = PassengerPane::Application.find(@configuration, :host => host)
88
+ application.restart
89
+ else
90
+ @configuration.httpd.restart
91
+ end
92
+ end
93
+
94
+ def register
95
+ @configuration.applications.each do |app|
96
+ app.register
97
+ end
98
+ end
99
+
100
+ def self.usage
101
+ puts "Usage: #{File.basename($0)} <command> [options]"
102
+ puts
103
+ puts "Commands:"
104
+ puts " list: List all configured applications"
105
+ puts " register: Register all configured hostnames with Directory Services*"
106
+ puts " info: Show information about the system"
107
+ puts " configure: Configure Apache for use with the Passenger Pane*"
108
+ puts " add <directory>: Add an application in a directory*"
109
+ puts " update <host>: Update an application*"
110
+ puts " delete <host>: Deletes an application*"
111
+ puts " restart <host>: Restart an application"
112
+ puts " restart: Restart Apache to pick up configuration changes*"
113
+ puts
114
+ puts "* requires root privileges"
115
+ puts
116
+ puts "Options:"
117
+ puts " -h, --help: Show this help text"
118
+ puts " -m, --machine Use machine readable output"
119
+ puts
120
+ puts "Attributes:"
121
+ puts " --host: Hostname for the application (ie. myapp.local)"
122
+ puts " --aliases: Aliases for the application (ie. assets.myapp.local)"
123
+ puts " --path: The folder with the application"
124
+ puts " --environment: The environment to run, usually development or production"
125
+ puts " --framework: The framework, either rack or rails"
126
+ end
127
+
128
+ def self.run(flags, args)
129
+ options = {}
130
+ command = args.shift.to_s
131
+
132
+ return usage if command == ''
133
+
134
+ flags.each do |name, value|
135
+ case name
136
+ when 'h', 'help'
137
+ command = 'help'
138
+ when 'm', 'machine'
139
+ options['machine_readable'] = true
140
+ else
141
+ options[name] = value
142
+ end
143
+ end
144
+
145
+ unless $stdin.tty?
146
+ data = YAML.load($stdin.read)
147
+ data.each do |name, value|
148
+ options[name] = value
149
+ end if data
150
+ end
151
+
152
+ case command
153
+ when 'info'
154
+ new(options).info
155
+ when 'configure'
156
+ needs_root
157
+ new(options).configure
158
+ when 'add'
159
+ needs_root
160
+ new(options).add(args.first)
161
+ when 'update'
162
+ needs_root
163
+ new(options).update(args.first)
164
+ when 'delete'
165
+ needs_root
166
+ new(options).delete(args.first)
167
+ when 'restart'
168
+ host = args.first
169
+ needs_root unless host
170
+ new(options).restart(host)
171
+ when 'list'
172
+ new(options).list
173
+ when 'register'
174
+ needs_root
175
+ new(options).register
176
+ else
177
+ path = File.expand_path(command)
178
+ if File.exist?(path)
179
+ needs_root
180
+ new(options).add(path)
181
+ else
182
+ usage
183
+ end
184
+ end
185
+ end
186
+
187
+ def self.needs_root
188
+ puts "[!] The command might not run because it requires root privileges" unless ENV['USER'] == 'root'
189
+ end
190
+ end
191
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ppane
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Eloy Duran
14
+ - Manfred Stienstra
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-12-14 00:00:00 +01:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: " The Passenger Pane is a preference pane on Mac OS X. Ppane is the backend tool\n that does all the heavy lifting. It manages virtual hosts in your Apache configuration\n as well as hostname registration with Domain Services.\n"
24
+ email:
25
+ - eloy@fngtps.com
26
+ - manfred@fngtps.com
27
+ executables:
28
+ - ppane
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ files:
34
+ - lib/option_parser.rb
35
+ - lib/passenger_pane/application.rb
36
+ - lib/passenger_pane/configuration.rb
37
+ - lib/passenger_pane/directory_services.rb
38
+ - lib/passenger_pane/httpd_conf.rb
39
+ - lib/passenger_pane/runner.rb
40
+ - lib/passenger_pane.rb
41
+ - LICENSE
42
+ - bin/ppane
43
+ has_rdoc: true
44
+ homepage:
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=utf-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.7
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: "Configuration tool for applications running on Phusion Passenger\xE2\x84\xA2."
77
+ test_files: []
78
+