Commit d3b8b1ea authored by Francesc Guasch's avatar Francesc Guasch Committed by GitHub
Browse files

Merge pull request #336 from UPC/335_iso

335 iso
parents 6c32f4ff d9325664
......@@ -17,7 +17,8 @@ my $help;
my ($DEBUG, $ADD_USER );
my $FILE_CONFIG = "/etc/ravada.conf";
my $FILE_CONFIG_DEFAULT = "/etc/ravada.conf";
my $FILE_CONFIG;
my $ADD_USER_LDAP;
my $IMPORT_DOMAIN;
......@@ -25,9 +26,12 @@ my $CHANGE_PASSWORD;
my $NOFORK;
my $MAKE_ADMIN_USER;
my $REMOVE_ADMIN_USER;
my $START = 1;
my $URL_ISOS;
my $USAGE = "$0 "
." [--debug] [--config=$FILE_CONFIG] [--add-user=name] [--add-user-ldap=name]"
." [--debug] [--config=$FILE_CONFIG_DEFAULT] [--add-user=name] [--add-user-ldap=name]"
." [--change-password] [--make-admin=username]"
." [-X] [start|stop|status]"
."\n"
......@@ -36,17 +40,19 @@ my $USAGE = "$0 "
." --change-password : changes the password of an user\n"
." --import-domain : import a domain\n"
." --make-admin : make user admin\n"
." --config : config file, defaults to $FILE_CONFIG"
." --config : config file, defaults to $FILE_CONFIG_DEFAULT"
." -X : start in foreground\n"
." --url-isos=(URL|default)\n"
;
$FILE_CONFIG = undef if ! -e $FILE_CONFIG;
$START = 0 if scalar @ARGV && $ARGV[0] ne '&';
GetOptions ( help => \$help
,debug => \$DEBUG
,'no-fork'=> \$NOFORK
,'config=s'=> \$FILE_CONFIG
,'add-user=s'=> \$ADD_USER
,'url-isos=s'=> \$URL_ISOS
,'make-admin=s' => \$MAKE_ADMIN_USER
,'remove-admin=s' => \$REMOVE_ADMIN_USER
,'change-password'=> \$CHANGE_PASSWORD
......@@ -54,6 +60,8 @@ GetOptions ( help => \$help
,'import-domain=s' => \$IMPORT_DOMAIN
) or exit;
$START = 1 if $DEBUG || $FILE_CONFIG || $NOFORK;
#####################################################################
#
# check arguments
......@@ -219,6 +227,20 @@ sub import_domain {
$ravada->import_domain(name => $name, vm => 'KVM', user => $user);
}
sub set_url_isos {
my $url = shift;
my $rvd_back = Ravada->new(%CONFIG);
if ($url =~ /^default$/i) {
my $sth = $rvd_back->connector->dbh->prepare("DROP TABLE iso_images");
$sth->execute;
$sth->finish;
my $rvd_back2 = Ravada->new(%CONFIG);
} else {
$rvd_back->_set_url_isos($url);
print "ISO_IMAGES table URLs set from $url\n";
}
}
sub DESTROY {
return if !$PID_LONGS;
warn "Killing pid: $PID_LONGS";
......@@ -231,25 +253,15 @@ sub DESTROY {
}
#################################################################
if ($ADD_USER) {
add_user($ADD_USER);
exit;
} elsif ($ADD_USER_LDAP) {
add_user($ADD_USER_LDAP);
exit;
} elsif ($CHANGE_PASSWORD) {
change_password();
exit;
} elsif ($IMPORT_DOMAIN) {
import_domain($IMPORT_DOMAIN);
exit;
} elsif ($MAKE_ADMIN_USER) {
make_admin($MAKE_ADMIN_USER);
exit;
} elsif ($REMOVE_ADMIN_USER) {
remove_admin($REMOVE_ADMIN_USER);
exit;
}
die "Already started" if Proc::PID::File->running( name => 'rvd_back');
start();
add_user($ADD_USER) if $ADD_USER;
add_user($ADD_USER_LDAP) if $ADD_USER_LDAP;
change_password() if $CHANGE_PASSWORD;
import_domain($IMPORT_DOMAIN) if $IMPORT_DOMAIN;
make_admin($MAKE_ADMIN_USER) if $MAKE_ADMIN_USER;
remove_admin($REMOVE_ADMIN_USER) if $REMOVE_ADMIN_USER;
set_url_isos($URL_ISOS) if $URL_ISOS;
if ($START) {
die "Already started" if Proc::PID::File->running( name => 'rvd_back');
start();
}
......@@ -198,8 +198,9 @@ sub _update_isos {
,arch => 'amd64'
,xml => 'yakkety64-amd64.xml'
,xml_volume => 'yakkety64-volume.xml'
,md5 => '6bd80e10bf223a04d3aafe0f997d046b'
,md5_url => 'http://archive.ubuntu.com/ubuntu/dists/zesty/main/installer-amd64/current/images/MD5SUMS'
,url => 'http://archive.ubuntu.com/ubuntu/dists/zesty/main/installer-amd64/current/images/netboot/mini.iso'
,rename_file => 'xubuntu_zesty_mini.iso'
}
,xubuntu_xenial => {
name => 'Xubuntu Xenial Xerus'
......@@ -208,6 +209,7 @@ sub _update_isos {
,xml => 'yakkety64-amd64.xml'
,xml_volume => 'yakkety64-volume.xml'
,md5 => 'fe495d34188a9568c8d166efc5898d22'
,rename_file => 'xubuntu_xenial_mini.iso'
}
,lubuntu_zesty => {
name => 'Lubuntu Zesty Zapus'
......@@ -220,7 +222,8 @@ sub _update_isos {
,lubuntu_xenial => {
name => 'Lubuntu Xenial Xerus'
,description => 'Xubuntu 16.04 Xenial Xerus 64 bits (LTS)'
,url => 'http://cdimage.ubuntu.com/lubuntu/releases/16.04.2/release/lubuntu-16.04.2-desktop-amd64.iso'
,url => 'http://cdimage.ubuntu.com/lubuntu/releases/16.04.2/release/'
,file_re => 'lubuntu-16.04.2-desktop-amd64.iso'
,md5_url => 'http://cdimage.ubuntu.com/lubuntu/releases/16.04.2/release/MD5SUMS'
,xml => 'yakkety64-amd64.xml'
,xml_volume => 'yakkety64-volume.xml'
......@@ -511,6 +514,23 @@ sub _update_data {
$self->_update_domain_drivers_options();
}
sub _set_url_isos($self, $new_url='http://localhost/iso/') {
$new_url .= '/' if $new_url !~ m{/$};
my $sth = $CONNECTOR->dbh->prepare(
"SELECT id,url FROM iso_images "
."WHERE url is NOT NULL"
);
my $sth_update = $CONNECTOR->dbh->prepare(
"UPDATE iso_images set url=? WHERE id=?"
);
$sth->execute();
while ( my ($id, $url) = $sth->fetchrow) {
$url =~ s{\w+://(.*?)/(.*)}{$new_url$2};
$sth_update->execute($url, $id);
}
$sth->finish;
}
sub _upgrade_table {
my $self = shift;
my ($table, $field, $definition) = @_;
......@@ -600,6 +620,12 @@ sub _create_tables {
closedir $ls;
}
sub _clean_iso_mini {
my $sth = $CONNECTOR->dbh->prepare("DELETE FROM iso_images WHERE device like ?");
$sth->execute('%/mini.iso');
$sth->finish;
}
sub _upgrade_tables {
my $self = shift;
# return if $CONNECTOR->dbh->{Driver}{Name} !~ /mysql/i;
......@@ -611,11 +637,13 @@ sub _upgrade_tables {
$self->_upgrade_table('requests','at_time','int(11) DEFAULT NULL');
$self->_clean_iso_mini();
$self->_upgrade_table('iso_images','md5_url','varchar(255)');
$self->_upgrade_table('iso_images','sha256','varchar(255)');
$self->_upgrade_table('iso_images','sha256_url','varchar(255)');
$self->_upgrade_table('iso_images','file_re','char(64)');
$self->_upgrade_table('iso_images','device','varchar(255)');
$self->_upgrade_table('iso_images','rename_file','varchar(80) DEFAULT NULL');
$self->_upgrade_table('users','language','char(3) DEFAULT NULL');
if ( $self->_upgrade_table('users','is_external','int(11) DEFAULT 0')) {
......
......@@ -72,6 +72,7 @@ our $CONNECTOR = \$Ravada::CONNECTOR;
our $WGET = `which wget`;
chomp $WGET;
our $CACHE_DOWNLOAD = 1;
##########################################################################
......@@ -809,8 +810,12 @@ sub _iso_name {
my $req = shift;
my $iso_name;
($iso_name) = $iso->{url} =~ m{.*/(.*)} if $iso->{url};
($iso_name) = $iso->{device} if !$iso_name;
if ($iso->{rename_file}) {
$iso_name = $iso->{rename_file};
} else {
($iso_name) = $iso->{url} =~ m{.*/(.*)} if $iso->{url};
($iso_name) = $iso->{device} if !$iso_name;
}
confess "Unknown iso_name for ".Dumper($iso) if !$iso_name;
......@@ -988,7 +993,8 @@ sub _search_iso {
sub _download($self, $url) {
confess "Wrong url '$url'" if $url =~ m{\*};
my $cache = $self->_cache_get($url);
my $cache;
$cache = $self->_cache_get($url) if $CACHE_DOWNLOAD && $url !~ m{^http.?://localhost};
return $cache if $cache;
my $ua = new LWP::UserAgent;
......@@ -1005,7 +1011,7 @@ sub _cache_get($self, $url) {
my $file = _cache_filename($url);
my @stat = stat($file) or return;
return if time-$stat[9] > 3600;
return if time-$stat[9] > 300;
open my $in ,'<' , $file or return;
return join("",<$in>);
}
......@@ -1043,16 +1049,12 @@ sub _fetch_filename {
if (!$row->{file_re}) {
my ($new_url, $file);
($new_url, $file) = $row->{url} =~ m{(.*)/(.*)} if $row->{url};
($file) = $row->{device} =~ m{.*/(.*)} if !$file;
confess "No filename in $row->{url}" if !$file;
if ($file =~ /\*/) {
$row->{url} = $new_url;
$row->{file_re} = $file;
} else {
$row->{filename} = $file;
return;
}
($file) = $row->{device} =~ m{.*/(.*)}
if !$file && $row->{device};
confess "No filename in $row->{name} $row->{url}" if !$file;
$row->{url} = $new_url;
$row->{file_re} = $file;
}
confess "No file_re" if !$row->{file_re};
......@@ -1068,7 +1070,7 @@ sub _fetch_filename {
}
die "No ".qr($row->{file_re})." found on $row->{url}<br><pre>$content</pre>" if !$file;
$row->{filename} = $file;
$row->{filename} = ($row->{rename_file} or $file);
$row->{url} .= "/" if $row->{url} !~ m{/$};
$row->{url} .= $file;
}
......
use warnings;
use strict;
use Carp qw(confess);
use Data::Dumper;
use IPC::Run3;
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');
init($test->connector);
$Ravada::DEBUG=0;
$Ravada::SECONDS_WAIT_CHILDREN = 1;
sub test_download {
my ($vm, $id_iso) = @_;
my $iso;
eval { $iso = $vm->_search_iso($id_iso) };
if ($@ =~ /No md5/) {
warn "Probably this release file is obsolete.\n$@";
return;
}
is($@,'');
unlink($iso->{device}) or die "$! $iso->{device}"
if $iso->{device} && -e $iso->{device};
diag("Testing download $iso->{name}");
my $req1 = Ravada::Request->download(
id_iso => $id_iso
, id_vm => $vm->id
, delay => 4
);
is($req1->status, 'requested');
rvd_back->_process_all_requests_dont_fork();
is($req1->status, 'done');
is($req1->error, '');
}
sub local_urls {
rvd_back->_set_url_isos('http://localhost/iso/');
}
sub search_id_isos {
my $vm = shift;
my $sth=$test->dbh->prepare(
"SELECT * FROM iso_images"# where name like 'Xubuntu%'"
);
$sth->execute;
my @id_iso;
while ( my $row = $sth->fetchrow_hashref ) {
next if !$row->{url};
eval {$vm->Ravada::VM::KVM::_fetch_filename($row);};
if ($@ =~ /Can't connect to localhost/) {
diag("Skipped tests, see http://ravada.readthedocs.io/en/latest/devel-docs/local_iso_server.html");
return;
}
diag($@) if $@ && $@ !~ /No.*found/i;
if (!$row->{filename}) {
diag("skipped test $row->{name} $row->{url} $row->{file_re}");
next;
}
push @id_iso,($row->{id});
}
return @id_iso;
}
##################################################################
for my $vm_name ('KVM') {
my $rvd_back = rvd_back();
local_urls();
my $vm = $rvd_back->search_vm($vm_name);
SKIP: {
my $msg = "SKIPPED: No virtual managers found";
if ($vm && $vm_name =~ /kvm/i && $>) {
$msg = "SKIPPED: Test must run as root";
$vm = undef;
}
skip($msg,10) if !$vm;
for my $id_iso (search_id_isos($vm)) {
test_download($vm, $id_iso);
}
}
}
done_testing();
Markdown is supported
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