Skip to content
Dean Hamstead edited this page Nov 18, 2022 · 4 revisions

Writing Modules

This is applicable since Rex 0.33.

If you want to share tasks between projects or with others you can write modules. Modules are in fact normal Perl Packages.

Creating Directory structure

A module exists of a directory and a Module.pm file inside it.

For example:

+ Apache/   <-- Directory
+-- Module.pm   <-- Module file

Inside this Apache Module you can put additional files, like configuration files or templates.

+ Apache/
+--+ templates/
   +-- httpd.conf.tpl
+-- Module.pm

The Module.pm File

Inside the Module.pm File you define the tasks you want to share.

package Apache;
  
use Rex -base;
 
task prepare => sub {
   install "apache2";
   install "libapache2-php5";
};
 
task configure => sub {
   file "/etc/apache2/httpd.conf",
      content => template("templates/httpd.conf.tpl"),
      owner   => "root";
};
 
1; # don't forget this last line!

Using the Module

If you want to use your new module you have to do 2 things.

First, create a directory called lib in the same directory where your Rexfile is.

+-- Rexfile
+-- lib/

After this copy your module into the lib directory.

+-- Rexfile
+--+ lib/
   +--+ Apache/
      +--+ templates/
         +-- httpd.conf.tpl
      +-- Module.pm

Than you can include your Module into your Rexfile.

# Rexfile
include qw/Apache/;
 
user "root";

group httpds => "webserver[01..10]";
group mysqls => "db[01..04]";
  
task "setup_httpds", group => "httpds", sub {
   Apache::prepare();
   Apache::configure();
};
 
task "setup_mysqls", group => "mysqls", sub {
   # code
};

Share your Module

If you want to share your module you can now upload it to any version control system and share it with your colleagues or other people.