Skip to content

Commit

Permalink
reviewd pause / resume in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejol committed Oct 11, 2016
1 parent 9e54a1b commit c524096
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 6 deletions.
16 changes: 15 additions & 1 deletion lib/Ravada/Domain.pm
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,17 @@ before 'remove' => \&_allow_remove;
after 'remove' => \&_after_remove_domain;

before 'prepare_base' => \&_allow_prepare_base;
after 'prepare_base' => sub { my $self = shift; $self->is_base(1) };
after 'prepare_base' => sub {
my $self = shift;

my ($user) = @_;

$self->is_base(1);
if ($self->{_was_active} ) {
$self->resume($user);
}
delete $self->{_was_active};
};

before 'start' => \&_allow_manage;
before 'pause' => \&_allow_manage;
Expand Down Expand Up @@ -115,6 +125,10 @@ sub _allow_prepare_base {
$self->_check_has_clones();

$self->is_base(0);
if ($self->is_active) {
$self->pause($user);
$self->{_was_active} = 1;
}
};

sub _check_has_clones {
Expand Down
3 changes: 3 additions & 0 deletions lib/Ravada/Domain/KVM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ sub is_paused {
my $self = shift;
my ($state, $reason) = $self->domain->get_state();

return 0 if $state == 1;
#TODO, find out which one of those id "1" and remove it from this list
#
return $state &&
($state == Sys::Virt::Domain::STATE_PAUSED_UNKNOWN
|| $state == Sys::Virt::Domain::STATE_PAUSED_USER
Expand Down
15 changes: 11 additions & 4 deletions lib/Ravada/Domain/Void.pm
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ sub _store {
my ($var, $value) = @_;

my $data = {};
$data = LoadFile($self->disk_device ) if -e $self->disk_device();

my ($disk) = $self->disk_device();
$data = LoadFile($disk) if -e $disk;

$data->{$var} = $value;
DumpFile($self->disk_device , $data);

DumpFile($disk, $data);

}

Expand All @@ -87,7 +91,10 @@ sub _value{

my ($var) = @_;

my $data = LoadFile($self->disk_device );
my ($disk) = $self->disk_device();

my $data = {} ;
$data = LoadFile($disk) if -e $disk;

return $data->{$var};

Expand Down Expand Up @@ -191,7 +198,7 @@ sub list_volumes {
my $self = shift;
my $data = LoadFile($self->disk_device) if -e $self->disk_device;

return [] if !exists $data->{device};
return () if !exists $data->{device};
return keys %{$data->{device}};
}

Expand Down
22 changes: 22 additions & 0 deletions lib/Ravada/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ our %VALID_ARG = (
,id_owner => 1
,id_template => 1
}
,pause_domain => $args_manage
,resume_domain => $args_manage
,remove_domain => $args_manage
,prepare_base => $args_manage
,shutdown_domain => { name => 1, uid => 1, timeout => 2 }
Expand Down Expand Up @@ -175,6 +177,26 @@ sub start_domain {
return $self->_new_request(command => 'start' , args => encode_json($args));
}

=head2 pause_domain
Requests to pause a domain
my $req = Ravada::Request->pause_domain( name => 'name', uid => $user->id );
=cut

sub pause_domain {
my $proto = shift;
my $class=ref($proto) || $proto;

my $args = _check_args('pause_domain', @_);

my $self = {};
bless($self,$class);

return $self->_new_request(command => 'pause' , args => encode_json($args));
}


sub _check_args {
my $sub = shift;
Expand Down
3 changes: 2 additions & 1 deletion t/lib/Test/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ sub _remove_old_domains_vm {
my $domain = $vm->search_domain($dom_name);
next if !$domain;

$domain->shutdown_now($USER_ADMIN) if $domain;
eval { $domain->shutdown_now($USER_ADMIN); };
warn "Error shutdown $dom_name $@" if $@;

eval {
$domain->remove( $USER_ADMIN );
Expand Down
35 changes: 35 additions & 0 deletions t/vm/20_base.t
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ sub test_prepare_base {
ok($@ && $@ =~ /has \d+ clones/i
,"[$vm_name] Don't prepare if there are clones ".($@ or '<UNDEF>'));
ok($domain->is_base);
test_devices_clone($vm_name, $domain_clone);

$domain_clone->remove($USER);

Expand All @@ -113,6 +114,27 @@ sub test_prepare_base {

}

sub test_prepare_base_active {
my $vm_name = shift;

my $domain = test_create_domain($vm_name);

ok(!$domain->is_base,"Domain ".$domain->name." should not be base") or return;
eval { $domain->start($USER) if !$domain->is_active() };
ok(!$@,$@);
eval { $domain->resume($USER) if $domain->is_paused() };
ok(!$@,$@);

ok($domain->is_active,"[$vm_name] Domain ".$domain->name." should be active") or return;
ok(!$domain->is_paused,"[$vm_name] Domain ".$domain->name." should not be paused") or exit;

eval { $domain->prepare_base($USER) };
ok(!$@,"[$vm_name] Prepare base, expecting error='', got '$@'");

ok($domain->is_active,"[$vm_name] Domain ".$domain->name." should be active") or exit;
ok(!$domain->is_paused,"[$vm_name] Domain ".$domain->name." should not be paused") or return;
}

sub touch_mtime {
for my $disk (@_) {

Expand All @@ -127,6 +149,18 @@ sub touch_mtime {

}

sub test_devices_clone {
my $vm_name = shift;
my $domain = shift;

my @volumes = $domain->list_volumes();
ok(scalar(@volumes),"[$vm_name] Expecting at least 1 volume cloned "
." got ".scalar(@volumes));
for my $disk (@volumes ) {
ok(-e $disk,"Checking volume ".Dumper($disk)." exists");
}
}

#######################################################################33


Expand Down Expand Up @@ -154,6 +188,7 @@ for my $vm_name (@VMS) {

my $domain = test_create_domain($vm_name);
test_prepare_base($vm_name, $domain);
test_prepare_base_active($vm_name);
}
}

Expand Down

0 comments on commit c524096

Please sign in to comment.