Permalink
Browse files

Import the CLI we prepared earlier.

  • Loading branch information...
1 parent 13ba0e3 commit 86dc2315eb42fb3bc195aeb1fe3692995de6e5bd @Manfred Manfred committed Dec 14, 2010
Showing with 2,052 additions and 51 deletions.
  1. +2 −1 .gitignore
  2. +1 −3 Rakefile
  3. +10 −0 bin/ppane
  4. +28 −0 lib/option_parser.rb
  5. +7 −0 lib/passenger_pane.rb
  6. +212 −0 lib/passenger_pane/application.rb
  7. +54 −0 lib/passenger_pane/configuration.rb
  8. +19 −0 lib/passenger_pane/directory_services.rb
  9. +66 −0 lib/passenger_pane/httpd_conf.rb
  10. +191 −0 lib/passenger_pane/runner.rb
  11. +255 −0 test/application_test.rb
  12. 0 test/{ → attic}/config_installer_test.rb
  13. 0 test/{ → attic}/config_uninstaller_test.rb
  14. 0 test/{ → attic}/fixtures/blog.vhost.conf
  15. 0 test/{ → attic}/fixtures/franky.vhost.conf
  16. 0 test/{ → attic}/fixtures/staging.vhost.conf
  17. 0 test/{ → attic}/fixtures/wiki.vhost.conf
  18. 0 test/{ → attic}/hosts_installer_test.rb
  19. 0 test/{ → attic}/passenger_application_test.rb
  20. 0 test/{ → attic}/passenger_pref_test.rb
  21. 0 test/{ → attic}/shared_passenger_behaviour_test.rb
  22. +53 −0 test/attic/test_helper.rb
  23. +35 −0 test/configuration_test.rb
  24. +25 −0 test/directory_services_test.rb
  25. +509 −0 test/fake/etc/apache2/httpd.conf
  26. +10 −0 test/fake/etc/apache2/passenger_pane_vhosts/blog.vhost.conf
  27. +9 −0 test/fake/etc/apache2/passenger_pane_vhosts/franky.vhost.conf
  28. +9 −0 test/fake/etc/apache2/passenger_pane_vhosts/staging.vhost.conf
  29. +12 −0 test/fake/etc/apache2/passenger_pane_vhosts/wiki.vhost.conf
  30. +126 −0 test/httpd_conf_test.rb
  31. +122 −0 test/runner_test.rb
  32. +20 −47 test/test_helper.rb
  33. +63 −0 test/test_helper/add_allow_switch.rb
  34. +151 −0 test/test_helper/add_allow_switch_test.rb
  35. +15 −0 test/test_helper/capture_output.rb
  36. +10 −0 test/test_helper/collector.rb
  37. +15 −0 test/test_helper/fake_apache_directory.rb
  38. +23 −0 test/test_helper/temporary_directory.rb
