We wanted to integrate the sunspot_rails gem into our cucumber features. I found a ticket where mat adviced to call Sunspot::Rails::Server.start / stop in one of the available cucumber hooks.
Code found in features/support/env.rb is run when Cucumber begins and exits so this seemed the right place to start Solr:
Sunspot::Rails::Server.new.start at_exit do Sunspot::Rails::Server.new.stop end
Also, make sure you’ve added a cucumber environment to the config/sunspot.yml file; we copied the test-environment for that:
test: &TEST solr: hostname: localhost port: 8981 log_level: WARNING cucumber: <<: *TEST
Running cucumber will let all scenarios fail unfortunately and we got a ‘Connection refused – connect(2) (RSolr::RequestError)‘ in our console.
The reason for this is simple (after I stepped into the sunspot_rails gem to be honest): the Sunspot::Rails::Server.new.start method will eventually launch a Java Servlet Container (Jetty) with the Solr WAR and uses the port defined in your sunspot.yml.
Now, starting up a server could take some time, so if cucumber’s features are run before the Solr server is up and running, the ‘Connection refused’ sounds trivial then.
First I added a sleep after starting up the server:
Sunspot::Rails::Server.new.start sleep(5)
and this will work for most cases, but I thought it would be better to let cucumber wait just long enough until the server is up and running. When the server is up, you can open up your browser and enter http://localhost:8981/solr to administering Solr. This we can use to find out if we’ve waited long enough and start our features. I’ve put this into a seperate class named CukeSunspot and created this file in the features/support directory:
require "net/http" class CukeSunspot def initialize @server = Sunspot::Rails::Server.new end def start @started = Time.now @server.start up end private def port @server.port end def uri "http://0.0.0.0:#{port}/solr/" end def up while starting puts "Sunspot server is starting..." end puts "Sunspot server took #{'%.2f' % (Time.now - @started)} sec. to get up and running. Let's cuke!" end def starting begin sleep(1) request = Net::HTTP.get_response(URI.parse(uri)) false rescue Errno::ECONNREFUSED true end end end
Now we can the alternative start method and remove the explicit sleep statement in our global before hook. features/support/env.rb:
Sunspot::Rails::Server.new.start at_exit do Sunspot::Rails::Server.new.stop end
And voila, if you run cucumber, you scenario’s should run successfully. On my machine:
Sunspot server is starting... Sunspot server is starting... Sunspot server took 3.74 sec. to get up and running. Let's cuke!
Setup a self-signed SSL site with Apache2
Some things need to be secure. Login and registration pages are often among them. This guide will show you how to quickly set-up a SSL site with a self-signed certificate and automatic HTTP-to-HTTPS redirect. This is ideal for setting up staging environments.
I’ll assume you have a standard Debian system with the apache2 package installed and ready.
The first step is to generate a key. You must choose a passphrase here. We’ll remove that later on for easier Apache2 restarts
Next, you need to generate a Certificate Sign Request or CSR. Some things to consider:
Next, sign the request with your key.
Then, create an insecure version of your key. This will remove the pass phrase. If you don’t do this apache will ask for the pass phrase when it loads the key.
A good place to keep your key and certificate is
/etc/apache2/ssl. Make sure you chmod 600 it for the root user.Okay, setup your VirtualHosts. This example is for a Passenger-powered example app.
<VirtualHost *:80> ServerName example.com Redirect permanent / https://example.com/ </VirtualHost> <VirtualHost *:443> ServerAdmin support@example.com ServerName example.com # SSL Engine Switch SSLEngine on # SSL Cipher Suite: SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL # Server Certificate SSLCertificateFile /etc/apache2/ssl/server.crt # Server Private Key SSLCertificateKeyFile /etc/apache2/ssl/server.key # Set header to indentify https requests for Mongrel RequestHeader set X-Forwarded-Proto "https" BrowserMatch ".*MSIE.*" \ nokeepalive ssl-unclean-shutdown \ downgrade-1.0 force-response-1.0 DocumentRoot /var/rails/example/current/public <Directory "/var/rails/example/current/public"> AllowOverride all Allow from all Options -MultiViews </Directory> </VirtualHost>There is a file name
/etc/apache2/ports.confthat configures which ports apache listen on. Make it look like this:NameVirtualHost *:80 Listen 80 <IfModule mod_ssl.c> NameVirtualHost *:443 Listen 443 </IfModule>All set. Now restart apache2 and you should be good to go.