Rex

 view release on metacpan or  search on metacpan

lib/Rex/Box/Base.pm  view on Meta::CPAN

#
# (c) Jan Gehring <jan.gehring@gmail.com>
#

=head1 NAME

Rex::Box::Base - Rex/Boxes Base Module

=head1 DESCRIPTION

This is a Rex/Boxes base module.

=head1 METHODS

These methods are shared across all other Rex::Box modules.

=cut

package Rex::Box::Base;

use v5.12.5;
use warnings;

our $VERSION = '1.16.0'; # VERSION

use Rex::Commands -no => [qw/auth/];
use Rex::Helper::Run;
use Rex::Commands::Fs;
use Rex::Commands::Virtualization;
use Rex::Commands::SimpleCheck;
use Rex::Helper::IP;

BEGIN {
  LWP::UserAgent->use;
}

use Time::HiRes    qw(tv_interval gettimeofday);
use File::Basename qw(basename);
use Data::Dumper;

sub new {
  my $that  = shift;
  my $proto = ref($that) || $that;
  my $self  = {@_};

  bless( $self, $proto );

  # default auth for rex boxes
  $self->{__auth} = {
    user        => Rex::Config->get_user(),
    password    => Rex::Config->get_password(),
    private_key => Rex::Config->get_private_key(),
    public_key  => Rex::Config->get_public_key(),
  };

  # for box this is needed, because we have changing ips
  Rex::Config->set_openssh_opt(
    StrictHostKeyChecking => "no",
    UserKnownHostsFile    => "/dev/null",
    LogLevel              => "QUIET"
  );

  return $self;
}

=head2 info

Returns a hashRef of vm information.

=cut

sub info {
  my ($self) = @_;
  return $self->{info};
}

=head2 name($vmname)

Sets the name of the virtual machine.

=cut

sub name {
  my ( $self, $name ) = @_;
  $self->{name} = $name;
}

=head2 setup(@tasks)

Sets the tasks that should be executed as soon as the VM is available through SSH.

=cut

=head2 storage('path/to/vm/disk')

Sets the disk path of the virtual machine. Works only on KVM

=cut

sub storage {
  my ( $self, $folder ) = @_;

  $self->{storage_path} = $folder;
}

sub setup {
  my ( $self, @tasks ) = @_;
  $self->{__tasks} = \@tasks;
}

=head2 import_vm()

lib/Rex/Box/Base.pm  view on Meta::CPAN

   },
 );

=cut

sub network {
  my ( $self, %option ) = @_;
  $self->{__network} = \%option;
}

=head2 forward_port(%option)

Set ports to be forwarded to the VM. This is not supported by all Box providers.

 $box->forward_port(
   name => [$from_host_port, $to_vm_port],
   name2 => [$from_host_port_2, $to_vm_port_2],
   ...
 );

=cut

sub forward_port {
  my ( $self, %option ) = @_;
  $self->{__forward_port} = \%option;
}

=head2 list_boxes

List all available boxes.

=cut

sub list_boxes {
  my ($self) = @_;

  my $vms = vm list => "all";

  return @{$vms};
}

=head2 url($url)

The URL where to download the Base VM Image. You can use self-made images or prebuild images from L<http://box.rexify.org/>.

=cut

sub url {
  my ( $self, $url, $force ) = @_;
  $self->{url}   = $url;
  $self->{force} = $force;
}

=head2 auth(%option)

Configure the authentication to the VM.

 $box->auth(
   user => $user,
   password => $password,
   private_key => $private_key,
   public_key => $public_key,
 );

=cut

sub auth {
  my ( $self, %auth ) = @_;
  if (%auth) {
    $self->{__auth} = \%auth;
  }
  else {
    return $self->{__auth};
  }
}

=head2 options(%option)

Addition options for boxes

 $box->options(
   opt1 => $val1,
   opt2 => $val2,
 );

=cut

sub options {
  my ( $self, %opt ) = @_;
  if (%opt) {
    $self->{__options} = \%opt;
  }
  else {
    return $self->{__options};
  }
}

sub wait_for_ssh {
  my ( $self, $ip, $port ) = @_;

  if ( !$ip ) {
    ( $ip, $port ) = Rex::Helper::IP::get_server_and_port( $self->ip, 22 );
  }

  print "Waiting for SSH to come up on $ip:$port.";
  while ( !is_port_open( $ip, $port ) ) {
    print ".";
    sleep 1;
  }

  print "\n";
}

sub _download {
  my ($self) = @_;

  my $filename = basename( $self->{url} );
  my $force    = $self->{force} || FALSE;
  my $fs       = Rex::Interface::Fs->create;

  if ( $fs->is_file("./tmp/$filename") ) {



( run in 0.383 second using v1.01-cache-2.11-cpan-a5abf4f5562 )