Commit 75defdf0 authored by Francesc Guasch's avatar Francesc Guasch
Browse files

Merge branch 'master' into develop

parents 6dfdc781 31841469
......@@ -1467,6 +1467,8 @@ sub _upgrade_tables {
$self->_upgrade_table('vms','public_ip',"varchar(128) DEFAULT NULL");
$self->_upgrade_table('vms','is_active',"int DEFAULT 0");
$self->_upgrade_table('vms','enabled',"int DEFAULT 1");
$self->_upgrade_table('vms','display_ip',"varchar(128) DEFAULT NULL");
$self->_upgrade_table('vms','nat_ip',"varchar(128) DEFAULT NULL");
$self->_upgrade_table('vms','min_free_memory',"int DEFAULT 600000");
$self->_upgrade_table('vms', 'max_load', 'int not null default 10');
......@@ -1631,7 +1633,15 @@ Returns the IP for NATed environments
=cut
sub nat_ip {
sub nat_ip($self=undef, $new_ip=undef) {
if (defined $new_ip) {
if (!length $new_ip) {
delete $CONFIG->{nat_ip};
} else {
$CONFIG->{nat_ip} = $new_ip;
}
}
return $CONFIG->{nat_ip} if exists $CONFIG->{nat_ip};
}
......
......@@ -1062,8 +1062,8 @@ sub _around_display_info($orig,$self,$user ) {
sub _set_display_ip($self, $display) {
my $new_ip = ( $self->_vm->nat_ip
or $self->_vm->display_ip
or $self->_vm->public_ip
or Ravada::display_ip()
);
unlock_hash(%$display);
$display->{listen_ip} = $display->{ip};
......
......@@ -624,7 +624,7 @@ sub ip {
return $ip if $ip && $ip !~ /^127\./;
$name = Ravada::display_ip();
$name = $self->display_ip();
if ($name) {
if ($name =~ /^\d+\.\d+\.\d+\.\d+$/) {
......@@ -650,8 +650,59 @@ Returns the IP of the VM when it is in a NAT environment
=cut
sub nat_ip($self) {
return Ravada::nat_ip();
sub nat_ip($self, $value=undef) {
$self->_data( nat_ip => $value ) if defined $value;
if ($self->is_local) {
return $self->_data('nat_ip') if $self->_data('nat_ip');
return Ravada::nat_ip(); #deprecated
}
return $self->_data('nat_ip');
}
=head2 display_ip
Returns the display IP of the Virtual Manager
=cut
sub display_ip($self, $value=undef) {
return $self->_set_display_ip($value) if defined $value;
if ($self->is_local) {
return $self->_data('display_ip') if $self->_data('display_ip');
return Ravada::display_ip(); #deprecated
}
return $self->_data('display_ip');
}
sub _set_display_ip($self, $value) {
my %ip_address = $self->_list_ip_address();
confess "Error: $value is not in any interface in node ".$self->name
.". Found ".Dumper(\%ip_address)
if !exists $ip_address{$value};
$self->_data( display_ip => $value );
}
sub _list_ip_address($self) {
my @cmd = ("ip","address","show");
my ($out, $err) = $self->run_command(@cmd);
my $dev;
my %address;
for my $line (split /\n/,$out) {
my ($dev_found) = $line =~ /^\d+: (.*?):/;
if ($dev_found) {
$dev = $dev_found;
next;
}
my ($inet) = $line =~ m{inet (\d+\.\d+\.\d+\.\d+)/};
if ($inet) {
die "Error: no device found for $inet in node ".$self->name."\n$out" if !$dev;
$address{$inet} = $dev;
}
}
return %address;
}
sub _interface_ip($self, $remote_ip=undef) {
......
......@@ -259,6 +259,19 @@ sub _create_2_clones_same_port($vm, $node, $base, $ip_local, $ip_remote) {
}
}
sub _start_clone_in_node($vm, $node, $base) {
my $found_clone;
for my $try ( 1 .. 20 ) {
my $clone1 = $base->clone(name => new_domain_name, user => user_admin);
_remove_tmp($clone1,$vm);
$clone1->start(user_admin);
$found_clone = $clone1;
last if $clone1->_vm->id == $node->id;
ok(scalar($base->list_vms) >1) or die Dumper([map { $_->name } $base->list_vms]);
}
return $found_clone;
}
sub test_removed_local_swap($vm, $node) {
diag("Testing removed local swap in ".$vm->type);
my $base = create_domain($vm);
......@@ -269,15 +282,8 @@ sub test_removed_local_swap($vm, $node) {
$base->set_base_vm(node => $node, user => user_admin);
ok(scalar($base->list_vms) >1) or die Dumper([map { $_->name } $base->list_vms]);
my $found_clone;
for my $try ( 1 .. 20 ) {
my $clone1 = $base->clone(name => new_domain_name, user => user_admin);
_remove_tmp($clone1,$vm);
$clone1->start(user_admin);
$found_clone = $clone1;
last if $clone1->_vm->id == $node->id;
ok(scalar($base->list_vms) >1) or die Dumper([map { $_->name } $base->list_vms]);
}
my $found_clone = _start_clone_in_node($vm, $node, $base);
is($found_clone->_vm->id, $node->id) or exit;
for my $clone_data ($base->clones) {
my $clone = Ravada::Domain->open($clone_data->{id});
......@@ -643,7 +649,7 @@ sub test_volatile_req_clone($vm, $node) {
is($req->status, 'done');
is($req->error,'');
is(scalar($base->clones),3);
is(scalar($base->clones),3) or exit;
($clone) = grep { $_->{id_vm} == $node->id } $base->clones;
last if $clone;
}
......@@ -1164,6 +1170,98 @@ sub test_migrate_req($vm, $node) {
$domain->remove(user_admin);
}
sub test_display_ip($vm, $node, $set_localhost_dp=0) {
my $vm_ip = $vm->ip;
if ($set_localhost_dp == 1) {
$vm_ip = "192.168.122.1";
rvd_back->display_ip($vm_ip);
} elsif ($set_localhost_dp == 2) {
$vm_ip = "192.168.130.1";
$vm->display_ip($vm_ip);
}
eval {
$node->display_ip("1.2.3.4");
};
like($@,qr(is not in any interface in node)i);
my $display_ip_1 = "127.0.0.1";
$node->display_ip($display_ip_1);
is($node->display_ip,$display_ip_1);
my $domain = create_domain($vm);
$domain->add_volume(size => 10*1024 , type => 'tmp');
$domain->add_volume(size => 10*1024 , type => 'swap');
my $req = Ravada::Request->start_domain(
remote_ip => '3.4.5.7'
,id_domain => $domain->id
,uid => user_admin->id
);
wait_request();
like($domain->display(user_admin), qr/$vm_ip/);
$domain->shutdown_now(user_admin);
$domain->prepare_base(user_admin);
$domain->set_base_vm(node => $node, user => user_admin);
my $domain2 = _start_clone_in_node($vm, $node, $domain);
is($domain2->_vm->id,$node->id) or exit;
like($domain2->display(user_admin),qr/$display_ip_1/) or exit;
_remove_domain($domain);
$node->_data(display_ip => '');
rvd_back->display_ip('') if $set_localhost_dp;
}
sub test_nat($vm, $node, $set_localhost_natip=0) {
my $nat_ip_1 = "5.6.7.8";
$node->nat_ip($nat_ip_1);
my $vm_ip = $vm->ip;
if ($set_localhost_natip == 1) {
$vm_ip = "22.22.22.22";
rvd_back->nat_ip($vm_ip);
} elsif ($set_localhost_natip == 2) {
$vm_ip = "33.33.33.33";
$vm->nat_ip($vm_ip);
}
$node->nat_ip($nat_ip_1);
is($node->nat_ip,$nat_ip_1);
my $domain = create_domain($vm);
$domain->add_volume(size => 10*1024 , type => 'tmp');
$domain->add_volume(size => 10*1024 , type => 'swap');
my $req = Ravada::Request->start_domain(
remote_ip => '3.4.5.7'
,id_domain => $domain->id
,uid => user_admin->id
);
wait_request();
like($domain->display(user_admin), qr/$vm_ip/);
$domain->shutdown_now(user_admin);
$domain->prepare_base(user_admin);
$domain->set_base_vm(node => $node, user => user_admin);
my $domain2 = _start_clone_in_node($vm, $node, $domain);
is($domain2->_vm->id,$node->id) or exit;
like($domain2->display(user_admin),qr/$nat_ip_1/) or exit;
_remove_domain($domain);
$node->_data(nat_ip => '');
rvd_back->nat_ip('') if $set_localhost_natip;
$vm->nat_ip('') if $set_localhost_natip;
$node->_data(nat_ip => '');
}
##################################################################################
clean();
......@@ -1208,6 +1306,13 @@ for my $vm_name ( vm_names() ) {
test_migrate($vm, $node);
test_migrate_req($vm, $node);
test_nat($vm, $node);
test_nat($vm, $node, 1); # also set deprecated localhost ip
test_nat($vm, $node, 2); # also set localhost ip
test_display_ip($vm, $node);
test_display_ip($vm, $node, 1); # also set deprecated localhost ip
test_display_ip($vm, $node, 2); # also set localhost ip
test_set_vm_fail($vm, $node);
test_volatile_req_clone($vm, $node);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment