NAME

Rex::Commands - All the basic commands

DESCRIPTION

This module is the core commands module.

SYNOPSIS

desc "Task description";

task "taskname", sub { ... };
task "taskname", "server1", ..., "server20", sub { ... };

group "group" => "server1", "server2", ...;

user "user";

password "password";
    
environment live => sub {
   user "root";
   password "foobar";
   pass_auth;
   group frontend => "www01", "www02";
};

COMMANDLIST

EXPORTED FUNCTIONS

no_ssh([$task])

Disable ssh for all tasks or a specified task.

If you want to disable ssh connection for your complete tasks (for example if you only want to use libVirt) put this in the main section of your Rexfile.

no_ssh;

If you want to disable ssh connection for a given task, put no_ssh in front of the task definition.

no_ssh task "mytask", "myserver", sub {
   say "Do something without a ssh connection";
};
task($name [, @servers], $funcref)

This function will create a new task.

Create a local task (a server independent task)
task "mytask", sub {
   say "Do something";
};

If you call this task with (R)?ex it will run on your local machine. You can explicit run this task on other machines if you specify the -H command line parameter.

Create a server bound task.
task "mytask", "server1", sub {
   say "Do something";
};

You can also specify more than one server.

task "mytask", "server1", "server2", "server3", sub {
   say "Do something";
};

Or you can use some expressions to define more than one server.

task "mytask", "server[1..3]", sub {
   say "Do something";
};

If you want, you can overwrite the servers with the -H command line parameter.

Create a group bound task.

You can define server groups with the group function.

group "allserver" => "server[1..3]", "workstation[1..10]";

task "mytask", group => "allserver", sub {
   say "Do something";
};
desc($description)

Set the description of a task.

desc "This is a task description of the following task";
task "mytask", sub {
   say "Do something";
}
group($name, @servers)

With this function you can group servers, so that you don't need to write too much ;-)

group "servergroup", "www1", "www2", "www3", "memcache01", "memcache02", "memcache03";

Or with the expression syntax.

group "servergroup", "www[1..3]", "memcache[01..03]";
batch($name, @tasks)

With the batch function you can call tasks in a batch.

batch "name", "task1", "task2", "task3";

And call it with the -b console parameter. rex -b name

user($user)

Set the user for the ssh connection.

password($password)

Set the password for the ssh connection (or for the private key file).

port($port)

Set the port where the ssh server is listening.

sudo_password($password)

Set the password for the sudo command.

timeout($seconds)

Set the timeout for the ssh connection and other network related stuff.

max_connect_retries($count)

Set the maximum number of connection retries.

get_random($count, @chars)

Returns a random string of $count characters on the basis of @chars.

my $rnd = get_random(8, 'a' .. 'z');
do_task($task)

Call $task from an other task. Will execute the given $task with the servers defined in $task.

task "task1", "server1", sub {
   say "Running on server1";
   do_task "task2";
};

task "task2", "server2", sub {
   say "Running on server2";
};

You may also use an arrayRef for $task if you want to call multiple tasks.

do_task [ qw/task1 task2 task3/ ];
public_key($key)

Set the public key.

private_key($key)

Set the private key.

pass_auth

If you want to use password authentication, then you need to call pass_auth.

user "root";
password "root";

pass_auth;
key_auth

If you want to use pubkey authentication, then you need to call key_auth.

user "root";
password "root";

pass_auth;
parallelism($count)

Will execute the tasks in parallel on the given servers. $count is the thread count to be used.

logging

With this function you can define the logging behaviour of (R)?ex.

Logging to a file
logging to_file => "rex.log";
Logging to syslog
logging to_syslog => $facility;
needs($package [, @tasks])

With needs you can define dependencies between tasks. The "needed" tasks will be called with the same server configuration as the calling task.

needs will not execute before, around and after hooks.

Depend on all tasks in a given package.

Depend on all tasks in the package MyPkg. All tasks will be called with the server server1.

task "mytask", "server1", sub {
   needs MyPkg;
};
Depend on a single task in a given package.

Depend on the uname task in the package MyPkg. The uname task will be called with the server server1.

task "mytask", "server1", sub {
   needs MyPkg "uname";
};
environment($name => $code)

Define an environment. With environments one can use the same task for different hosts. For example if you want to use the same task on your integration-, test- and production servers.

# define default user/password
user "root";
password "foobar";
pass_auth;
    
# define default frontend group containing only testwww01.
group frontend => "testwww01";
    
# define live environment, with different user/password 
# and a frontend server group containing www01, www02 and www03.
environment live => sub {
   user "root";
   password "livefoo";
   pass_auth;
      
   group frontend => "www01", "www02", "www03";
};
    
# define stage environment with default user and password. but with 
# a own frontend group containing only stagewww01.
environment stage => sub {
   group frontend => "stagewww01";
};
   
task "prepare", group => "frontend", sub {
    say run "hostname";
};

Calling this task rex prepare will execute on testwww01. Calling this task with rex -E live prepare will execute on www01, www02, www03. Calling this task rex -E stage prepare will execute on stagewww01.

LOCAL(&)

With the LOCAL function you can do local commands within a task that is defined to work on remote servers.

task "mytask", "server1", "server2", sub {
    # this will call 'uptime' on the servers 'server1' and 'server2'
    say run "uptime";
    
    # this will call 'uptime' on the local machine.
    LOCAL {
       say run "uptime";
    };
};
path(@path)

Set the execution path for all commands.

path "/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/pkg/bin", "/usr/pkg/sbin";
set($key, $value)

Set a configuration parameter. These Variables can be used in templates as well.

set database => "db01";
     
task "prepare", sub {
   my $db = get "database";
};

Or in a template

DB: <%= $::database %>
get($key, $value)

Get a configuration parameter.

set database => "db01";
     
task "prepare", sub {
   my $db = get "database";
};

Or in a template

DB: <%= $::database %>
before($task => sub {})

Run code before connecting to the server.

before mytask => sub {
  my ($server) = @_;
  run "vzctl start vm$server";
};
after($task => sub {})

Run code after the task is finished.

after mytask => sub {
  my ($server, $failed) = @_;
  if($failed) { say "Connection to $server failed."; }

  run "vzctl stop vm$server";
};
around($task => sub {})

Run code before and after the task is finished.

around mytask => sub {
  my ($server, $position) = @_;

  unless($position) {
     say "Before Task\n";
  }
  else {
     say "After Task\n";
  }
};

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 19:

Non-ASCII character seen before =encoding in '{ ...'. Assuming UTF-8

Around line 753:

'=item' outside of any '=over'

=over without closing =back