Hide Show 53 lines of Pod
our
$VERSION
=
'1.16.0'
;
$|++;
use
vars
qw(@EXPORT %vm_infos $VM_STRUCT)
;
@EXPORT
=
qw(box list_boxes get_box boxes)
;
Rex::Config->register_set_handler(
"box"
,
sub
{
my
(
$type
,
@data
) =
@_
;
Rex::Config->set(
"box_type"
,
$type
);
if
(
ref
(
$data
[0] ) ) {
Rex::Config->set(
"box_options"
,
$data
[0] );
}
else
{
Rex::Config->set(
"box_options"
, {
@data
} );
}
}
);
Hide Show 8 lines of Pod
sub
new {
my
$class
=
shift
;
return
Rex::Box->create(
@_
);
}
Hide Show 33 lines of Pod
sub
box(&) {
my
$code
=
shift
;
my
$self
= Rex::Box->create;
$code
->(
$self
);
$self
->import_vm();
$self
->provision_vm();
return
$self
->ip;
}
Hide Show 12 lines of Pod
sub
list_boxes {
my
$box
= Rex::Box->create;
my
@ret
=
$box
->list_boxes;
my
$ref
= LOCAL {
if
( -f
".box.cache"
) {
my
$yaml_str
=
eval
{
local
(
@ARGV
, $/ ) = (
".box.cache"
); <>; };
$yaml_str
.=
"\n"
;
my
$yaml_ref
= Load(
$yaml_str
);
for
my
$box
(
keys
%{
$yaml_ref
} ) {
my
(
$found_box
) =
grep
{
$_
->{name} eq
$box
}
@ret
;
if
( !
$found_box
) {
$yaml_ref
->{
$box
} =
undef
;
delete
$yaml_ref
->{
$box
};
}
}
open
(
my
$fh
,
">"
,
".box.cache"
) or
die
($!);
print
$fh
Dump(
$yaml_ref
);
close
(
$fh
);
}
if
(
wantarray
) {
return
@ret
;
}
return
\
@ret
;
};
return
@{
$ref
};
}
Hide Show 12 lines of Pod
sub
get_box {
my
(
$box_name
) =
@_
;
my
$box
= Rex::Box->create(
name
=>
$box_name
);
$box
->info;
if
(
$box
->status eq
"stopped"
) {
$box
->start;
$box
->wait_for_ssh;
}
my
$box_ip
=
$box
->ip;
my
$box_info
=
$box
->info;
return
LOCAL {
if
( -f
".box.cache"
) {
Rex::Logger::debug(
"Loading box information of cache file: .box.cache."
);
my
$yaml_str
=
eval
{
local
(
@ARGV
, $/ ) = (
".box.cache"
); <>; };
$yaml_str
.=
"\n"
;
my
$yaml_ref
= Load(
$yaml_str
);
%vm_infos
= %{
$yaml_ref
};
}
if
(
exists
$vm_infos
{
$box_name
} ) {
return
$vm_infos
{
$box_name
};
}
my
$pid
=
fork
;
if
(
$pid
== 0 ) {
print
"Gathering system information from $box_name.\nThis may take a while.."
;
while
(1) {
print
"."
;
sleep
1;
}
CORE::
exit
;
}
my
$old_q
= $::QUIET;
$::QUIET = 1;
eval
{
$vm_infos
{
$box_name
} = run_task
"Commands:Box:get_sys_info"
,
on
=>
$box_ip
;
} or
do
{
$::QUIET =
$old_q
;
print
STDERR
"\n"
;
Rex::Logger::info(
"There was an error connecting to your Box. Please verify the login credentials.\nERROR: $@\n"
,
"warn"
);
Rex::Logger::debug(
"You have to define login credentials before calling get_box()"
);
kill
9,
$pid
;
CORE::
exit
(1);
};
$::QUIET =
$old_q
;
for
my
$key
(
keys
%{
$box_info
} ) {
$vm_infos
{
$box_name
}->{
$key
} =
$box_info
->{
$key
};
}
open
(
my
$fh
,
">"
,
".box.cache"
) or
die
($!);
print
$fh
Dump( \
%vm_infos
);
close
(
$fh
);
kill
9,
$pid
;
print
"\n"
;
return
$vm_infos
{
$box_name
};
};
}
Hide Show 34 lines of Pod
sub
boxes {
my
(
$action
,
@data
) =
@_
;
if
(
substr
(
$action
, 0, 1 ) eq
"-"
) {
$action
=
substr
(
$action
, 1 );
}
if
(
$action
eq
"init"
) {
if
( -f
".box.cache"
) {
unlink
".box.cache"
;
}
my
$yaml_ref
=
$VM_STRUCT
;
my
@vms
;
if
(
ref
$yaml_ref
->{vms} eq
"HASH"
) {
for
my
$vm
(
keys
%{
$yaml_ref
->{vms} } ) {
push
(
@vms
,
{
name
=>
$vm
,
%{
$yaml_ref
->{vms}->{
$vm
} }
}
);
}
}
else
{
@vms
= @{
$yaml_ref
->{vms} };
}
my
$box_vms
= {};
for
my
$vm_ref
(
@vms
) {
my
$vm
=
$vm_ref
->{name};
box {
my
(
$box
) =
@_
;
$box
->name(
$vm
);
for
my
$key
(
keys
%{
$vm_ref
} ) {
if
(
ref
(
$vm_ref
->{
$key
} ) eq
"HASH"
) {
$box
->
$key
( %{
$vm_ref
->{
$key
} } );
}
elsif
(
ref
(
$vm_ref
->{
$key
} ) eq
"ARRAY"
) {
$box
->
$key
( @{
$vm_ref
->{
$key
} } );
}
else
{
$box
->
$key
(
$vm_ref
->{
$key
} );
}
}
$box_vms
->{
$vm
} =
$box
;
};
}
return
$box_vms
;
}
if
(
$action
eq
"stop"
) {
for
my
$box
(
@data
) {
my
$o
= Rex::Commands::Box->new(
name
=>
$box
);
$o
->stop;
}
}
if
(
$action
eq
"start"
) {
for
my
$box
(
@data
) {
my
$o
= Rex::Commands::Box->new(
name
=>
$box
);
$o
->start;
}
}
}
task
'get_sys_info'
,
sub
{
return
{ get_system_information() };
}, {
dont_register
=> 1,
exit_on_connect_fail
=> 0 };
sub
load_init_file {
my
(
$class
,
$file
) =
@_
;
if
( !-f
$file
) {
die
(
"Error: Wrong configuration file: $file."
);
}
my
$yaml_str
=
eval
{
local
(
@ARGV
, $/ ) = (
$file
); <>; };
$yaml_str
.=
"\n"
;
my
$yaml_ref
= Load(
$yaml_str
);
if
( !
exists
$yaml_ref
->{type} ) {
die
(
"You have to define a type."
);
}
my
$type
=
ucfirst
$yaml_ref
->{type};
set
box_type
=>
$type
;
if
(
exists
$yaml_ref
->{
"\L$type"
} ) {
set
box_options
=>
$yaml_ref
->{
"\L$type"
};
}
elsif
(
exists
$yaml_ref
->{
$type
} ) {
set
box_options
=>
$yaml_ref
->{
$type
};
}
$VM_STRUCT
=
$yaml_ref
;
}
sub
import
{
my
(
$class
,
%option
) =
@_
;
if
(
$option
{init_file} ) {
my
$file
=
$option
{init_file};
$class
->load_init_file(
$file
);
@_
= (
$class
);
}
__PACKAGE__->export_to_level( 1,
@_
);
}
1;