Skip to content

Commit cabbc6a

Browse files
author
Jan
committed
initial commit
0 parents  commit cabbc6a

File tree

9 files changed

+425
-0
lines changed

9 files changed

+425
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
*.sw*
3+

README.pod

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
=pod
2+
3+
(R)?ex is a small script to ease the execution of remote commands. You can write small tasks in a file named I<Rexfile>.
4+
5+
=head2 Dependencies
6+
7+
=over 4
8+
9+
=item *
10+
11+
L<Net::SSH::Expect>
12+
13+
=item *
14+
15+
L<Scope::With>
16+
17+
=back
18+
19+
=head2 Usage
20+
21+
A small example:
22+
23+
user "root";
24+
25+
desc "Show Unix version";
26+
task "uname", "server1", "server2", sub {
27+
run "uname -a";
28+
};
29+
30+
bash# rex -T
31+
Tasks
32+
uname Show Unix version
33+
34+
bash# rex uname
35+
Running task: uname
36+
Connecting to server1 (root)
37+
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
38+
Running task: uname
39+
Connecting to server2 (root)
40+
Linux debian01 2.6.26-2-amd64 #1 SMP Tue Aug 31 09:11:22 UTC 2010 x86_64 GNU/Linux
41+
=cut

bin/rex

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env perl
2+
3+
#
4+
# (c) Jan Gehring <jan.gehring@gmail.com>
5+
#
6+
# vim: set ts=3 sw=3 tw=0:
7+
# vim: set expandtab:
8+
9+
10+
use strict;
11+
use warnings;
12+
13+
use FindBin;
14+
15+
use lib "$FindBin::Bin/../lib";
16+
17+
use Rex::Config;
18+
use Rex::Group;
19+
use Rex::Task;
20+
use Rex::Commands;
21+
use Rex::Commands::Run;
22+
23+
use Getopt::Std;
24+
use Scope::With;
25+
26+
use constant VERSION => '1.0.0';
27+
28+
my %opts;
29+
getopts('Thv', \%opts);
30+
31+
if($opts{'h'}) {
32+
print "(R)?ex - (Remote)? Execution\n";
33+
printf " %-15s %s\n", "-T", "List all known tasks.";
34+
printf " %-15s %s\n", "-h", "Display this help";
35+
printf " %-15s %s\n", "-v", "Display (R)?ex Version";
36+
exit 0;
37+
} elsif($opts{'v'}) {
38+
print "(R)?ex " . VERSION . "\n";
39+
exit 0;
40+
}
41+
42+
if(-f "Rexfile") {
43+
eval {
44+
do("Rexfile");
45+
};
46+
47+
if($@) { print $@ . "\n"; exit 1; }
48+
} else {
49+
print STDERR "No Rexfile found.\n";
50+
exit 1;
51+
}
52+
53+
if($opts{'T'}) {
54+
print "Tasks\n";
55+
for my $task (Rex::Task->get_tasks) {
56+
printf " %-25s %s\n", $task, Rex::Task->get_desc($task);
57+
}
58+
}
59+
60+
if(defined $ARGV[0]) {
61+
if(Rex::Task->is_task($ARGV[0])) {
62+
Rex::Task->run($ARGV[0]);
63+
}
64+
}
65+

doc/Rexfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
user "root";
2+
3+
group "frontend", "server1", "server2", "server3", "server4";
4+
group "local", "mango", "debian01";
5+
6+
desc "Search the libs";
7+
task "search_lib", group => frontend, sub {
8+
run "ls /usr/lib |wc -l";
9+
};
10+
11+
desc "Show free space on /";
12+
task "show_disk_free", sub {
13+
run "df -h";
14+
};
15+
16+
desc "Start test server";
17+
task "start_server", sub {
18+
print "start_server\n";
19+
};
20+
21+
desc "Show Unix version";
22+
task "uname", group => "local", sub {
23+
run "export MYVAR='(R)?ex'";
24+
run "uname -a";
25+
run "echo 'Running: ' \$MYVAR";
26+
run "id";
27+
};
28+

