Unverified Commit 12b2713c authored by Francesc Guasch's avatar Francesc Guasch Committed by GitHub
Browse files

Fix/809 get drivers (#815)

* test(frontend): get domain drivers

issue #809

* wip(frontend): get domain drivers from front

issue #809

* fix(drivers): moved get drivers to front

issue #809

* test(settings): check settings aplied and readable

issue #809
parent 05b821cd
......@@ -81,6 +81,7 @@ requires 'autostart';
requires 'hybernate';
requires 'hibernate';
requires 'get_driver';
##########################################################
has 'domain' => (
......@@ -580,11 +581,12 @@ sub _data($self, $field, $value=undef, $table='domains') {
} else {
@field_select = ( id_domain => $self->id );
}
$self->{$data} = $self->_select_domain_db( _table => $table, @field_select );
confess "No DB info for domain @field_select in $table ".$self->name
if ! exists $self->{$data};
confess "No field $field in $data @field_select ".Dumper($self->{$data})
confess "No field $field in $data ".Dumper(\@field_select)."\n".Dumper($self->{$data})
if !exists $self->{$data}->{$field};
return $self->{$data}->{$field};
......@@ -884,6 +886,7 @@ sub _insert_db {
my ($vm) = ref($self) =~ /.*\:\:(\w+)$/;
confess "Unknown domain from ".ref($self) if !$vm;
$field{vm} = $vm;
$self->{_data}->{name} = $field{name} if $field{name};
my $query = "INSERT INTO domains "
."(" . join(",",sort keys %field )." )"
......@@ -931,6 +934,7 @@ sub pre_remove { }
sub _pre_remove_domain($self, $user=undef) {
$self->_allow_remove($user);
$self->is_volatile() if $self->is_known || $self->domain;
$self->list_disks() if ($self->is_known && $self->is_known_extra)
......@@ -945,7 +949,7 @@ sub _after_remove_domain {
$self->_remove_iptables(user => $user);
if ($self->is_base) {
if ($self->is_known && $self->is_base) {
$self->_do_remove_base(@_);
$self->_remove_files_base();
}
......@@ -958,9 +962,7 @@ sub _after_remove_domain {
sub _remove_domain_db {
my $self = shift;
$self->_select_domain_db or return;
my $id = $self->id;
my $id = $self->{_data}->{id} or return;
my $type = $self->type;
my $sth = $$CONNECTOR->dbh->prepare("DELETE FROM domains "
." WHERE id=?");
......@@ -977,6 +979,7 @@ sub _remove_domain_db {
sub _finish_requests_db {
my $self = shift;
return if !$self->{_data}->{id};
$self->_select_domain_db or return;
my $id = $self->id;
......@@ -1655,7 +1658,7 @@ sub open_iptables {
,id_domain => $self->id
,remote_ip => $args{remote_ip}
);
die "ERROR: Machine ".$self->name." is not active\n"
die "INFO: Machine ".$self->name." is not active, starting up.\n"
}
$self->_add_iptable(%args);
......@@ -2078,8 +2081,6 @@ Returns all the drivers if not passwed
=cut
sub get_driver {}
sub _dbh {
my $self = shift;
_init_connector() if !$CONNECTOR || !$$CONNECTOR;
......@@ -2124,7 +2125,7 @@ Returns the virtual machine type as a string.
sub type {
my $self = shift;
if (!$self->is_known) {
if (!exists $self->{_data} || !exists $self->{_data}->{vm}) {
my ($type) = ref($self) =~ /.*::([a-zA-Z][a-zA-Z0-9]*)/;
confess "Unknown type from ".ref($self) if !$type;
return $type;
......
......@@ -24,6 +24,7 @@ use XML::LibXML;
no warnings "experimental::signatures";
use feature qw(signatures);
extends 'Ravada::Front::Domain::KVM';
with 'Ravada::Domain';
has 'domain' => (
......@@ -38,21 +39,17 @@ has '_vm' => (
,required => 0
);
has readonly => (
isa => 'Int'
,is => 'rw'
,default => 0
);
##################################################
#
our $TIMEOUT_SHUTDOWN = 60;
our $OUT;
our %GET_DRIVER_SUB = (
network => \&_get_driver_network
,sound => \&_get_driver_sound
,video => \&_get_driver_video
,image => \&_get_driver_image
,jpeg => \&_get_driver_jpeg
,zlib => \&_get_driver_zlib
,playback => \&_get_driver_playback
,streaming => \&_get_driver_streaming
);
our %SET_DRIVER_SUB = (
network => \&_set_driver_network
,sound => \&_set_driver_sound
......@@ -75,6 +72,12 @@ our %REMOVE_CONTROLLER_SUB = (
);
##################################################
sub BUILD {
my ($self, $arg) = @_;
$self->readonly( $arg->{readonly} or 0);
}
=head2 name
Returns the name of the domain
......@@ -1327,28 +1330,6 @@ sub clean_swap_volumes {
}
}
=head2 get_driver
Gets the value of a driver
Argument: name
my $driver = $domain->get_driver('video');
=cut
sub get_driver {
my $self = shift;
my $name = shift;
my $sub = $GET_DRIVER_SUB{$name};
die "I can't get driver $name for domain ".$self->name
if !$sub;
return $sub->($self);
}
=head2 set_driver
Sets the value of a driver
......@@ -1368,104 +1349,9 @@ sub set_driver {
die "I can't get driver $name for domain ".$self->name
if !$sub;
return $sub->($self,@_);
}
sub _get_driver_generic {
my $self = shift;
my $xml_path = shift;
my ($tag) = $xml_path =~ m{.*/(.*)};
my @ret;
my $doc = XML::LibXML->load_xml(string => $self->domain->get_xml_description);
for my $driver ($doc->findnodes($xml_path)) {
my $str = $driver->toString;
$str =~ s{^<$tag (.*)/>}{$1};
push @ret,($str);
}
return $ret[0] if !wantarray && scalar@ret <2;
return @ret;
}
sub _get_driver_graphics {
my $self = shift;
my $xml_path = shift;
my ($tag) = $xml_path =~ m{.*/(.*)};
my @ret;
my $doc = XML::LibXML->load_xml(string => $self->domain->get_xml_description);
for my $tags (qw(image jpeg zlib playback streaming)){
for my $driver ($doc->findnodes($xml_path)) {
my $str = $driver->toString;
$str =~ s{^<$tag (.*)/>}{$1};
push @ret,($str);
}
return $ret[0] if !wantarray && scalar@ret <2;
return @ret;
}
}
sub _get_driver_image {
my $self = shift;
my $image = $self->_get_driver_graphics('/domain/devices/graphics/image',@_);
#
# if ( !defined $image ) {
# my $doc = XML::LibXML->load_xml(string => $self->domain->get_xml_description);
# Ravada::VM::KVM::xml_add_graphics_image($doc);
# }
return $image;
}
sub _get_driver_jpeg {
my $self = shift;
return $self->_get_driver_graphics('/domain/devices/graphics/jpeg',@_);
}
sub _get_driver_zlib {
my $self = shift;
return $self->_get_driver_graphics('/domain/devices/graphics/zlib',@_);
}
sub _get_driver_playback {
my $self = shift;
return $self->_get_driver_graphics('/domain/devices/graphics/playback',@_);
}
sub _get_driver_streaming {
my $self = shift;
return $self->_get_driver_graphics('/domain/devices/graphics/streaming',@_);
}
sub _get_driver_video {
my $self = shift;
return $self->_get_driver_generic('/domain/devices/video/model',@_);
}
sub _get_driver_network {
my $self = shift;
return $self->_get_driver_generic('/domain/devices/interface/model',@_);
}
sub _get_driver_sound {
my $self = shift;
my $xml_path ="/domain/devices/sound";
my @ret;
my $doc = XML::LibXML->load_xml(string => $self->domain->get_xml_description);
for my $driver ($doc->findnodes($xml_path)) {
push @ret,('model="'.$driver->getAttribute('model').'"');
}
return $ret[0] if !wantarray && scalar@ret <2;
return @ret;
my $ret = $sub->($self,@_);
$self->xml_description();
return $ret;
}
sub _text_to_hash {
......
......@@ -11,6 +11,7 @@ use IPC::Run3 qw(run3);
use Moose;
use YAML qw(LoadFile DumpFile);
extends 'Ravada::Front::Domain::Void';
with 'Ravada::Domain';
has 'domain' => (
......@@ -119,21 +120,6 @@ sub _store {
}
sub _value{
my $self = shift;
my ($var) = @_;
my ($disk) = $self->_config_file();
my $data = {} ;
$data = LoadFile($disk) if -e $disk;
return $data->{$var};
}
sub shutdown {
my $self = shift;
$self->_store(is_active => 0);
......@@ -176,11 +162,6 @@ sub prepare_base {
}
}
sub _config_file {
my $self = shift;
return "$DIR_TMP/".$self->name.".yml";
}
sub list_disks {
return disk_device(@_);
}
......@@ -399,13 +380,7 @@ sub set_memory {
$self->_set_info(memory => $value );
}
sub get_driver {
my $self = shift;
my $name = shift;
my $drivers = $self->_value('drivers');
return $drivers->{$name};
}
sub set_driver {
my $self = shift;
......
......@@ -713,11 +713,7 @@ sub search_domain {
$sth->execute($name);
my ($id, $tipo) = $sth->fetchrow or confess "ERROR: Unknown domain name $name";
if ($tipo =~ /KVM/) {
return Ravada::Front::Domain::KVM->new(id => $id, name => $name);
}else {
return Ravada::Front::Domain->new(id => $id);
}
return Ravada::Front::Domain->open($id);
}
=head2 list_requests
......
......@@ -10,9 +10,13 @@ Ravada::Front::Domain - Frontent domain information for Ravada
=cut
use Carp qw(cluck confess croak);
use Data::Dumper;
use JSON::XS;
use Moose;
use Ravada::Front::Domain::KVM;
use Ravada::Front::Domain::Void;
no warnings "experimental::signatures";
use feature qw(signatures);
......@@ -31,15 +35,29 @@ our $CONNECTOR = \$Ravada::Front::CONNECTOR;
###########################################################################
sub BUILD($self, $arg) {
my $id = $arg->{id} or confess "ERROR: id required";
my $ret = $self->_select_domain_db( id => $id);
my $id = $arg->{id};
my $name = $arg->{name};
$self->_select_domain_db( id => $id) if defined $id;
$self->_select_domain_db( name => $name) if defined $name;
$self->{_data}->{id} = $id if defined $id;
$self->{_data}->{name} = $name if defined $name;
# confess "ERROR: Domain '".$self->name." not found "
# if $self->is_volatile && ! $self->is_active;
}
sub open($self, $id) {
return Ravada::Front::Domain->new( id => $id );
my $domain = Ravada::Front::Domain->new( id => $id );
if ($domain->type eq 'KVM') {
$domain = Ravada::Front::Domain::KVM->new( id => $id );
} elsif ($domain->type eq 'Void') {
$domain = Ravada::Front::Domain::Void->new( id => $id );
}
die "ERROR: Unknown domain id: $id\n"
unless exists $domain->{_data}->{name} && $domain->{_data}->{name};
return $domain;
}
sub autostart($self ) { return $self->_data('autostart') }
......@@ -86,6 +104,7 @@ sub is_removed { return 0 }
sub list_volumes { confess "TODO" }
sub name($self) {
return $self->{_data}->{name} if exists $self->{_data} && $self->{_data}->{name};
return $self->_data('name')
}
......@@ -103,4 +122,6 @@ sub shutdown_now { confess "TODO" }
sub spinoff_volumes { confess "TODO" }
sub start { confess "TODO" }
sub get_driver {}
1;
......@@ -10,6 +10,17 @@ our %GET_CONTROLLER_SUB = (
usb => \&_get_controller_usb
);
our %GET_DRIVER_SUB = (
network => \&_get_driver_network
,sound => \&_get_driver_sound
,video => \&_get_driver_video
,image => \&_get_driver_image
,jpeg => \&_get_driver_jpeg
,zlib => \&_get_driver_zlib
,playback => \&_get_driver_playback
,streaming => \&_get_driver_streaming
);
=head2 get_controller
Calls the method to get the specified controller info
......@@ -45,4 +56,125 @@ sub _get_controller_usb {
return @ret;
}
1;
\ No newline at end of file
=head2 get_driver
Gets the value of a driver
Argument: name
my $driver = $domain->get_driver('video');
=cut
sub get_driver {
my $self = shift;
my $name = shift;
my $sub = $GET_DRIVER_SUB{$name};
die "I can't get driver $name for domain ".$self->name
if !$sub;
$self->xml_description if ref($self) !~ /Front/;
return $sub->($self);
}
sub _get_driver_generic {
my $self = shift;
my $xml_path = shift;
my ($tag) = $xml_path =~ m{.*/(.*)};
my @ret;
my $doc = XML::LibXML->load_xml(string => $self->_data_extra('xml'));
for my $driver ($doc->findnodes($xml_path)) {
my $str = $driver->toString;
$str =~ s{^<$tag (.*)/>}{$1};
push @ret,($str);
}
return $ret[0] if !wantarray && scalar@ret <2;
return @ret;
}
sub _get_driver_graphics {
my $self = shift;
my $xml_path = shift;
my ($tag) = $xml_path =~ m{.*/(.*)};
my @ret;
my $doc = XML::LibXML->load_xml(string => $self->_data_extra('xml'));
for my $tags (qw(image jpeg zlib playback streaming)){
for my $driver ($doc->findnodes($xml_path)) {
my $str = $driver->toString;
$str =~ s{^<$tag (.*)/>}{$1};
push @ret,($str);
}
return $ret[0] if !wantarray && scalar@ret <2;
return @ret;
}
}
sub _get_driver_image {
my $self = shift;
my $image = $self->_get_driver_graphics('/domain/devices/graphics/image',@_);
#
# if ( !defined $image ) {
# my $doc = XML::LibXML->load_xml(string => $self->domain->get_xml_description);
# Ravada::VM::KVM::xml_add_graphics_image($doc);
# }
return $image;
}
sub _get_driver_jpeg {
my $self = shift;
return $self->_get_driver_graphics('/domain/devices/graphics/jpeg',@_);
}
sub _get_driver_zlib {
my $self = shift;
return $self->_get_driver_graphics('/domain/devices/graphics/zlib',@_);
}
sub _get_driver_playback {
my $self = shift;
return $self->_get_driver_graphics('/domain/devices/graphics/playback',@_);
}
sub _get_driver_streaming {
my $self = shift;
return $self->_get_driver_graphics('/domain/devices/graphics/streaming',@_);
}
sub _get_driver_video {
my $self = shift;
return $self->_get_driver_generic('/domain/devices/video/model',@_);
}
sub _get_driver_network {
my $self = shift;
return $self->_get_driver_generic('/domain/devices/interface/model',@_);
}
sub _get_driver_sound {
my $self = shift;
my $xml_path ="/domain/devices/sound";
my @ret;
my $doc = XML::LibXML->load_xml(string => $self->_data_extra('xml'));
for my $driver ($doc->findnodes($xml_path)) {
push @ret,('model="'.$driver->getAttribute('model').'"');
}
return $ret[0] if !wantarray && scalar@ret <2;
return @ret;
}
1;
package Ravada::Front::Domain::Void;
use Moose;
use YAML qw(LoadFile);
extends 'Ravada::Front::Domain';
my $DIR_TMP = "/var/tmp/rvd_void";
sub get_driver {
my $self = shift;
my $name = shift;
my $drivers = $self->_value('drivers');
return $drivers->{$name};
}
sub _value{
my $self = shift;
my ($var) = @_;
my ($disk) = $self->_config_file();
my $data = {} ;
$data = LoadFile($disk) if -e $disk;
return $data->{$var};
}
sub _config_file {
my $self = shift;
return "$DIR_TMP/".$self->name.".yml";
}
1;
......@@ -429,12 +429,10 @@ Returns true or false if domain exists.
sub search_domain($self, $name, $force=undef) {
$self->connect();
my @all_domains;
eval { @all_domains = $self->vm->list_all_domains() };
confess $@ if $@;
my $dom;
eval { $dom = $self->vm->get_domain_by_name($name); };
confess $@ if $@ && $@ !~ /error code: 42,/;
if (!$dom) {
return if !$force;
return if !$self->_domain_in_db($name);
......@@ -444,8 +442,8 @@ sub search_domain($self, $name, $force=undef) {
my $domain;
my @domain = ( );
@domain = ( domain => $dom ) if $dom;
@domain = ( id_owner => $Ravada::USER_DAEMON->id)
push @domain, ( domain => $dom ) if $dom;
push @domain, ( id_owner => $Ravada::USER_DAEMON->id)
if $force && !$self->_domain_in_db($name);
eval {
$domain = Ravada::Domain::KVM->new(
......
......@@ -49,6 +49,7 @@ sub create_domain {
);
$domain->_insert_db(name => $args{name} , id_owner => $args{id_owner}