30 mins to a Ruby on Rails application with the Windows Azure vmdepot (Part 1)

Note: If you like this post, the UK Windows Azure Users Group will be running a free conference day in London on the 11th February which you can register for here: http://lanyrd.com/2013/open-source-on-azure/. Windows Azure is a great cloud platform to kickstart development with non-Microsoft languages and frameworks. If you haven’t used it before we’ll show you how to get up and running, what it can do for you and how you can get your hands on some free time to have a play.

I’ve been working quite a bit on a project with Ruby on Rails and Scala. It’s an open source project and I’ll be blogging about it and writing a paper shortly but in advance of that I thought I’d write a two part article on how you can get a Ruby on Rails test environment up and running within a short space of time. In this post I’ll cover how you would do this with a developer image that Microsoft and their partners are making available which contains pretty much everything you need to get up and running.

Everything in this post is based on Infrastructure as a Service (IaaS). In the next post I’ll look at how we can use PaaS with Windows Azure Sql Database and a Worker Role to build an automatically scalable Ruby applications.

Everything in this post took me in the region of 30 mins to setup. I’m fairly new to Ruby and don’t use it at all in my day to day coding. I have fallen in love with its simplicity and sheer community support (with a great ecosystem to match). It’s evident to me now that some really good parts of ASP.NET MVC were copied from Ruby on Rails. It makes me happy that Microsoft has learned to watch community development and respond to how people want to use tools in the real world.

To begin with we’ll navigate to http://vmdepot.msopentech.com/List/Index and in the search box type in devpack.

Press the triangle image marked deployment script. Follow the instructions and copy the resulting text. The output should look this and you can see I’m deploying this to North Europe and using a domain name called rubytime and a username and password of azurecoder and MyRubySuccess9000 in this case.

> azure vm create rubytime -o vmdepot-77-1-16 -l “North Europe” azurecoder MyRubySuccess9000 –ssh

If you haven’t already downloaded the CLI then do it’s amazing and supported on Windows, Linux and MacOSX. It’s written in node.js so will install the node runtime locally.

http://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/command-line-tools/

 

You should of course also have an azure subscription and follow the documentation above to setup an account. You can get free trials for 90 days which will allow you run a small instance.

I downloaded Bitwise Tunnelier to use SSH from my Windows 8 laptop. Once you’ve followed the above instructions and created the VM you can go about setting up a basic Rails site. The devpack contains a wealth of runtimes and tools including PHP, Ruby, Python, Java and various frameworks. Great for building and testing your code on. It also contains mysql, postgress sql, apache and nginx as well as support for git and svn.

To get up and running with a new Rails application enter the following at the SSH prompt:

> cd stack
> sudo ctlscript.sh stop apache

The above will run a pre-defined script to stop apache. We’re going to test with the Rails server first.

Now we’ll create the Rails application called test.

> sudo rails create test

The above will generate an application called test with the default Rails skeleton. We should generate a default controller and view called index and create a default model.

> rails generate test controller index
> rails generate test model

We should navigate to the test directory and edit the index view adding some text like “Welcome to Ruby on Rails on Windows Azure!”.

Now we can navigate to the demo/config directory.

> cd demo/config

And edit the route.rb file with the following to point to our new site index view.

root :to => ‘test#index’

We then rename the public index file to index.old otherwise the rails server will display the default.

rename public/index.html to index.old

We’re now ready to run the Rails server.

> sudo rails s -p80

Before being able to see our masterpiece on the public internet we’ll have to add an endpoint through the management portal for port 80. Navigate to your Windows Azure VM instance on the management portal and add an Endpoint entry called HTTP, set the protocol to TCP and add port 80.

Okay, so now that we’ve got all of this working we want to extend our new application to be able to use a database. As luck would have it the Bitnami image comes with MySql already installed, configured and running and allows us to login to PhpMyAdmin which will enable us to add some test data.

In order to facilitate access we’ll stop the Rails server using the terminal and run the script in the stack directory using:

> sudo ctlscript.sh start apache

And then setup a rule in our SSH client so that we can port forward.

Setting up SSH port forwarding

Setting up SSH port forwarding

We can then request http://localhost:8888/phpmyadmin/ so that we can use PhpMyAdmin. Logging into PhpMyAdmin can be done using root as a username and bitnami as a password. Not very secure but we can always change this later on (also we haven’t exposed the MySql service to the world so nobody without the SSH login details will have access at this stage).

