Unverified Commit 78da0434 authored by Francesc Guasch's avatar Francesc Guasch Committed by GitHub
Browse files

Fix remove volatile volumes in nodes (#1353)

fix(nodes): remove volatiles reviewed

also fixed small bug showing clones in frontend

fixes issue #1345
parent 2de07598
......@@ -3483,7 +3483,11 @@ sub _cmd_list_isos($self, $request){
sub _cmd_set_time($self, $request) {
my $id_domain = $request->args('id_domain');
my $domain = Ravada::Domain->open($id_domain)
or confess "Error: domain $id_domain not found";
or do {
$request->retry(0);
Ravada::Request->refresh_vms();
die "Error: domain $id_domain not found\n";
};
return if !$domain->is_active;
eval { $domain->set_time() };
die "$@ , retry.\n" if $@;
......@@ -3606,8 +3610,9 @@ sub _refresh_down_domains($self, $active_domain, $active_vm) {
while ( my ($id_domain, $name, $id_vm) = $sth->fetchrow ) {
next if exists $active_domain->{$id_domain};
my $domain = Ravada::Domain->open($id_domain) or next;
next if $domain->is_hibernated;
my $domain;
eval { $domain = Ravada::Domain->open($id_domain) };
next if !$domain || $domain->is_hibernated;
if (defined $id_vm && !$active_vm->{$id_vm} ) {
$domain->_set_data(status => 'shutdown');
......@@ -3638,13 +3643,17 @@ sub _refresh_volatile_domains($self) {
);
$sth->execute();
while ( my ($id_domain, $name, $id_vm, $id_owner) = $sth->fetchrow ) {
my $domain = Ravada::Domain->open(id => $id_domain, _force => 1);
my $domain;
eval { $domain = Ravada::Domain->open(id => $id_domain, _force => 1) } ;
if ( !$domain || $domain->status eq 'down' || !$domain->is_active) {
if ($domain) {
$domain->_post_shutdown(user => $USER_DAEMON);
$domain->remove($USER_DAEMON);
} else {
my $sth= $CONNECTOR->dbh->prepare("DELETE FROM users WHERE id=?");
confess;
my $sth= $CONNECTOR->dbh->prepare(
"DELETE FROM users where id=? "
." AND is_temporary=1");
$sth->execute($id_owner);
$sth->finish;
}
......@@ -4004,7 +4013,9 @@ sub _clean_volatile_machines($self, %args) {
eval { $domain_real->remove($USER_DAEMON) };
warn $@ if $@;
} elsif ($domain->{id_owner}) {
my $sth = $CONNECTOR->dbh->prepare("DELETE FROM users where id=?");
my $sth = $CONNECTOR->dbh->prepare(
"DELETE FROM users where id=? "
."AND is_temporary=1");
$sth->execute($domain->{id_owner});
}
......
......@@ -584,6 +584,7 @@ Removes the user
=cut
sub remove($self) {
confess if $self->name eq 'f';
my $sth = $$CON->dbh->prepare("DELETE FROM users where id=?");
$sth->execute($self->id);
$sth->finish;
......
......@@ -2292,7 +2292,7 @@ sub clone {
my $vm = $self->_vm;
if ($self->volatile_clones ) {
$vm = $vm->balance_vm();
$vm = $vm->balance_vm($self);
} elsif( !$vm->is_local ) {
for my $node ($self->_vm->list_nodes) {
$vm = $node if $node->is_local;
......@@ -4063,7 +4063,14 @@ sub set_base_vm($self, %args) {
} elsif ($value) {
$request->status("working", "Syncing base volumes to ".$vm->host)
if $request;
$self->migrate($vm, $request);
eval {
$self->migrate($vm, $request);
};
my $err = $@;
if ( $err ) {
$self->_set_base_vm_db($vm->id, 0);
die $err;
}
$self->_set_clones_autostart(0);
} else {
$self->_set_vm($vm,1); # force set vm on domain
......@@ -4112,7 +4119,6 @@ sub remove_base_vm($self, %args) {
confess "ERROR: Unknown arguments ".join(',',sort keys %args).", valid are user and vm."
if keys %args;
warn $vm->name;
return $self->set_base_vm(vm => $vm, user => $user, value => 0);
}
......
......@@ -182,11 +182,14 @@ sub remove_disks {
$self->_vm->connect();
for my $file ($self->list_disks( device => 'disk')) {
if (! -e $file ) {
if (! $self->_vm->file_exists($file) ) {
next;
}
eval {
$self->_vol_remove($file);
$self->_vol_remove($file);
};
warn "Error: removing $file $@" if $@;
# if ( -e $file ) {
# unlink $file or die "$! $file";
# }
......
......@@ -394,7 +394,9 @@ sub _around_create_domain {
$self->_check_duplicate_name($name);
if ($id_base) {
$base = $self->search_domain_by_id($id_base)
my $vm_local = $self;
$vm_local = $self->new( host => 'localhost') if !$vm_local->is_local;
$base = $vm_local->search_domain_by_id($id_base)
or confess "Error: I can't find domain $id_base on ".$self->name;
$volatile = 1 if $base->volatile_clones;
if ($add_to_pool) {
......@@ -418,9 +420,9 @@ sub _around_create_domain {
confess("Error: Requested to add a clone for the pool but this base has no pools")
if !$base->pools;
}
$args_create{listen_ip} = $self->listen_ip($remote_ip);
$args_create{spice_password} = $self->_define_spice_password($remote_ip);
$self->_pre_create_domain(%args_create);
$args_create{listen_ip} = $self->listen_ip($remote_ip);
return $base->_search_pool_clone($owner) if $from_pool;
......@@ -430,6 +432,7 @@ sub _around_create_domain {
my $vm = $self->balance_vm($base) or die "Error: No free nodes available.";
$request->status("creating machine on ".$vm->name) if $request;
$self = $vm;
$args_create{listen_ip} = $self->listen_ip($remote_ip);
}
my $domain = $self->$orig(%args_create, volatile => $volatile);
......@@ -1534,6 +1537,7 @@ sub balance_vm($self, $base=undef) {
if ($base) {
@vms = $base->list_vms();
} else {
confess "Error: we need a base to balance ";
@vms = $self->list_nodes();
}
return $vms[0] if scalar(@vms)<1;
......
......@@ -930,7 +930,7 @@ sub _domain_create_common {
};
close $out;
warn "$! $name_out" if !$out;
confess $@ if !$dom;
confess $@;# if !$dom;
}
my $domain = Ravada::Domain::KVM->new(
......@@ -996,7 +996,9 @@ sub _domain_create_from_base {
my $base = $args{base};
my $with_cd = delete $args{with_cd};
$base = $self->_search_domain_by_id($args{id_base}) if $args{id_base};
my $vm_local = $self;
$vm_local = $self->new( host => 'localhost') if !$vm_local->is_local;
$base = $vm_local->_search_domain_by_id($args{id_base}) if $args{id_base};
confess "Unknown base id: $args{id_base}" if !$base;
my $vm = $self->vm;
......
......@@ -64,7 +64,7 @@ sub clone($self, $file_clone) {
,$file_clone
);
my ($out, $err) = $self->vm->run_command(@cmd);
confess $err if $err;
confess $self->vm->name." ".$err if $err;
return $file_clone;
}
......
......@@ -137,6 +137,7 @@ ravadaApp.directive("solShowMachine", swMach)
};
function machinesPageC($scope, $http, $interval, $timeout, request, listMach) {
$scope.list_machines_time = 0;
if( $scope.check_netdata && $scope.check_netdata != "0" ) {
var url = $scope.check_netdata;
$scope.check_netdata = 0;
......@@ -161,7 +162,6 @@ ravadaApp.directive("solShowMachine", swMach)
subscribe_ping_backend(url);
};
subscribe_list_machines= function(url) {
ws_connected = false;
$timeout(function() {
if (!ws_connected) {
......@@ -179,6 +179,7 @@ ravadaApp.directive("solShowMachine", swMach)
ws = new WebSocket(url);
};
ws.onmessage = function (event) {
$scope.list_machines_time++;
var data = JSON.parse(event.data);
$scope.$apply(function () {
......@@ -194,7 +195,11 @@ ravadaApp.directive("solShowMachine", swMach)
){
$scope.list_machines[mach.id] = mach;
$scope.list_machines[mach.id].childs = {};
$scope.list_machines[mach.id].childs_loading = true;
if ($scope.list_machines_time < 3) {
$scope.list_machines[mach.id].childs_loading = true;
} else {
$scope.list_machines[mach.id].childs_loading = false;
}
}
}
$scope.n_clones = 0;
......@@ -212,7 +217,6 @@ ravadaApp.directive("solShowMachine", swMach)
){
childs[mach.id] = mach;
$scope.n_clones++;
$scope.list_machines[mach.id_base].childs_loading = false;
}
}
if ($scope.auto_hide_clones) {
......
......@@ -99,7 +99,6 @@ setlocale(LC_CTYPE, $old_locale);
#####
#####
plugin I18N => {namespace => 'Ravada::I18N', default => 'en'};
plugin 'RenderFile';
my %config;
%config = (config => $CONFIG_FRONT->{config}) if $CONFIG_FRONT->{config};
......
......@@ -429,6 +429,51 @@ sub test_removed_base_file_and_swap_remote($vm, $node) {
$base->remove(user_admin);
}
sub test_set_vm_fail($vm, $node) {
return if $vm->type ne 'KVM';
diag("Test set vm fail");
my $base = create_domain($vm);
$base->volatile_clones(1);
my $pool2 = create_storage_pool($vm);
$vm->default_storage_pool_name($pool2);
$base->add_volume( size => 11000 );
$base->prepare_base(user_admin);
$base->_set_base_vm_db($node->id, 1);
my $req = Ravada::Request->set_base_vm(
id_domain => $base->id
, id_vm => $node->id
, value => 1
, uid => user_admin->id
);
rvd_back->_process_requests_dont_fork();
is($req->status, 'done');
like($req->error, qr/storage pool/i);
is($base->base_in_vm($node->id),0) or exit;
$req = Ravada::Request->clone(
id_domain => $base->id
,number => 3
,uid => user_admin->id
);
rvd_back->_process_all_requests_dont_fork();
rvd_back->_process_all_requests_dont_fork();
is($req->status, 'done');
is($req->error,'');
ok(scalar($base->clones));
_remove_domain($base);
my $pool = $vm->vm->get_storage_pool_by_name($pool2);
eval {
$pool->destroy();
$pool->undefine();
};
warn $@ if$@ && $@ !~ /libvirt error code: 49,/;
$vm->default_storage_pool_name('default');
}
sub test_set_vm($vm, $node) {
my $base = create_domain($vm);
......@@ -545,11 +590,10 @@ sub test_volatile($vm, $node) {
sub test_volatile_req($vm, $node) {
my $base = create_domain($vm);
$base->volatile_clones(1);
$base->prepare_base(user_admin);
$base->set_base_vm(user => user_admin, node => $node);
$base->volatile_clones(1);
ok($base->base_in_vm($node->id));
my @clones;
my $clone;
for ( 1 .. 20 ) {
......@@ -572,15 +616,69 @@ sub test_volatile_req($vm, $node) {
is($clone->_vm->id, $node->id) or exit;
shutdown_domain_internal($clone);
rvd_back->_cmd_refresh_vms();
for my $vol ( $clone->list_volumes ) {
ok(!$vm->file_exists($vol),$vol) or exit;
ok(!$node->file_exists($vol),$vol) or exit;
}
for (@clones) {
$_->remove(user_admin);
_remove_domain($base);
}
sub test_volatile_req_clone($vm, $node) {
my $base = create_domain($vm);
$base->prepare_base(user_admin);
$base->set_base_vm(user => user_admin, node => $node);
$base->volatile_clones(1);
ok($base->base_in_vm($node->id));
my $clone;
for ( 1 .. 20 ) {
my $req = Ravada::Request->clone(
id_domain => $base->id
,number => 3
,uid => user_admin->id
);
rvd_back->_process_all_requests_dont_fork();
rvd_back->_process_all_requests_dont_fork();
is($req->status, 'done');
is($req->error,'');
is(scalar($base->clones),3);
($clone) = grep { $_->{id_vm} == $node->id } $base->clones;
last if $clone;
}
is($clone->{id_vm}, $node->id) or exit;
my @vols;
my @clones;
for my $clone_data ($base->clones) {
my $clone2 = Ravada::Domain->open($clone_data->{id});
push @clones,($clone2);
push @vols,($clone2->list_volumes);
shutdown_domain_internal($clone2);
}
rvd_back->_cmd_refresh_vms();
for my $vol ( @vols ) {
ok(!$vm->file_exists($vol),$vol) or exit;
ok(!$node->file_exists($vol),$vol) or exit;
}
my @req;
for my $clone2 (@clones) {
my $req = Ravada::Request->remove_domain(
name => $clone2->name
,uid => user_admin->id
);
push @req,($req);
}
wait_request();
for my $req (@req) {
is($req->status,'done');
like($req->error,qr/(^$|Unknown)/);
}
$base->remove(user_admin);
}
sub test_volatile_tmp_owner($vm, $node) {
my $base = create_domain($vm);
$base->prepare_base(user_admin);
......@@ -711,7 +809,6 @@ sub test_remove_base($vm, $node, $volatile) {
}
$base->remove(user_admin);
}
sub _check_internal_autostart($domain, $expected) {
......@@ -961,7 +1058,7 @@ sub test_fill_memory($vm, $node, $migrate) {
}
ok(exists $nodes{$vm->name},"Expecting some clones to node ".$vm->name." ".$vm->id);
ok(exists $nodes{$node->name},"Expecting some clones to node ".$node->name." ".$node->id);
_remove_clones($base);
_remove_domain($base);
}
##################################################################################
......@@ -1004,6 +1101,9 @@ for my $vm_name ( 'Void', 'KVM') {
start_node($node);
test_set_vm_fail($vm, $node);
test_volatile_req_clone($vm, $node);
test_change_base($vm, $node);
test_change_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