Posted at
PerlDay 13

How to use Rex to manage VirtualBox

More than 5 years have passed since last update.

Fist of all, I'm Chinese. I found no PerlChina Advent and came here. Can here post some artices without using japanese? Seems no prohibit~OK, let's go!

Rex means Remote EXecute. It's a Deployment & Configuration Management, which works like ansible, and DSL designed like puppet. Find here: http://www.rexify.org/

But today, I will show something different -- use rex to manage a vbox -- yes, just like vagrant do!

First, generate a vbox template:

$ rexify project-name --template box

$ cd project-name

Now we get a file called 'Rexfile', just like 'Vagrantfile' after vagrant init. An example of Rexfile as follow:


Rexfile

set box => "VBox";

task mytask => sub {
box {
my ($box) = @_;
$box->name("boxname");
$box->url("http://box.rexify.org/box/base-image.box");
$box->network(1 => {
type => "bridged" # default "nat",
bridge => "eth0",
});
$box->forward_port(ssh => [2222, 22]);
$box->share_folder(boxhome => "/path/to/myuser");
$box->auth(
user => "root",
password => "box",
);
$box->setup(qw/setup_frontend/);
};
};

Then, we can run the box start/stop:

$ rex init --name=boxname

$ rex stop --name=boxname

Attention, there is one setup task in the Rexfile, we can define our own automatic task to customize this box after it start, a small example as follow:


Rexfile

task 'setup_frontend', sub {

install nginx;
file '/etc/nginx.conf',
content => template('template/httpd.conf.tpl'),
owner => "root",
group => "root",
on_change => sub { service nginx => "restart"; };
};

Vagrant has provision support for puppet/chef, Rex do it all himself.

BTW, Rex support to define these vboxes in YAML:


boxes.yml

type: VBox

vms:
fe01:
url: http://box.rexify.org/box/ubuntu-server-12.10-amd64.ova
network:
1:
type: bridged
bridge: eth0
setup: setup_frontend
db01:
url: http://box.rexify.org/box/ubuntu-server-12.10-amd64.ova
network:
1:
type: bridged
bridge: eth0
setup: setup_db

Only one line was needed if we use YAML:

use Rex::Commands::Box init_file => "boxes.yml";

Rex support VMware and Amazon vms too