Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
initial commit
  • Loading branch information
Jan committed Nov 5, 2010
0 parents commit cabbc6a
Show file tree
Hide file tree
Showing 9 changed files with 425 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.DS_Store
*.sw*

41 changes: 41 additions & 0 deletions README.pod
@@ -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 changes: 65 additions & 0 deletions 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]);
}
}

28 changes: 28 additions & 0 deletions doc/Rexfile
@@ -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";
};

52 changes: 52 additions & 0 deletions lib/Rex/Commands.pm
@@ -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;
47 changes: 47 additions & 0 deletions lib/Rex/Commands/Run.pm
@@ -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;
34 changes: 34 additions & 0 deletions lib/Rex/Config.pm
@@ -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;
37 changes: 37 additions & 0 deletions lib/Rex/Group.pm
@@ -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.