Commit fba130ca authored by Francesc Guasch's avatar Francesc Guasch
Browse files

[#51] IPtables support from request and placeholder

parent 2eafb92a
......@@ -824,7 +824,7 @@ sub _cmd_start {
my $uid = $request->args('uid');
my $user = Ravada::Auth::SQL->search_by_id($uid);
$domain->start($user);
$domain->start(user => $user, remote_ip => $request->args('remote_ip'));
my $msg = 'Domain '
."<a href=\"/machine/view/".$domain->id.".html\">"
.$request->args('name')."</a>"
......
......@@ -100,6 +100,8 @@ before 'prepare_base' => \&_allow_prepare_base;
};
before 'start' => \&_start_preconditions;
after 'start' => \&_post_start;
before 'pause' => \&_allow_manage;
before 'resume' => \&_allow_manage;
before 'shutdown' => \&_allow_manage_args;
......@@ -109,7 +111,11 @@ before 'remove_base' => \&_can_remove_base;
after 'remove_base' => \&_remove_base_db;
sub _start_preconditions{
_allow_manage(@_);
if (scalar @_ %2 ) {
_allow_manage_args(@_);
} else {
_allow_manage(@_);
}
_check_free_memory();
_check_used_memory(@_);
}
......@@ -620,7 +626,11 @@ sub clone {
sub _post_shutdown {
my $self = shift;
my %args = @_;
my $user = Ravada::Auth::SQL->search_by_id($self->id_owner);
my $user;
eval { $user = Ravada::Auth::SQL->search_by_id($self->id_owner) };
return if !$user;
if ($user->is_temporary) {
$self->remove($user);
my $req= $args{request};
......@@ -633,4 +643,18 @@ sub _post_shutdown {
}
}
sub _post_start {
my $self = shift;
return if scalar @_ % 2;
my %args = @_;
my $remote_ip = $args{remote_ip} or return;
my $owner = Ravada::Auth::SQL->search_by_id($self->id_owner);
my $display = $self->display($owner);
my ($local_ip, $local_port) = $display =~ m{\w+://(.*):(\d+)};
warn "$remote_ip -> $local_ip, $local_port ".$display;
}
1;
......@@ -43,7 +43,7 @@ our %VALID_ARG = (
,remove_domain => $args_manage
,shutdown_domain => { name => 1, uid => 1, timeout => 2 }
,screenshot_domain => { id_domain => 1, filename => 2 }
,start_domain => $args_manage
,start_domain => {%$args_manage, remote_ip => 1 }
);
our $CONNECTOR;
......
......@@ -638,7 +638,11 @@ sub show_link {
return access_denied($c) if $USER->id != $domain->id_owner && !$USER->is_admin;
if ( !$domain->is_active ) {
my $req = Ravada::Request->start_domain(name => $domain->name, uid => $USER->id);
my $req = Ravada::Request->start_domain(
uid => $USER->id
,name => $domain->name
,remote_ip => _remote_ip($c)
);
$RAVADA->wait_request($req);
warn "ERROR: ".$req->error if $req->error();
......@@ -732,7 +736,10 @@ sub manage_machine {
&& !$USER->is_admin;
Ravada::Request->shutdown_domain(name => $domain->name, uid => $USER->id) if $c->param('shutdown');
Ravada::Request->start_domain(name => $domain->name, uid => $USER->id) if $c->param('start');
Ravada::Request->start_domain( uid => $USER->id
,name => $domain->name
, remote_ip => _remote_ip($c)
) if $c->param('start');
Ravada::Request->pause_domain(name => $domain->name, uid => $USER->id)
if $c->param('pause');
......@@ -871,8 +878,10 @@ sub prepare_machine {
my $file_screenshot = "$DOCUMENT_ROOT/img/screenshots/".$domain->id.".png";
if (! -e $file_screenshot && $domain->can_screenshot() ) {
if ( !$domain->is_active() ) {
Ravada::Request->start_domain( name => $domain->name
,uid => $USER->id
Ravada::Request->start_domain(
uid => $USER->id
,name => $domain->name
,remote_ip => _remote_ip($c)
);
sleep 3;
}
......@@ -896,7 +905,10 @@ sub start_machine {
return login($c) if !_logged_in($c);
my ($domain, $type) = _search_requested_machine($c);
my $req = Ravada::Request->start_domain(name => $domain->name, uid => $USER->id);
my $req = Ravada::Request->start_domain( uid => $USER->id
,name => $domain->name
,remote_ip => _remote_ip($c)
);
return $c->render(json => { req => $req->id });
}
......
use warnings;
use strict;
use Data::Dumper;
use JSON::XS;
use Test::More;
use Test::SQL::Data;
use lib 't/lib';
use Test::Ravada;
my $test = Test::SQL::Data->new(config => 't/etc/sql.conf');
use_ok('Ravada');
my $FILE_CONFIG = 't/etc/ravada.conf';
my @ARG_RVD = ( config => $FILE_CONFIG, connector => $test->connector);
my %ARG_CREATE_DOM = (
KVM => [ id_iso => 1 ]
,Void => [ ]
);
my $RVD_BACK = rvd_back($test->connector, $FILE_CONFIG);
my $USER = create_user("foo","bar");
##########################################################
sub test_create_domain {
my $vm_name = shift;
my $ravada = Ravada->new(@ARG_RVD);
my $vm = $ravada->search_vm($vm_name);
ok($vm,"I can't find VM $vm_name") or return;
my $name = new_domain_name();
if (!$ARG_CREATE_DOM{$vm_name}) {
diag("VM $vm_name should be defined at \%ARG_CREATE_DOM");
return;
}
my @arg_create = @{$ARG_CREATE_DOM{$vm_name}};
my $domain;
eval { $domain = $vm->create_domain(name => $name
, id_owner => $USER->id
, @{$ARG_CREATE_DOM{$vm_name}})
};
ok($domain,"No domain $name created with ".ref($vm)." ".($@ or '')) or exit;
ok($domain->name
&& $domain->name eq $name,"Expecting domain name '$name' , got "
.($domain->name or '<UNDEF>')
." for VM $vm_name"
);
return $domain;
}
sub test_fw_domain {
my ($vm_name, $domain) = @_;
my $remote_ip = '99.88.77.66';
$domain->start( user => $USER, remote_ip => $remote_ip);
ok($domain->is_alive);
#TODO check iptables for an entry allowing 127.0.0.1 to $domain->display
}
#######################################################
remove_old_domains();
remove_old_disks();
for my $vm_name (qw( Void KVM )) {
diag("Testing $vm_name VM");
my $CLASS= "Ravada::VM::$vm_name";
use_ok($CLASS) or next;
my $vm;
eval { $vm = $RVD_BACK->search_vm($vm_name) };
SKIP: {
my $msg = "SKIPPED test: No $vm_name VM found ";
diag($msg) if !$vm;
skip $msg,10 if !$vm;
my $domain = test_create_domain($vm_name);
test_fw_domain($vm_name, $domain);
};
}
done_testing();
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