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

[#61] new method is known to avoid forks and evals

parent c4387b50
......@@ -165,7 +165,7 @@ sub _allow_remove {
my ($user) = @_;
$self->_allowed($user);
$self->_check_has_clones();
$self->_check_has_clones() if $self->is_known();
}
......@@ -186,10 +186,9 @@ sub _allow_prepare_base {
sub _check_has_clones {
my $self = shift;
my @clones;
return if !$self->is_known();
eval { @clones = $self->clones };
die $@ if $@ && $@ !~ /No DB info/i;
my @clones = $self->clones;
die "Domain ".$self->name." has ".scalar @clones." clones : ".Dumper(\@clones)
if $#clones>=0;
}
......@@ -270,8 +269,10 @@ sub _allowed {
##################################################################################3
sub _init_connector {
$CONNECTOR = \$Ravada::CONNECTOR;
$CONNECTOR = \$Ravada::Front::CONNECTOR if !defined $$CONNECTOR;
return if $CONNECTOR && $$CONNECTOR;
$CONNECTOR = \$Ravada::CONNECTOR if $Ravada::CONNECTOR;
$CONNECTOR = \$Ravada::Front::CONNECTOR if !defined $$CONNECTOR
&& defined $Ravada::Front::CONNECTOR;
}
=head2 id
......@@ -315,10 +316,23 @@ sub __open {
# confess $row;
}
=head2 is_known
Returns if the domain is known in Ravada.
=cut
sub is_known {
my $self = shift;
return $self->_select_domain_db(name => $self->name);
}
sub _select_domain_db {
my $self = shift;
my %args = @_;
_init_connector();
if (!keys %args) {
my $id;
eval { $id = $self->id };
......@@ -337,7 +351,7 @@ sub _select_domain_db {
$sth->finish;
$self->{_data} = $row;
return $row;
return $row if $row->{id};
}
sub _prepare_base_db {
......@@ -403,6 +417,8 @@ sub _after_remove_domain {
sub _remove_domain_db {
my $self = shift;
return if !$self->is_known();
$self->_select_domain_db or return;
my $sth = $$CONNECTOR->dbh->prepare("DELETE FROM domains "
." WHERE id=?");
......@@ -531,6 +547,8 @@ Returns a list of the filenames of this base-type domain
sub list_files_base {
my $self = shift;
return if !$self->is_known();
my $id;
eval { $id = $self->id };
return if $@ && $@ =~ /No DB info/i;
......@@ -681,6 +699,7 @@ sub _remove_temporary_machine {
my %args = @_;
my $user;
return if !$self->is_known();
eval { $user = Ravada::Auth::SQL->search_by_id($self->id_owner) };
return if !$user;
......
......@@ -47,7 +47,8 @@ Returns the name of the domain
sub name {
my $self = shift;
return $self->domain->get_name;
$self->{_name} = $self->domain->get_name if !$self->{_name};
return $self->{_name};
}
sub _wait_down {
......@@ -101,10 +102,14 @@ sub remove_disks {
my $removed = 0;
return if !$self->is_known();
my $id;
eval { $id = $self->id };
return if $@ && $@ =~ /No DB info/i;
die $@ if $@;
$self->_vm->connect();
for my $file ($self->list_disks) {
if (! -e $file ) {
warn "WARNING: $file already removed for ".$self->domain->get_name."\n"
......@@ -118,6 +123,7 @@ sub remove_disks {
$removed++;
}
$self->_vm->disconnect();
warn "WARNING: No disk files removed for ".$self->domain->get_name."\n"
if !$removed;
......@@ -131,13 +137,10 @@ sub _vol_remove {
my ($name) = $file =~ m{.*/(.*)} if $file =~ m{/};
my $vol;
eval { $vol = $self->storage->get_volume_by_name($name) };
if (!$vol) {
# cluck "WARNING: I can't find volume $name" if !$warning;
return;
my @vols = $self->storage->list_volumes();
for my $vol ( @vols ) {
$vol->delete() if$vol->get_name eq $name;
}
$vol->delete();
return 1;
}
......
......@@ -36,7 +36,9 @@ sub BUILD {
return if -e $file_img;
$self->add_volume(name => 'void-diska' , size => $args->{disk}
, path => $file_img);
, path => $file_img)
if !$args->{is_readonly};
$self->_set_default_info();
$self->set_memory($args->{memory}) if $args->{memory};
}
......
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