Skip to content

Commit

Permalink
Merge branch 'main' into feature/network
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejol authored Jan 22, 2024
2 parents f028d48 + 369a0df commit 6baee9e
Show file tree
Hide file tree
Showing 31 changed files with 2,071 additions and 149 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
**Bugfixes**

**Refactors**

38 changes: 28 additions & 10 deletions lib/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package Ravada;
use warnings;
use strict;

our $VERSION = '2.1.0';
our $VERSION = '2.2.0';

use utf8;

Expand Down Expand Up @@ -237,10 +237,11 @@ sub _add_internal_network($self) {
." VALUES(?,?,?,1,0)"
);
my $n=0;
my %done;
for my $net (split /\n/,$out) {
next if $net =~ /dev virbr/;
my ($address) = $net =~ m{(^[\d\.]+/\d+)};
next if !$address;
next if !$address || $done{address}++;
$sth->execute("internal$n",$address, ++$n+1);

}
Expand Down Expand Up @@ -3456,7 +3457,10 @@ sub remove_domain {
$sth->execute($name);

my ($id,$vm_type)= $sth->fetchrow;
confess "Error: Unknown domain $name" if !$id;
if (!$id) {
warn "Error: Unknown domain $name, maybe already removed.\n";
return;
}

my $user = Ravada::Auth::SQL->search_by_id( $arg{uid});
die "Error: user ".$user->name." can't remove domain $id"
Expand All @@ -3474,7 +3478,7 @@ sub remove_domain {
eval { $domain = Ravada::Domain->open(id => $id, _force => 1, id_vm => $vm->id) };
warn $@ if $@;
if (!$domain) {
warn "Warning: I can't find domain '$id', maybe already removed.\n";
warn "Warning: I can't find domain [$id ] '$name' in ".$vm->name.", maybe already removed.\n";
Ravada::Domain::_remove_domain_data_db($id);
return;
};
Expand Down Expand Up @@ -3842,7 +3846,7 @@ sub process_requests {
$self->_timeout_requests();
}

my $sth = $CONNECTOR->dbh->prepare("SELECT id,id_domain FROM requests "
my $sth = $CONNECTOR->dbh->prepare("SELECT id,id_domain,command FROM requests "
." WHERE "
." ( status='requested' OR status like 'retry%' OR status='waiting')"
." AND ( at_time IS NULL OR at_time = 0 OR at_time<=?) "
Expand All @@ -3852,25 +3856,35 @@ sub process_requests {

my @reqs;
my %duplicated;
while (my ($id_request,$id_domain)= $sth->fetchrow) {
while (my ($id_request, $id_domain, $command)= $sth->fetchrow) {
my $req;
eval { $req = Ravada::Request->open($id_request) };

next if $@ && $@ =~ /I can't find/;
warn $@ if $@;
next if !$req;

if ($req->command eq 'ping_backend') {
$req->status("done");
next;
}
next if !$req->requirements_done;

next if $request_type ne 'all' && $req->type ne $request_type;

next if $duplicated{"id_req.$id_request"}++;

$id_domain = $req->defined_arg('id_domain') if !defined $id_domain && $req->defined_arg('id_domain');

next if defined $id_domain && $duplicated{$id_domain.".$command"}++;

next if $req->command !~ /shutdown/i
&& $self->_domain_working($id_domain, $req);

my $domain = '';
$domain = $id_domain if $id_domain;
$domain .= ($req->defined_arg('name') or '');

next if $domain && $duplicated{$domain};
my $id_base = $req->defined_arg('id_base');
next if $id_base && $duplicated{$id_base};
Expand Down Expand Up @@ -5675,6 +5689,7 @@ sub _cmd_set_time($self, $request) {
};
return if !$domain->is_active;
eval { $domain->set_time() };
return if $@ =~ /Guest agent is not responding/ && $domain->get_info()->{ip};
die "$@ , retry.\n" if $@;
}

Expand Down Expand Up @@ -6093,22 +6108,24 @@ sub _remove_unnecessary_downs($self, $domain) {

sub _refresh_volatile_domains($self) {
my $sth = $CONNECTOR->dbh->prepare(
"SELECT id, name, id_vm, id_owner FROM domains WHERE is_volatile=1"
"SELECT id, name, id_vm, id_owner, vm FROM domains WHERE is_volatile=1"
);
$sth->execute();
while ( my ($id_domain, $name, $id_vm, $id_owner) = $sth->fetchrow ) {
while ( my ($id_domain, $name, $id_vm, $id_owner, $type) = $sth->fetchrow ) {
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 {
cluck "Warning: temporary user id=$id_owner should already be removed";
my $user;
eval { $user = Ravada::Auth::SQL->search_by_id($id_owner) };
warn $@ if $@;
$user->remove() if $user;
if ($user && $user->is_temporary) {
cluck "Warning: temporary user id=$id_owner should already be removed";
$user->remove();
}
}
my $sth_del = $CONNECTOR->dbh->prepare("DELETE FROM domains WHERE id=?");
$sth_del->execute($id_domain);
Expand All @@ -6117,6 +6134,7 @@ sub _refresh_volatile_domains($self) {
$sth_del = $CONNECTOR->dbh->prepare("DELETE FROM requests where id_domain=?");
$sth_del->execute($id_domain);
$sth_del->finish;
Ravada::Domain::_remove_domain_data_db($id_domain, $type);
}
}
}
Expand Down
93 changes: 68 additions & 25 deletions lib/Ravada/Domain.pm
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ sub _around_start($orig, $self, @arg) {
$self->_dettach_host_devices();
}
$$CONNECTOR->disconnect;
$self->status('starting') if $self->is_known();
eval { $self->$orig(%arg) };
$error = $@;
last if !$error;
Expand Down Expand Up @@ -547,7 +548,6 @@ sub _search_already_started($self, $fast = 0) {
eval { $domain = $vm->search_domain($self->name) };
if ( $@ ) {
warn $@;
$vm->enabled(0) if !$vm->is_local;
next;
}
next if !$domain;
Expand Down Expand Up @@ -600,6 +600,7 @@ sub _balance_vm($self, $request=undef) {
}
die $@;
}
return if !$vm_free;
return $vm_free->id;
}

Expand Down Expand Up @@ -659,8 +660,10 @@ sub _allow_remove($self, $user) {
&& $self->id_base
&& ($user->can_remove_clones() || $user->can_remove_clone_all())
) {
my $base = $self->open(id => $self->id_base, id_vm => $self->_vm->id);
return if ($user->can_remove_clone_all() || ($base->id_owner == $user->id));
my $base = $self->open($self->id_base);

die "ERROR: remove not allowed for user ".$user->name
unless ($user->can_remove_clone_all() || ($base->id_owner == $user->id));
}

}
Expand Down Expand Up @@ -909,6 +912,8 @@ sub _pre_prepare_base($self, $user, $request = undef ) {
sleep 1;
}
}
$self->_unlock_host_devices() if !$self->is_active;

# $self->_post_remove_base();
if (!$self->is_local) {
my $vm_local = Ravada::VM->open( type => $self->vm );
Expand Down Expand Up @@ -983,6 +988,8 @@ sub _around_autostart($orig, $self, @arg) {
confess "ERROR: Autostart can't be activated on base ".$self->name
if $value && $self->is_base;

confess "Error: autostart can't be set on volatile domains" if $self->is_volatile && defined $value;

confess "ERROR: You can't set autostart on readonly domains"
if defined $value && $self->readonly;
my $autostart = 0;
Expand Down Expand Up @@ -1724,15 +1731,20 @@ sub open($class, @args) {

my $vm_changed;
if (!$vm && ( $id_vm || defined $row->{id_vm} ) ) {
$id_vm = $row->{id_vm} if !defined $id_vm;
$self->_check_proper_id_vm($id, \$id_vm);
eval {
$vm = Ravada::VM->open(id => ( $id_vm or $row->{id_vm} )
, readonly => $readonly);
$vm = Ravada::VM->open(id => $id_vm, readonly => $readonly);
};
warn $@ if $@;
if ($@ && $@ =~ /I can't find VM id=/) {
$vm = Ravada::VM->open( type => $self->type );
$vm_changed = $vm;
warn "Error connecting to $id_vm ".$@ if $@;
if (!$vm) {
Ravada::VM::_clean_cache();
}
eval {
$vm = Ravada::VM->open(id => $id_vm, readonly => $readonly);
};
warn "Error connecting to $id_vm [retried]".$@ if $@;
return if !$vm;
}
my $vm_local;
if ( !$vm || !$vm->is_active ) {
Expand Down Expand Up @@ -1761,6 +1773,27 @@ sub open($class, @args) {
return $domain;
}

sub _check_proper_id_vm($self, $id, $id_vm) {
my @instances = ({ id_vm => $$id_vm } , $self->list_instances($id) );
for my $instance ( @instances ) {
my $vm;
eval {
$vm = Ravada::VM->open($instance->{id_vm});
};
warn $@ if $@ && $@ !~ /I can't find VM/;
next if !$vm;

return if $$id_vm == $instance->{id_vm};

$$id_vm = $instance->{id_vm};
my $sth = $$CONNECTOR->dbh->prepare("UPDATE domains set id_vm=?"
." WHERE id=?"
);
$sth->execute($$id_vm, $id);
return;
}
}

=head2 check_status
Checks if a virtual machine known status is in sync.
Expand Down Expand Up @@ -2244,7 +2277,10 @@ sub _pre_remove_domain($self, $user, @) {
warn "Warning: $@" if $@;
}
$self->pre_remove();
$self->_remove_iptables() if $self->is_known();
if ($self->is_known) {
$self->_remove_iptables();
$self->_unlock_host_devices();
}
eval { $self->shutdown_now($user) if $self->is_active };
warn "Warning: $@" if $@;

Expand Down Expand Up @@ -2298,7 +2334,8 @@ sub restore($self,$user){
# check the node is active
# search the domain in another node if it is not
sub _check_active_node($self) {
return $self->_vm if $self->_vm->is_active(1);
return 0 if !$self->_vm;
return $self->_vm if $self->_vm && $self->_vm->is_active(1);

for my $node ($self->_vm->list_nodes) {
next if !$node->is_local;
Expand All @@ -2313,9 +2350,7 @@ sub _check_active_node($self) {

}

sub _after_remove_domain {
my $self = shift;
my ($user, $cascade) = @_;
sub _after_remove_domain($self, $user, $cascade=undef) {

$self->_remove_iptables( );
$self->remove_expose();
Expand Down Expand Up @@ -2713,6 +2748,8 @@ sub _do_remove_base($self, $user) {
my $vm_local = $self->_vm->new( host => 'localhost' );
for my $vol ($self->list_volumes_info) {
next if !$vol->file || $vol->file =~ /\.iso$/;
next if !$self->_vm->file_exists($vol->file);

my ($dir) = $vol->file =~ m{(.*)/};

next if !$self->is_local && $self->_vm->shared_storage($vm_local, $dir);
Expand Down Expand Up @@ -4020,8 +4057,10 @@ sub _remove_temporary_machine($self) {
$owner= Ravada::Auth::SQL->search_by_id($self->id_owner) if $self->is_known();

if ($self->is_removed) {
$self->remove_disks();
$self->_after_remove_domain();
eval { $self->remove_disks(); };
die $@ if $@ && $@ !~ /domain not available/;
$owner = Ravada::Utils::user_daemon() if !$owner;
$self->_after_remove_domain($owner);
}
$self->remove(Ravada::Utils::user_daemon);

Expand Down Expand Up @@ -4989,13 +5028,12 @@ sub rsync($self, @args) {
$msg = "Domain::rsync ".(time - $t0)." seconds $file";
warn $msg if $DEBUG_RSYNC;
$request->error($msg) if $request;
}
if ($rsync->err) {
$request->status("done") if $request;
$request->error(join(" ",@{$rsync->err})) if $request;
confess "error syncing to ".$node->host."\n"
.Dumper($files)."\n"
if ($rsync->err) {
$request->status("done") if $request;
$request->error(join(" ",@{$rsync->err})) if $request;
confess "error syncing from $src to $dst \n"
.join(' ',@{$rsync->err});
}
}
$request->error("rsync done ".(time - $time_rsync)." seconds") if $request;
$node->refresh_storage_pools();
Expand Down Expand Up @@ -5096,6 +5134,7 @@ sub _set_base_vm_db($self, $id_vm, $value) {

my $id_is_base = $self->_id_base_in_vm($id_vm);
if (!defined $id_is_base) {
return if !$value && !$self->is_known;
my $sth = $$CONNECTOR->dbh->prepare(
"INSERT INTO bases_vm (id_domain, id_vm, enabled) "
." VALUES(?, ?, ?)"
Expand Down Expand Up @@ -5213,6 +5252,7 @@ sub _check_all_parents_in_node($self, $vm) {

sub _set_clones_autostart($self, $value) {
for my $clone_data ($self->clones) {
next if $clone_data->{is_volatile};
my $clone = Ravada::Domain->open($clone_data->{id}) or next;
$clone->_internal_autostart(0);
}
Expand Down Expand Up @@ -6814,12 +6854,15 @@ Returns a list of instances of the virtual machine in all the physical nodes
=cut

sub list_instances($self) {
return () if !$self->is_known();
sub list_instances($self, $id=undef) {
return () if !$id && !$self->is_known();

$id = $self->id if !defined $id;

my $sth = $$CONNECTOR->dbh->prepare("SELECT * FROM domain_instances "
." WHERE id_domain=?"
);
$sth->execute($self->id);
$sth->execute($id);

my @instances;
while (my $row = $sth->fetchrow_hashref) {
Expand Down
4 changes: 1 addition & 3 deletions lib/Ravada/Domain/KVM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ sub _disk_device($self, $with_info=undef, $attribute=undef, $value=undef) {
my ($boot_node) = $disk->findnodes('boot');
my $info = {};
eval { $info = $self->_volume_info($file)
if $file && $device eq 'disk' or $device eq 'cdrom' };
if $file && ( $device eq 'disk' or $device eq 'cdrom') };
die $@ if $@ && $@ !~ /not found/i;
$info->{device} = $device;
if (!$info->{name} ) {
Expand Down Expand Up @@ -900,8 +900,6 @@ sub start {
$self->_set_displays_ip($set_password, $listen_ip);
}

$self->status('starting');

my $error;
for ( 1 .. 60 ) {
eval { $self->domain->create() };
Expand Down
6 changes: 2 additions & 4 deletions lib/Ravada/Front/Domain/Void.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use feature qw(signatures);

extends 'Ravada::Front::Domain';

my $DIR_TMP = "/var/tmp/rvd_void/".getpwuid($>);

our %GET_CONTROLLER_SUB = (
'mock' => \&_get_controller_mock
,'disk' => \&_get_controller_disk
Expand Down Expand Up @@ -58,11 +56,11 @@ sub _value{

sub _config_file {
my $self = shift;
return "$DIR_TMP/".$self->name.".yml";
return "/var/tmp/rvd_void/".getpwuid($>)."/".$self->name.".yml";
}

sub _config_dir {
return $DIR_TMP;
return "/var/tmp/rvd_void/".getpwuid($>);
}

sub list_controllers {
Expand Down
Loading

0 comments on commit 6baee9e

Please sign in to comment.