Unverified Commit e3e8ebcd authored by Francesc Guasch's avatar Francesc Guasch Committed by GitHub
Browse files

fix(backend): makes ravada handle auto startup (#1254)

Before we set internal startup. If a machine is in
two nodes it could get started in both nodes.
parent 685c428a
......@@ -170,6 +170,7 @@ sub do_start {
my $t_refresh = 0;
my $ravada = Ravada->new( %CONFIG );
autostart_machines($ravada);
# Ravada::Request->enforce_limits();
#Ravada::Request->refresh_vms();
for (;;) {
......@@ -207,6 +208,21 @@ sub clean_old_requests {
$ravada->clean_old_requests();
}
sub autostart_machines {
my $ravada = shift;
for my $domain ( $ravada->list_domains_data ) {
next unless $domain->{autostart} && ! $domain->{is_base}
&& $domain->{status} !~ /active/i;
print "Auto start $domain->{name} [$domain->{status}]\n" if $VERBOSE;
Ravada::Request->start_domain(
id_domain => $domain->{id}
,uid => $domain->{id_owner}
);
}
}
sub start {
{
my $ravada = Ravada->new( %CONFIG );
......
......@@ -754,7 +754,15 @@ sub _around_autostart($orig, $self, @arg) {
my $autostart = 0;
my @orig_args = ();
push @orig_args, ( $value) if defined $value;
if ( $self->$orig(@orig_args) ) {
# We only set the internal autostart when domain is not in nodes
if ($self->_domain_in_nodes) {
if (defined $value) {
$autostart = $value;
} else {
$autostart = $self->_data('autostart');
}
} elsif ( $self->$orig(@orig_args) ) {
$autostart = 1;
}
$self->_data(autostart => $autostart) if defined $value;
......@@ -3634,6 +3642,7 @@ sub _pre_migrate($self, $node, $request = undef) {
$self->_check_equal_storage_pools($node) if $self->_vm->is_active;
$self->_internal_autostart(0);
return if !$self->id_base;
$self->check_status();
......@@ -3760,6 +3769,7 @@ sub set_base_vm($self, %args) {
$request->status("working", "Syncing base volumes to ".$vm->host)
if $request;
$self->migrate($vm, $request);
$self->_set_clones_autostart(0);
} else {
if ($vm->is_active) {
my $vm_local = $self->_vm->new( host => 'localhost' );
......@@ -3785,6 +3795,13 @@ sub set_base_vm($self, %args) {
return $self->_set_base_vm_db($vm->id, $value);
}
sub _set_clones_autostart($self, $value) {
for my $clone_data ($self->clones) {
my $clone = Ravada::Domain->open($clone_data->{id});
$clone->_internal_autostart(0);
}
}
sub migrate_base($self, %args) {
return $self->set_base_vm(%args);
}
......@@ -4685,4 +4702,16 @@ sub list_instances($self) {
return @instances;
}
sub _base_in_nodes($self) {
my $base = Ravada::Front::Domain->open($self->id_base);
confess "Error: no id_base ".($self->id_base or '<NULL>')
.Dumper($self) if !$base;
return $base->list_instances > 1;
}
sub _domain_in_nodes($self) {
return $self->_base_in_nodes() if $self->id_base;
return $self->list_instances > 1;
}
1;
......@@ -2113,7 +2113,9 @@ sub internal_id($self) {
return $self->domain->get_id();
}
sub autostart($self, $value=undef, $user=undef) {
sub autostart { return _internal_autostart(@_) }
sub _internal_autostart($self, $value=undef, $user=undef) {
$self->domain->set_autostart($value) if defined $value;
return $self->domain->get_autostart();
}
......
......@@ -629,7 +629,9 @@ sub is_removed {
return 1;
}
sub autostart {
sub autostart { return _internal_autostart(@_) }
sub _internal_autostart {
my $self = shift;
my $value = shift;
......
......@@ -313,7 +313,7 @@ sub list_domains($self, %args) {
$row->{node} = $domain->_vm->name if $domain->_vm;
$row->{remote_ip} = $domain->client_status
if $domain->client_status && $domain->client_status ne 'connected';
$row->{autostart} = $domain->autostart;
$row->{autostart} = $domain->_data('autostart');
if (!$row->{status} ) {
if ($row->{is_active}) {
$row->{status} = 'active';
......
......@@ -302,8 +302,8 @@ sub _connect_ssh($self, $disconnect=0) {
confess "Don't connect to local ssh"
if $self->is_local;
if ( $self->readonly ) {
warn $self->name." readonly, don't do ssh";
if ( $self->readonly || $> ) {
confess $self->name." readonly or not root, don't do ssh";
return;
}
......
......@@ -473,6 +473,35 @@ sub test_remove_base($vm, $node, $volatile) {
}
sub _check_internal_autostart($domain, $expected) {
if ($domain->type eq 'KVM') {
ok($domain->domain->get_autostart) if $expected;
ok(!$domain->domain->get_autostart) if !$expected;
} elsif ($domain->type eq 'Void') {
ok($domain->_value('autostart')) if $expected;
ok(!$domain->_value('autostart'),$domain->name) or exit if !$expected;
} else {
diag("WARNING: I don't know how to check ".$domain->type." internal autostart");
}
}
# check autostart is managed by Ravada when nodes
sub test_autostart($vm, $node) {
my $base = create_domain($vm);
$base->prepare_base(user_admin);
my $domain = $base->clone(name => new_domain_name , user => user_admin);
$domain->autostart(1,user_admin);
is($domain->autostart,1);
_check_internal_autostart($domain,1);
$base->set_base_vm(node => $node, user => user_admin);
is($domain->autostart,1) or exit;
_check_internal_autostart($domain,0);
$domain->remove(user_admin);
$base->remove(user_admin);
}
##################################################################################
clean();
......@@ -513,6 +542,7 @@ for my $vm_name ( 'Void', 'KVM') {
test_set_vm($vm, $node);
test_autostart($vm, $node);
test_volatile($vm, $node);
test_remove_req($vm, $node);
......
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