Posts Tagged “Ruby”

SUSE Studio is  an awesome tool, with a couple of clicks you can create an openSUSE/SUSE based system and deploy to your hard drive, an usb flash,  a live dvd, a VMware/VirtualBox/Xen server and even Amazon EC2 cloud.

Suppose you want to create a tailored SUSE Studio appliance to run a Ruby on Rails app, this is a list of things you have to take care of:

  • install all the gems required by the app (this can be a long list).
  • install and configure the database used by the app.
  • install and configure a webserver.
  • ensure all the required services are started at boot time.

You can save some time by cloning this appliance shared on SUSE Gallery, but this is still going to be boring.

Dister to the rescue!

Dister is a command line tool similar to the one used by Heroku (one of the coolest ways to run your Rails app into the cloud). Within a few steps you will be able to create a SUSE Studio appliance running your rails application, download it and deploy wherever you want.

Dister is named after SUSE Studio robot. It has been created by  Dominik Mayer and me during the latest hackweek.

How it works

We are going to create a SUSE Studio appliance running a rails application called “SUSE history”. The app uses bundler to handle its dependencies.
This is its Gemfile file:

source 'http://rubygems.org'
gem 'rails', '3.0.5'
gem 'pg'
gem "flutie", "~> 1.1"

As you can see the app uses rails3, the flutie gem and PostgreSQL as database.

Appliance creation

Move into the suse_history directory and execute the following command:

dister create suse_history

dister create

Creating a SUSE Studio appliance with dister


As you can see dister has already done a couple of things for you:

  • created an appliance using latest version of openSUSE supported by SUSE Studio (you can use a different base system of course)
  • added the devel:language:ruby:extensions repository to the appliance: this repo contains tons of ruby packages (like mod_passenger)
  • installed a couple of things:
    • devel_C_C++ pattern: this will allow you to build native gems.
    • devel_ruby pattern: provides ruby, rubygems and a couple of development packages needed to build native gems.
    • rubygem-bundler: bundler is required by dister in order to handle the dependencies of your rails application.
    • rubygem-passenger-apache2: dister uses passenger and apache2 to deploy your rails application.
    • postgresql-server: dister noticed suse_history uses PostgreSQL as database, hence it automatically installs it.
    • rubygem-pg: dister noticed suse_history uses PostgreSQL as database, hence it automatically installs the ruby’s library forPostgreSQL.
  • uploaded a custom build script which ensures:
    • mod_passenger module is loaded by Apache
    • both Apache and PostgreSQL are always started at boot time.
    • all dependencies are installed: this is done only during the first boot using bundler.
    • the database user required by your rails app is created. This is done only during the first boot using some SQL code.
    • the database used by the appliance is properly initialized (aka rails db:create db:migrate). This is done only during the first boot.

Upload your code

It’s time to upload suse_history code. This is done using the following command:

dister push

dister push output
As you can see dister packaged the application source code and all its dependencies into a single archive. Then uploaded the archive to SUSE Studio as an overlay file. Dister uploaded also the configuration files required by Apache and by PostgreSQL setup.

Build your appliance

It’s build time!

dister build

dister build
The appliance has automatically being built using the raw disk. You can use different formats of course.

Testdrive

Testdrive is one of the coolest features of SUSE Studio. Unfortunately dister doesn’t support it yet. Just visit your appliance page and start testdrive from your browser.
Just enable testdrive networking and connect to your appliance:
suse history running inside of testdrive

Download

Your appliance is working flawlessly. Download it and deploy it wherever you want.

dister download

Current status

As you can see dister handles pretty fine a simple Rails application, but there’s still room for improvements.

Here’s a small list of the things on my TODO list:

  • The dependency management should install gems using rpm packages. This would make the installation of native gems easier, right now the user has to manually add all the development libraries required by the gem. Moreover it would reduce the size of the overlay files uploaded to SUSE Studio.
  • SUSE Studio Testdrive should be supported.
  • It should be possible to deploy the SUSE Studio directly to EC2.
  • Fix bugs!

Bugs and enhancements are going to be tracked here.

Contribute

Dister code can be found here on github, fork it and start contributing.

If you are a student you can work on dister during the next Google Summer of code, apply now!

Tags: , , ,

Comments No Comments »

Let me introduce a small project I’ve been working on with a friend of mine, Giuseppe Capizzi. The project is called jump and allows you to quickly change directories in the bash shell using bookmarks.

Thanks to Jump, you won’t have to type those long paths anymore.

