Sha256: 54732920f50c2b97e2926cc99ca52b965959e85cd4d418cdb42bd9db2393a5f2

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongrel_in_a_tunnel-0.1 lib/mongrel_in_a_tunnel/init.rb