lib/Rex/Virtualization/VBox/forward_port.pm

filter: Statementstmt 41.0% 16 / 39 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 1 totaltotal 37.5% 21 / 56 CC 8 SCAR 38.0
CC
8
Cov
33%
execute
72.0
BEGIN
1.0
BEGIN
1.0
CRAP 44.7
« lib/Rex/Pkg.pm ↑ Summary lib/Rex/Virtualization/VBox/bridge.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Virtualization::VBox::forward_port;
6  
7 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '1.16.1'; # VERSION
11  
12 1 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Rex::Virtualization::VBox::info;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #
17 # vm forward_port => "foovm", add => { http => [80, 90] };
18 # vm forward_port => "foovm", remove => "http";
19 #
20  
21 0 ▶sub execute {
Subroutine execute: not called CC=8 SCAR=42.8
Pod undocumented
22 0 my ( $class, $arg1, $action, $option ) = @_;
23  
24 0 ▶ unless ($arg1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg1) 0 0 0
25 0 die("You have to define the vm name!");
26 }
27  
28 0 my $dom = $arg1;
29  
30 0 ▶ unless ($dom) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dom) 0 0 0
31 0 die("VM $dom not found.");
32 }
33  
34 0 ▶ if ( $action eq "add" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq 'add') { } 0 0 0
35 0 for my $rule ( keys %{$option} ) {
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 ▶ if ( $option ne "-all" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option ne '-all') { } 0 0 0
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};
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 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
63 0 die("Error setting port forwarding options for vm $dom");
64 }
65  
66 }
67  
68 1 1;
« lib/Rex/Pkg.pm ↑ Summary lib/Rex/Virtualization/VBox/bridge.pm »