Commit 83fae2e7 authored by Francesc Guasch's avatar Francesc Guasch
Browse files

changed dopmains to machines, show only known domains

parent eba406cc
......@@ -132,7 +132,7 @@ sub remove_domain {
my $self = shift;
my $name = shift or confess "Missing domain name";
my $domain = $self->search_domain($name)
my $domain = $self->search_domain($name, 1)
or confess "ERROR: I can't find domain $name";
$domain->remove();
}
......@@ -146,11 +146,17 @@ sub remove_domain {
sub search_domain {
my $self = shift;
my $name = shift;
my $import = shift;
for my $vm (@{$self->vm}) {
my $domain = $vm->search_domain($name);
return $domain if $domain;
return if !$domain;
my $id;
eval { $id = $domain->id };
# TODO import the domain in the database with an _insert_db or something
return $domain if $id || $import;
}
return;
}
=head2 search_domain_by_id
......@@ -189,6 +195,31 @@ sub list_domains {
return @domains;
}
=head2 list_domains_data
List all domains in raw format. Return a list of id => { name , id , is_active , is_base }
my $list = $ravada->list_domains_data();
$c->render(json => $list);
=cut
sub list_domains_data {
my $self = shift;
my @domains;
for ($self->list_domains()) {
push @domains,{ $_->id => { id => $_->id
, name => $_->name
,is_base => $_->is_base
,is_active => $_->is_active
}
}
}
}
=head2 list_bases
List all base domains
......@@ -328,10 +359,6 @@ sub _cmd_create {
my $domain;
eval {$domain = $self->create_domain(%{$request->args}) };
if ($request->args('is_base') ) {
$request->status('preparing base');
$domain->prepare_base();
}
$request->status('done');
$request->error($@);
......
......@@ -29,8 +29,13 @@ has 'timeout_shutdown' => (
);
##################################################################################3
#
our $CONNECTOR = \$Ravada::CONNECTOR;
##################################################################################3
#
sub id {
return $_[0]->_data('id');
......@@ -42,7 +47,6 @@ sub file_base_img {
return $file ;
}
##################################################################################
sub _data {
......@@ -84,7 +88,7 @@ sub _select_domain_db {
}
}
my $sth = $self->connector->dbh->prepare(
my $sth = $$CONNECTOR->dbh->prepare(
"SELECT * FROM domains WHERE ".join(",",map { "$_=?" } sort keys %args )
);
$sth->execute(map { $args{$_} } sort keys %args);
......@@ -102,7 +106,7 @@ sub _prepare_base_db {
if (!$self->_select_domain_db) {
$self->_insert_db( name => $self->name );
}
my $sth = $self->connector->dbh->prepare(
my $sth = $$CONNECTOR->dbh->prepare(
"UPDATE domains set is_base='y',file_base_img=? "
." WHERE id=?"
);
......@@ -120,7 +124,7 @@ sub _insert_db {
."(" . join(",",sort keys %field )." )"
." VALUES (". join(",", map { '?' } keys %field )." ) "
;
my $sth = $self->connector->dbh->prepare($query);
my $sth = $$CONNECTOR->dbh->prepare($query);
eval { $sth->execute( map { $field{$_} } sort keys %field ) };
if ($@) {
warn "$query\n".Dumper(\%field);
......@@ -134,7 +138,7 @@ sub _remove_domain_db {
my $self = shift;
$self->_select_domain_db or return;
my $sth = $self->connector->dbh->prepare("DELETE FROM domains "
my $sth = $$CONNECTOR->dbh->prepare("DELETE FROM domains "
." WHERE id=?");
$sth->execute($self->id);
$sth->finish;
......
......@@ -23,15 +23,10 @@ has 'storage' => (
,required => 1
);
has 'connector' => (
is => 'ro'
,isa => 'DBIx::Connector'
,required => 1
);
##################################################
#
our $TIMEOUT_SHUTDOWN = 60;
our $CONNECTOR = \$Ravada::CONNECTOR;
##################################################
......@@ -292,4 +287,8 @@ Pauses the domain
sub pause {
}
#sub BUILD {
# warn "Builder KVM.pm";
#}
1;
......@@ -21,11 +21,6 @@ with 'Ravada::VM';
##########################################################################
#
has connector => (
is => 'ro'
,isa => 'DBIx::Connector'
,required => 1
);
has vm => (
isa => 'Sys::Virt'
......@@ -56,8 +51,12 @@ our $IP = _init_ip();
#
our ($DOWNLOAD_FH, $DOWNLOAD_TOTAL);
our $CONNECTOR = \$Ravada::CONNECTOR;
##########################################################################
sub BUILD {
}
sub connect {
my $self = shift;
......@@ -118,16 +117,13 @@ sub create_domain {
if !$args{id_iso} && !$args{id_base};
my $domain;
my @fields = ( name => $args{name} );
if ($args{id_iso}) {
$domain = $self->_domain_create_from_iso(@_);
} elsif($args{id_base}) {
$domain = $self->_domain_create_from_base(@_);
push @fields, ( id_base => $args{id_base} );
} else {
confess "TODO";
}
$domain->_insert_db(@fields);
return $domain;
}
......@@ -145,12 +141,19 @@ sub search_domain {
my $name = shift;
for ($self->vm->list_all_domains()) {
return Ravada::Domain::KVM->new(
next if $_->get_name ne $name;
my $domain;
eval {
$domain = Ravada::Domain::KVM->new(
domain => $_
,storage => $self->storage_pool
,connector => $self->connector
) if $_->get_name eq $name;
);
};
warn $@ if $@;
return $domain if $domain;
}
return;
}
=head2 search_domain_by_id
......@@ -165,7 +168,7 @@ sub search_domain_by_id {
my $self = shift;
my $id = shift;
my $sth = $self->connector->dbh->prepare("SELECT name FROM domains "
my $sth = $$CONNECTOR->dbh->prepare("SELECT name FROM domains "
." WHERE id=?");
$sth->execute($id);
my ($name) = $sth->fetchrow;
......@@ -187,12 +190,14 @@ sub list_domains {
my @list;
for my $name ($self->vm->list_all_domains()) {
push @list, (Ravada::Domain::KVM->new(
my $domain ;
eval { $domain = Ravada::Domain::KVM->new(
domain => $name
,storage => $self->storage_pool
,connector => $self->connector
)
);
);
$domain->id();
};
push @list,($domain) if $domain;
}
return @list;
}
......@@ -271,9 +276,9 @@ sub _domain_create_from_iso {
my $dom = $self->vm->define_domain($xml->toString());
$dom->create if $args{active};
return Ravada::Domain::KVM->new(domain => $dom , storage => $self->storage_pool
,connector => $self->connector
);
my $domain = Ravada::Domain::KVM->new(domain => $dom , storage => $self->storage_pool);
$domain->_insert_db(name => $args{name});
return $domain;
}
sub _create_disk {
return _create_disk_qcow2(@_);
......@@ -323,7 +328,7 @@ sub _search_domain_by_id {
my $self = shift;
my $id = shift;
my $sth = $self->connector->dbh->prepare("SELECT * FROM domains WHERE id=?");
my $sth = $$CONNECTOR->dbh->prepare("SELECT * FROM domains WHERE id=?");
$sth->execute($id);
my $row = $sth->fetchrow_hashref;
$sth->finish;
......@@ -364,11 +369,10 @@ sub _domain_create_from_base {
my $dom = $self->vm->define_domain($xml->toString());
$dom->create;
return Ravada::Domain::KVM->new(domain => $dom , storage => $self->storage_pool
,connector => $self->connector
);
my $domain = Ravada::Domain::KVM->create(domain => $dom , storage => $self->storage_pool);
$domain->_insert_db(name => $args{name}, id_base => $base->id);
return $domain;
}
sub _iso_name {
......@@ -441,7 +445,7 @@ sub _search_iso {
my $self = shift;
my $id_iso = shift or croak "Missing id_iso";
my $sth = $self->connector->dbh->prepare("SELECT * FROM iso_images WHERE id = ?");
my $sth = $$CONNECTOR->dbh->prepare("SELECT * FROM iso_images WHERE id = ?");
$sth->execute($id_iso);
my $row = $sth->fetchrow_hashref;
die "Missing iso_image id=$id_iso" if !keys %$row;
......
var app = angular.module("ravada.app",[]);
app.controller("new_base",[ '$scope', '$http', function($scope, $http) {
app.controller("new_machine",[ '$scope', '$http', function($scope, $http) {
$http.get('/list_images.json').then(function(response) {
$scope.images = response.data;
......@@ -20,11 +20,11 @@ var app = angular.module("ravada.app",[]);
});
app.directive("solShowNewbase",function() {
app.directive("solShowNewmachine",function() {
return {
restrict: "E",
templateUrl: '/templates/new_base.html',
templateUrl: '/templates/new_machine.html',
};
});
......
<h2>New Base</h2>
<form name="new_base" method="post" action="#"
ng-controller="new_base"
<form name="new_machine" method="post" action="#"
ng-controller="new_machine"
novalidate
>
<label for "name">Name</label><input type="text" size="20" name="name" ><br/>
<label for="id_base">ISO image</label>
<label for="id_iso">ISO image</label>
<select name ="id_iso"
ng-model="id_iso"
ng-options="item.id as item.name for item in images"
......
......@@ -72,27 +72,19 @@ get '/ip/*' => sub {
return quick_start_domain($c,$base->id,$ip);
};
any '/bases' => sub {
any '/machines' => sub {
my $c = shift;
return access_denied($c) if !_logged_in($c);
return bases($c);
return domains($c);
};
any '/bases/new' => sub {
any '/machines/new' => sub {
my $c = shift;
return access_denied($c) if !_logged_in($c);
return new_base($c);
};
any '/domains' => sub {
my $c = shift;
return access_denied($c) if !_logged_in($c);
return domains($c);
return new_machine($c);
};
any '/users' => sub {
......@@ -113,7 +105,10 @@ get '/list_images.json' => sub {
$c->render(json => $RAVADA->list_images_data);
};
get '/list_domains.json' => sub {
my $c = shift;
$c->render(json => $RAVADA->list_domains_data);
};
###################################################
......@@ -216,19 +211,10 @@ sub show_failure {
#######################################################
sub bases {
my $c = shift;
my @bases = $RAVADA->list_bases();
$c->render(template => 'bootstrap/bases'
,bases => \@bases
);
}
sub domains {
my $c = shift;
my @domains = $RAVADA->list_domains();
$c->render(template => 'bootstrap/domains'
$c->render(template => 'bootstrap/machines'
,domains => \@domains
);
......@@ -244,7 +230,7 @@ sub users {
}
sub new_base {
sub new_machine {
my $c = shift;
my @error = ();
my $ram = ($c->param('ram') or 2);
......@@ -252,7 +238,7 @@ sub new_base {
if ($c->param('submit')) {
push @error,("Name is mandatory") if !$c->param('name');
if (!@error) {
my $domain = req_new_base($c);
my $domain = req_new_domain($c);
if ($domain) {
return show_link($c, $domain);
} else {
......@@ -263,7 +249,7 @@ sub new_base {
my @images = $RAVADA->list_images();
warn join("\n",@error) if @error;
$c->render(template => 'bootstrap/new_base'
$c->render(template => 'bootstrap/new_machine'
,name => $c->param('name')
,ram => $ram
,disk => $disk
......@@ -272,13 +258,12 @@ sub new_base {
);
};
sub req_new_base {
sub req_new_domain {
my $c = shift;
my $name = $c->param('name');
my $req = Ravada::Request->create_domain(
name => $name
,id_iso => $c->param('id_iso')
,is_base => 1
);
wait_request_done($c,$req);
......
......@@ -8,4 +8,3 @@ CREATE TABLE `users` (
UNIQUE KEY `name` (`name`)
);
INSERT INTO users (name,password,change_password) VALUES ( 'root',password('root'),'y');
......@@ -75,7 +75,6 @@ sub test_req_create_base {
my $req = Ravada::Request->create_domain(
name => $name
,id_iso => 1
,is_base => 1
);
ok($req);
ok($req->status);
......@@ -96,6 +95,7 @@ sub test_req_create_base {
my $domain = $ravada->search_domain($name);
ok($domain,"I can't find domain $name");
$domain->prepare_base();
ok($domain && $domain->is_base,"Domain $name should be base");
return $domain;
}
......
......@@ -12,7 +12,7 @@ use_ok('Ravada::Domain::KVM');
my $test = Test::SQL::Data->new( config => 't/etc/ravada.conf');
my $ravada = Ravada->new( connector => $test->connector);
my $cont = 0;
my $CONT= 0;
sub test_vm_kvm {
my $vm = $ravada->vm->[0];
......@@ -27,7 +27,7 @@ sub test_remove_domain {
my $name = shift;
my $domain;
$domain = $ravada->search_domain($name);
$domain = $ravada->search_domain($name,1);
if ($domain) {
diag("Removing domain $name");
......@@ -46,7 +46,7 @@ sub test_remove_domain_by_name {
diag("Removing domain $name");
$ravada->remove_domain($name);
my $domain = $ravada->search_domain($name);
my $domain = $ravada->search_domain($name, 1);
die "I can't remove old domain $name"
if $domain;
......@@ -65,7 +65,7 @@ sub test_new_domain {
my $active = shift;
my ($name) = $0 =~ m{.*/(.*)\.t};
$name .= "_".$cont++;
$name .= "_".$CONT++;
test_remove_domain($name);
......@@ -114,9 +114,6 @@ sub test_domain{
my $active = shift;
$active = 1 if !defined $active;
my ($name) = $0 =~ m{.*/(.*)\.t};
test_remove_domain($name);
my $n_domains = scalar $ravada->list_domains();
my $domain = test_new_domain($active);
......@@ -138,10 +135,48 @@ sub test_domain{
ok(!$domain->is_active,"domain should be inactive") if defined $active && $active==0;
ok($domain->is_active,"domain should active") if defined $active && $active==1;
ok(test_domain_in_virsh($domain->name,$domain->name)," not in virsh list all");
my $vm_domain;
eval { $vm_domain = $ravada->vm->[0]->vm->get_domain_by_name($domain->name)};
ok($vm_domain,"Domain ".$domain->name." missing in VM") or exit;
test_remove_domain($domain->name);
}
}
sub test_domain_in_virsh {
my $name = shift;
for my $vm_domain ($ravada->vm->[0]->vm->list_all_domains) {
return 1 if $vm_domain->get_name eq $name;
}
return 0;
}
sub test_domain_missing_in_db {
# test when a domain is in the VM but not in the DB
my $active = shift;
$active = 1 if !defined $active;
my $n_domains = scalar $ravada->list_domains();
my $domain = test_new_domain($active);
if (ok($domain,"test domain not created")) {
my $sth = $test->connector->dbh->prepare("DELETE FROM domains WHERE id=?");
$sth->execute($domain->id);
my $domain2 = $ravada->search_domain($domain->name);
ok(!$domain2,"This domain should not show up in Ravada, it's not in the DB");
my $vm_domain;
eval { $vm_domain = $ravada->vm->[0]->vm->get_domain_by_name($domain->name)};
ok($vm_domain,"I can't find the domain in the VM") or return;
test_remove_domain($domain->name);
}
}
sub test_domain_by_name {
my $domain = test_new_domain();
......@@ -181,6 +216,7 @@ sub remove_old_domains {
test_vm_kvm();
remove_old_domains();
test_domain_missing_in_db();
test_domain_inactive();
test_domain();
test_domain_by_name();
......
......@@ -6,10 +6,10 @@
%= include 'bootstrap/navigation'
<div class="container theme-showcase" role="main">
<div class="jumbotron">
<h2>Bases</h2>
<a href="/bases/new">New Base</a><br/>
<h2>Virtual Machines</h2>
<a href="/machines/new">New Machine</a><br/>
<ul>
% for (@$bases) {
% for (@$domains) {
<li><%= $_->name %></li>
% }
</ul>
......
......@@ -13,8 +13,7 @@
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><i class="fa fa-wrench"></i>&nbsp;Tools<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="/bases">Bases</a></li>
<li><a href="/domains">Domains</a></li>
<li><a href="/machines">Virtual Machines</a></li>
<li><a href="/users">Users</a></li>
</ul>
</li>
......@@ -31,4 +30,4 @@
</ul>
</nav>
\ No newline at end of file
</nav>
......@@ -8,8 +8,8 @@
<div id="page-wrapper">
<div class="container-fluid">
<div class="row">
<div ng-controller="new_base">
<sol-show-newbase></sol-show-newbase>
<div ng-controller="new_machine">
<sol-show-newmachine></sol-show-newmachine>
</div>
<%= join("<br/>",@$error ) %>
</div>
......
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