File Coverage

lib/Rex/Virtualization/VBox/forward_port.pm
Criterion Covered Total %
statement 14 37 37.8
branch 0 10 0.0
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 19 54 35.1


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Virtualization::VBox::forward_port;
6              
7 1     1   17 use v5.12.5;
  1         5  
8 1     1   8 use warnings;
  1         3  
  1         57  
9              
10             our $VERSION = '1.14.2.3'; # TRIAL VERSION
11              
12 1     1   7 use Rex::Logger;
  1         4  
  1         6  
13 1     1   23 use Rex::Helper::Run;
  1         2  
  1         75  
14 1     1   7 use Rex::Virtualization::VBox::info;
  1         4  
  1         10  
15              
16             #
17             # vm forward_port => "foovm", add => { http => [80, 90] };
18             # vm forward_port => "foovm", remove => "http";
19             #
20              
21             sub execute {
22 0     0 0   my ( $class, $arg1, $action, $option ) = @_;
23              
24 0 0         unless ($arg1) {
25 0           die("You have to define the vm name!");
26             }
27              
28 0           my $dom = $arg1;
29              
30 0 0         unless ($dom) {
31 0           die("VM $dom not found.");
32             }
33              
34 0 0         if ( $action eq "add" ) {
35 0           for my $rule ( keys %{$option} ) {
  0            
36              
37 0           my $from_port = $option->{$rule}->[0];
38 0           my $to_port = $option->{$rule}->[1];
39              
40 0           i_run
41             "VBoxManage modifyvm \"$dom\" --natpf1 \"$rule,tcp,,$from_port,,$to_port\"";
42             }
43             }
44             else {
45 0 0         if ( $option ne "-all" ) {
46 0           i_run "VBoxManage modifyvm \"$dom\" --natpf1 delete \"$option\"";
47             }
48             else {
49             # if no name is given, remove all redirects
50             # output: Forwarding(0)="ssh,tcp,,2222,,22"
51 0           my $info = Rex::Virtualization::VBox::info->execute($dom);
52 0           my @keys = grep { m/^Forwarding/ } keys %{$info};
  0            
  0            
53              
54 0           for my $k (@keys) {
55 0           my @_t = split( /,/, $info->{$k} );
56 0           i_run "VBoxManage modifyvm \"$dom\" --natpf1 delete \"$_t[0]\"";
57             }
58             }
59              
60             }
61              
62 0 0         if ( $? != 0 ) {
63 0           die("Error setting port forwarding options for vm $dom");
64             }
65              
66             }
67              
68             1;