Commit 0a598d53 authored by Francesc Guasch's avatar Francesc Guasch Committed by Fernando Verdugo
Browse files

Fix/838 develop2 (#876)

* fix(backend): skip iptables for non-root and fix start extra arg

issue #838

* test(backend); check domain found in VM

issue #838

* test(backend); store mock domains on user allowed dir

issue #838

* refactor(test): check remote ip

issue #838

* fix(backend): volatiles removed on stop and iptables only if root

issue #838

* fix(frontend): list clones right and fetch domain owner

issue #838

* test(backend): skip right if no VM

issue #838

* test: skip KVM on non-root

issue #838

* test(download): do not download iso and checks

issue #838

* test(download): do not download iso and checks

issue #838

* refactor(backend): do not pass vm on clone

issue #838

* fix(backend): do not pre remove if removed

issue #838

* test: start mock domains inactive

issue #838

* refactor(backend): check create domain args

issue #838

* refactor(backend): check args on create domain

issue #838

* wip(backend): don't pass request on start

issue #838

* wip(backend): fix volatiles and start

issue #838

* wip(backend): fixed remove volumes when already removed

issue #838

* test(backend): fixed dir for mock VMs

issue #838

* test(backend): mock VMs dir consolidated

issue #838

* test(backend): check domain removed

issue #838

* wip(test): there should be no password after hibernation

issue #838

* refactor(backend): stop stale requests

issue #838

* refactor(test): get data from remote

issue #838

* refactor(test): remove hardcoded mem stat value

issue #838

* wip(backend): toggle as true if not base

issue #838

* test(backend): remove only this test mock domains

issue #838

* test(stress): do not test if skipped

issue #838

* test(iptables): check remove iptables for local access

issue #838

* wip(requests): do not repeat cleanup requests

issue #838

* test(frontend): return empty if no domain found

issue #838

* wip(backend): fixed typos on merge

issue #838

* test(backend): search unknown returns empty

issue #838

* wip(bakckend): check args on create

issue #838
parent 2cacf098
......@@ -1368,12 +1368,23 @@ sub create_domain {
my %args = @_;
my $request = $args{request};
%args = %{$request->args} if $request;
if ($request) {
my %args_r = %{$request->args};
delete $args_r{'at'};
for my $field (keys %args_r) {
confess "Error: Argument $field different in request "
if $args{$field} && $args{$field} ne $args_r{$field};
$args{$field} = $args_r{$field};
}
}
my $vm_name = delete $args{vm};
my $start = $args{start};
my $start = delete $args{start};
my $id_base = $args{id_base};
my $vm_name = delete $args{vm};
my $id_owner = $args{id_owner};
my $id_owner = $args{id_owner} or confess "Error: missing id_owner ".Dumper(\%args);
_check_args(\%args,qw(iso_file id_base id_iso id_owner name active swap memory disk id_template start remote_ip request vm));
confess "ERROR: Argument vm required" if !$id_base && !$vm_name;
my $vm;
if ($vm_name) {
......@@ -1403,7 +1414,6 @@ sub create_domain {
confess "I can't find any vm ".Dumper($self->vm) if !$vm;
my $domain;
delete $args{'at'};
eval { $domain = $vm->create_domain(%args)};
my $error = $@;
......@@ -1433,10 +1443,11 @@ sub create_domain {
}
sub _check_args($args,@) {
my %args_check = %$args;
for my $field (@_) {
delete $args->{$field};
delete $args_check{$field};
}
confess "ERROR: Unknown arguments ".Dumper($args) if keys %$args;
confess "ERROR: Unknown arguments ".Dumper(\%args_check) if keys %args_check;
lock_hash(%$args);
}
......@@ -1933,7 +1944,6 @@ sub _kill_stale_process($self) {
);
$sth->execute(time - 60 );
while (my ($id, $pid, $command, $start_time) = $sth->fetchrow) {
next if $command eq 'refresh_vms' && time - $start_time < 120;
if ($pid == $$ ) {
warn "HOLY COW! I should kill pid $pid stale for ".(time - $start_time)
." seconds, but I won't because it is myself";
......
......@@ -295,7 +295,7 @@ sub _start_preconditions{
my %args = @args;
my $user = delete $args{user};
my $remote_ip = delete $args{remote_ip};
$request = $args{request} if exists $args{request};
$request = delete $args{request} if exists $args{request};
confess "ERROR: Unknown argument ".join("," , sort keys %args)
."\n\tknown: remote_ip, user" if keys %args;
_allow_manage_args(@_);
......@@ -1503,7 +1503,6 @@ sub clone {
name => $name
,id_base => $self->id
,id_owner => $uid
,vm => $self->vm
,@args_copy
);
die if $clone->_data('id_vm') ne $vm->id;
......@@ -1531,7 +1530,6 @@ sub _copy_clone($self, %args) {
name => $name
,id_base => $base->id
,id_owner => $user->id
,_vm => $self->_vm
,@copy_arg
);
my @volumes = $self->list_volumes_target;
......@@ -1628,7 +1626,6 @@ sub _post_shutdown {
$self->needs_restart(0) if $self->is_known()
&& $self->needs_restart()
&& !$self->is_active;
_test_iptables_jump();
}
sub _around_is_active($orig, $self) {
......@@ -1713,6 +1710,7 @@ sub _remove_iptables {
my %args = @_;
my $user = delete $args{user};
my $port = delete $args{port};
my $id_vm = delete $args{id_vm};
......@@ -1831,8 +1829,10 @@ sub _post_start {
}
$self->get_info();
Ravada::Request->enforce_limits(at => time + 60)
if !Ravada::Request::done_recently(undef, 60, 'enforce_limits');
# get the display so it is stored for front access
$self->display($arg{user}) if $self->is_active;
Ravada::Request->enforce_limits(at => time + 60);
$self->post_resume_aux;
}
......@@ -1924,7 +1924,7 @@ sub _open_port($self, $user, $remote_ip, $local_ip, $local_port, $jump = 'ACCEPT
,d => $local_ip
,dport => $local_port
,j => $jump
);
) if !$>;
$self->_log_iptable(iptables => \@iptables_arg, user => $user, remote_ip => $remote_ip);
......@@ -1946,14 +1946,15 @@ sub _open_port($self, $user, $remote_ip, $local_ip, $local_port, $jump = 'ACCEPT
,d => $local_ip
,dport => $local_port
,j => $jump
);
) if !$>;
$self->_log_iptable(
iptables => [
$remote_ip2
, $local_ip, 'filter', $IPTABLES_CHAIN, $jump
,{'protocol' => 'tcp', 's_port' => 0, 'd_port' => $local_port}
]
, user => $user,remote_ip => $local_ip);
, user => $user,remote_ip => $local_ip
);
}
}
......
......@@ -196,6 +196,7 @@ Cleanup operations executed before removing this domain
sub pre_remove_domain {
my $self = shift;
return if $self->is_removed;
$self->xml_description();
$self->domain->managed_save_remove() if $self->domain->has_managed_save_image;
}
......@@ -262,9 +263,6 @@ sub remove {
$self->_post_remove_base_domain() if $self->is_base();
eval { $self->domain->undefine() if $self->domain };
die $@ if $@ && $@ !~ /libvirt error code: 42/;
}
......
......@@ -117,9 +117,7 @@ sub _store {
my $data = $self->_load();
$data->{$var} = $value;
my ($path) = $self->_config_file() =~ m{(.*/).*};
make_path($path) or die "Error: I can't mkdir $path"
if ! -e $path;
make_path($self->_config_dir()) if !-e $self->_config_dir;
eval { DumpFile($self->_config_file(), $data) };
chomp $@;
confess $@ if $@;
......@@ -151,11 +149,11 @@ sub _store_remote($self, $var, $value) {
my $data = $self->_load_remote();
$data->{$var} = $value;
$self->_vm->run_command("mkdir -p ".$self->_config_dir());
open my $lock,">>","$disk.lock" or die "I can't open lock: $disk.log: $!";
_lock($lock);
open my $lock,">>","$disk.lock" or die "I can't open lock: $disk.lock: $!";
$self->_vm->run_command("mkdir","-p ".$self->_config_dir);
$self->_vm->write_file($disk, Dump($data));
_unlock($lock);
unlink("$disk.lock");
return $self->_value($var);
}
......
......@@ -259,8 +259,7 @@ Returns a list of the domains as a listref
sub list_domains($self, %args) {
my $query = "SELECT d.name, d.id, id_base, is_base, id_vm, status, is_public "
." ,vms.name as node , is_volatile, client_status "
." ,id_owner "
." ,vms.name as node , is_volatile, client_status, id_owner "
." FROM domains d LEFT JOIN vms "
." ON d.id_vm = vms.id ";
......
......@@ -106,7 +106,7 @@ our %COMMAND = (
}
,priority => {
limit => 20
,commands => ['clone','start','cleanup']
,commands => ['clone','start']
}
);
lock_hash %COMMAND;
......
......@@ -298,11 +298,27 @@ sub _around_create_domain {
my $owner = Ravada::Auth::SQL->search_by_id($id_owner) or confess "Unknown user id: $id_owner";
my $base;
my $remote_ip = delete $args{remote_ip};
my $id_base = delete $args{id_base};
my $id_iso = delete $args{id_iso};
my $active = delete $args{active};
my $name = delete $args{name};
my $swap = delete $args{swap};
# args get deleted but kept on @_ so when we call $self->$orig below are passed
delete $args{disk};
delete $args{memory};
delete $args{request};
delete $args{iso_file};
delete @args{'description','remove_cpu','vm'};
confess "ERROR: Unknown args ".Dumper(\%args) if keys %args;
if ($id_base) {
$base = $self->search_domain_by_id($id_base)
or confess "Error: I can't find domain $id_base on ".$self->name;
}
confess "ERROR: User ".$owner->name." is not allowed to create machines"
unless $owner->is_admin
|| $owner->can_create_machine()
......@@ -314,8 +330,7 @@ sub _around_create_domain {
$self->_pre_create_domain(@_);
my $domain = $self->$orig(@_);
$domain->add_volume_swap( size => $args{swap}) if $args{swap};
$domain->add_volume_swap( size => $swap ) if $swap;
if ($id_base) {
$domain->run_timeout($base->run_timeout)
......@@ -325,12 +340,11 @@ sub _around_create_domain {
$domain->is_volatile(1) if $user->is_temporary() ||($base && $base->volatile_clones());
my @start_args = ( user => $owner );
my $remote_ip = $args{remote_ip};
push @start_args, (remote_ip => $remote_ip) if $remote_ip;
$domain->_post_start(@start_args) if $domain->is_active;
eval {
$domain->start(@start_args) if $domain->is_volatile && ! $domain->is_active;
$domain->start(@start_args) if $active || ($domain->is_volatile && ! $domain->is_active);
};
die $@ if $@ && $@ !~ /code: 55,/;
......
......@@ -109,6 +109,7 @@ sub create_domain {
);
$domain->_set_default_drivers();
$domain->_set_default_info();
$domain->_store( is_active => 0 );
}
$domain->set_memory($args{memory}) if $args{memory};
......@@ -181,7 +182,7 @@ sub _list_domains_remote($self, %args) {
}
sub list_domains($self, %args) {
return $self->_list_domains_local(%args) if $self->host eq 'localhost';
return $self->_list_domains_local(%args) if $self->is_local();
return $self->_list_domains_remote(%args);
}
......
......@@ -315,7 +315,7 @@ ok($ravada,"I can't launch a new Ravada");# or exit;
remove_old_domains();
remove_old_disks();
for my $vm_name ( qw(Void KVM)) {
for my $vm_name ( vm_names() ) {
my $vm;
eval {
$vm= $ravada->search_vm($vm_name) if $ravada;
......
......@@ -71,8 +71,9 @@ sub test_remove_domain {
ok(!search_domain_db($name),"Domain $name still in db");
$domain_f = $RVD_FRONT->search_domain($name);
ok(!$domain_f,"Expecting no domain $name in front");
$domain_f = undef;
eval { $domain_f = $RVD_FRONT->search_domain($name) };
ok(!$domain_f,"Expecting no domain $name in front ".Dumper($domain_f));
my $list_domains = $RVD_FRONT->list_domains;
is(scalar@$list_domains,0, Dumper($list_domains));
......
......@@ -329,10 +329,11 @@ sub _remove_old_domains_vm {
sub _remove_old_domains_void {
my $vm = shift;
return _remove_old_domains_void_remote($vm) if !$vm->is_local;
my $base_name = base_domain_name();
opendir my $dir, $vm->dir_img or return;
while ( my $file = readdir($dir) ) {
next if $file !~ /^tst_/;
next if $file !~ /^$base_name/;
my $path = $vm->dir_img."/".$file;
next if ! -f $path
|| $path !~ m{\.(yml|qcow|img)$};
......@@ -346,9 +347,11 @@ sub _remove_old_domains_void_remote($vm) {
eval { $vm->connect };
warn $@ if $@;
return if !$vm->_do_is_active;
$vm->run_command("rm -f ".$vm->dir_img."/*yml "
.$vm->dir_img."/*qcow "
.$vm->dir_img."/*img"
my $base_name = base_domain_name();
$vm->run_command("rm -f ".$vm->dir_img."/$base_name*yml "
.$vm->dir_img."/$base_name*qcow "
.$vm->dir_img."/$base_name*img"
);
}
......
......@@ -378,9 +378,7 @@ sub test_req_remove_base_fail {
}
ok($req->status eq 'requested' || $req->status eq 'done');
rvd_back->process_requests();
rvd_back->process_long_requests(0,1);
wait_request($req);
rvd_back->_process_all_requests_dont_fork();
ok($req->status eq 'done', "Expected req->status 'done', got "
."'".$req->status."'");
......
......@@ -383,7 +383,7 @@ for my $vm_name ( vm_names() ) {
}
diag($msg) if !$vm;
skip $msg if !$vm;
skip $msg,10 if !$vm;
diag("Testing remove on $vm_name");
......
......@@ -357,7 +357,7 @@ for my $vm_name ( vm_names() ) {
}
diag($msg) if !$vm;
skip $msg if !$vm;
skip $msg,10 if !$vm;
diag("Testing shutdown on $vm_name");
......
......@@ -27,12 +27,19 @@ sub test_create_domain {
my $domain = create_domain($vm);
my $domain_open = Ravada::Domain->open($domain->id);
ok($domain_open,"Expecting domain id ".$domain->id);
is(ref($domain_open),"Ravada::Domain::$vm_type"
,"Expecting domain in $vm_type");
my $id_domain = $domain->id;
like($id_domain,qr/^\d+/);
my $domain2 = $vm->search_domain($domain->name);
ok($domain2);
is(ref($domain2),"Ravada::Domain::$vm_type"
,"Expecting domain in $vm_type");
$domain->remove(user_admin);
if (defined $id_domain) {
......
......@@ -266,7 +266,8 @@ sub test_remove_domain_already_gone {
my $domain_b = rvd_back->search_domain($domain->name);
ok(!$domain_b);
my $domain_f = rvd_front->search_domain($domain->name);
my $domain_f;
eval { $domain_f = rvd_front->search_domain($domain->name)};
ok(!$domain_f,"[$vm_name] Expecting no domain ".$domain->name." in front") or exit;
}
......
......@@ -58,7 +58,7 @@ for my $vm_name ( q'KVM' ) {
}
diag($msg) if !$vm;
skip $msg if !$vm;
skip $msg,10 if !$vm;
diag("Testing remove on $vm_name");
......@@ -69,4 +69,4 @@ for my $vm_name ( q'KVM' ) {
clean();
done_testing();
\ No newline at end of file
done_testing();
......@@ -59,7 +59,7 @@ sub test_volatile_clone {
and do {
is($clone_listed->{can_hibernate},0);
ok(exists $clone_listed->{client_status},"Expecting client_status field");
like($clone_listed->{client_status},qr(.))
like($clone_listed->{client_status},qr(.));
};
......
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