You can find jump’s source code, detailed documentation and installation instructions here.

SUSE packages can be found here.

Tags: ,

Comments 7 Comments »

This post explains how to execute a single unit test (or even a single test method) instead of running the complete unit test suite.

In order to run the unit tests of your rails application, basically you have these official possibilities:

  • rake test: runs all unit, functional and integration tests.
  • rake test:units: runs all the unit tests.
  • rake test:functionals: runs all the functional tests.
  • rake test:integration: runs all the integration tests.

Each one of these commands requires some time and they are not the best solution while developing a new feature or fixing a bug. In this circumstance we just want to have a quick feedback from the unit test of the code we are editing.

Waiting for all the unit/functional tests to complete decreases our productivity, what we need is to execute just a single unit test. Fortunately there are different solutions for this problem, let’s go through them.

The easy approach: use your favorite IDE

Most of the IDE supporting ruby allow you to run a single unit test.
If you are using Netbeans running a single unit test is really easy:

  • make sure the editor if showing the file you want to test or the file containing its unit tests
  • Hit Ctrl+Shift+F6 or click on the following menu entry: Debug->Debug Test File

Two new windows will be opened: one will contain the output produced by your unit test, the other one will show the results of the unit test.

As you will notice the summary window contains also some useful information like the:

  • hyper links to the exact location of the code that produced the error/failure.
  • execution time required by each one of the test methods.

As you will experience it will be like “compiling” your ruby code.

From the console

If you are not using Netbeans you can always rely on some command line tools.

No additional tools

These “tricks” don’t require additional gems, hence they will work out of the box.

The first solution is to call this rake task:

rake test TEST=path_to_test_file

So the final command should look like

rake test TEST=test/unit/invitation_test.rb

Unfortunately on my machine this command repeats the same test three times, I hope you won’t have the same weird behavior also on your systems…

Alternatively you can use the following command:

ruby -I"lib:test" path_to_test_file"

It’s even possible to call a specific test method of your testcase:

ruby -I"lib:test" path_to_test_file -n name_of_the_method"

So calling:

ruby -I"lib:test" test/unit/invitation_test.rb -n test_should_create_invitation

will execute only InvitationTest::test_should_create_invitation.

It’s also possible to execute only the test methods matching a regular expression. Look at this example:

ruby -I"lib:test" test/unit/invitation_test.rb -n /.*between.*/

This command will execute only the test methods matching the /.*between.*/ regexp.

Using the single_test gem

If you want to avoid the awful syntax showed in the previous paragraph there’s a gem that can help you, it’s called single_test.

The github page contains a nice documentation, but let’s go through the most common use cases.

You can install the gem as a rails plugin:

script/plugin install git://github.com/grosser/single_test.git

single_test will add new rake tasks to your rails project, but won’t override the original ones.

Suppose we want to execute the unit test of user.rb, just type the following command:

rake test:user

If you want to execute the functional test of User just call:

rake test:user_c

Appending “_c” to the class name will automatically execute its functional test (if it exists).

It’s still possible to execute a specif test method:

rake test:user_c:test_name

So calling:

rake test:user_c:test_update_user

Will execute the test_update_user method written inside of test/functional/user_controller_test.rb.

It’s still possible to use regexp:

rake test:invitation:.*between.*

This syntax is equivalent to ruby -I"lib:test" test/unit/invitation_test.rb -n /.*between.*/.

Possible issues

When a single unit test is run all the usual database initialization tasks are not performed. If your code is relying on newly created migrations you will surely have lots of errors. This is happening because the new migrations have not been applied to the test database.

In order to fix these errors just execute:

rake db:test:prepare

before running your unit test.

Tags: , , ,

Comments 7 Comments »

Also this year I’ll attend the Linux day (a day dedicated to Gnu/Linux  and FLOSS that occurs every year in Italy) organized by my LUG. Guess what I’ll be talking about… ;)

While organizing the event somebody proposed to setup a local server with some music released under CC license. He suggested to download some albums from Jamendo (due to network issues we won’t be able to provide direct access to the website).

Since nobody wanted to download the albums by hand, last night I wrote a small ruby program that does the dirty job.

Requirements:

Ruby and json gem have to be installed on you machine.

Usage:

Help:

./jamendo_downloader.rb –help

Download the top 10 rock albums:

./jamendo_downloader.rb -g rock -t 10

Have fun

I think there’s nothing more to say… enjoy my jamendo_downloader

Tags: , ,

Comments 2 Comments »