MySql Admin homescreen

MySql Admin homescreen

To test this out I’ve created a simple Users table with four columns: UserId, Username, Firstname and Surname.

Creating the table

Creating the table

We should create a couple of test users like so.

Adding table rows

Adding table rows

Now that we’ve completed building a table with a couple of users we’re primed to be able to use ActiveRecord and create a Rails application to display our data.

We’ll now update our database.yml file which resides in the db directory off of the Rails application root. It should look something like this:

 

test:
  adapter: mysql2
  database: demo
  host: 127.0.0.1
  port: 3306
  username: root
  password: bitnami

Okay. All good. We’ve proved we can create a database and add rows! We’re going to junk all of this though because Ruby does everything much better and quicker than we can. You can see from the trace below that if we use rails to scaffold everything it will create everything that we need to build a complex user model including a variety of CRUD functions in a custom controller for the model, a migration to allow us to add the new model to the MySql DB.

azurecoder@rubytime:~/demo$ rails generate scaffold User user_id:int username:string firstname:string surname:string
invoke active_record
create db/migrate/20130127121055_create_users.rb
create app/models/user.rb
invoke test_unit
create test/unit/user_test.rb
create test/fixtures/users.yml
invoke resource_route
route resources :users
invoke scaffold_controller
create app/controllers/users_controller.rb
invoke erb
create app/views/users
create app/views/users/index.html.erb
create app/views/users/edit.html.erb
create app/views/users/show.html.erb
create app/views/users/new.html.erb
create app/views/users/_form.html.erb
invoke test_unit
create test/functional/users_controller_test.rb
invoke helper
create app/helpers/users_helper.rb
invoke test_unit
create test/unit/helpers/users_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/users.js.coffee
invoke scss
create app/assets/stylesheets/users.css.scss
invoke scss
create app/assets/stylesheets/scaffolds.css.scss

Now that we’ve built the scaffold we’re ready to test our application and see whether we can insert and read users.

Don’t do this at home but for brevity I decided to enable all routes in the routes.rb file. I’m having a lazy moment and I’m using Windows Azure so this will be down within the next few hours but every Rails site I looked at including the comment code in the routes says this is a bad idea for your application.

match ‘:controller(/:action(/:id))(.:format)’ 

Anyway, to create a new database I just navigate back to the root and issued the following:

> rake db:create

 Now that this has been done it’s easy to add stuff to the new Db through the views that have been created for us (first run the server again using the sudo rails s -p80 command):

Adding a user to the database

Adding a user to the database

The upshot is that I can now add a user and check to see whether the user has been added.

Listing users

Listing users

There you go. Even for a relative novice like me this is a great developer experience without the need to install anything.

 

Tagged with: , , , , , ,
Posted in Linux Virtual Machines
5 comments on “30 mins to a Ruby on Rails application with the Windows Azure vmdepot (Part 1)
  1. anil says:

    It’s possible to deploy RubyOnRails in Windows Azure?
    SQL Server support This language?
    It’s possible give me documents on that

    • azurecoder says:

      Yes it is. I’ll be posting part 2 which will describe this. The deployment is much more involved on windows as you’ll see but can be scripted for use with a worker role or vm. However, you can consume SQL Azure from Ubuntu with little effort.

  2. Jason says:

    This was a very helpful review. One suggestion, though.

    This line:
    > sudo rails create test
    should have ‘new’ instead of ‘create’, and ‘test’ is a reserved Rails word, so some other name must be used.

    • azurecoder says:

      Thanks for pointing this out. You are right “test” is a reserved word. I think I cut and paste my site name and just inadvertantly replaced this. I also wrote this in hindsight so I think I’ve confused the rake db:create with rails new. I will update the text.

5 Pings/Trackbacks for "30 mins to a Ruby on Rails application with the Windows Azure vmdepot (Part 1)"
  1. [...] Blog: 30 Mins to a Ruby on Rails application with the Windows Azure vmdepot (Part 1) by @azurecoder (posted Jan. 27) [...]

  2. [...] Blog: 30 Mins to a Ruby on Rails application with the Windows Azure vmdepot (Part 1) by @azurecoder (posted Jan. 27) [...]

  3. [...] Easiest thing to do after that is to download Bitvise Tunnelier which I’ve discussed in a previous post. [...]

  4. [...] excellent work done by Thomas Conte and Richard Conway, I thought it would be interesting to get a Ruby application running in Azure’s PaaS – [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>