This file is indexed.

/usr/share/perl5/Rex/Commands/Box.pm is in rex 1.4.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

=head1 NAME

Rex::Commands::Box - Functions / Class to manage Virtual Machines

=head1 DESCRIPTION

This is a Module to manage Virtual Machines or Cloud Instances in a simple way. Currently it supports Amazon, KVM and VirtualBox.

Version <= 1.0: All these functions will not be reported.

=head1 SYNOPSIS

 use Rex::Commands::Box;
 
 set box => "VBox";
 
 group all_my_boxes => map { get_box($_->{name})->{ip} } list_boxes;
 
 task mytask => sub {
 
   box {
     my ($box) = @_;
     $box->name("boxname");
     $box->url("http://box.rexify.org/box/base-image.box");
 
     $box->network(1 => {
      type => "nat",
     });
 
     $box->network(1 => {
      type => "bridged",
      bridge => "eth0",
     });
 
     $box->forward_port(ssh => [2222, 22]);
 
     $box->share_folder(myhome => "/home/myuser");
 
     $box->auth(
      user => "root",
      password => "box",
     );
 
     $box->setup(qw/task_to_customize_box/);
 
   };
 
 };

=head1 EXPORTED FUNCTIONS

=cut

package Rex::Commands::Box;

use strict;
use warnings;

our $VERSION = '1.4.1'; # VERSION

use YAML;
use Data::Dumper;

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

$|++;

################################################################################
# BEGIN of class methods
################################################################################

require Exporter;
use base qw(Exporter);
use vars qw(@EXPORT %vm_infos $VM_STRUCT);
use Rex::Box;

#@EXPORT = qw(box $box);
@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} );
    }
  }
);

=head2 new(name => $box_name)

Constructor if used in OO mode.

 my $box = Rex::Commands::Box->new(name => "box_name");

=cut

sub new {
  my $class = shift;
  return Rex::Box->create(@_);
}

=head2 box(sub {})

With this function you can create a new Rex/Box. The first parameter of this function is the Box object. With this object you can define your box.

 box {
   my ($box) = @_;
   $box->name("boxname");
   $box->url("http://box.rexify.org/box/base-image.box");
 
   $box->network(1 => {
    type => "nat",
   });
 
   $box->network(1 => {
    type => "bridged",
    bridge => "eth0",
   });
 
   $box->forward_port(ssh => [2222, 22]);
 
   $box->share_folder(myhome => "/home/myuser");
 
   $box->auth(
    user => "root",
    password => "box",
   );
 
   $box->setup(qw/task_to_customize_box/);
 };


=cut

sub box(&) {
  my $code = shift;

  #### too much black magic...
  #my ($caller_box) = do {
  #  my $pkg = caller();
  #  no strict 'refs';
  #  \*{ $pkg . "::box" };
  #};

  my $self = Rex::Box->create;

  #local( *$caller_box );
  #*$caller_box = \$self;

  $code->($self);

  #*$caller_box = \{}; # undef $box

  $self->import_vm();

  $self->provision_vm();

  return $self->ip;
}

=head2 list_boxes

This function returns an array of hashes containing all information that can be gathered from the hypervisor about the Rex/Box. This function doesn't start a Rex/Box.

 use Data::Dumper;
 task "get_infos", sub {
   my @all_boxes = list_boxes;
   print Dumper(\@all_boxes);
 };

=cut

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};
}

=head2 get_box($box_name)

This function tries to gather all information of a Rex/Box. This function also starts a Rex/Box to gather all information of the running system.

 use Data::Dumper;
 task "get_box_info", sub {
   my $data = get_box($box_name);
   print Dumper($data);
 };

=cut

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 "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.",
        "warn"
      );
      Rex::Logger::debug(
        "You have to define login credentials before calling get_box()");

      # cleanup
      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};
  };

}

=head2 boxes($action, @data)

With this function you can control your boxes. Currently there are 3 actions.

=over 4

=item init

This action can only  be used if you're using a YAML file to describe your Rex/Boxes.

 task "prepare_boxes", sub {
   boxes "init";
 };

=item start

This action start one or more Rex/Boxes.

 task "start_boxes", sub {
   boxes "start", "box1", "box2";
 };

=item stop

This action stop one or more Rex/Boxes.

 task "stop_boxes", sub {
   boxes "stop", "box1", "box2";
 };

=back

=cut

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} };
    }

    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} );
          }
        }
      };
    }
  }

  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 import {
  my ( $class, %option ) = @_;

  if ( $option{init_file} ) {
    my $file = $option{init_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;

    # set special box options, like amazon out
    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;

    @_ = ($class);
  }

  __PACKAGE__->export_to_level( 1, @_ );
}

1;