Turn your digital data into dynamic graphics with this exciting, leading-edge cookbook. Packed with recipes and practical guidance it will quickly make you a proficient user of the D3 JavaScript library.

Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

Packt Publishing

A fast and practical guide to supporting multiple operating systems with the Xen hypervisor

Base Appliance Image

We will use an Ubuntu Feisty domain image as the base image for creating these appliances. This image should be made as sparse and small as possible, and free of any cruft. A completely stripped down version of Linux with only the bare necessities would be a great start. In this case, we will not need any graphical desktop environments, so we can completely eliminate software packages like the X11 and any window manager like Gnome or KDE. Once we have a base image, we can back it up and then start using it for creating Xen appliances. In this article we will use an Ubuntu Feisty domain as the base image. Once this domain image is ready we are going to update it and clean it up a little bit so it can be our base.

Edit the sources list for apt and add in other repositories that we will need to get software packages we will need when creating these appliances.

Update your list of software. This will connect to the apt repositories and get the latest list of packages. We will do the actual update in the next step.

Upgrade the distribution to ensure that you have the latest versions of all the packages.

Automatically clean the image so all unused packages are removed. This will ensure that the image stays free of cruft.

Now we have the base appliance image ready, we will use it to create some Xen appliances. You can make a backup of the original base image and every time you create an appliance you can use a copy as the starting point or template. The images are nothing but domU images, which are customized for running only specific applications. You start them up and run them like ay other Xen guest domains.

MySQL Database Server

MySQL is one of the most popular open-source databases in the world. It is a key component of the LAMP architecture – (Linux Apache MySQL and PHP). It is also very easy to get started with MySQL and is one of the key factors driving its adoption across the enterprise. In this section we will create a Xen appliance that will run a MySQL database server and also provide the ability to automatically backup the database on a given schedule.

Time for Action – Create our first Xen appliance

We will use our base Ubuntu Feisty domain image, and add MySQL and other needed software to it. Please ensure that you have updated your base image to the latest versions of the repositories and software packages before creating this appliance.

Install mysql-server using apt.

Once it is installed, Ubuntu will automatically start the database server. So before we make our other changes, stop MySQL.

Edit the /etc/mysql/my.cnf and comment out the line for the bind-address parameter. This will ensure that MySQL will accept connections from external machines and not just the localhost.

Start a mysql console session to test that everything is installed and working correctly.

Next we will install the utility for doing the automated backups. In order to do that we will first need to install the wget utility for transferring files. This is not a part of the base Ubuntu Feisty installation.

Download the automysqlbackup script from the website.

Copy this script to wherever you like, maybe /opt. Create a link to this location so it’s easy to do future updates.

Schedule this backup script to be run daily by creating a crontab entry for it, in the following format.

45 5 * * * root /opt/automysqlbackup.sh >/dev/null 2>&1

Now we have a MySQL database server with automatic daily backups as a nice reusable Xen appliance.

What just happened?

We created our first Xen appliance! It is running the open-source MySQL database server along with an automated backup of the database as per the given schedule. This image is essentially a domU image and it can be uploaded along with its configuration file to a repository somewhere, and can be used by anyone in the enterprise or elsewhere with their Xen server. You can either start up the domain manually as and when you need it or set it up to boot automatically when your xend server starts.

Ruby on Rails Appliance

Ruby on Rails is one of the hottest web development frameworks around. It is simple to use and you can use all the expressive power of the Ruby language. It provides a great feature set and has really put the Ruby language on the map. Ruby on Rails is gaining rapid adoption across the IT landscape and for a wide variety of web applications. In this section, we are going to create a Rails appliance that contains Ruby, Rails, and the Mongrel cluster for serving the Rails application and nginx web server for the static content. This appliance gives you a great starting point for your explorations into the world of Ruby on Rails and can be an excellent learning resource.

Time for Action – Rails on Xen

We will use our base Ubuntu Feisty domain image and add Rails and other needed software to it. Please ensure that you have updated your base image to the latest versions of the repositories and software packages before creating this appliance.

Install the packages required for compiling software on an Ubuntu system. This is required as we will be compiling some native extensions. Once the image is done, you can always remove this package if you want to save space.

Install Ruby and other packages that are needed for it.

Download the RubyGems package from RubyForge. We will use this to install any Ruby libraries or packages that we will need, including Rails.

Now install Rails. The first time when you run this command on a clean Ubuntu Feisty system, you will get the following error. Ignore this error and just run the command once again and it will work fine.

This will install Rails and all of its dependencies.

Create a new Rails application. This will create everything needed in a directory named xenbook.

$ rails xenbook

Change into the directory of the application that we created in the previous step and start the server up. This will start Ruby’s built-in web server, webrick by default.

Launch a web browser and navigate to the web page for our xenbook application.

We have everything working for a simple Rails install. However, we are using webrick, which is a bit slow. So let’s install the Mongrel server and use it with Rails. We will actually install mongrel_cluster that will let us use a cluster of Mongrel processes for serving up our Rails application.

