Commit 43b3eab8 authored by Francesc Guasch's avatar Francesc Guasch Committed by Fernando Verdugo
Browse files

Feature #465 internal ip (#878)

* test(backend): check internal ip

issue #465

* test(backend): minimal ubuntu for testing

issue #465

* feature(frontend): show internal ip

issue #465

* wip(backend): fetch internal IP

issue #465

* feat(frontend): show machine information

issue #465

* test(frontend): expecting no IP after shutdown

issue #465

* wip(backend): remove ip after shutdown

issue #465

* wip(backend): refresh machine status if no ip and active

issue #465
parent ed16749d
......@@ -2620,6 +2620,14 @@ sub _cmd_refresh_storage($self, $request=undef) {
$vm->refresh_storage();
}
sub _cmd_refresh_machine($self, $request) {
my $id_domain = $request->args('id_domain');
my $domain = Ravada::Domain->open($id_domain);
$domain->get_info();
}
sub _cmd_change_owner($self, $request) {
my $uid = $request->args('uid');
my $id_domain = $request->args('id_domain');
......@@ -2731,6 +2739,7 @@ sub _refresh_active_domain($self, $vm, $domain, $active_domain) {
|| $domain->_data('id_vm') != $vm->id;
}
$domain->_set_data(status => $status);
$domain->info(Ravada::Utils::user_daemon) if $is_active;
$active_domain->{$domain->id} = $is_active;
}
......@@ -2857,6 +2866,7 @@ sub _req_method {
,enforce_limits => \&_cmd_enforce_limits
,force_shutdown => \&_cmd_force_shutdown
,refresh_storage => \&_cmd_refresh_storage
,refresh_machine => \&_cmd_refresh_machine
,refresh_vms => \&_cmd_refresh_vms
,domain_autostart=> \&_cmd_domain_autostart
,change_owner => \&_cmd_change_owner
......
......@@ -1041,6 +1041,12 @@ sub info($self, $user) {
}
$info->{hardware} = $self->get_controllers();
my $internal_info = $self->get_info();
for (keys(%$internal_info)) {
die "Field $_ already in info" if exists $info->{$_};
$info->{$_} = $internal_info->{$_};
}
return $info;
}
......@@ -1617,6 +1623,11 @@ sub _post_shutdown {
$self->_remove_temporary_machine();
return;
}
my $info = $self->_data('info');
$info = decode_json($info) if $info;
$info = {} if !$info;
delete $info->{ip};
$self->_data(info => encode_json($info));
# only if not volatile
my $request;
$request = $arg{request} if exists $arg{request};
......
......@@ -1076,11 +1076,19 @@ sub get_info {
$info->{memory} = $info->{memory};
$info->{cpu_time} = $info->{cpuTime};
$info->{n_virt_cpu} = $info->{nVirtCpu};
$info->{ip} = $self->ip() if $self->is_active();
lock_keys(%$info);
return $info;
}
sub ip($self) {
my @ip;
eval { @ip = $self->domain->get_interface_addresses(Sys::Virt::Domain::INTERFACE_ADDRESSES_SRC_LEASE) };
warn $@ if $@;
return $ip[0]->{addrs}->[0]->{addr} if $ip[0];
}
=head2 set_max_mem
Set the maximum memory for the domain
......
......@@ -413,6 +413,7 @@ sub _set_default_info {
,cpu_time => 1
,n_virt_cpu => 1
,state => 'UNKNOWN'
,ip => $self->ip
};
$self->_store(info => $info);
my %controllers = $self->list_controllers;
......
......@@ -171,6 +171,8 @@ sub _get_driver_sound {
}
=pod
sub get_info {
my $self = shift;
......@@ -182,4 +184,6 @@ sub get_info {
return $info;
}
=cut
1;
......@@ -82,6 +82,7 @@ our %VALID_ARG = (
,change_max_memory => {uid => 1, id_domain => 1, ram => 1}
,refresh_vms => { }
,enforce_limits => { timeout => 2, _force => 2 }
,refresh_machine => { id_domain => 1 }
);
our %CMD_SEND_MESSAGE = map { $_ => 1 }
......@@ -1346,6 +1347,29 @@ sub enforce_limits {
return $req;
}
sub refresh_machine {
my $proto = shift;
my $class = ref($proto) || $proto;
my $args = _check_args('refresh_machine', @_ );
my $self = {};
bless($self, $class);
my $id_domain = $args->{id_domain};
my $id_requested = _requested('refresh_machine',id_domain => $id_domain);
return Ravada::Request->open($id_requested) if $id_requested;
my $req = _new_request($self
, command => 'refresh_machine'
, args => $args
);
return $req;
}
=head2 done_recently
Returns wether this command has been requested successfully recently.
......@@ -1381,14 +1405,20 @@ sub done_recently($self, $seconds=60,$command=undef) {
return $id;
}
sub _requested($command) {
sub _requested($command, %fields) {
_init_connector();
my $sth = $$CONNECTOR->dbh->prepare(
my $query =
"SELECT id FROM requests"
." WHERE command = ? "
." AND status <> 'done' "
);
$sth->execute($command);
;
my @values = ( $command );
for my $key( keys %fields ) {
$query.= " AND $key = ?";
push @values,($fields{$key});
}
my $sth = $$CONNECTOR->dbh->prepare($query);
$sth->execute(@values);
my ($id) = $sth->fetchrow;
return $id;
......
......@@ -310,6 +310,7 @@ sub _around_create_domain {
delete $args{memory};
delete $args{request};
delete $args{iso_file};
delete $args{id_template};
delete @args{'description','remove_cpu','vm'};
confess "ERROR: Unknown args ".Dumper(\%args) if keys %args;
......
......@@ -380,7 +380,11 @@ get '/machine/info/(:id).(:type)' => sub {
|| $USER->can_remove_machine($domain->id)
|| $USER->can_clone_all;
$c->render(json => $domain->info($USER) );
my $info = $domain->info($USER);
if ($domain->is_active && !$info->{ip}) {
Ravada::Request->refresh_machine(id_domain => $domain->id);
}
return $c->render(json => $info);
};
get '/machine/requests/(:id).json' => sub {
......
......@@ -26,9 +26,11 @@ my $RVD_FRONT = Ravada::Front->new( @rvd_args
my $USER = create_user('foo','bar', 1);
add_ubuntu_minimal_iso();
my %CREATE_ARGS = (
Void => { id_iso => search_id_iso('Alpine'), id_owner => $USER->id }
,KVM => { id_iso => search_id_iso('Alpine'), id_owner => $USER->id }
,KVM => { id_iso => search_id_iso('Ubuntu % Minimal'), id_owner => $USER->id }
,LXC => { id_template => 1, id_owner => $USER->id }
);
......@@ -105,6 +107,25 @@ sub test_domain_name {
}
sub test_domain_info {
my $domain = shift;
my $domain_b = Ravada::Domain->open($domain->id);
$domain_b->start(user => user_admin, remote_ip => '127.0.0.1') if !$domain_b->is_active;
$domain_b->open_iptables(user => user_admin, remote_ip => '127.0.0.1');
for ( 1 .. 30 ) {
last if $domain_b->ip;
sleep 1;
}
$domain_b->get_info;
ok(exists $domain->info(user_admin)->{ip}
,"Expecting ip field in domain info ".Dumper($domain->info(user_admin))) or exit;
$domain_b->shutdown_now(user_admin);
is($domain->info(user_admin)->{ip}, undef,"Expecting no IP after shutdown");
}
####################################################################
#
......@@ -152,7 +173,7 @@ for my $vm_name ('Void','KVM','LXC') {
$domain->name eq $name,"[$vm_name] Expecting domain name $name, got "
.($domain->name or '<UNDEF>'));
my $ip = '99.88.77.66';
my $ip = '127.0.0.1';
$req = $RVD_FRONT->start_domain(name => $name, user => $USER, remote_ip => $ip);
$RVD_FRONT->wait_request($req,10);
......@@ -189,7 +210,10 @@ for my $vm_name ('Void','KVM','LXC') {
is($domain->internal_id, $domain_back->domain->get_id);
}
test_domain_info($domain);
test_remove_domain($name);
test_domain_name($vm_name);
}
}
......
......@@ -46,6 +46,7 @@ create_domain
start_domain_internal shutdown_domain_internal
hibernate_domain_internal
remote_node
add_ubuntu_minimal_iso
);
our $DEFAULT_CONFIG = "t/etc/ravada.conf";
......@@ -95,6 +96,19 @@ sub arg_create_dom {
return @{$ARG_CREATE_DOM{$vm_name}};
}
sub add_ubuntu_minimal_iso {
my %info = ('bionic_minimal' => {
name => 'Ubuntu Bionic Minimal'
,url => 'http://archive.ubuntu.com/ubuntu/dists/bionic/main/installer-i386/current/images/netboot/mini.iso'
,xml => 'bionic-i386.xml'
,xml_volume => 'bionic32-volume.xml'
,rename_file => 'ubuntu_bionic_mini.iso'
,arch => 'i386'
,md5 => 'c7b21dea4d2ea037c3d97d5dac19af99'
});
$RVD_BACK->_update_table('iso_images','name',\%info);
}
sub vm_names {
return sort keys %ARG_CREATE_DOM;
}
......
......@@ -28,6 +28,7 @@
</span>
</div>
<div "panel-body" ng-show="domain.is_active">
<h3>Connection</h3>
<ul ng-show="domain.is_active">
<li ng-show="domain.spice_password">
<%=l 'The password for this virtual machine connection is :'%>
......@@ -47,6 +48,15 @@
<li><b>Display IP :</b> 192.168.1.106</li>
<li><b>Display Port :</b> 5900</li>
</ul>
<h3>Machine Information</h3>
<ul>
<li><b>Internal IP:</b><span ng-show="domain.ip"> {{domain.ip}}</span>
<span ng-hide="domain.ip"><i>waiting for network to come up</i></span>
</li>
<li><b>Max Memory:</b> {{domain.max_mem}}</li>
<li><b>Memory:</b> {{domain.memory}}</li>
<li><b>CPUs:</b> {{domain.nrVirtCpu}}</li>
</ul>
</div>
<div ng-show="domain.is_active">
<a type="button" class="btn btn-success"
......
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