Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bin/rex
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ BEGIN {

require Rex;
use Rex::CLI;
use Data::Dumper;

my $rex = Rex::CLI->new;
my $app = Rex->instance;
my $rex = Rex::CLI->new(app => $app);
$rex->__run__();

__END__
Expand Down
25 changes: 15 additions & 10 deletions lib/Rex.pm
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ use warnings;

# VERSION

use Moose;
use MooseX::Singleton;

# development version if this variable is not set
if ( !$Rex::VERSION ) {
Expand All @@ -94,6 +94,13 @@ BEGIN {
use Rex::Report;
use Rex::Notify;
use Rex::Require;

use Rex::MultiSub;
use Rex::MultiSub::PosValidatedList;
use Rex::MultiSub::ValidatedHash;
use Rex::MultiSub::Resource;
use Rex::MultiSub::Function;

use File::Basename;
eval { Net::SSH2->require; };
}
Expand All @@ -112,6 +119,13 @@ has feature_flags => (
isa => 'ArrayRef',
);

has run_stages => (
is => 'ro',
isa => 'ArrayRef',
default => sub { [] },
writer => '_set_run_stages',
);

has output => (
is => 'ro',
isa => 'Rex::Output',
Expand All @@ -121,15 +135,6 @@ has output => (
},
);

my $INSTANCE;

# returns a singleton
sub instance {
my $class = shift;
return $INSTANCE if $INSTANCE;
$INSTANCE = $class->new(@_);
}

sub push_lib_to_inc {
my $path = shift;

Expand Down
10 changes: 7 additions & 3 deletions lib/Rex/CLI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use warnings;

use Moose;

extends qw(Rex);

use FindBin;
use File::Basename qw(basename dirname);
use Time::HiRes qw(gettimeofday tv_interval);
Expand Down Expand Up @@ -56,13 +54,19 @@ if ( $#ARGV < 0 ) {
@ARGV = qw(-h);
}

has app => (
is => 'ro',
isa => 'Rex',
);

has run_list => (
is => 'ro',
isa => 'Rex::RunList',
lazy => 1,
default => sub {
my $self = shift;
Rex::RunList->instance( app => $self );
Rex::RunList->app( Rex->instance );
Rex::RunList->instance;
}
);

Expand Down
14 changes: 4 additions & 10 deletions lib/Rex/Commands/Task.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use warnings;
# VERSION

require Rex::Exporter;
use Data::Dumper;

use vars qw(@EXPORT);
use base qw(Rex::Exporter);
Expand Down Expand Up @@ -176,7 +177,8 @@ sub task {
use warnings;

$options->{'dont_register'} ||= $dont_register_tasks;
my $task_o = Rex::TaskList->create()->create_task( $task_name, @_, $options );
my $task_o = Rex::TaskList->create( app => Rex->instance )
->create_task( $task_name, @_, $options );

if (!$class->can($task_name_save)
&& $task_name_save =~ m/^[a-zA-Z_][a-zA-Z0-9_]+$/ )
Expand All @@ -188,15 +190,7 @@ sub task {
Rex::Logger::info("Running task $task_name on current connection");
my $param;

if ( scalar @_ == 1 && ref $_[0] eq "HASH" ) {
$param = $_[0];
}
elsif ( $REGISTER_SUB_HASH_PARAMETER && scalar @_ % 2 == 0 ) {
$param = {@_};
}
else {
$param = \@_;
}
$param = {@_};

$task_o->run( "<func>", params => $param );
};
Expand Down
21 changes: 3 additions & 18 deletions lib/Rex/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -795,12 +795,7 @@ sub get_template_function {
return sub {
my ( $content, $template_vars ) = @_;
$template_vars = {
Rex::Commands::Task::task()->get_opts,
(
Rex::Resource->is_inside_resource
? %{ Rex::Resource->get_current_resource()->get_all_parameters }
: ()
),
%{ Rex->instance->current_run_stage->params },
%{ $template_vars || {} }
}
if ( Rex::Commands::Task::task() );
Expand All @@ -814,12 +809,7 @@ sub get_template_function {
return sub {
my ( $content, $template_vars ) = @_;
$template_vars = {
Rex::Commands::Task::task()->get_opts,
(
Rex::Resource->is_inside_resource
? %{ Rex::Resource->get_current_resource()->get_all_parameters }
: ()
),
%{ Rex->instance->current_run_stage->params },
%{ $template_vars || {} }
}
if ( Rex::Commands::Task::task() );
Expand All @@ -832,12 +822,7 @@ sub get_template_function {
return sub {
my ( $content, $template_vars ) = @_;
$template_vars = {
Rex::Commands::Task::task()->get_opts,
(
Rex::Resource->is_inside_resource
? %{ Rex::Resource->get_current_resource()->get_all_parameters }
: ()
),
%{ Rex->instance->current_run_stage->params },
%{ $template_vars || {} }
}
if ( Rex::Commands::Task::task() );
Expand Down
27 changes: 27 additions & 0 deletions lib/Rex/DSL/Common.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

package Rex::DSL::Common;

use strict;
use warnings;

# VERSION

require Exporter;
require Rex::Config;
use Data::Dumper;
use base qw(Exporter);
use vars qw(@EXPORT);
use MooseX::Params::Validate;

@EXPORT = qw(dsl);

sub dsl {
my ( $name, $options, $function ) = @_;
}

1;
132 changes: 8 additions & 124 deletions lib/Rex/Function/Common.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,100 +20,9 @@ use MooseX::Params::Validate;

@EXPORT = qw(function);

my $__lookup_table;

sub function {
my ( $name, $options, $function ) = @_;
my $name_save = $name;

my $caller_pkg = caller;

if ( ref $options eq "CODE" ) {
$function = $options;
$options = {};
}

$options->{name_idx} //= 0;
$options->{params_list} //= [ name => { isa => 'Str' }, ];

my $app = Rex->instance;

push @{ $__lookup_table->{$name} },
{
options => $options,
code => $function,
};

# TODO add dry run
# TODO add reporting
my $func = sub {
$app->output->print_s(
{ title => $name, msg => $_[ $options->{name_idx} ] } );
my $ret = {};
eval {
my $found = 0;
for my $f (
sort {
scalar( @{ $b->{options}->{params_list} } ) <=>
scalar( @{ $a->{options}->{params_list} } )
} @{ $__lookup_table->{$name} }
)
{
my @args;
eval {
my @_x = @{ $f->{options}->{params_list} };
my @order = map { $_x[$_] } grep { $_ & 1 } 1 .. $#_x;

@args = pos_validated_list(
\@_, @order,
MX_PARAMS_VALIDATE_NO_CACHE => 1,
MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1
);

# get only the checked parameter inside @args array.
@args = splice( @args, 0, scalar(@order) );

$found = 1;
1;
} or do {

# TODO catch no "X parameter was given" errors
next;
};

my @rest_args = splice( @_, scalar(@args), scalar(@_) );
if ( scalar(@rest_args) % 2 != 0 ) {
die "Wrong number of arguments for $name function.";
}
my %arg_options = @rest_args;

# TODO check for common parameters like
# * timeout
# * only_notified
# * only_if
# * unless
# * creates

$ret = $f->{code}->( $app, @args, %arg_options );
last;
}
if ( !$found ) {
die "Function $name for provided parameter not found.";
}
$app->output->endln_ok();
1;
} or do {
$app->output->endln_failed();
die "Error running command: $name.\nError: $@\n";
};

if (wantarray) {
return split( /\r?\n/, $ret->{value} );
}

return $ret->{value};
};

if ( $name_save !~ m/^[a-zA-Z_][a-zA-Z0-9_]+$/ ) {
Rex::Logger::info(
"Please use only the following characters for function names:", "warn" );
Expand All @@ -123,41 +32,16 @@ sub function {
die "Wrong function name syntax.";
}

my ( $class, $file, @tmp ) = caller;

if (!$class->can($name)
&& $name_save =~ m/^[a-zA-Z_][a-zA-Z0-9_]+$/ )
{
no strict 'refs';
Rex::Logger::debug("Registering resource: ${class}::$name_save");
my $sub = Rex::MultiSub::Function->new(
name => $name_save,
function => $function,
params_list => $options->{params_list},
test_wantarray => 1,
);

my $code = $_[-2];
*{"${class}::$name_save"} = $func;
use strict;
}
elsif ( ( $class ne "main" && $class ne "Rex::CLI" )
&& !$class->can($name_save)
&& $name_save =~ m/^[a-zA-Z_][a-zA-Z0-9_]+$/ )
{
# if not in main namespace, register the function as a sub
no strict 'refs';
Rex::Logger::debug(
"Registering function (not main namespace): ${class}::$name_save");
my $code = $_[-2];
*{"${class}::$name_save"} = $func;

use strict;
}

if ( exists $options->{export} && $options->{export} ) {
no strict 'refs';
my ( $class, $file, @tmp ) = caller;

# register in caller namespace
push @{ $caller_pkg . "::ISA" }, "Rex::Exporter"
unless ( grep { $_ eq "Rex::Exporter" } @{ $caller_pkg . "::ISA" } );
push @{ $caller_pkg . "::EXPORT" }, $name_save;
use strict;
}
$sub->export( $class, $options->{export} );
}

1;
4 changes: 2 additions & 2 deletions lib/Rex/Function/Fs/is_file.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ function "is_file", {
},

# this is the code that will be executed
# first parameter of the code reference is always the Rex application object.
# first parameter of the code reference is always a Rex controller object.
# second, third, ... parameter are the real parameter the user passed to the
# function call.
sub {
my ( $app, $file ) = @_;
my ($file) = @_;
$file = resolv_path($file);

my $fs = Rex::Interface::Fs->create;
Expand Down
Loading