our
$VERSION
=
'1.16.0'
;
BEGIN {
LWP::UserAgent->
use
;
}
set
virtualization
=>
"Docker"
;
$|++;
sub
new {
my
$class
=
shift
;
my
$proto
=
ref
(
$class
) ||
$class
;
my
$self
=
$proto
->SUPER::new(
@_
);
bless
(
$self
,
ref
(
$class
) ||
$class
);
return
$self
;
}
sub
memory {
my
(
$self
,
$mem
) =
@_
;
Rex::Logger::debug(
"Memory option not supported."
);
}
sub
import_vm {
my
(
$self
) =
@_
;
my
$vms
= vm
list
=>
"all"
;
my
$vm_exists
= 0;
for
my
$vm
( @{
$vms
} ) {
if
(
$vm
->{name} eq
$self
->{name} ) {
Rex::Logger::debug(
"VM already exists. Don't import anything."
);
$vm_exists
= 1;
}
}
if
( !
$vm_exists
) {
my
(
$filename
);
if
(
$self
->{url} =~ m/^(http|https):/ ) {
$self
->_download;
$filename
=
"./tmp/"
. basename(
$self
->{url} );
Rex::Logger::info(
"Importing VM ./tmp/$filename"
);
my
@options
= (
import
=>
$self
->{name},
file
=>
$filename
,
%{
$self
},
);
vm
@options
;
}
else
{
vm
import
=>
$self
->{name},
file
=>
$self
->{url}, %{
$self
};
}
}
my
$vminfo
= vm
info
=>
$self
->{name};
unless
(
$vminfo
->{State}->{Running} ) {
$self
->start;
}
$self
->{info} = vm
guestinfo
=>
$self
->{name};
}
sub
list_boxes {
my
(
$self
) =
@_
;
my
$vms
= vm
list
=>
"all"
;
return
@{
$vms
};
}
sub
info {
my
(
$self
) =
@_
;
$self
->ip;
return
$self
->{info};
}
sub
ip {
my
(
$self
) =
@_
;
$self
->{info} = vm
guestinfo
=>
$self
->{name};
if
(
$self
->{info}->{redirects}
&&
$self
->{info}->{redirects}->{tcp}
&&
$self
->{info}->{redirects}->{tcp}->{22} )
{
return
(
$self
->{info}->{redirects}->{tcp}->{22}->[0]->{ip} eq
"0.0.0.0"
?
"127.0.0.1"
:
$self
->{info}->{redirects}->{tcp}->{22}->[0]->{ip}
)
.
":"
.
$self
->{info}->{redirects}->{tcp}->{22}->[0]->{port};
}
else
{
return
$self
->{info}->{network}->[0]->{ip};
}
}
sub
create {
my
(
$self
) =
@_
;
my
@options
= (
forward_port
=> [
$self
->{__forward_port}->{ssh}->[0] .
":"
.
$self
->{__forward_port}->{ssh}->[1]
],
image
=>
$self
->{url},
);
Rex::Virtualization::Docker::create->execute(
$self
->{name},
@options
);
}
1;