Commit 895eb266 authored by Francesc Guasch's avatar Francesc Guasch
Browse files

[#533] if domain already removed clean the DB

parent 169dea7d
......@@ -1083,11 +1083,18 @@ sub remove_domain {
lock_hash(%arg);
my $domain = $self->search_domain($arg{name}, 1)
or die "ERROR: I can't find domain '$arg{name}', maybe already removed.";
my $domain = $self->search_domain($arg{name}, 1);
my $user = Ravada::Auth::SQL->search_by_id( $arg{uid});
$domain->remove( $user);
if ($domain) {
$domain->remove( $user);
} else {
$domain = Ravada::Front::Domain->search_domain($arg{name});
if ($domain) {
$domain->_allow_remove($user); # dies if not allowed
$domain->_after_remove_domain($user);
}
}
}
=head2 search_domain
......
......@@ -927,6 +927,7 @@ sub _after_remove_domain {
# removes domain in other VMs
sub _remove_domain_cascade($self,$user, $cascade = 1) {
return if !$self->_vm;
my $sth = $$CONNECTOR->dbh->prepare("SELECT id,name FROM vms WHERE is_active=1");
my ($id, $name);
$sth->execute();
......
......@@ -26,10 +26,15 @@ has 'readonly' => (
,default => 1
);
our $CONNECTOR = \$Ravada::Front::CONNECTOR;
our $CONNECTOR;
#
###########################################################################
sub _init_connector {
$CONNECTOR= \$Ravada::CONNECTOR;
$CONNECTOR= \$Ravada::Front::CONNECTOR if !$$CONNECTOR;
}
sub BUILD($self, $arg) {
my $id = $arg->{id} or confess "ERROR: id required";
my $ret = $self->_select_domain_db( id => $id);
......@@ -88,6 +93,7 @@ sub resume { confess "TODO" }
sub screenshot { confess "TODO" }
sub search_domain($self,$name) {
_init_connector();
my $sth = $$CONNECTOR->dbh->prepare("SELECT id FROM domains WHERE name=?");
$sth->execute($name);
my ($id) = $sth->fetchrow;
......@@ -102,4 +108,5 @@ sub shutdown { confess "TODO" }
sub shutdown_now { confess "TODO" }
sub spinoff_volumes { confess "TODO" }
sub start { confess "TODO" }
1;
......@@ -215,7 +215,7 @@ sub _open_type {
my $proto = {};
bless $proto,$class;
my $vm = $proto->new(@_);
my $vm = $proto->new(%args);
eval { $vm->vm };
warn $@ if $@;
return if $@;
......
......@@ -20,7 +20,7 @@ use_ok('Ravada');
##########################################################
sub test_vm_connect {
my $vm = Ravada::VM::KVM->new(backend => $BACKEND );
my $vm = Ravada::VM::KVM->new();
ok($vm);
ok($vm->type eq 'KVM');
ok($vm->host eq 'localhost');
......
......@@ -20,6 +20,7 @@ my @ARG_RVD = ( config => $FILE_CONFIG, connector => $test->connector);
my $RVD_BACK;
init( $test->connector, $FILE_CONFIG);
eval { $RVD_BACK = rvd_back($test->connector, $FILE_CONFIG) };
ok($RVD_BACK) or exit;
......@@ -218,6 +219,23 @@ sub test_remove_domain {
}
sub test_remove_domain_already_gone {
my $vm_name = shift;
my $domain = create_domain($vm_name);
if ($vm_name eq 'KVM') {
$domain->domain->undefine();
} elsif ($vm_name eq 'Void') {
unlink $domain->_config_file();
}
rvd_back->remove_domain( name => $domain->name, uid => user_admin->id);
my $domain_b = rvd_back->search_domain($domain->name);
ok(!$domain_b);
my $domain_f = rvd_front->search_domain($domain->name);
ok(!$domain_f,"[$vm_name] Expecting no domain ".$domain->name." in front") or exit;
}
sub test_search_domain {
my $domain = shift;
my $domain0 = rvd_back()->search_domain($domain->name);
......@@ -451,6 +469,8 @@ for my $vm_name (qw( Void KVM )) {
test_vm_connect($vm_name, $host, $conf);
test_search_vm($vm_name, $host, $conf);
test_remove_domain_already_gone($vm_name);
test_create_domain_nocd($vm_name, $host);
my $domain = test_create_domain($vm_name, $host);
......
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