Skip to content

Commit

Permalink
Merge branch 'main' into refactor/test_ldap_storage
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejol committed Nov 14, 2024
2 parents f42ea3e + c740b38 commit 613b7b2
Show file tree
Hide file tree
Showing 19 changed files with 863 additions and 229 deletions.
9 changes: 9 additions & 0 deletions lib/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2941,7 +2941,9 @@ sub _upgrade_tables {

$self->_upgrade_table('domains','needs_restart','int not null default 0');
$self->_upgrade_table('domains','shutdown_disconnected','int not null default 0');
$self->_upgrade_table('domains','shutdown_grace_time','int not null default 10');
$self->_upgrade_table('domains','shutdown_timeout','int default null');
$self->_upgrade_table('domains','log_status','text');
$self->_upgrade_table('domains','date_changed','timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP');
$self->_upgrade_table('domains','balance_policy','int default 0');

Expand Down Expand Up @@ -5373,12 +5375,16 @@ sub _cmd_change_hardware {

my $user = Ravada::Auth::SQL->search_by_id($uid);

my $info = $domain->info($user);

die "Error: User ".$user->name." not allowed\n"
unless $user->is_admin
|| $hardware eq 'memory'
|| ($hardware eq 'network'
&& $user->can_change_hardware_network($domain, $data)
)
|| ($hardware eq 'vcpus' && keys %$data == 1 && exists $data->{n_virt_cpu}
&& $data->{n_virt_cpu} <= $info->{max_virt_cpu})
;

$domain->change_hardware(
Expand Down Expand Up @@ -6523,12 +6529,15 @@ sub _shutdown_disconnected($self) {
if ($is_active && $domain->client_status eq 'disconnected') {
next if $self->_domain_just_started($domain) || $self->_verify_connection($domain);
next if $req_shutdown;
next if !$domain->check_grace('disconnected');
Ravada::Request->shutdown_domain(
uid => Ravada::Utils::user_daemon->id
,id_domain => $domain->id
,at => time + 120
,check => 'disconnected'
);
my $user = Ravada::Auth::SQL->search_by_id($domain->id_owner);
$user->send_message("The virtual machine has been disconnected for too long. Shutting down ".$dom->{name});
} elsif ($req_shutdown) {
$req_shutdown->status('done','Canceled') if $req_shutdown;
}
Expand Down
71 changes: 67 additions & 4 deletions lib/Ravada/Domain.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ our $CONNECTOR;
our $MIN_FREE_MEMORY = 1024*1024;
our $IPTABLES_CHAIN = 'RAVADA';

our %PROPAGATE_FIELD = map { $_ => 1} qw( run_timeout shutdown_disconnected);
our %PROPAGATE_FIELD = map { $_ => 1} qw( run_timeout shutdown_disconnected shutdown_grace_time);

our $TIME_CACHE_NETSTAT = 60; # seconds to cache netstat data output
our $RETRY_SET_TIME=10;
Expand Down Expand Up @@ -2167,13 +2167,13 @@ sub info($self, $user) {
,autostart => $self->autostart
,volatile_clones => $self->volatile_clones
,id_vm => $self->_data('id_vm')
,auto_compact => $self->auto_compact
,auto_compact => ($self->auto_compact or 0)
,date_changed => $self->_data('date_changed')
,is_volatile => $self->_data('is_volatile')
};

$info->{alias} = ( $self->_data('alias') or $info->{name} );
for (qw(comment screenshot id_owner shutdown_disconnected is_compacted has_backups balance_policy)) {
for (qw(comment screenshot id_owner shutdown_disconnected is_compacted has_backups balance_policy shutdown_grace_time shutdown_timeout)) {
$info->{$_} = $self->_data($_);
}
if ($self->is_known() ) {
Expand Down Expand Up @@ -3291,6 +3291,7 @@ sub _post_shutdown {
$self->needs_restart(0) if $self->is_known()
&& $self->needs_restart()
&& !$is_active;
$self->clean_status('disconnected');
}

sub _schedule_compact($self) {
Expand Down Expand Up @@ -5818,8 +5819,42 @@ sub client_status($self, $force=0) {
$self->_data('client_status', $status);
$self->_data('client_status_time_checked', time );

if ($self->_data('shutdown_grace_time')) {
if ($status eq 'disconnected') {
$self->log_status($status);
} else {
$self->clean_status('disconnected');
}
}

return $status;
}
sub clean_status($self, $type) {
my $json_status = $self->_data('log_status');
my $h_status = {};
if ($json_status) {
eval { $h_status = decode_json($json_status) };
$h_status = {} if $@;
}
$h_status->{disconnected} = [];
$self->_data('log_status', encode_json($h_status));
}

sub log_status($self, $type) {
my $json_status = $self->_data('log_status');
my $h_status = {};
if ($json_status) {
eval { $h_status = decode_json($json_status) };
$h_status = {} if $@;
}
my $time = time();
for my $old ( @{$h_status->{$type}} ) {
return if $old >= $time-60;
}
push @{$h_status->{$type}},(time());

$self->_data('log_status', encode_json($h_status));
}

sub _run_netstat($self, $force=undef) {
if (!$force && $self->_vm->{_netstat}
Expand Down Expand Up @@ -7276,7 +7311,12 @@ sub add_host_device($self, $host_device) {
my $id_hd = $host_device;
$id_hd = $host_device->id if ref($host_device);

confess if !$id_hd;
my $sth0 = $$CONNECTOR->dbh->prepare("SELECT id FROM host_devices_domain "
." WHERE id_host_device=? AND id_domain=?"
);
$sth0->execute($id_hd, $self->id);
my ($found) = $sth0->fetchrow;
return if $found;

my $sth = $$CONNECTOR->dbh->prepare("INSERT INTO host_devices_domain "
."(id_host_device, id_domain) "
Expand Down Expand Up @@ -7997,4 +8037,27 @@ sub _volatile_active($self) {
return 1;

}

sub check_grace($self,$type) {
return 1 if !$self->_data('shutdown_grace_time');
my $log_status = $self->_data('log_status');
return if !$log_status;
my $hash_status;
eval {
$hash_status= decode_json($log_status);
};
if ($@) {
warn "$@ : '$log_status'";
return;
}
my $log = $hash_status->{$type};
return if !$log;
my $old;
for my $current (@$log) {
$old = $current if !defined $old || $current<$old;
}
return 1 if time-$old >= 60*$self->_data('shutdown_grace_time');
return 0;
}

1;
5 changes: 3 additions & 2 deletions lib/Ravada/Domain/KVM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1813,8 +1813,9 @@ sub set_memory {
my $value = shift;

my $max_mem = $self->get_max_mem();
confess "ERROR: invalid argument '$value': cannot set memory higher than max memory"
die "ERROR: invalid argument '$value': cannot set memory higher than max memory"
." ($max_mem)"
."\n"
if $value > $max_mem;

$self->_set_memory_xml($value);
Expand Down Expand Up @@ -3079,7 +3080,7 @@ sub _change_hardware_vcpus($self, $index, $data) {

};
if ($@) {
warn $@;
warn "Error on set current ".$@;
$self->_data('needs_restart' => 1) if $self->is_active;
}
my ($vcpus) = $doc->findnodes('/domain/vcpu');
Expand Down
2 changes: 1 addition & 1 deletion lib/Ravada/HostDevice/Templates.pm
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ our @TEMPLATES_VOID = (
]
}
,{ name => "GPU Mediated Device"
,list_command => "lsusb "
,list_command => "lsusb"
,list_filter => '.*'
,template_args => encode_json(
{ uuid => '^(.*?) '}
Expand Down
8 changes: 7 additions & 1 deletion lib/Ravada/I18N/ca.po
Original file line number Diff line number Diff line change
Expand Up @@ -1853,7 +1853,13 @@ msgid "Virtual Machine will start on host start."
msgstr "La màquina virtual s'iniciarà a l'inici de l'amfitrió."

msgid "Shutdown disconnected"
msgstr "L'apagat s'ha desconnectat"
msgstr "Atura quan es desconnecti"

msgid "Shutdown grace time"
msgstr "Temps de gràcia d'aturada"

msgid "Minutes of grace time before shutdown."
msgstr "Minuts que esperarem abans d'aturar."

msgid "Virtual Machine will be shutdown when user disconnects."
msgstr "La màquina virtual s'apagarà quan l'usuari es desconnecti."
Expand Down
Loading

0 comments on commit 613b7b2

Please sign in to comment.