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.
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.
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).
To test this out I’ve created a simple Users table with four columns: UserId, Username, Firstname and Surname.
We should create a couple of test users like so.
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:
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
route resources :users
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.
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):
The upshot is that I can now add a user and check to see whether the user has been added.
There you go. Even for a relative novice like me this is a great developer experience without the need to install anything.