View
@@ -2,4 +2,5 @@ build
.DS_Store
Passenger.xcodeproj/*.mode1v3
Passenger.xcodeproj/*.pbxuser
-pkg
+pkg
+tmp
View
@@ -47,10 +47,8 @@ end
require 'rake/testtask'
Rake::TestTask.new do |t|
- t.libs << "test"
t.test_files = FileList['test/*_test.rb']
- t.verbose = nil
- t.options = '-rs'
+ t.verbose = true
end
desc "Generate Security.framework BridgeSupport file"
View
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+
+if $0 == __FILE__
+ $:.unshift(File.expand_path('../../lib', __FILE__))
+end
+
+require 'passenger_pane'
+require 'option_parser'
+
+PassengerPane::Runner.run(*OptionParser.parse(ARGV))
View
@@ -0,0 +1,28 @@
+class OptionParser
+ def self.parse(argv)
+ return [{},[]] if argv.empty?
+
+ options = {}
+ rest = []
+ switch = nil
+
+ for value in argv
+ # values is a switch
+ if value[0] == 45
+ switch = value.slice((value[1] == 45 ? 2 : 1)..-1)
+ options[switch] = nil
+ else
+ if switch
+ # we encountered a switch so this
+ # value belongs to that switch
+ options[switch] = value
+ switch = nil
+ else
+ rest << value
+ end
+ end
+ end
+
+ [options, rest]
+ end
+end
@@ -0,0 +1,7 @@
+module PassengerPane
+ autoload :Application, 'passenger_pane/application'
+ autoload :Configuration, 'passenger_pane/configuration'
+ autoload :DirectoryServices, 'passenger_pane/directory_services'
+ autoload :HttpdConf, 'passenger_pane/httpd_conf'
+ autoload :Runner, 'passenger_pane/runner'
+end
@@ -0,0 +1,212 @@
+require 'fileutils'
+
+module PassengerPane
+ class Application
+ RAILS_APP_REGEXP = /::Initializer\.run|Application\.initialize!/
+ ATTRIBUTES = [:config_filename, :host, :aliases, :path, :framework, :environment, :vhost_address, :user_defined_data]
+
+ def self.glob(configuration)
+ File.join(
+ configuration.apache_directory,
+ configuration.passenger_vhosts,
+ "*.#{configuration.passenger_vhosts_ext}"
+ )
+ end
+
+ def self.all(configuration)
+ Dir.glob(glob(configuration)).map do |config_filename|
+ new(configuration, :config_filename => config_filename)
+ end
+ end
+
+ def self.find(configuration, conditions={})
+ if conditions[:host]
+ all(configuration).detect do |app|
+ app.host == conditions[:host]
+ end
+ end
+ end
+
+ attr_accessor *ATTRIBUTES
+
+ def initialize(configuration, options={})
+ @configuration = configuration
+ set(options)
+ if options[:config_filename]
+ @new = false
+ _parse
+ elsif options[:path]
+ @new = true
+ _derive
+ else
+ raise ArgumentError, "Please specify either a :config_filename or :path"
+ end
+ @before_changes = to_hash
+ end
+
+ def set(options)
+ options.each do |key, value|
+ setter = "#{key}="
+ send(setter, value) if respond_to?(setter)
+ end
+ end
+
+ def new?
+ @new
+ end
+
+ # -- Virtual Host reading and writing
+
+ def contents
+ @contents ||= File.read(@config_filename)
+ end
+
+ attr_writer :contents
+
+ 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
+
+ def _document_root
+ File.join(@path, 'public')
+ end
+
+ def _directory_defaults
+ %{
+ <Directory "#{_document_root}">
+ Order allow,deny
+ Allow from all
+ </Directory>
+ }.strip
+ end
+
+ def _config_filename
+ File.join(
+ @configuration.apache_directory,
+ @configuration.passenger_vhosts,
+ "#{@host}.#{@configuration.passenger_vhosts_ext}"
+ )
+ end
+
+ 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
+
+ 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
+
+ def rails?; @framework == 'rails' end
+ def rack?; @framework == 'rack' end
+
+ 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
+
+ def write
+ FileUtils.mkdir_p(File.dirname(@config_filename))
+ File.open(@config_filename, 'w') do |file|
+ file.write(vhost_snippet)
+ end; true
+ end
+
+ # -- Dirty checking
+
+ def to_hash
+ hash = { 'hosts' => [host, *aliases.split] }
+ ATTRIBUTES.each do |key|
+ hash[key.to_s] = instance_variable_get("@#{key}")
+ end; hash
+ end
+
+ def changed?
+ @before_changes != to_hash
+ end
+
+ def added_hosts
+ to_hash['hosts'] - @before_changes['hosts']
+ end
+
+ def removed_hosts
+ @before_changes['hosts'] - to_hash['hosts']
+ end
+
+ # -- Directory services
+
+ def register
+ PassengerPane::DirectoryServices.register(to_hash['hosts'])
+ end
+
+ def unregister
+ PassengerPane::DirectoryServices.unregister(to_hash['hosts'])
+ end
+
+ def sync_host_registration
+ if new?
+ register
+ else
+ PassengerPane::DirectoryServices.register(added_hosts) and
+ PassengerPane::DirectoryServices.unregister(removed_hosts)
+ end
+ end
+
+ # -- Persisting
+
+ def save
+ write and sync_host_registration
+ end
+
+ def delete
+ FileUtils.rm_rf(config_filename)
+ unregister
+ true
+ end
+
+ # -- Operational
+
+ 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
+ end
+end
@@ -0,0 +1,54 @@
+module PassengerPane
+ class Configuration
+ CONFIG_FILENAME = '~/.passenger_pane.yml'
+ APACHE_DIRECTORY = '/private/etc/apache2'
+
+ def self.defaults
+ {
+ :ruby_binary => "/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby",
+ :httpd_binary => "/usr/sbin/httpd",
+ :httpd_conf => "httpd.conf",
+ :passenger_vhosts => "passenger_pane_vhosts",
+ :passenger_vhosts_ext => "vhost.conf",
+ :apache_restart_command => "/bin/launchctl stop org.apache.httpd"
+ }
+ end
+
+ def self.config_filename
+ File.expand_path(CONFIG_FILENAME)
+ end
+
+ def self.auto
+ configuration = new
+ if File.exist?(config_filename)
+ configuration.set(YAML.load_file(config_filename))
+ end
+ configuration
+ end
+
+ attr_accessor :apache_directory, *defaults.keys
+
+ def initialize(apache_directory=APACHE_DIRECTORY)
+ self.apache_directory = apache_directory
+ set(self.class.defaults)
+ end
+
+ def set(options)
+ options.each do |key, value|
+ begin
+ send("#{key}=", value)
+ rescue NoMethodError
+ raise ArgumentError, "There is no configuration named `#{key}', valid options are: #{self.class.defaults.keys.join(', ')} and apache_directory."
+ end
+ end
+ end
+
+ def httpd
+ @httpd ||= PassengerPane::HttpdConf.new(self)
+ end
+
+ def applications
+ PassengerPane::Application.all(self)
+ end
+ end
+end
@@ -0,0 +1,19 @@
+module PassengerPane
+ class DirectoryServices
+ def self.registered_hosts
+ `/usr/bin/dscl localhost -list /Local/Default/Hosts`.split("\n")
+ end
+
+ def self.register(hosts)
+ hosts.each do |host|
+ system "/usr/bin/dscl localhost -create /Local/Default/Hosts/#{host} IPAddress 127.0.0.1"
+ end
+ end
+
+ def self.unregister(hosts)
+ hosts.each do |host|
+ system "/usr/bin/dscl localhost -delete /Local/Default/Hosts/#{host}"
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 86dc231

Please sign in to comment.