Commit 4e0b796a authored by Francesc Guasch's avatar Francesc Guasch
Browse files

domain locking implemented

parent 3404cdd0
......@@ -296,7 +296,23 @@ sub search_domain {
return;
}
=head2 search_domain_by_id
my $domain = $ravada->search_domain_by_id($id);
=cut
sub search_domain_by_id {
my $self = shift;
my $id = shift or confess "ERROR: missing argument id";
my $sth = $CONNECTOR->dbh->prepare("SELECT name FROM domains WHERE id=?");
$sth->execute($id);
my ($name) = $sth->fetchrow;
confess "Unknown domain id=$id" if !$name;
return $self->search_domain($name);
}
=head2 list_domains
......@@ -746,14 +762,14 @@ sub _cmd_prepare_base {
my $request = shift;
$request->status('working');
my $name = $request->args('name') or confess "Missing argument name";
my $id_domain = $request->id_domain or confess "Missing request id_domain";
my $uid = $request->args('uid') or confess "Missing argument uid";
my $user = Ravada::Auth::SQL->search_by_id( $uid);
my $domain = $self->search_domain($name);
my $domain = $self->search_domain_by_id($id_domain);
die "Unknown domain '$name'\n" if !$domain;
die "Unknown domain id '$id_domain'\n" if !$domain;
$domain->prepare_base($user);
......
......@@ -368,6 +368,27 @@ sub is_base {
return $ret;
};
=head2 is_locked
Shows if the domain has running or pending requests. It could be considered
too as the domain is busy doing something like starting, shutdown or prepare base.
Returns true if locked.
=cut
sub is_locked {
my $self = shift;
my $sth = $$CONNECTOR->dbh->prepare("SELECT count(*) FROM requests "
." WHERE id_domain=?");
$sth->execute($self->id);
my ($count) = $sth->fetchrow;
$sth->finish;
return $count;
}
=head2 id_owner
Returns the id of the user that created this domain
......
......@@ -74,6 +74,7 @@ sub list_domains {
my $domain ;
eval { $domain = $self->search_domain($row->{name}) };
$row->{is_active} = 1 if $domain && $domain->is_active;
$row->{is_locked} = 1 if $domain && $domain->is_locked;
push @domains, ($row);
}
$sth->finish;
......
......@@ -22,6 +22,7 @@ our %FIELD = map { $_ => 1 } qw(error);
our %FIELD_RO = map { $_ => 1 } qw(id name);
our $args_manage = { name => 1 , uid => 1 };
our $args_prepare = { id_domain => 1 , uid => 1 };
our %VALID_ARG = (
create_domain => {
......@@ -32,10 +33,10 @@ our %VALID_ARG = (
,id_owner => 1
,id_template => 1
}
,prepare_base => $args_prepare
,pause_domain => $args_manage
,resume_domain => $args_manage
,remove_domain => $args_manage
,prepare_base => $args_manage
,shutdown_domain => { name => 1, uid => 1, timeout => 2 }
,start_domain => $args_manage
);
......@@ -272,19 +273,16 @@ sub prepare_base {
my $class=ref($proto) || $proto;
my %args = @_;
confess "Missing domain name" if !$args{name};
confess "Missing uid" if !$args{uid};
for (keys %args) {
confess "Invalid argument $_" if !$VALID_ARG{'remove_domain'}->{$_};
}
$args{name} = $args{name}->name if ref($args{name}) =~ /Domain/;
my $args = _check_args('prepare_base', @_);
my $self = {};
bless($self,$class);
return $self->_new_request(command => 'prepare_base'
, args => encode_json( \%args ));
, id_domain => $args{id_domain}
, args => encode_json( $args ));
}
......
......@@ -75,8 +75,8 @@ get '/ip/*' => sub {
any '/machines' => sub {
my $c = shift;
return access_denied($c) if !_logged_in($c)
|| !$USER->is_admin;
return login($c) if !_logged_in($c);
return access_denied($c) if !$USER->is_admin;
return domains($c);
};
......@@ -658,7 +658,7 @@ sub prepare_machine {
my $domain = _search_requested_machine($c);
my $req = Ravada::Request->prepare_base(
name => $domain->name
id_domain => $domain->id
,uid => $USER->id
);
......
......@@ -26,7 +26,7 @@ sub test_req_prepare_base {
my $domain0 = $RAVADA->search_domain($name);
ok(!$domain0->is_base,"Domain $name should not be base");
my $req = Ravada::Request->prepare_base(name => $name, uid => $USER->id);
my $req = Ravada::Request->prepare_base(id_domain => $domain0->id, uid => $USER->id);
$RAVADA->_process_requests_dont_fork();
ok($req->status('done'),"Request should be done, it is".$req->status);
......
......@@ -56,18 +56,6 @@ sub test_remove_domain {
}
sub wait_request {
my $req = shift;
my $status = '';
for ( 1 .. 100 ) {
last if $req->status eq 'done';
next if $req->status eq $status;
diag("Request ".$req->command." ".$req->status);
$status=$req->status;
sleep 1;
}
}
sub test_req_create_domain_iso {
my $vm_name = shift;
......@@ -110,6 +98,7 @@ sub test_req_create_domain_iso {
my $domain = $vm->search_domain($name);
ok($domain,"[$vm_name] I can't find domain $name");
ok(!$domain->is_locked,"Domain $name should not be locked");
$USER->mark_all_messages_read();
return $domain;
......@@ -152,18 +141,21 @@ sub test_req_prepare_base {
my $vm = shift;
my $name = shift;
my $domain = $vm->search_domain($name);
ok($domain, "Searching for domain $name, got ".ref($name)) or return;
ok(!$domain->is_base, "Expecting domain base=0 , got: '".$domain->is_base."'");
my $req = Ravada::Request->prepare_base(
name => $name
id_domain => $domain->id
,uid => $USER->id
);
ok($req);
ok($req->status);
my $domain = $vm->search_domain($name);
ok($domain, "Searching for domain $name, got ".ref($name)) or return;
ok(!$domain->is_base, "Expecting domain base=0 , got: '".$domain->is_base."'");
ok($domain->is_locked,"Domain $name should be locked when preparing base");
$ravada->_process_requests_dont_fork();
ok(!$req->error,"Expecting error='', got '".$req->error."'");
ok($domain->is_base, "Expecting domain base=1 , got: '".$domain->is_base."'");
}
......
......@@ -92,6 +92,7 @@ sub test_manage_domain {
my $domain = shift;
$domain->start($USER) if !$domain->is_active();
ok(!$domain->is_locked,"Domain ".$domain->name." should not be locked");
my $display;
eval { $display = $domain->display($USER) };
......
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