Skip to content
Permalink
Browse files

initial commit

  • Loading branch information...
Jan
Jan committed Nov 5, 2010
0 parents commit cabbc6a8aa9eb2c0d9d3fc2439be51924edc3ff8
Showing with 425 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +41 −0 README.pod
  3. +65 −0 bin/rex
  4. +28 −0 doc/Rexfile
  5. +52 −0 lib/Rex/Commands.pm
  6. +47 −0 lib/Rex/Commands/Run.pm
  7. +34 −0 lib/Rex/Config.pm
  8. +37 −0 lib/Rex/Group.pm
  9. +118 −0 lib/Rex/Task.pm
@@ -0,0 +1,3 @@
.DS_Store
*.sw*

@@ -0,0 +1,41 @@
=pod

(R)?ex is a small script to ease the execution of remote commands. You can write small tasks in a file named I<Rexfile>.

=head2 Dependencies

=over 4

=item *

L<Net::SSH::Expect>

=item *

L<Scope::With>

=back

=head2 Usage

A small example:

user "root";

desc "Show Unix version";
task "uname", "server1", "server2", sub {
run "uname -a";
};

bash# rex -T
Tasks
uname Show Unix version

bash# rex uname
Running task: uname
Connecting to server1 (root)
Linux mango 2.6.27-openvz-briullov.1-r4 #1 SMP Tue Nov 24 23:25:52 CET 2009 x86_64 Intel(R) Pentium(R) D CPU 2.80GHz GenuineIntel GNU/Linux
Running task: uname
Connecting to server2 (root)
Linux debian01 2.6.26-2-amd64 #1 SMP Tue Aug 31 09:11:22 UTC 2010 x86_64 GNU/Linux
=cut
65 bin/rex
@@ -0,0 +1,65 @@
#!/usr/bin/env perl

#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=3 sw=3 tw=0:
# vim: set expandtab:


use strict;
use warnings;

use FindBin;

use lib "$FindBin::Bin/../lib";

use Rex::Config;
use Rex::Group;
use Rex::Task;
use Rex::Commands;
use Rex::Commands::Run;

use Getopt::Std;
use Scope::With;

use constant VERSION => '1.0.0';

my %opts;
getopts('Thv', \%opts);

if($opts{'h'}) {
print "(R)?ex - (Remote)? Execution\n";
printf " %-15s %s\n", "-T", "List all known tasks.";
printf " %-15s %s\n", "-h", "Display this help";
printf " %-15s %s\n", "-v", "Display (R)?ex Version";
exit 0;
} elsif($opts{'v'}) {
print "(R)?ex " . VERSION . "\n";
exit 0;
}

if(-f "Rexfile") {
eval {
do("Rexfile");
};

if($@) { print $@ . "\n"; exit 1; }
} else {
print STDERR "No Rexfile found.\n";
exit 1;
}

if($opts{'T'}) {
print "Tasks\n";
for my $task (Rex::Task->get_tasks) {
printf " %-25s %s\n", $task, Rex::Task->get_desc($task);
}
}

if(defined $ARGV[0]) {
if(Rex::Task->is_task($ARGV[0])) {
Rex::Task->run($ARGV[0]);
}
}

@@ -0,0 +1,28 @@
user "root";

group "frontend", "server1", "server2", "server3", "server4";
group "local", "mango", "debian01";

desc "Search the libs";
task "search_lib", group => frontend, sub {
run "ls /usr/lib |wc -l";
};

desc "Show free space on /";
task "show_disk_free", sub {
run "df -h";
};

desc "Start test server";
task "start_server", sub {
print "start_server\n";
};

desc "Show Unix version";
task "uname", group => "local", sub {
run "export MYVAR='(R)?ex'";
run "uname -a";
run "echo 'Running: ' \$MYVAR";
run "id";
};

@@ -0,0 +1,52 @@
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=3 sw=3 tw=0:
# vim: set expandtab:

package Rex::Commands;

use strict;
use warnings;

use Data::Dumper;

require Exporter;

use vars qw(@EXPORT $current_desc);
use base qw(Exporter);

@EXPORT = qw(task desc group user password);

sub task {
my($class, $file, @tmp) = caller;
my $task_name = shift;
if($class ne "main") {
$task_name = $class . ":" . $task_name;
}

if($current_desc) {
push(@_, $current_desc);
$current_desc = "";
}

Rex::Task->create_task($task_name, @_);
}

sub desc {
$current_desc = shift;
}

sub group {
Rex::Group->create_group(@_);
}

sub user {
Rex::Config->set_user(@_);
}

sub password {
Rex::Config->set_password(@_);
}

1;
@@ -0,0 +1,47 @@
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=3 sw=3 tw=0:
# vim: set expandtab:

package Rex::Commands::Run;

use strict;
use warnings;

require Exporter;
use Data::Dumper;

use vars qw(@EXPORT);
use base qw(Exporter);

@EXPORT = qw(run);

sub run {
my $cmd = shift;

my @ret = ();
if(defined $::ssh) {
$::ssh->send($cmd);

while(defined (my $line = $::ssh->read_line()) ) {
$line =~ s/[\r\n]//gms;
next if($line =~ m/^$/);
push @ret, $line;
}

shift @ret;
} else {
push @ret, `$cmd`;
chomp @ret;
}

if(scalar(@ret) >= 1) {
print join("\n", @ret);
print "\n";
}

return join("\n", @ret);
}

1;
@@ -0,0 +1,34 @@
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=3 sw=3 tw=0:
# vim: set expandtab:

package Rex::Config;

use strict;
use warnings;

use vars qw($user $password);

sub set_user {
my $class = shift;
$user = shift;
}

sub set_password {
my $class = shift;
$password = shift;
}

sub get_user {
my $class = shift;
return $user;
}

sub get_password {
my $class = shift;
return $password;
}

1;
@@ -0,0 +1,37 @@
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=3 sw=3 tw=0:
# vim: set expandtab:

package Rex::Group;

use strict;
use warnings;


use vars qw(%groups);

sub create_group {
my $class = shift;
my $group_name = shift;
my @server = @_;

@{$groups{$group_name}} = @server;
}

sub get_group {
my $class = shift;
my $group_name = shift;

return @{$groups{$group_name}};
}

sub is_group {
my $class = shift;
my $group_name = shift;

if(defined $groups{$group_name}) { return 1; }
return 0;
}
1;

0 comments on commit cabbc6a

Please sign in to comment.
You can’t perform that action at this time.