< back

OpenVZ API

A Ruby API to automate OpenVZ Administration

I love OpenVZ, I think its one of the easiest to use virtualisation technologies on the market and it adds almost no overhead compared to other technologies.

I’ve been using it for a couple of years now and I always wanted to have a nicer way to automate container creation, configuration or actions than writing shell scripts. There are already a couple of webinterfaces around, but none of them I liked.

Another possibility would be to use libvirt - but libvirt always felt a bit too complex, since its a general API implementation for several hypervisors.

So I started to implement my own API, which should rather be a simple and minimalistic approach. The project is hosted on GitHub but you can as well install it by Rubygems.

Get ruby-openvz

Installing

 $ gem install openvz

Example: Restart

 require 'rubygems'
 require 'openvz'

 container = OpenVZ::Container.new('109')
 container.restart

Example: Provisioning

Here is the example of how a whole provisioning for a new container could look like:

 require 'rubygems'
 require 'openvz'

 container = OpenVZ::Container.new('110')

 container.create( :ostemplate => 'debain-6.0-boostrap',
                   :config     => 'vps.basic' )

 container.deboostrap( :dist   => 'squeeze',
                       :mirror => 'http://cdn.debian.net/debian' )

 container.set( :nameserver => '8.8.8.8',
                :ipadd      => '10.0.0.2',
                :hostname   => 'foo.ono.at' )

 container.start

 # Update the system
 container.command('aptitude update ; aptitude -y upgrade ; apt-key update')

 # Install puppet
 container.command('aptitude -o Aptitude::Cmdline::ignore-trust-violations=true -y install puppet')

 # Install puppet
 container.command('puppetd -t --server=puppet.ono.at')

The first version is basically able to replace the provisioning scripts I’ve been using for a while. In future versions I’d probably like to integrate this into mcollective and puppet to orchestrate my machines. :-)