Using Rex To Automate Your Datacentre

In my day to day work I come across a lot of tasks that I actually “hate” to be honest. Most of these tasks consist of things that are repetitive and take up a lot of time. A co-worker of mine once told me, “if you have to do a task more than once, automate it!”. This is one piece of advice that intrigued me. Ever since that day I have been thinking about automation and how to make processes flow smoother than they did before. But the main question always is, what tool are you going to use for that?
if(typeof __ez_fad_position != ‘undefined’){__ez_fad_position(‘div-gpt-ad-howtoforge_com-box-3-0’)};

 if(typeof __ez_fad_position != ‘undefined’){__ez_fad_position(‘div-gpt-ad-howtoforge_com-medrectangle-3-0’)};

Puppet, Chef, CFEngine etc. etc.

As a system or hosting engineer or whatever normally does is first type in “datacentre automation” in Google and what you get is a lot of commercial companies trying to sell you automation frameworks. Things like Puppet, Chef etc. come along and when you look at their documentation, well, it’s huge and takes a lot of time to install and configure properly. Not to mention that you need to install a daemon on every server to get the framework working (imagine trying to do that on 40-50 servers, and imagine if you have like 100-200 servers. So that wasn’t actually an option for me at the time of my quest to find an easy framework to automate my tasks.

 

Rex

Rex is a small and lightweight framework I came across visiting a website (I forgot which one it was). At first I wasn’t to sure what to make up out of it, but while browsing trough the documentation I got excited and saw the possibilities of using it. Within minutes I got it up and running (thanks to their handy-dandy YUM repository (I use CentOS)), and the cool thing is, it’s written in Perl so in theory you could run it on any platform (Windows included). The big advantage to Rex is that you can use your ssh keys (or user-pass combo) from your “master” server where you have Rex installed and connect using ssh so you don’t have to install daemons on your target servers!

 

How Rex is structured

When you first create a Rex project (easy after you install it check the install docs: http://rexify.org/get/index.html) by using “rexify”. It will create a folder for you with a very clean structure:

  • A file called a Rexfile, here you define all your hosts and usernames and passwords
  • A lib folder with a perl module (.pm file) in it. It automatically creates a task for the default server group called uptime so you can test if your connection with your server group is working

 

Give me an example of what I could use?!

Well, everyone knows how important it is to keep all your servers up to date. So what I did was that I created a task that updates all my servers (using yum update) and checks if it succeeded, and reports to me by email, see an example below with the standard uptime task:

package task;
use Rex -base;
desc "Get uptime of server";
task "uptime", group => 'all', sub {
   say run "uptime";
};
desc "Run updates";
task "runupdate", group => 'all', sub {
        # Variables
        my $EMAIL='<your e-mail>';
        my $hostname = connection->server;
        # Execute yum update
        say "Executing yum update on $hostname";
        sudo "yum -y -e 0 -d 0 update";
        if($? == 0) {
             run 'echo "Update was successful" | mail -s "Update was completed on `hostname -s`" ' . $EMAIL;
        } else {
             run 'echo "Update was not successful" | mail -s "Update did not work on `hostname -s`" ' . $EMAIL;
        };
};

 if(typeof __ez_fad_position != ‘undefined’){__ez_fad_position(‘div-gpt-ad-howtoforge_com-medrectangle-4-0’)};

Give me MORE! I need MORE!

I think to best understand the full potential you’re just going to have to try it out for yourself, install it and start experimenting. Check the rexify.org website for full documentation (it’s pretty well documented and if you don’t understand something just go to the #rex IRC channel on FreeNode, the developer is available there aswell). I would just like to take some time and thank jfried, the developer of this marvelous framework! Support and help out, and make this framework even bigger!

 Don’t forget to check out my blog: http://livebyt.es for more articles about hosting! 

About the Author

Leave a Reply