lib/Rex/Commands.pm

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# (c) Jan Gehring <jan.gehring@gmail.com>
3+
#
4+
# vim: set ts=3 sw=3 tw=0:
5+
# vim: set expandtab:
6+
7+
package Rex::Commands;
8+
9+
use strict;
10+
use warnings;
11+
12+
use Data::Dumper;
13+
14+
require Exporter;
15+
16+
use vars qw(@EXPORT $current_desc);
17+
use base qw(Exporter);
18+
19+
@EXPORT = qw(task desc group user password);
20+
21+
sub task {
22+
my($class, $file, @tmp) = caller;
23+
my $task_name = shift;
24+
if($class ne "main") {
25+
$task_name = $class . ":" . $task_name;
26+
}
27+
28+
if($current_desc) {
29+
push(@_, $current_desc);
30+
$current_desc = "";
31+
}
32+
33+
Rex::Task->create_task($task_name, @_);
34+
}
35+
36+
sub desc {
37+
$current_desc = shift;
38+
}
39+
40+
sub group {
41+
Rex::Group->create_group(@_);
42+
}
43+
44+
sub user {
45+
Rex::Config->set_user(@_);
46+
}
47+
48+
sub password {
49+
Rex::Config->set_password(@_);
50+
}
51+
52+
1;

lib/Rex/Commands/Run.pm

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#
2+
# (c) Jan Gehring <jan.gehring@gmail.com>
3+
#
4+
# vim: set ts=3 sw=3 tw=0:
5+
# vim: set expandtab:
6+
7+
package Rex::Commands::Run;
8+
9+
use strict;
10+
use warnings;
11+
12+
require Exporter;
13+
use Data::Dumper;
14+
15+
use vars qw(@EXPORT);
16+
use base qw(Exporter);
17+
18+
@EXPORT = qw(run);
19+
20+
sub run {
21+
my $cmd = shift;
22+
23+
my @ret = ();
24+
if(defined $::ssh) {
25+
$::ssh->send($cmd);
26+
27+
while(defined (my $line = $::ssh->read_line()) ) {
28+
$line =~ s/[\r\n]//gms;
29+
next if($line =~ m/^$/);
30+
push @ret, $line;
31+
}
32+
33+
shift @ret;
34+
} else {
35+
push @ret, `$cmd`;
36+
chomp @ret;
37+
}
38+
39+
if(scalar(@ret) >= 1) {
40+
print join("\n", @ret);
41+
print "\n";
42+
}
43+
44+
return join("\n", @ret);
45+
}
46+
47+
1;

lib/Rex/Config.pm

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# (c) Jan Gehring <jan.gehring@gmail.com>
3+
#
4+
# vim: set ts=3 sw=3 tw=0:
5+
# vim: set expandtab:
6+
7+
package Rex::Config;
8+
9+
use strict;
10+
use warnings;
11+
12+
use vars qw($user $password);
13+
14+
sub set_user {
15+
my $class = shift;
16+
$user = shift;
17+
}
18+
19+
sub set_password {
20+
my $class = shift;
21+
$password = shift;
22+
}
23+
24+
sub get_user {
25+
my $class = shift;
26+
return $user;
27+
}
28+
29+
sub get_password {
30+
my $class = shift;
31+
return $password;
32+
}
33+
34+
1;

lib/Rex/Group.pm

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#
2+
# (c) Jan Gehring <jan.gehring@gmail.com>
3+
#
4+
# vim: set ts=3 sw=3 tw=0:
5+
# vim: set expandtab:
6+
7+
package Rex::Group;
8+
9+
use strict;
10+
use warnings;
11+
12+
13+
use vars qw(%groups);
14+
15+
sub create_group {
16+
my $class = shift;
17+
my $group_name = shift;
18+
my @server = @_;
19+
20+
@{$groups{$group_name}} = @server;
21+
}
22+
23+
sub get_group {
24+
my $class = shift;
25+
my $group_name = shift;
26+
27+
return @{$groups{$group_name}};
28+
}
29+
30+
sub is_group {
31+
my $class = shift;
32+
my $group_name = shift;
33+
34+
if(defined $groups{$group_name}) { return 1; }
35+
return 0;
36+
}
37+
1;

0 commit comments

Comments
 (0)