Commit 49fc44ad authored by Francesc Guasch's avatar Francesc Guasch
Browse files

[#320] KVM volatile domains should be removed on shutdown

Letting libvirt do the removal automatically
parent 9a94375a
......@@ -720,6 +720,7 @@ sub _upgrade_tables {
$self->_upgrade_table('domains','spice_password','varchar(20) DEFAULT NULL');
$self->_upgrade_table('domains','description','text DEFAULT NULL');
$self->_upgrade_table('domains','run_timeout','int DEFAULT NULL');
$self->_upgrade_table('domains','is_volatile','int NOT NULL DEFAULT 0');
}
......
......@@ -1031,9 +1031,9 @@ sub _post_shutdown {
my %arg = @_;
my $timeout = $arg{timeout};
$self->_remove_temporary_machine(@_);
$self->_remove_iptables(@_);
$self->clean_swap_volumes(@_) if $self->id_base() && !$self->is_active;
$self->_remove_temporary_machine(@_);
if (defined $timeout) {
if ($timeout<2 && $self->is_active) {
......@@ -1127,11 +1127,7 @@ sub _remove_temporary_machine {
return if !$self->is_known();
eval { $user = Ravada::Auth::SQL->search_by_id($self->id_owner) };
return if !$user;
if ($user->is_temporary) {
$self->remove($user);
if ($self->is_volatile) {
my $req= $args{request};
$req->status(
"removing"
......@@ -1139,6 +1135,11 @@ sub _remove_temporary_machine {
." because user "
.$user->name." is temporary")
if $req;
my $domain_copy;
eval { $domain_copy = $self->_vm->search_domain($self->name) };
warn "going to remove";sleep 1;
$self->remove($user) if $domain_copy ;
}
}
......@@ -1369,6 +1370,16 @@ sub is_public {
return $self->_data('is_public');
}
=head2 is_volatile
Returns if the domain is volatile, so it will be removed on shutdown
=cut
sub is_volatile($self) {
return $self->_data('is_volatile');
}
=head2 run_timeout
Sets or get the domain run timeout. When it expires it is shut down.
......
......@@ -143,6 +143,7 @@ sub _around_create_domain {
$domain->run_timeout($base->run_timeout)
if defined $base->run_timeout();
}
die "TODO if user->is_temporary domain->is_temporary(1)";
return $domain;
}
......
......@@ -622,6 +622,10 @@ sub _domain_create_common {
my $xml = shift;
my %args = @_;
my $id_owner = delete $args{id_owner} or confess "ERROR: The id_owner is mandatory";
my $user = Ravada::Auth::SQL->search_by_id($id_owner)
or confess "ERROR: User id $id_owner doesn't exist";
$self->_xml_modify_memory($xml,$args{memory}) if $args{memory};
$self->_xml_modify_network($xml , $args{network}) if $args{network};
$self->_xml_modify_mac($xml);
......@@ -632,8 +636,12 @@ sub _domain_create_common {
my $dom;
eval {
$dom = $self->vm->define_domain($xml->toString());
$dom->create if $args{active};
if ($user->is_temporary) {
$dom = $self->vm->create_domain($xml->toString());
} else {
$dom = $self->vm->define_domain($xml->toString());
$dom->create if $args{active};
}
};
if ($@) {
my $out;
......
......@@ -69,6 +69,29 @@ sub allow_anonymous {
}
sub test_volatile {
my ($vm_name, $base) = @_;
my $name = new_domain_name();
my $user_name = new_domain_name();
my $user = Ravada::Auth::SQL::add_user(name => $user_name, is_temporary => 1);
my $clone = $base->clone(
user => $user
, name => $name
);
is($clone->is_active,1,"[$vm_name] Expecting clone active");
$clone->start($user) if !$clone->is_active;
is($clone->is_volatile,1);
eval { $clone->shutdown_now(user_admin) if $clone->is_active};
is(''.$@,'',"[$vm_name] Expecting no error after shutdown");
my $vm = rvd_back->search_vm($vm_name);
my $domain2 = $vm->search_domain($name);
ok(!$domain2,"[$vm_name] Expecting domain removed after shutdown");
}
################################################################################
......@@ -87,15 +110,16 @@ for my $vm_name ('Void', 'KVM') {
}
skip($msg,10) if !$vm;
diag("Testing timeout for $vm_name");
diag("Testing volatile for $vm_name");
create_network();
my $base= create_domain($vm_name);
$base->prepare_base(user_admin());
$base->is_public(1);
allow_anonymous($base);
test_volatile($vm_name);
test_volatile($vm_name, $base);
delete_network();
}
......
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