Startup the server again. You will see that this time Rails starts up using the Mongrel server and not webrick. Rails is smart enough to recognize that you have Mongrel installed on the machine and automatically starts using it.

We have everything in place for serving up dynamic Rails pages. However, it advised to use a web server such as Apache for serving up the static parts of Rails. We will use tiny and super fast web server called nginx for this purpose. Let’s first install nginx from the Ubuntu repositories using apt. Ubuntu will automatically startup nginx once it is installed, using a default configuration. We will be modifying this configuration to match our environment later on in this section.

Copy the mongrel_cluster service file to Ubuntu’s startup services directory.

Change the permissions on it so it is executable. Also add mongrel_cluster to the Ubuntu startup services list so it starts up by default when the server starts up.

You can move the directory of the Rails application that we created earlier to the more standard location of /var/www. Change the permissions on the directory to be readable by the www-data user and group.

Generate the configuration for a cluster of three Mongrels running on port 8000. The user and group in our case, needs to be www-data. If you change your mind about the user and group, you can always change it after the file has been generated by editing the file.

Create a symbolic link for the configuration file that we just generated in our xenbook application directory to the /etc location.

You can now see that everything is up and running by listing the processes on the system using the ps command. You will see a cluster of three Mongrels and three master nginx processes running.

Now you should be able to access your application on port 80 of your server.

We have successfully setup our Rails application serving up the Rails pages using a cluster of Mongrels for dynamic data and nginx for super fast static web serving.

What just happened?

We installed everything needed to run a simple Rails application on our appliance image. We installed Ruby along with its development libraries. These libraries are needed when compiling Ruby extensions. We also installed the compilers and associated packages needed for doing the actual compilations on our image. We installed the RubyGems package. RubyGems are somewhat analogous to the jar files in the Java world and the egg packaging format used in the Python world. They are a simple way to package Ruby extensions and libraries into one downloadable archive. This archive contains metadata describing how to actually lay it down on a target system and how to compile any native extensions if needed. Most of the software libraries and applications in the Ruby world are distributed using this mechanism these days. We installed Rails, Mongrel and mongrel_cluster using their gems. Mongrel is the replacement web server for webrick which is tightly integrated with Rails and is able to serve up Rails application in a much more performant manner. The recommended and scalable way of using Mongrel is to cluster it and use a pack of running Mongrel processes for serving a Rails application. We then finally installed nginx. It is a super fast web server and reverse proxy. It has a very small footprint and is able to serve static items much faster than either Apache or lighttpd. It is fast becoming the favored way to deploy a Rails application with a cluster of Mongrels. We configured everything to work together and voila! We have a Ruby on Rails Xen appliance ready for serving up your Rails applications in a Xen environment!

Subversion Appliance

Subversion is an open-source revision control system that was designed to be a simple and efficient alternative to CVS. It is widely used in the open-source community and a lot of high profile projects such as KDE have switched over to it recently. In this section we are going to create a Xen appliance that contains Subversion and Apache. This appliance can be used by your development team or you can use it for playing with and experimenting with Subversion itself.

Time for Action – A revision control appliance

We will use our base Ubuntu Feisty domain image and add Subversion and other needed software to it. Please ensure that you have updated your base image to the latest versions of the repositories and software packages before creating this appliance.

Install Subversion first using apt.

Install Apache2 if you don’t already have it installed.

Add basic authentication to the SVN server.

Create a directory for storing the Subversion repository. Create the actual repository itself and set its permissions to be writable by the www-data user and group.

Configure DAV and SVN by editing the file - /etc/apache2/mods-available/dav_svn.conf. Here are the contents of that file with all the comments removed to save space.

DAV svn

SVNPath /var/svn/repos

AuthType Basic

AuthName "Subversion Repository"

AuthUserFile /etc/apache2/dav_svn.passwd

Require valid-user

Restart apache.

# /etc/init.d/apache2 restart

Import any file into the repository for testing to making sure your SVN installation is working properly.

Try to access the SVN repository in your browser and you should be presented with a username and password prompt.

Put in your username and password and you should be able to access your spanking new SVN repository.

What just happened?

We created a Xen appliance that provides a Subversion repository that is served by Apache2 using a basic authentication scheme. We strove for simplicity in this appliance and did not try to add and use all of the myriad things that are possible using SVN and Apache2. The idea is to show you how simple it is to get started with using Subversion. You can experiment with Subversion and add functionality that you need and extend this appliance as you go.

Summary

In this article, we explored the use of Xen appliances to provide targeted pieces of functionality in small ready to use packages. They are a simple way to share and reuse your domU images that are customized for providing a certain use. In this article, we created these appliances:

MySQL Database – An appliance that provides the populate MySQL server along with a way to automatically backup the database on a specified schedule.

Ruby on Rails appliance – An appliance that provides a way to host a Rails application that runs using a cluster of Mongrels fronted by nginx web server processes.

Subversion - An appliance that provides a Subversion repository served up by using Apache2.

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.