Skip to content

Commit

Permalink
fixed remove domain not in db
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejol committed Oct 7, 2016
1 parent 7c94772 commit 7894046
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
21 changes: 20 additions & 1 deletion lib/Ravada/Domain.pm
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ our $CONNECTOR = \$Ravada::CONNECTOR;
before 'display' => \&_allowed;

before 'remove' => \&_allow_remove;
after 'remove' => \&_remove_domain_db;
after 'remove' => \&_after_remove_domain;

before 'prepare_base' => \&_allow_prepare_base;
after 'prepare_base' => sub { my $self = shift; $self->is_base(1) };
Expand Down Expand Up @@ -247,6 +247,12 @@ sub _insert_db {

}

sub _after_remove_domain {
my $self = shift;
$self->_remove_files_base();
$self->_remove_domain_db();
}

sub _remove_domain_db {
my $self = shift;

Expand All @@ -257,6 +263,14 @@ sub _remove_domain_db {
$sth->finish;
}

sub _remove_files_base {
my $self = shift;

for my $file ( $self->list_files_base ) {
unlink $file or die "$! $file" if -e $file;
}
}


=head2 is_base
Expand Down Expand Up @@ -313,6 +327,11 @@ sub clones {
sub list_files_base {
my $self = shift;

my $id;
eval { $id = $self->id };
return if $@ && $@ =~ /No DB info/i;
die $@ if $@;

my $sth = $$CONNECTOR->dbh->prepare("SELECT file_base_img "
." FROM file_base_images "
." WHERE id_domain=?");
Expand Down
20 changes: 9 additions & 11 deletions lib/Ravada/Domain/KVM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ sub remove_disks {
my $self = shift;

my $removed = 0;

my $id;
eval { $id = $self->id };
return if $@ && $@ =~ /No DB info/i;
die $@ if $@;
for my $file ($self->list_disks) {
if (! -e $file ) {
warn "WARNING: $file already removed for ".$self->domain->get_name."\n";
Expand Down Expand Up @@ -131,8 +136,6 @@ sub _vol_remove {
Removes this domain. It removes also the disk drives and base images.
TODO: check if it is base for other domains, and refuse if it is.
=cut

sub remove {
Expand All @@ -143,17 +146,16 @@ sub remove {

$self->domain->destroy if $self->domain->is_active();

eval { $self->remove_disks() };
warn "WARNING: Problem removing disks for ".$self->name." : $@" if $@ && $0 !~ /\.t$/;
$self->remove_disks();
# warn "WARNING: Problem removing disks for ".$self->name." : $@" if $@ && $0 !~ /\.t$/;

eval { $self->_remove_file_image() };
warn "WARNING: Problem removing file image for ".$self->name." : $@" if $@ && $0 !~ /\.t$/;
$self->_remove_file_image();
# warn "WARNING: Problem removing file image for ".$self->name." : $@" if $@ && $0 !~ /\.t$/;

# warn "WARNING: Problem removing ".$self->file_base_img." for ".$self->name
# ." , I will try again later : $@" if $@;

$self->domain->undefine();
eval { $self->_remove_file_image() };
}


Expand Down Expand Up @@ -485,8 +487,4 @@ sub list_volumes {
return $self->disk_device();
}

sub file_base_img {
confess "DEPRECATED";
}

1;
3 changes: 2 additions & 1 deletion t/kvm/20_domain_kvm.t
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ sub test_remove_domain {

if ($domain) {
diag("Removing domain $name");
$domain->remove($user);
eval { $domain->remove($user) };
ok(!$@,"Domain $name should be removed ".$@) or exit;
}
$domain = $RAVADA->search_domain($name);
die "I can't remove old domain $name"
Expand Down
19 changes: 10 additions & 9 deletions t/kvm/30_request.t
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ sub test_req_prepare_base {

my $domain = $RAVADA->search_domain($name);
ok($domain->is_base,"Domain $name should be base");
ok($domain->file_base_img,"Domain $name has no file_base_img");
ok(scalar $domain->list_files_base," Domain $name should have files_base, got ".
scalar $domain->list_files_base);

}

Expand All @@ -49,10 +50,9 @@ sub test_remove_domain {
eval { $domain->remove($USER) };
ok(!$@ , "Error removing domain $name : $@") or exit;

ok(! -e $domain->file_base_img ,"Image file was not removed "
. $domain->file_base_img )
if $domain->file_base_img;

for my $file ( $domain->list_files_base ) {
ok(! -e $file,"Image file $file should be removed");
}
}
$domain = $RAVADA->search_domain($name,1);
ok(!$domain, "I can't remove old domain $name") or exit;
Expand All @@ -70,9 +70,10 @@ sub test_dont_remove_father {
eval { $domain->remove($USER) };
ok($@ , "Error removing domain $name with clones should not be allowed");

ok( -e $domain->file_base_img ,"Image file was removed "
. $domain->file_base_img )
if $domain->file_base_img;
for my $file ( $domain->list_files_base ) {
ok( -e $file,"Image file $file should not be removed") or exit;
}


}
$domain = $RAVADA->search_domain($name,1);
Expand Down Expand Up @@ -144,7 +145,7 @@ sub test_req_create_domain_iso {

ok($req->status eq 'done'
,"Status of request is ".$req->status." it should be done");
ok(!$req->error,"Error ".$req->error." creating domain ".$name);
ok(!$req->error,"Error '".$req->error."' creating domain ".$name);

my $domain = $RAVADA->search_domain($name);

Expand Down
3 changes: 3 additions & 0 deletions t/lib/Test/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ sub _remove_old_domains_vm {
eval {
$domain->remove( $USER_ADMIN );
};
if ( $@ && $@ =~ /No DB info/i ) {
eval { $domain->domain->undefine() if $domain->domain };
}
ok(!$@ , "Error removing domain $dom_name ".ref($domain).": $@") or exit;
}

Expand Down

0 comments on commit 7894046

Please sign in to comment.