Commit c91cfe4e authored by Francesc Guasch's avatar Francesc Guasch
Browse files

[#65] Replace all the global db by the singleton

parent c7e62be1
......@@ -105,7 +105,7 @@ sub clean_killed_requests {
sub start {
{
my $ravada = Ravada->new( config => $FILE_CONFIG );
$Ravada::CONNECTOR->dbh;
$ravada->connector->dbh;
}
for (;;) {
my $pid = fork();
......
......@@ -22,13 +22,10 @@ use Ravada::Domain::Driver;
use Ravada::Utils;
our $TIMEOUT_SHUTDOWN = 20;
our $CONNECTOR;
our $MIN_FREE_MEMORY = 1024*1024;
our $IPTABLES_CHAIN = 'RAVADA';
_init_connector();
requires 'name';
requires 'remove';
requires 'display';
......@@ -94,6 +91,12 @@ has '_vm' => (
,required => 1
);
has 'connector' => (
is => 'rw'
,builder => 'Ravada::_connect_dbh'
);
##################################################################################3
#
......@@ -328,13 +331,6 @@ sub _allowed {
}
##################################################################################3
sub _init_connector {
return if $CONNECTOR && $$CONNECTOR;
$CONNECTOR = \$Ravada::CONNECTOR if $Ravada::CONNECTOR;
$CONNECTOR = \$Ravada::Front::CONNECTOR if !defined $$CONNECTOR
&& defined $Ravada::Front::CONNECTOR;
}
=head2 id
Returns the id of the domain
my $id = $domain->id();
......@@ -352,8 +348,6 @@ sub _data {
my $self = shift;
my $field = shift or confess "Missing field name";
_init_connector();
return $self->{_data}->{$field} if exists $self->{_data}->{$field};
$self->{_data} = $self->_select_domain_db( name => $self->name);
......@@ -391,8 +385,6 @@ sub _select_domain_db {
my $self = shift;
my %args = @_;
_init_connector();
if (!keys %args) {
my $id;
eval { $id = $self->id };
......@@ -403,7 +395,7 @@ sub _select_domain_db {
}
}
my $sth = $$CONNECTOR->dbh->prepare(
my $sth = $self->_dbh->prepare(
"SELECT * FROM domains WHERE ".join(",",map { "$_=?" } sort keys %args )
);
$sth->execute(map { $args{$_} } sort keys %args);
......@@ -422,7 +414,7 @@ sub _prepare_base_db {
confess "CRITICAL: The data should be already inserted";
# $self->_insert_db( name => $self->name, id_owner => $self->id_owner );
}
my $sth = $$CONNECTOR->dbh->prepare(
my $sth = $self->_dbh->prepare(
"INSERT INTO file_base_images "
." (id_domain , file_base_img, target )"
." VALUES(?,?,?)"
......@@ -434,7 +426,7 @@ sub _prepare_base_db {
}
$sth->finish;
$sth = $$CONNECTOR->dbh->prepare(
$sth = $self->_dbh->prepare(
"UPDATE domains SET is_base=1 "
." WHERE id=?");
$sth->execute($self->id);
......@@ -447,8 +439,6 @@ sub _insert_db {
my $self = shift;
my %field = @_;
_init_connector();
for (qw(name id_owner)) {
confess "Field $_ is mandatory ".Dumper(\%field)
if !exists $field{$_};
......@@ -462,7 +452,7 @@ sub _insert_db {
."(" . join(",",sort keys %field )." )"
." VALUES (". join(",", map { '?' } keys %field )." ) "
;
my $sth = $$CONNECTOR->dbh->prepare($query);
my $sth = $self->_dbh->prepare($query);
eval { $sth->execute( map { $field{$_} } sort keys %field ) };
if ($@) {
#warn "$query\n".Dumper(\%field);
......@@ -495,7 +485,7 @@ sub _remove_domain_db {
return if !$self->is_known();
$self->_select_domain_db or return;
my $sth = $$CONNECTOR->dbh->prepare("DELETE FROM domains "
my $sth = $self->_dbh->prepare("DELETE FROM domains "
." WHERE id=?");
$sth->execute($self->id);
$sth->finish;
......@@ -514,7 +504,7 @@ sub _remove_id_base {
my $self = shift;
my $sth = $$CONNECTOR->dbh->prepare(
my $sth = $self->_dbh->prepare(
"UPDATE domains set id_base=NULL "
." WHERE id=?"
);
......@@ -533,7 +523,7 @@ sub is_base {
$self->_select_domain_db or return 0;
if (defined $value ) {
my $sth = $$CONNECTOR->dbh->prepare(
my $sth = $self->_dbh->prepare(
"UPDATE domains SET is_base=? "
." WHERE id=?");
$sth->execute($value, $self->id );
......@@ -556,9 +546,7 @@ Returns true if locked.
sub is_locked {
my $self = shift;
$self->_init_connector() if !defined $$CONNECTOR;
my $sth = $$CONNECTOR->dbh->prepare("SELECT id FROM requests "
my $sth = $self->_dbh->prepare("SELECT id FROM requests "
." WHERE id_domain=? AND status <> 'done'");
$sth->execute($self->id);
my ($id) = $sth->fetchrow;
......@@ -603,9 +591,7 @@ Returns a list of clones from this virtual machine
sub clones {
my $self = shift;
_init_connector();
my $sth = $$CONNECTOR->dbh->prepare("SELECT id, name FROM domains "
my $sth = $self->_dbh->prepare("SELECT id, name FROM domains "
." WHERE id_base = ?");
$sth->execute($self->id);
my @clones;
......@@ -624,8 +610,6 @@ Returns the number of clones from this virtual machine
sub has_clones {
my $self = shift;
_init_connector();
return scalar $self->clones;
}
......@@ -645,7 +629,7 @@ sub list_files_base {
return if $@ && $@ =~ /No DB info/i;
die $@ if $@;
my $sth = $$CONNECTOR->dbh->prepare("SELECT file_base_img, target "
my $sth = $self->_dbh->prepare("SELECT file_base_img, target "
." FROM file_base_images "
." WHERE id_domain=?");
$sth->execute($self->id);
......@@ -741,7 +725,7 @@ sub _post_remove_base_domain {}
sub _remove_base_db {
my $self = shift;
my $sth = $$CONNECTOR->dbh->prepare("DELETE FROM file_base_images "
my $sth = $self->_dbh->prepare("DELETE FROM file_base_images "
." WHERE id_domain=?");
$sth->execute($self->id);
......@@ -867,7 +851,7 @@ sub _remove_iptables {
my $ipt_obj = _obj_iptables();
my $sth = $$CONNECTOR->dbh->prepare(
my $sth = $self->_dbh->prepare(
"UPDATE iptables SET time_deleted=?"
." WHERE id=?"
);
......@@ -1027,7 +1011,7 @@ sub _log_iptable {
my $iptables = $args{iptables};
my $sth = $$CONNECTOR->dbh->prepare(
my $sth = $self->_dbh->prepare(
"INSERT INTO iptables "
."(id_domain, id_user, remote_ip, time_req, iptables)"
."VALUES(?, ?, ?, ?, ?)"
......@@ -1044,7 +1028,7 @@ sub _active_iptables {
confess "Missing \$user" if !$user;
my $sth = $$CONNECTOR->dbh->prepare(
my $sth = $self->_dbh->prepare(
"SELECT id,iptables FROM iptables "
." WHERE "
." id_domain=?"
......@@ -1074,7 +1058,7 @@ sub _rename_domain_db {
my $new_name = $args{name} or confess "Missing new name";
my $sth = $$CONNECTOR->dbh->prepare("UPDATE domains set name=?"
my $sth = $self->_dbh->prepare("UPDATE domains set name=?"
." WHERE id=?");
$sth->execute($new_name, $self->id);
$sth->finish;
......@@ -1096,9 +1080,8 @@ sub is_public {
my $self = shift;
my $value = shift;
_init_connector();
if (defined $value) {
my $sth = $$CONNECTOR->dbh->prepare("UPDATE domains set is_public=?"
my $sth = $self->_dbh->prepare("UPDATE domains set is_public=?"
." WHERE id=?");
$sth->execute($value, $self->id);
$sth->finish;
......@@ -1148,7 +1131,7 @@ sub _post_rename {
return if !defined $filename;
my $sth = $$CONNECTOR->dbh->prepare(
my $sth = $self->_dbh->prepare(
"UPDATE domains set file_screenshot=? "
." WHERE id=?"
);
......@@ -1170,14 +1153,12 @@ sub drivers {
my $name = shift;
my $type = (shift or $self->_vm->type);
_init_connector();
$type = 'qemu' if $type =~ /^KVM$/;
my $query = "SELECT id from domain_drivers_types "
." WHERE vm=?";
$query .= " AND name=?" if $name;
my $sth = $$CONNECTOR->dbh->prepare($query);
my $sth = Ravada::DB->instance->dbh->prepare($query);
my @sql_args = ($type);
push @sql_args,($name) if $name;
......@@ -1205,7 +1186,7 @@ sub set_driver_id {
my $self = shift;
my $id = shift;
my $sth = $$CONNECTOR->dbh->prepare(
my $sth = $self->_dbh->prepare(
"SELECT d.name,o.value "
." FROM domain_drivers_types d, domain_drivers_options o"
." WHERE d.id=o.id_driver_type "
......@@ -1223,7 +1204,7 @@ sub set_driver_id {
sub remote_ip {
my $self = shift;
my $sth = $$CONNECTOR->dbh->prepare(
my $sth = $self->_dbh->prepare(
"SELECT remote_ip FROM iptables "
." WHERE "
." id_domain=?"
......@@ -1239,8 +1220,7 @@ sub remote_ip {
sub _dbh {
my $self = shift;
_init_connector() if !$CONNECTOR || !$$CONNECTOR;
return $$CONNECTOR->dbh;
return $self->connector->dbh;
}
1;
......@@ -5,8 +5,6 @@ use strict;
use Moose;
_init_connector();
has 'domain' => (
isa => 'Any'
,is => 'ro'
......@@ -19,17 +17,9 @@ has 'id' => (
##############################################################################
our $CONNECTOR;
our $TABLE_DRIVERS = "domain_drivers_types";
our $TABLE_OPTIONS= "domain_drivers_options";
sub _init_connector {
return if $CONNECTOR && $$CONNECTOR;
$CONNECTOR = \$Ravada::CONNECTOR if $Ravada::CONNECTOR;
$CONNECTOR = \$Ravada::Front::CONNECTOR if !defined $$CONNECTOR
&& defined $Ravada::Front::CONNECTOR;
}
##############################################################################
sub get_value {
......@@ -46,8 +36,6 @@ sub _data {
my $self = shift;
my $field = shift or confess "Missing field name";
_init_connector();
return $self->{_data}->{$field} if exists $self->{_data}->{$field};
$self->{_data} = $self->_select_driver_db( id => $self->id);
......@@ -61,13 +49,11 @@ sub _select_driver_db {
my $self = shift;
my %args = @_;
_init_connector();
if (!keys %args) {
%args =( id => $self->id );
}
my $sth = $$CONNECTOR->dbh->prepare(
my $sth = Ravada::DB->instance->dbh->prepare(
"SELECT * FROM $TABLE_DRIVERS WHERE ".join(",",map { "$_=?" } sort keys %args )
);
$sth->execute(map { $args{$_} } sort keys %args);
......@@ -82,10 +68,9 @@ sub _select_driver_db {
sub get_options {
my $self = shift;
_init_connector();
my $query = "SELECT * from $TABLE_OPTIONS WHERE id_driver_type=? ORDER by name";
my $sth = $$CONNECTOR->dbh->prepare($query);
my $sth = Ravada::DB->instance->dbh->prepare($query);
$sth->execute($self->id);
my @ret;
......
......@@ -38,7 +38,6 @@ has 'fork' => (
,default => 1
);
our $CONNECTOR;# = \$Ravada::CONNECTOR;
our $TIMEOUT = 20;
our @VM_TYPES = ('KVM');
our $DIR_SCREENSHOTS = "/var/www/img/screenshots";
......@@ -55,12 +54,12 @@ Internal constructor
sub BUILD {
my $self = shift;
if ($self->connector) {
$CONNECTOR = $self->connector;
$self->connector(Ravada::DB->instance(connector => $self->connector));
} else {
Ravada::_init_config($self->config());
$CONNECTOR = Ravada::_connect_dbh();
$self->connector(Ravada::_connect_dbh());
}
$CONNECTOR->dbh();
$self->connector->dbh();
}
=head2 list_bases
......@@ -73,7 +72,7 @@ Returns a list of the base domains as a listref
sub list_bases {
my $self = shift;
my $sth = $CONNECTOR->dbh->prepare("SELECT * FROM domains where is_base=1");
my $sth = Ravada::DB->instance->dbh->prepare("SELECT * FROM domains where is_base=1");
$sth->execute();
my @bases = ();
......@@ -106,7 +105,7 @@ sub list_machines_user {
my $self = shift;
my $user = shift;
my $sth = $CONNECTOR->dbh->prepare(
my $sth = Ravada::DB->instance->dbh->prepare(
"SELECT id,name,is_public, file_screenshot"
." FROM domains "
." WHERE is_base=1"
......@@ -162,7 +161,7 @@ sub search_clone_data {
my $query = "SELECT * FROM domains WHERE "
.(join(" AND ", map { "$_ = ? " } sort keys %args));
my $sth = $CONNECTOR->dbh->prepare($query);
my $sth = Ravada::DB->instance->dbh->prepare($query);
$sth->execute( map { $args{$_} } sort keys %args );
my $row = $sth->fetchrow_hashref;
return ( $row or {});
......@@ -192,7 +191,7 @@ sub list_domains {
}
$where = "WHERE $where" if $where;
my $sth = $CONNECTOR->dbh->prepare("$query $where");
my $sth = Ravada::DB->instance->dbh->prepare("$query $where");
$sth->execute(map { $args{$_} } sort keys %args);
my @domains = ();
......@@ -246,7 +245,7 @@ sub domain_exists {
my $self = shift;
my $name = shift;
my $sth = $CONNECTOR->dbh->prepare(
my $sth = Ravada::DB->instance->dbh->prepare(
"SELECT id FROM domains "
." WHERE name=?"
);
......@@ -285,7 +284,7 @@ sub list_iso_images {
my $self = shift;
my @iso;
my $sth = $CONNECTOR->dbh->prepare(
my $sth = Ravada::DB->instance->dbh->prepare(
"SELECT * FROM iso_images ORDER BY name"
);
$sth->execute;
......@@ -307,7 +306,7 @@ sub list_lxc_templates {
my $self = shift;
my @template;
my $sth = $CONNECTOR->dbh->prepare(
my $sth = Ravada::DB->instance->dbh->prepare(
"SELECT * FROM lxc_templates ORDER BY name"
);
$sth->execute;
......@@ -327,7 +326,7 @@ Returns a reference to a list of the users
sub list_users {
my $self = shift;
my $sth = $CONNECTOR->dbh->prepare("SELECT * FROM users ");
my $sth = Ravada::DB->instance->dbh->prepare("SELECT * FROM users ");
$sth->execute();
my @users = ();
......@@ -494,7 +493,7 @@ sub search_clone {
confess "Unknown arguments ".Dumper(\%args) if keys %args;
my $sth = $CONNECTOR->dbh->prepare(
my $sth = Ravada::DB->instance->dbh->prepare(
"SELECT id,name FROM domains "
." WHERE id_base=? AND id_owner=? "
);
......@@ -524,7 +523,7 @@ sub search_domain {
my $name = shift;
my $sth = $CONNECTOR->dbh->prepare("SELECT * FROM domains WHERE name=?");
my $sth = Ravada::DB->instance->dbh->prepare("SELECT * FROM domains WHERE name=?");
$sth->execute($name);
my $row = $sth->fetchrow_hashref;
......@@ -545,7 +544,7 @@ Returns a list of ruquests : ( id , domain_name, status, error )
sub list_requests {
my $self = shift;
my $sth = $CONNECTOR->dbh->prepare("SELECT id, command, args, date_changed, status, error "
my $sth = Ravada::DB->instance->dbh->prepare("SELECT id, command, args, date_changed, status, error "
." FROM requests "
." WHERE command NOT IN (SELECT command FROM requests WHERE command = 'list_vm_types')"
." ORDER BY date_changed DESC LIMIT 4"
......@@ -574,7 +573,7 @@ sub search_domain_by_id {
my $self = shift;
my $id = shift;
my $sth = $CONNECTOR->dbh->prepare("SELECT * FROM domains WHERE id=?");
my $sth = Ravada::DB->instance->dbh->prepare("SELECT * FROM domains WHERE id=?");
$sth->execute($id);
my $row = $sth->fetchrow_hashref;
......@@ -639,7 +638,7 @@ sub list_bases_anonymous {
my $net = Ravada::Network->new(address => $ip);
my $sth = $CONNECTOR->dbh->prepare("SELECT * FROM domains where is_base=1 AND is_public=1");
my $sth = Ravada::DB->instance->dbh->prepare("SELECT * FROM domains where is_base=1 AND is_public=1");
$sth->execute();
my @bases = ();
......
......@@ -16,6 +16,7 @@ use Moose::Util::TypeConstraints;
use NetAddr::IP;
use Ravada::DB;
#########################################################
......@@ -23,10 +24,6 @@ has 'address' => ( is => 'ro', isa => NetAddrIP, coerce => 1 );
#########################################################
our $CONNECTOR;
#########################################################
=head1 Description
......@@ -35,11 +32,6 @@ our $CONNECTOR;
=cut
sub _init_connector {
$CONNECTOR = \$Ravada::CONNECTOR;
$CONNECTOR = \$Ravada::Front::CONNECTOR if !defined $$CONNECTOR;
}
=head2 allowed
Returns true if the IP is allowed to run a domain
......@@ -97,7 +89,7 @@ sub _allowed_domain {
my $self = shift;
my ($id_domain, $id_network) = @_;
my $sth = $$CONNECTOR->dbh->prepare("SELECT allowed FROM domains_network "
my $sth = Ravada::DB->instance->dbh->prepare("SELECT allowed FROM domains_network "
." WHERE id_domain=? AND id_network=? ");
$sth->execute($id_domain, $id_network);
my ($allowed) = $sth->fetchrow;
......@@ -110,7 +102,7 @@ sub _allowed_domain_anonymous {
my $self = shift;
my ($id_domain, $id_network) = @_;
my $sth = $$CONNECTOR->dbh->prepare("SELECT anonymous FROM domains_network "
my $sth = Ravada::DB->instance->dbh->prepare("SELECT anonymous FROM domains_network "
." WHERE id_domain=? AND id_network=? AND allowed=1");
$sth->execute($id_domain, $id_network);
my ($allowed) = $sth->fetchrow;
......@@ -123,9 +115,7 @@ sub _allowed_domain_anonymous {
sub list_networks {
my $self = shift;
_init_connector();
my $sth = $$CONNECTOR->dbh->prepare(
my $sth = Ravada::DB->instance->dbh->prepare(
"SELECT * "
." FROM networks "
." ORDER BY n_order"
......
......@@ -62,24 +62,12 @@ our %VALID_ARG = (
our %CMD_SEND_MESSAGE = map { $_ => 1 }
qw( create start shutdown prepare_base remove remove_base rename_domain screenshot);
our $CONNECTOR;
sub _init_connector {
$CONNECTOR = \$Ravada::CONNECTOR;
$CONNECTOR = \$Ravada::Front::CONNECTOR if !$$CONNECTOR;
}
=head2 BUILD
Internal object builder, do not call
=cut
sub BUILD {
_init_connector();
}
sub _request {
my $proto = shift;
my $class=ref($proto) || $proto;
......@@ -103,9 +91,7 @@ sub open {
my $id = shift or confess "Missing request id";
_init_connector() if !$CONNECTOR || !$$CONNECTOR;
my $sth = $$CONNECTOR->dbh->prepare("SELECT * FROM requests "
my $sth = Ravada::DB->instance->dbh->prepare("SELECT * FROM requests "
." WHERE id=?");
$sth->execute($id);
my $row = $sth->fetchrow_hashref;
......@@ -421,9 +407,8 @@ sub _new_request {
$args{at_time} = $args{args}->{at} if exists $args{args}->{at};
$args{args} = encode_json($args{args});
}
_init_connector() if !$CONNECTOR || !$$CONNECTOR;
my $sth = $$CONNECTOR->dbh->prepare(
my $sth = Ravada::DB->instance->dbh->prepare(
"INSERT INTO requests (".join(",",sort keys %args).")"
." VALUES ( "
.join(",", map { '?' } keys %args)
......@@ -438,7 +423,7 @@ sub _new_request {
}
sub _last_insert_id {
my $driver = $$CONNECTOR->dbh->{Driver}->{Name};
my $driver = Ravada::DB->instance->dbh->{Driver}->{Name};
if ( $driver =~ /sqlite/i ) {
return _last_insert_id_sqlite(@_);
......@@ -451,7 +436,7 @@ sub _last_insert_id {
sub _last_insert_id_mysql {
my $self = shift;
my $sth = $$CONNECTOR->dbh->prepare("SELECT last_insert_id()");
my $sth = Ravada::DB->instance->dbh->prepare("SELECT last_insert_id()");
$sth->execute;
my ($id) = $sth->fetchrow;
$sth->finish;
......@@ -462,7 +447,7 @@ sub _last_insert_id_mysql {
sub _last_insert_id_sqlite {
my $self = shift;
my $sth = $$CONNECTOR->dbh->prepare("SELECT last_insert_rowid()");
my $sth = Ravada::DB->instance->dbh->prepare("SELECT last_insert_rowid()");
$sth->execute;
my ($id) = $sth->fetchrow;
$sth->finish;
......@@ -485,7 +470,7 @@ sub status {
my $message = shift;
if (!defined $status) {
my $sth = $$CONNECTOR->dbh->prepare("SELECT * FROM requests "
my $sth = Ravada::DB->instance->dbh->prepare("SELECT * FROM requests "
." WHERE id=?");
$sth->execute($self->{id});
my $row = $sth->fetchrow_hashref;
......@@ -494,7 +479,7 @@ sub status {
return ($row->{status} or 'unknown');
}
my $sth = $$CONNECTOR->dbh->prepare("UPDATE requests set status=? "
my $sth = Ravada::DB->instance->dbh->prepare("UPDATE requests set status=? "
." WHERE id=?");
$sth->execute($status, $self->{id});
$sth->finish;
......@@ -508,7 +493,7 @@ sub _search_domain_name {
my $self = shift;
my $domain_id = shift;
my $sth = $$CONNECTOR->dbh->prepare("SELECT name FROM domains where id=?");
my $sth = Ravada::DB->instance->dbh->prepare("SELECT name FROM domains where id=?");
$sth->execute($domain_id);
return $sth->fetchrow;
}
......@@ -538,7 +523,7 @@ sub _send_message {
$subject = $message if $message && $self->status eq 'done'
&& length ($message)<60;