Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejol committed Sep 10, 2018
2 parents 0da7236 + 42ce75a commit 3642186
Show file tree
Hide file tree
Showing 9 changed files with 459 additions and 90 deletions.
106 changes: 57 additions & 49 deletions lib/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ sub _update_isos {
,url => 'http://dl-cdn.alpinelinux.org/alpine/v3.7/releases/x86_64/'
,file_re => 'alpine-virt-3.7.\d+-x86_64.iso'
,sha256_url => 'http://dl-cdn.alpinelinux.org/alpine/v3.7/releases/x86_64/alpine-virt-3.7.0-x86_64.iso.sha256'
,min_disk_size => '10'
,min_disk_size => '1'
}
,artful => {
name => 'Ubuntu Artful Aardvark'
Expand Down Expand Up @@ -1026,6 +1026,8 @@ sub _upgrade_tables {
$self->_upgrade_table('vms','min_free_memory',"text DEFAULT NULL");
$self->_upgrade_table('vms', 'max_load', 'int not null default 10');
$self->_upgrade_table('vms', 'active_limit','int DEFAULT NULL');
$self->_upgrade_table('vms', 'base_storage','varchar(64) DEFAULT NULL');
$self->_upgrade_table('vms', 'clone_storage','varchar(64) DEFAULT NULL');

$self->_upgrade_table('requests','at_time','int(11) DEFAULT NULL');
$self->_upgrade_table('requests','pid','int(11) DEFAULT NULL');
Expand Down Expand Up @@ -1328,61 +1330,62 @@ sub create_domain {
my %args = @_;
my $vm_name = delete $args{vm};

my @create_args = (%args);

my $id_owner = delete $args{id_owner} or croak "ERROR: Argument id_owner required ";
my $name = delete $args{name} or confess "ERROR: Argument name required";

my $request = delete $args{request};
my $id_base = delete $args{id_base};
confess "ERROR: Argument vm required" if !$id_base && !$vm_name;

_check_args(\%args,qw(iso_file id_base id_iso active swap memory disk id_template));
my $start = $args{start};
my $id_base = $args{id_base};
my $request = $args{request};
my $id_owner = $args{id_owner};

my $vm;
if ($request) {
%args = %{$request->args};
$vm_name = $request->defined_arg('vm') if $request->defined_arg('vm');
$id_base = $request->defined_arg('id_base') if $request->defined_arg('id_base');
}
if ($vm_name) {
$vm = $self->search_vm($vm_name);
confess "ERROR: vm $vm_name not found" if !$vm;
}
if ($id_base) {
my $base = Ravada::Domain->open($id_base)
or confess "ERROR: Base domain id:$id_base not found";
$vm = $self->search_vm($base->vm);

#TODO: check if base vm matches vm_name when both supplied
or confess "Unknown base id: $id_base";
$vm = $base->_vm;
}

confess "No vm found" if !$vm;
confess "No vm found, request = ".Dumper(request => $request) if !$vm;

carp "WARNING: no VM defined, we will use ".$vm->name
if !$vm_name && !$args{id_base};
if !$vm_name && !$id_base;

confess "I can't find any vm ".Dumper($self->vm) if !$vm;

my $domain;
$request->status("creating") if $request;
eval { $domain = $vm->create_domain(@create_args) };
my $domain;
eval { $domain = $vm->create_domain(%args)};

my $error = $@;
if ( $request ) {
$request->error($error) if $error;
if ($error =~ /has \d+ requests/) {
$request->status('retry');
}
if (!$error && $request->defined_arg('start')) {
$request->status("starting");
eval {
my $user = Ravada::Auth::SQL->search_by_id($request->args('id_owner'));
$domain->start(
user => $user
,remote_ip => $request->defined_arg('remote_ip')
,request => $request
)
};
my $error = $@;
$request->error($error) if $error;
}
} elsif ($@) {
die $@;
} elsif ($error) {
die $error;
}
if (!$error && $start) {
$request->status("starting") if $request;
eval {
my $user = Ravada::Auth::SQL->search_by_id($id_owner);
my $remote_ip;
$remote_ip = $request->defined_arg('remote_ip') if $request;
$domain->start(
user => $user
,remote_ip => $remote_ip
,request => $request
)
};
my $error = $@;
die $error if $error && !$request;
$request->error($error) if $error;
}
return $domain;
}
Expand All @@ -1407,26 +1410,32 @@ sub remove_domain {
my $self = shift;
my %arg = @_;

confess "Argument name required "
if !$arg{name};
my $name = delete $arg{name} or confess "Argument name required ";

confess "Argument uid required "
if !$arg{uid};

lock_hash(%arg);

my $domain = $self->search_domain($arg{name}, 1);
my $sth = $CONNECTOR->dbh->prepare("SELECT id FROM domains WHERE name = ?");
$sth->execute($name);

my ($id)= $sth->fetchrow;
confess "Error: Unknown domain $name" if !$id;

my $user = Ravada::Auth::SQL->search_by_id( $arg{uid});
die "Error: user ".$user->name." can't remove domain $id"
if !$user->can_remove_machine($id);

my $domain = Ravada::Domain->open(id => $id, _force => 1)
or do {
warn "Warning: I can't find domain '$id', maybe already removed.";
$sth = $CONNECTOR->dbh->prepare("DELETE FROM domains where id=?");
$sth->execute($id);
return;
};

if ($domain) {
$domain->remove( $user);
} else {
$domain = Ravada::Front::Domain->search_domain($arg{name});
if ($domain) {
$domain->_allow_remove($user); # dies if not allowed
$domain->_after_remove_domain($user);
}
}
$domain->remove( $user);
}

=head2 search_domain
Expand Down Expand Up @@ -2092,7 +2101,7 @@ sub _cmd_create{
warn "$$ creating domain ".Dumper($request->args) if $DEBUG;
my $domain;

$domain = $self->create_domain(%{$request->args},request => $request);
$domain = $self->create_domain(request => $request);

my $msg = '';

Expand Down Expand Up @@ -2858,9 +2867,8 @@ sub _enforce_limits_active($self, $request) {
}
for my $id_user(keys %domains) {
next if scalar @{$domains{$id_user}}<2;

my $user = Ravada::Auth::SQL->search_by_id($id_user);
next if $user->is_admin();
next if $user->is_admin;

my @domains_user = sort { $a->start_time <=> $b->start_time
|| $a->id <=> $b->id }
Expand Down
1 change: 1 addition & 0 deletions lib/Ravada/Domain.pm
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ sub open($class, @args) {
bless $vm0, $vm_class;

$vm = $vm0->new( readonly => $readonly );
$vm = $vm0->new( );
}

my $domain = $vm->search_domain($row->{name}, $force);
Expand Down
8 changes: 6 additions & 2 deletions lib/Ravada/Domain/KVM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,17 @@ sub _create_qcow_base {

my @base_img;

my $base_name = $self->name;
for my $vol_data ( $self->list_volumes_target()) {
my ($file_img,$target) = @$vol_data;
my $base_img = $file_img;

my $pool_base = $self->_vm->default_storage_pool_name;
$pool_base = $self->_vm->base_storage_pool() if $self->_vm->base_storage_pool();

my $dir_base = $self->_vm->_storage_path($pool_base);

my @cmd;
$base_img =~ s{\.\w+$}{\.ro.qcow2};
$base_img =~ s{(.*)/(.*)\.\w+$}{$dir_base/$2\.ro.qcow2};

die "ERROR: base image already exists '$base_img'" if -e $base_img;

Expand Down
59 changes: 56 additions & 3 deletions lib/Ravada/VM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ sub _around_create_domain {
my %args = @_;

my $id_owner = delete $args{id_owner} or confess "ERROR: Missing id_owner";
my $owner = Ravada::Auth::SQL->search_by_id($id_owner);
my $owner = Ravada::Auth::SQL->search_by_id($id_owner) or confess "Error: Missing user $id_owner";

my $base;
my $id_base = delete $args{id_base};
Expand Down Expand Up @@ -452,7 +452,7 @@ sub ip {
return $ip if $ip && $ip !~ /^127/ && $ip =~ /^\d+\.\d+\.\d+\.\d+$/;

warn "WARNING: I can't find the IP of host ".$self->host.", using localhost."
." This virtual machine won't be available from the network.";
." This virtual machine won't be available from the network." if $0 !~ /\.t$/;

return '127.0.0.1';
}
Expand Down Expand Up @@ -525,7 +525,7 @@ sub _check_require_base {
delete $args{start};
delete $args{remote_ip};

delete @args{'_vm','name','vm', 'memory','description'};
delete @args{'_vm','name','vm', 'memory','description','id_iso'};

confess "ERROR: Unknown arguments ".join(",",keys %args)
if keys %args;
Expand Down Expand Up @@ -659,9 +659,62 @@ sub default_storage_pool_name {
$sth->execute($value,$id);
$self->{_data}->{default_storage} = $value;
}
$self->_select_vm_db();
return $self->_data('default_storage');
}

=head2 base_storage_pool
Set the storage pool for bases in this Virtual Machine Manager
$vm->base_storage_pool('pool2');
=cut

sub base_storage_pool {
my $self = shift;
my $value = shift;

#TODO check pool exists
if (defined $value) {
my $id = $self->id();
my $sth = $$CONNECTOR->dbh->prepare(
"UPDATE vms SET base_storage=?"
." WHERE id=?"
);
$sth->execute($value,$id);
$self->{_data}->{base_storage} = $value;
}
$self->_select_vm_db();
return $self->_data('base_storage');
}

=head2 clone_storage_pool
Set the storage pool for clones in this Virtual Machine Manager
$vm->clone_storage_pool('pool3');
=cut

sub clone_storage_pool {
my $self = shift;
my $value = shift;

#TODO check pool exists
if (defined $value) {
my $id = $self->id();
my $sth = $$CONNECTOR->dbh->prepare(
"UPDATE vms SET clone_storage=?"
." WHERE id=?"
);
$sth->execute($value,$id);
$self->{_data}->{clone_storage} = $value;
}
$self->_select_vm_db();
return $self->_data('clone_storage');
}

=head2 min_free_memory
Returns the minimun free memory necessary to start a new virtual machine
Expand Down
25 changes: 20 additions & 5 deletions lib/Ravada/VM/KVM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ our $WGET = `which wget`;
chomp $WGET;

our $CACHE_DOWNLOAD = 1;
our $VERIFY_ISO = 1;

our %_CREATED_DEFAULT_STORAGE = ();
##########################################################################
Expand Down Expand Up @@ -377,6 +378,9 @@ sub dir_img {
}

sub _storage_path($self, $storage) {
if (!ref($storage)) {
$storage = $self->vm->get_storage_pool_by_name($storage);
}
my $xml = XML::LibXML->load_xml(string => $storage->get_xml_description());

my $dir = $xml->findnodes('/pool/target/path/text()');
Expand Down Expand Up @@ -780,6 +784,8 @@ sub _create_disk_qcow2 {
confess "Missing name" if !$name;

my $dir_img = $self->dir_img;
my $clone_pool = $self->clone_storage_pool();
$dir_img = $self->_storage_path($clone_pool) if $clone_pool;

my @files_out;

Expand Down Expand Up @@ -952,7 +958,7 @@ sub _iso_name($self, $iso, $req, $verbose=1) {
my $device = ($iso->{device} or $self->dir_img."/$iso_name");

confess "Missing MD5 and SHA256 field on table iso_images FOR $iso->{url}"
if $iso->{url} && !$iso->{md5} && !$iso->{sha256};
if $VERIFY_ISO && $iso->{url} && !$iso->{md5} && !$iso->{sha256};

my $downloaded = 0;
if (! -e $device || ! -s $device) {
Expand Down Expand Up @@ -1076,8 +1082,10 @@ sub _search_iso {
return $row if $file_iso;

$self->_fetch_filename($row);# if $row->{file_re};
$self->_fetch_md5($row) if !$row->{md5} && $row->{md5_url};
$self->_fetch_sha256($row) if !$row->{sha256} && $row->{sha256_url};
if ($VERIFY_ISO) {
$self->_fetch_md5($row) if !$row->{md5} && $row->{md5_url};
$self->_fetch_sha256($row) if !$row->{sha256} && $row->{sha256_url};
}

if ( !$row->{device} && $row->{filename}) {
if (my $volume = $self->search_volume($row->{filename})) {
Expand Down Expand Up @@ -1197,8 +1205,15 @@ sub _fetch_filename {
confess "No file_re" if !$row->{file_re};
$row->{file_re} .= '$' if $row->{file_re} !~ m{\$$};

my @found = $self->_search_url_file($row->{url}, $row->{file_re});
die "No ".qr($row->{file_re})." found on $row->{url}" if !@found;
my @found = $self->search_volume_re(qr($row->{file_re}));
if (@found) {
$row->{device} = $found[0]->get_path;
$row->{filename} = $found[0]->get_path =~ m{.*/(.*)};
return;
} else {
@found = $self->_search_url_file($row->{url}, $row->{file_re}) if !@found;
die "No ".qr($row->{file_re})." found on $row->{url}" if !@found;
}
my $url = $found[-1];
my ($file) = $url =~ m{.*/(.*)};
Expand Down
Loading

0 comments on commit 3642186

Please sign in to comment.