require 'gem_plugin' require 'mongrel' # give this class the name you want for your command mongrel_in_a_tunnel module SSL class Start < GemPlugin::Plugin "/commands" include Mongrel::Command::Base def configure options [ ['', '--ssl-cert', 'Specify the SSL certificate to use', :@ssl_cert, '/opt/local/etc/stunnel/stunnel.pem'], ['', '--ssl-port', 'Port to listen for HTTPS requests', :@ssl_port, 443], ['', '--ssl-listen', 'IP address to listen on', :@ssl_listen, '127.0.0.1'], ['-p', '--port', 'port for mongrel', :@port, 3000], ['-e', '--environment', 'RAILS_ENV', :@environment, nil] ] end def validate @ssl_port = @ssl_port.to_i valid? (@ssl_port > 0 && @ssl_port < 65000), true return @valid end def stunnel_config GemPlugin::Manager.instance.resource "mongrel_in_a_tunnel", "/stunnel.conf" end def run # generate config file and pass the file descriptor to the stunnel process # launch stunnel puts "Listening for SSL requests on #{@ssl_listen}:#{@ssl_port}" Kernel.fork do `stunnel #{stunnel_config} -f` end args = [] args << "start" args << "-e #{@environment}" if @environment args << "-p #{@port}" begin command = GemPlugin::Manager.instance.create("/commands/mongrel::start", :argv => args) rescue OptionParser::InvalidOption STDERR.puts "#$! for command '#{cmd_name}'" STDERR.puts "Try #{cmd_name} -h to get help." return false rescue STDERR.puts "ERROR RUNNING '#{cmd_name}': #$!" STDERR.puts "Use help command to get help" return false end # run mongrel command.run end end end