Commit d3dfe26a authored by Francesc Guasch's avatar Francesc Guasch
Browse files

refactor(backend): properly start temporary domains

issue #1264
parent 4b1222e6
...@@ -1691,18 +1691,13 @@ sub create_domain { ...@@ -1691,18 +1691,13 @@ sub create_domain {
my $user = Ravada::Auth::SQL->search_by_id($id_owner); my $user = Ravada::Auth::SQL->search_by_id($id_owner);
$request->status("creating machine") if $request; $request->status("creating machine") if $request;
if ( $base && $base->is_base ) { if ( $base && $base->is_base && $base->volatile_clones || $user->is_temporary ) {
$request->status("balancing") if $request; $request->status("balancing") if $request;
$vm = $vm->balance_vm($base) or die "Error: No free nodes available."; $vm = $vm->balance_vm($base) or die "Error: No free nodes available.";
$request->status("creating machine on ".$vm->name) if $request; $request->status("creating machine on ".$vm->name) if $request;
} }
confess "No vm found, request = ".Dumper(request => $request) if !$vm; confess "Error: missing vm " if !$vm;
carp "WARNING: no VM defined, we will use ".$vm->name
if !$vm_name && !$id_base;
confess "I can't find any vm ".Dumper($self->vm) if !$vm;
my $domain; my $domain;
eval { $domain = $vm->create_domain(%args)}; eval { $domain = $vm->create_domain(%args)};
......
...@@ -710,7 +710,8 @@ sub prepare_base($self, $with_cd) { ...@@ -710,7 +710,8 @@ sub prepare_base($self, $with_cd) {
for my $volume ($self->list_volumes_info()) { for my $volume ($self->list_volumes_info()) {
my $base_file = $volume->base_filename; my $base_file = $volume->base_filename;
next if !$base_file || $base_file =~ /\.iso$/; next if !$base_file || $base_file =~ /\.iso$/;
confess "Error: file '$base_file' already exists" if $self->_vm->file_exists($base_file); confess "Error: file '$base_file' already exists in ".$self->_vm->name
if $self->_vm->file_exists($base_file);
} }
for my $volume ($self->list_volumes_info()) { for my $volume ($self->list_volumes_info()) {
......
...@@ -626,7 +626,8 @@ sub _interface_ip($self, $remote_ip=undef) { ...@@ -626,7 +626,8 @@ sub _interface_ip($self, $remote_ip=undef) {
my %route; my %route;
my ($default_gw , $default_ip); my ($default_gw , $default_ip);
my $remote_ip_addr = NetAddr::IP->new($remote_ip); my $remote_ip_addr = NetAddr::IP->new($remote_ip)
or confess "I can't find netaddr for $remote_ip";
for my $line ( split( /\n/, $out ) ) { for my $line ( split( /\n/, $out ) ) {
if ( $line =~ m{^default via ([\d\.]+)} ) { if ( $line =~ m{^default via ([\d\.]+)} ) {
...@@ -638,7 +639,8 @@ sub _interface_ip($self, $remote_ip=undef) { ...@@ -638,7 +639,8 @@ sub _interface_ip($self, $remote_ip=undef) {
return $ip if $remote_ip && $remote_ip eq $ip; return $ip if $remote_ip && $remote_ip eq $ip;
my $netaddr = NetAddr::IP->new($network); my $netaddr = NetAddr::IP->new($network)
or confess "I can't find netaddr for $network";
return $ip if $remote_ip_addr->within($netaddr); return $ip if $remote_ip_addr->within($netaddr);
$default_ip = $ip if !defined $default_ip && $ip !~ /^127\./; $default_ip = $ip if !defined $default_ip && $ip !~ /^127\./;
......
...@@ -971,6 +971,7 @@ sub clean_remote_node { ...@@ -971,6 +971,7 @@ sub clean_remote_node {
my $node = shift; my $node = shift;
_remove_old_domains_vm($node); _remove_old_domains_vm($node);
wait_request(debug => 0);
_remove_old_disks($node); _remove_old_disks($node);
flush_rules_node($node) if !$node->is_local() && $node->is_active; flush_rules_node($node) if !$node->is_local() && $node->is_active;
} }
......
...@@ -206,6 +206,10 @@ sub test_iptables_close($vm, $node) { ...@@ -206,6 +206,10 @@ sub test_iptables_close($vm, $node) {
); );
is(scalar @found,1,$vm->name." $remote_ip2:$local_port2".Dumper(\@found)); is(scalar @found,1,$vm->name." $remote_ip2:$local_port2".Dumper(\@found));
_remove_domain($domain);
}
sub _remove_domain($domain) {
for my $clone0 ( $domain->clones) { for my $clone0 ( $domain->clones) {
my $clone = Ravada::Domain->open($clone0->{id}); my $clone = Ravada::Domain->open($clone0->{id});
$clone->remove(user_admin); $clone->remove(user_admin);
...@@ -254,7 +258,6 @@ sub test_removed_local_swap($vm, $node) { ...@@ -254,7 +258,6 @@ sub test_removed_local_swap($vm, $node) {
my $found_clone; my $found_clone;
for my $try ( 1 .. 20 ) { for my $try ( 1 .. 20 ) {
diag("try $try");
my $clone1 = $base->clone(name => new_domain_name, user => user_admin); my $clone1 = $base->clone(name => new_domain_name, user => user_admin);
_remove_tmp($clone1,$vm); _remove_tmp($clone1,$vm);
$clone1->start(user_admin); $clone1->start(user_admin);
...@@ -535,7 +538,8 @@ sub test_volatile_req($vm, $node) { ...@@ -535,7 +538,8 @@ sub test_volatile_req($vm, $node) {
ok($base->base_in_vm($node->id)); ok($base->base_in_vm($node->id));
my @clones; my @clones;
for ( 1 .. 4 ) { my $clone;
for ( 1 .. 20 ) {
my $clone_name = new_domain_name; my $clone_name = new_domain_name;
my $req = Ravada::Request->create_domain( my $req = Ravada::Request->create_domain(
id_base => $base->id id_base => $base->id
...@@ -546,13 +550,13 @@ sub test_volatile_req($vm, $node) { ...@@ -546,13 +550,13 @@ sub test_volatile_req($vm, $node) {
is($req->status, 'done'); is($req->status, 'done');
is($req->error,''); is($req->error,'');
my $clone = rvd_back->search_domain($clone_name); $clone = rvd_back->search_domain($clone_name);
is($clone->is_active(),1,"[".$vm->type."] expecting clone ".$clone->name is($clone->is_active(),1,"[".$vm->type."] expecting clone ".$clone->name
." active on node ".$clone->_vm->name); ." active on node ".$clone->_vm->name);
push @clones,($clone); push @clones,($clone);
last if $clone->_vm->id == $node->id; last if $clone->_vm->id == $node->id;
} }
is($clones[-1]->_vm->id, $node->id) or exit; is($clone->_vm->id, $node->id) or exit;
for (@clones) { for (@clones) {
$_->remove(user_admin); $_->remove(user_admin);
...@@ -567,9 +571,11 @@ sub test_volatile_tmp_owner($vm, $node) { ...@@ -567,9 +571,11 @@ sub test_volatile_tmp_owner($vm, $node) {
$base->set_base_vm(user => user_admin, node => $node); $base->set_base_vm(user => user_admin, node => $node);
my $user = Ravada::Auth::SQL::add_user(name => 'mcnulty', is_temporary => 1); my $user = Ravada::Auth::SQL::add_user(name => 'mcnulty', is_temporary => 1);
my $owner = Ravada::Auth::SQL->search_by_id($user->id);
ok($owner) or exit;
my @clones; my @clones;
for ( 1 .. 4 ) { for ( 1 .. 10 ) {
my $clone_name = new_domain_name; my $clone_name = new_domain_name;
my $req = Ravada::Request->create_domain( my $req = Ravada::Request->create_domain(
id_base => $base->id id_base => $base->id
...@@ -586,7 +592,7 @@ sub test_volatile_tmp_owner($vm, $node) { ...@@ -586,7 +592,7 @@ sub test_volatile_tmp_owner($vm, $node) {
push @clones,($clone); push @clones,($clone);
last if $clone->_vm->id == $node->id; last if $clone->_vm->id == $node->id;
} }
is($clones[-1]->_vm->id, $node->id); is($clones[-1]->_vm->id, $node->id) or exit;
for (@clones) { for (@clones) {
$_->shutdown_now(user_admin); $_->shutdown_now(user_admin);
...@@ -726,6 +732,37 @@ sub test_duplicated_set_base_vm($vm, $node) { ...@@ -726,6 +732,37 @@ sub test_duplicated_set_base_vm($vm, $node) {
$sth->execute; $sth->execute;
} }
sub test_create_active($vm, $node) {
diag("Test create active machine");
my $base = create_domain($vm);
$base->prepare_base(user_admin);
$base->set_base_vm(vm => $node, user => user_admin);
my $remote_ip = $node->ip or confess "No node ip";
my $clone;
for ( 1 .. 20 ) {
my $name = new_domain_name();
my $req = Ravada::Request->create_domain(
id_base => $base->id
,name => $name
,start => 1
,remote_ip => '1.2.3.4'
,id_owner => user_admin->id
);
wait_request(debug => 1);
$clone = rvd_front->search_domain($name);
ok($vm->search_domain($name),"Expecting clone $name in master node") or exit;
last if $clone->display(user_admin) =~ /$remote_ip/;
}
like($clone->display(user_admin), qr($remote_ip));
my $clone2 = rvd_front->search_domain($clone->name);
my $info = $clone2->info(user_admin);
like($info->{display}->{display}, qr($remote_ip)) or die Dumper($info->{display});
_remove_domain($base);
}
################################################################################## ##################################################################################
clean(); clean();
...@@ -764,6 +801,8 @@ for my $vm_name ( 'Void', 'KVM') { ...@@ -764,6 +801,8 @@ for my $vm_name ( 'Void', 'KVM') {
}; };
is($node->is_local,0,"Expecting ".$node->name." ".$node->ip." is remote" ) or BAIL_OUT(); is($node->is_local,0,"Expecting ".$node->name." ".$node->ip." is remote" ) or BAIL_OUT();
test_create_active($vm, $node);
test_removed_base_file_and_swap_remote($vm, $node); test_removed_base_file_and_swap_remote($vm, $node);
test_removed_remote_swap($vm, $node); test_removed_remote_swap($vm, $node);
test_removed_local_swap($vm, $node); test_removed_local_swap($vm, $node);
......
...@@ -41,6 +41,7 @@ sub test_graphics($vm, $node) { ...@@ -41,6 +41,7 @@ sub test_graphics($vm, $node) {
sub test_driver_clone($vm, $node, $domain, $driver_name, $option) { sub test_driver_clone($vm, $node, $domain, $driver_name, $option) {
$domain->remove_base(user_admin) if $domain->is_base; $domain->remove_base(user_admin) if $domain->is_base;
wait_request();
my $req = Ravada::Request->set_driver(uid => user_admin->id my $req = Ravada::Request->set_driver(uid => user_admin->id
, id_domain => $domain->id , id_domain => $domain->id
, id_option => $option->{id} , id_option => $option->{id}
...@@ -63,6 +64,7 @@ sub test_driver_clone($vm, $node, $domain, $driver_name, $option) { ...@@ -63,6 +64,7 @@ sub test_driver_clone($vm, $node, $domain, $driver_name, $option) {
$clone->remove(user_admin); $clone->remove(user_admin);
$domain->remove_base(user_admin); $domain->remove_base(user_admin);
wait_request();
} }
sub test_driver_migrate($vm, $node, $domain, $driver_name) { sub test_driver_migrate($vm, $node, $domain, $driver_name) {
...@@ -97,6 +99,7 @@ sub test_driver_migrate($vm, $node, $domain, $driver_name) { ...@@ -97,6 +99,7 @@ sub test_driver_migrate($vm, $node, $domain, $driver_name) {
last unless $ENV{TEST_LONG}; last unless $ENV{TEST_LONG};
} }
$domain->remove_base(user_admin); $domain->remove_base(user_admin);
wait_request();
} }
sub test_drivers_type($type, $vm, $node) { sub test_drivers_type($type, $vm, $node) {
...@@ -123,7 +126,7 @@ sub test_drivers_type($type, $vm, $node) { ...@@ -123,7 +126,7 @@ sub test_drivers_type($type, $vm, $node) {
ok(!$@,"Expecting no error, got : ".($@ or '')); ok(!$@,"Expecting no error, got : ".($@ or ''));
is($domain->get_driver($type), $option->{value}, $type); is($domain->get_driver($type), $option->{value}, $type);
$domain->prepare_base(user_admin) if !$domain->is_base; $domain->prepare_base(user_admin);
$domain->set_base_vm(node => $node, user => user_admin); $domain->set_base_vm(node => $node, user => user_admin);
my $clone = $domain->clone(name => new_domain_name, user => user_admin); my $clone = $domain->clone(name => new_domain_name, user => user_admin);
...@@ -134,7 +137,12 @@ sub test_drivers_type($type, $vm, $node) { ...@@ -134,7 +137,12 @@ sub test_drivers_type($type, $vm, $node) {
is($clone2->get_driver($type), $option->{value}, $type); is($clone2->get_driver($type), $option->{value}, $type);
$clone->remove(user_admin); $clone->remove(user_admin);
my @vols = $domain->list_files_base();
$domain->remove_base(user_admin); $domain->remove_base(user_admin);
wait_request(debug => 0);
for my $vol (@vols) {
ok (! -e $vol ) or die "$vol";
}
} }
$domain->remove(user_admin); $domain->remove(user_admin);
......
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