VM.pm 37.2 KB
Newer Older
1
2
3
4
use warnings;
use strict;

package Ravada::VM;
5

6
7
8
9
10
=head1 NAME

Ravada::VM - Virtual Managers library for Ravada

=cut
11

Francesc Guasch's avatar
Francesc Guasch committed
12
use Carp qw( carp croak cluck);
13
use Data::Dumper;
14
use File::Path qw(make_path);
15
use Hash::Util qw(lock_hash);
16
use IPC::Run3 qw(run3);
17
use JSON::XS;
18
19
20
use Socket qw( inet_aton inet_ntoa );
use Moose::Role;
use Net::DNS;
Francesc Guasch's avatar
Francesc Guasch committed
21
use Net::Ping;
Francesc Guasch's avatar
Francesc Guasch committed
22
use Net::SSH2 qw(LIBSSH2_FLAG_SIGPIPE);
23
24
25
26
use IO::Socket;
use IO::Interface;
use Net::Domain qw(hostfqdn);

27
28
use Ravada::Utils;

Francesc Guasch's avatar
Francesc Guasch committed
29
30
31
no warnings "experimental::signatures";
use feature qw(signatures);

32
33
34
35
36
37
38
39
40
requires 'connect';

# global DB Connection

our $CONNECTOR = \$Ravada::CONNECTOR;
our $CONFIG = \$Ravada::CONFIG;

our $MIN_MEMORY_MB = 128 * 1024;

Francesc Guasch's avatar
Francesc Guasch committed
41
our $SSH_TIMEOUT = 20 * 1000;
42
43
our $CACHE_TIMEOUT = 60;
our $FIELD_TIMEOUT = '_data_timeout';
Francesc Guasch's avatar
Francesc Guasch committed
44

Francesc Guasch's avatar
Francesc Guasch committed
45
our %VM; # cache Virtual Manager Connection
46
our %SSH;
Francesc Guasch's avatar
Francesc Guasch committed
47
48
49
50

our $ARP = `which arp`;
chomp $ARP;

51
52
53
54
55
56
57
58
# domain
requires 'create_domain';
requires 'search_domain';

requires 'list_domains';

# storage volume
requires 'create_volume';
Francesc Guasch's avatar
Francesc Guasch committed
59
requires 'list_storage_pools';
60
61
62

requires 'connect';
requires 'disconnect';
63
requires 'import_domain';
64

65
requires 'is_alive';
66
67

requires 'free_memory';
Francesc Guasch's avatar
Francesc Guasch committed
68
requires 'free_disk';
Francesc Guasch's avatar
Francesc Guasch committed
69
70
71

requires '_fetch_dir_cert';

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
############################################################

has 'host' => (
          isa => 'Str'
         , is => 'ro',
    , default => 'localhost'
);

has 'default_dir_img' => (
      isa => 'String'
     , is => 'ro'
);

has 'readonly' => (
    isa => 'Str'
    , is => 'ro'
    ,default => 0
);
90

Francesc Guasch's avatar
Francesc Guasch committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
has 'tls_host_subject' => (
    isa => 'Str'
    , is => 'ro'
    , builder => '_fetch_tls_host_subject'
    , lazy => 1
);

has 'tls_ca' => (
    isa => 'Str'
    , is => 'ro'
    , builder => '_fetch_tls_ca'
    , lazy => 1
);

has dir_cert => (
    isa => 'Str'
    ,is => 'ro'
    ,lazy => 1
    ,builder => '_fetch_dir_cert'
);

Francesc Guasch's avatar
Francesc Guasch committed
112
113
114
115
116
has 'store' => (
    isa => 'Bool'
    , is => 'rw'
    , default => 1
);
117
118
119
120
121
122
123
############################################################
#
# Method Modifiers definition
# 
#
around 'create_domain' => \&_around_create_domain;

124
125
before 'search_domain' => \&_pre_search_domain;
before 'list_domains' => \&_pre_list_domains;
126
127
128

before 'create_volume' => \&_connect;

129
around 'import_domain' => \&_around_import_domain;
Francesc Guasch's avatar
Francesc Guasch committed
130

Francesc Guasch's avatar
Francesc Guasch committed
131
around 'ping' => \&_around_ping;
Francesc Guasch's avatar
Francesc Guasch committed
132
around 'connect' => \&_around_connect;
Francesc Guasch's avatar
Francesc Guasch committed
133

134
135
136
137
#############################################################
#
# method modifiers
#
138

139
140
141
142
143
144
145
sub _init_connector {
    return if $CONNECTOR && $$CONNECTOR;
    $CONNECTOR = \$Ravada::CONNECTOR if $Ravada::CONNECTOR;
    $CONNECTOR = \$Ravada::Front::CONNECTOR if !defined $$CONNECTOR
                                                && defined $Ravada::Front::CONNECTOR;
}

146
147
148
149
sub _dbh($self) {
    return $$CONNECTOR->dbh();
}

150
151
152
153
154
155
156
157
158
159
160
161
=head1 Constructors

=head2 open

Opens a Virtual Machine Manager (VM)

Arguments: id of the VM

=cut

sub open {
    my $proto = shift;
162
163
164
165
166
167
168
169
170
    my %args;
    if (!scalar @_ % 2) {
        %args = @_;
        confess "ERROR: Don't set the id and the type "
            if $args{id} && $args{type};
        return _open_type($proto,@_) if $args{type};
    } else {
        $args{id} = shift;
    }
171
172
173
174
    my $class=ref($proto) || $proto;

    my $self = {};
    bless($self, $class);
175
    my $row = $self->_do_select_vm_db( id => $args{id});
176
    lock_hash(%$row);
177
    confess "ERROR: I can't find VM id=$args{id}" if !$row || !keys %$row;
178

179
180
181
182
    if ( $VM{$args{id}} && $VM{$args{id}}->name eq $row->{name} ) {
        my $vm = $VM{$args{id}};
        return _clean($vm);
    }
Francesc Guasch's avatar
Francesc Guasch committed
183

184
185
186
187
188
    my $type = $row->{vm_type};
    $type = 'KVM'   if $type eq 'qemu';
    $class .= "::$type";
    bless ($self,$class);

189
    $args{host} = $row->{hostname};
190
    $args{security} = decode_json($row->{security}) if $row->{security};
191

Francesc Guasch's avatar
Francesc Guasch committed
192
193
194
    my $vm = $self->new(%args);
    $VM{$args{id}} = $vm;
    return $vm;
195
196
197

}

Francesc Guasch's avatar
Francesc Guasch committed
198
199
200
201
sub _clean_cache {
    %VM = ();
}

202
203
sub BUILD {
    my $self = shift;
204
205
206

    my $args = $_[0];

207
208
209
    my $id = delete $args->{id};
    my $host = delete $args->{host};
    my $name = delete $args->{name};
Francesc Guasch's avatar
Francesc Guasch committed
210
211
    my $store = delete $args->{store};
    $store = 1 if !defined $store;
212
    my $public_ip = delete $args->{public_ip};
Francesc Guasch's avatar
Francesc Guasch committed
213

214
215
216
217
218
219
    delete $args->{readonly};
    delete $args->{security};

    # TODO check if this is needed
    delete $args->{connector};

Francesc Guasch's avatar
Francesc Guasch committed
220
221
    lock_hash(%$args);

222
    confess "ERROR: Unknown args ".join (",", keys (%$args)) if keys %$args;
Francesc Guasch's avatar
Francesc Guasch committed
223
    return if !$store;
224
225
    if ($id) {
        $self->_select_vm_db(id => $id)
226
227
    } else {
        my %query = (
Francesc Guasch's avatar
Francesc Guasch committed
228
            hostname => ($host or 'localhost')
229
230
            ,vm_type => $self->type
        );
Francesc Guasch's avatar
Francesc Guasch committed
231
        $query{name} = $name  if $name;
232
        $query{public_ip} = $public_ip if defined $public_ip;
233
234
        $self->_select_vm_db(%query);
    }
235
    $self->id;
Francesc Guasch's avatar
Francesc Guasch committed
236

237
238
}

239
240
241
242
243
244
245
246
247
248
sub _open_type {
    my $self = shift;
    my %args = @_;

    my $type = delete $args{type} or confess "ERROR: Missing VM type";
    my $class = "Ravada::VM::$type";

    my $proto = {};
    bless $proto,$class;

249
    my $vm = $proto->new(%args);
250
251
252
253
    eval { $vm->vm };
    warn $@ if $@;

    return $vm;
254
255
256

}

257
258
259
260
261
262
263
264
265
sub _check_readonly {
    my $self = shift;
    confess "ERROR: You can't create domains in read-only mode "
        if $self->readonly 

}

sub _connect {
    my $self = shift;
Francesc Guasch's avatar
Francesc Guasch committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
    my $result = $self->connect();
    if ($result) {
        $self->is_active(1);
    } else {
        $self->is_active(0);
    }
    return $result;
}

sub _around_connect($orig, $self) {
    my $result = $self->$orig();
    if ($result) {
        $self->is_active(1);
    } else {
        $self->is_active(0);
    }
    return $result;
283
284
285
286
287
288
289
}

sub _pre_create_domain {
    _check_create_domain(@_);
    _connect(@_);
}

290
291
292
293
294
295
296
297
298
299
sub _pre_search_domain($self,@) {
    $self->_connect();
    die "ERROR: VM ".$self->name." unavailable" if !$self->ping();
}

sub _pre_list_domains($self,@) {
    $self->_connect();
    die "ERROR: VM ".$self->name." unavailable" if !$self->ping();
}

300
301
sub _connect_ssh($self, $disconnect=0) {
    confess "Don't connect to local ssh"
Francesc Guasch's avatar
Francesc Guasch committed
302
303
        if $self->is_local;

304
    if ( $self->readonly ) {
305
        warn $self->name." readonly, don't do ssh";
306
307
        return;
    }
Francesc Guasch's avatar
Francesc Guasch committed
308
309
    return if !$self->ping();

310
311
312
    my @pwd = getpwuid($>);
    my $home = $pwd[7];

313
314
315
316
317
    my $ssh= $self->{_ssh};
    $ssh = $SSH{$self->host}    if exists $SSH{$self->host};

    if (! $ssh || $disconnect ) {
        $ssh->disconnect if $ssh && $disconnect;
Francesc Guasch's avatar
Francesc Guasch committed
318
        $ssh = Net::SSH2->new( timeout => $SSH_TIMEOUT );
319
320
        my $connect;
        for ( 1 .. 3 ) {
321
            eval { $connect = $ssh->connect($self->host) };
322
323
324
325
            last if $connect;
            warn "RETRYING ssh ".$self->host." ".join(" ",$ssh->error);
            sleep 1;
        }
326
327
328
329
330
331
332
        if ( !$connect) {
            eval { $connect = $ssh->connect($self->host) };
            if (!$connect) {
                $self->_cached_active(0);
                confess $ssh->error();
            }
        }
333
334
335
336
337
338
        $ssh->auth_publickey( 'root'
            , "$home/.ssh/id_rsa.pub"
            , "$home/.ssh/id_rsa"
        ) or $ssh->die_with_error();
        $self->{_ssh} = $ssh;
        $SSH{$self->host} = $ssh;
Francesc Guasch's avatar
Francesc Guasch committed
339
    }
340
    return $ssh;
341
342
}

343
sub _ssh_channel($self) {
344
    my $ssh = $self->_connect_ssh() or confess "ERROR: I can't connect to SSH in ".$self->host;
345
346
347
348
349
    my $ssh_channel;
    for ( 1 .. 5 ) {
        $ssh_channel = $ssh->channel();
        last if $ssh_channel;
        sleep 1;
350
    }
351
352
353
    if (!$ssh_channel) {
        $ssh = $self->_connect_ssh(1);
        $ssh_channel = $ssh->channel();
Francesc Guasch's avatar
Francesc Guasch committed
354
    }
355
356
357
    die $ssh->die_with_error    if !$ssh_channel;
    $ssh->blocking(1);
    return $ssh_channel;
Francesc Guasch's avatar
Francesc Guasch committed
358
359
}

360
361
362
363
sub _around_create_domain {
    my $orig = shift;
    my $self = shift;
    my %args = @_;
364
    my $remote_ip = delete $args{remote_ip};
Francesc Guasch's avatar
Francesc Guasch committed
365
    my $add_to_pool = delete $args{add_to_pool};
366
    my %args_create = %args;
367

368
    my $id_owner = delete $args{id_owner} or confess "ERROR: Missing id_owner";
Francesc Guasch's avatar
Francesc Guasch committed
369
    my $owner = Ravada::Auth::SQL->search_by_id($id_owner) or confess "Unknown user id: $id_owner";
370
371

    my $base;
Francesc Guasch's avatar
Francesc Guasch committed
372
    my $volatile = delete $args{volatile};
373
    my $id_base = delete $args{id_base};
Francesc Guasch's avatar
Francesc Guasch committed
374
375
376
377
     my $id_iso = delete $args{id_iso};
     my $active = delete $args{active};
       my $name = delete $args{name};
       my $swap = delete $args{swap};
Francesc Guasch's avatar
Francesc Guasch committed
378
       my $from_pool = delete $args{from_pool};
Francesc Guasch's avatar
Francesc Guasch committed
379

380
     # args get deleted but kept on %args_create so when we call $self->$orig below are passed
Francesc Guasch's avatar
Francesc Guasch committed
381
382
383
384
     delete $args{disk};
     delete $args{memory};
     delete $args{request};
     delete $args{iso_file};
385
     delete $args{id_template};
386
     delete @args{'description','remove_cpu','vm','start'};
Francesc Guasch's avatar
Francesc Guasch committed
387
388
389

    confess "ERROR: Unknown args ".Dumper(\%args) if keys %args;

Francesc Guasch's avatar
Francesc Guasch committed
390
    $self->_check_duplicate_name($name);
391
392
393
    if ($id_base) {
        $base = $self->search_domain_by_id($id_base)
            or confess "Error: I can't find domain $id_base on ".$self->name;
Francesc Guasch's avatar
Francesc Guasch committed
394
        $volatile = 1 if $base->volatile_clones;
Francesc Guasch's avatar
Francesc Guasch committed
395
396
397
398
399
        if ($add_to_pool) {
            confess "Error: you can't add to pool and also pick from pool" if $from_pool;
            $from_pool = 0;
        }
        $from_pool = 1 if !defined $from_pool && $base->pools();
400
    }
Francesc Guasch's avatar
Francesc Guasch committed
401

402
403
404
405
406
407
408
    confess "ERROR: User ".$owner->name." is not allowed to create machines"
        unless $owner->is_admin
            || $owner->can_create_machine()
            || ($base && $owner->can_clone);

    confess "ERROR: Base ".$base->name." is private"
        if !$owner->is_admin && $base && !$base->is_public();
409

Francesc Guasch's avatar
Francesc Guasch committed
410
411
412
413
414
415
    if ($add_to_pool) {
        confess "Error: This machine can only be added to a pool if it is a clone"
            if !$base;
        confess("Error: Requested to add a clone for the pool but this base has no pools")
            if !$base->pools;
    }
416
417
418
    $args_create{listen_ip} = $self->listen_ip($remote_ip);
    $args_create{spice_password} = $self->_define_spice_password($remote_ip);
    $self->_pre_create_domain(%args_create);
419

Francesc Guasch's avatar
Francesc Guasch committed
420
421
    return $base->_search_pool_clone($owner) if $from_pool;

422
    my $domain = $self->$orig(%args_create, volatile => $volatile);
Francesc Guasch's avatar
Francesc Guasch committed
423
    $domain->add_volume_swap( size => $swap )   if $swap;
424

425
    if ($id_base) {
426
427
        $domain->run_timeout($base->run_timeout)
            if defined $base->run_timeout();
Francesc Guasch's avatar
Francesc Guasch committed
428
429
430
431
432
433

        for my $port ( $base->list_ports ) {
            my %port = %$port;
            delete @port{'id','id_domain','public_port'};
            $domain->expose(%port);
        }
434
    }
435
    my $user = Ravada::Auth::SQL->search_by_id($id_owner);
436
    $domain->is_volatile(1)     if $user->is_temporary() ||($base && $base->volatile_clones());
Francesc Guasch's avatar
Francesc Guasch committed
437
438
439
440
441

    my @start_args = ( user => $owner );
    push @start_args, (remote_ip => $remote_ip) if $remote_ip;

    $domain->_post_start(@start_args) if $domain->is_active;
Francesc Guasch's avatar
Francesc Guasch committed
442
    eval {
Francesc Guasch's avatar
Francesc Guasch committed
443
           $domain->start(@start_args)      if $active || ($domain->is_volatile && ! $domain->is_active);
Francesc Guasch's avatar
Francesc Guasch committed
444
445
    };
    die $@ if $@ && $@ !~ /code: 55,/;
446

Francesc Guasch's avatar
Francesc Guasch committed
447
    $domain->info($owner);
448
    $domain->display($owner)    if $domain->is_active;
Francesc Guasch's avatar
Francesc Guasch committed
449

Francesc Guasch's avatar
Francesc Guasch committed
450
    $domain->is_pool(1) if $add_to_pool;
451
452
453
    return $domain;
}

454
455
456
457
458
459
460
461
462
sub _define_spice_password($self, $remote_ip) {
    my $spice_password = Ravada::Utils::random_name(4);
    if ($remote_ip) {
        my $network = Ravada::Network->new(address => $remote_ip);
        $spice_password = undef if !$network->requires_password;
    }
    return $spice_password;
}

Francesc Guasch's avatar
Francesc Guasch committed
463
464
465
466
467
468
469
470
471
sub _check_duplicate_name($self, $name) {
    my $sth = $$CONNECTOR->dbh->prepare("SELECT id,name,vm FROM domains where name=?");
    $sth->execute($name);
    my $row = $sth->fetchrow_hashref;
    confess "Error: machine with name '$name' already exists ".Dumper($row)
        if $row->{id};
    return 1;
}

472
473
474
sub _around_import_domain {
    my $orig = shift;
    my $self = shift;
475
    my ($name, $user, $spinoff) = @_;
476

477
    my $domain = $self->$orig($name, $user);
478
479
480

    $domain->_insert_db(name => $name, id_owner => $user->id);

481
    if ($spinoff) {
482
483
        warn "Spinning volumes off their backing files ...\n"
            if $ENV{TERM} && $0 !~ /\.t$/;
484
485
        $domain->spinoff_volumes();
    }
486
    return $domain;
487
488
}

489
490
############################################################
#
491

492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
sub _domain_remove_db {
    my $self = shift;
    my $name = shift;
    my $sth = $$CONNECTOR->dbh->prepare("DELETE FROM domains WHERE name=?");
    $sth->execute($name);
    $sth->finish;
}

=head2 domain_remove

Remove the domain. Returns nothing.

=cut


sub domain_remove {
    my $self = shift;
    $self->domain_remove_vm();
    $self->_domain_remove_bd();
}

=head2 name

Returns the name of this Virtual Machine Manager

    my $name = $vm->name();

=cut

sub name {
    my $self = shift;

524
    return $self->_data('name') if defined $self->{_data}->{name};
525

526
527
    my ($ref) = ref($self) =~ /.*::(.*)/;
    return ($ref or ref($self))."_".$self->host;
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
}

=head2 search_domain_by_id

Returns a domain searching by its id

    $domain = $vm->search_domain_by_id($id);

=cut

sub search_domain_by_id {
    my $self = shift;
      my $id = shift;

    my $sth = $$CONNECTOR->dbh->prepare("SELECT name FROM domains "
        ." WHERE id=?");
    $sth->execute($id);
    my ($name) = $sth->fetchrow;
    return if !$name;

    return $self->search_domain($name);
}

Francesc Guasch's avatar
Francesc Guasch committed
551
552
553
554
555
556
557
558
sub _domain_in_db($self, $name) {

    my $sth = $$CONNECTOR->dbh->prepare("SELECT id FROM domains WHERE name=?");
    $sth->execute($name);
    my ($id) =$sth->fetchrow;
    return $id;
}

559
560
561
562
563
564
565
566
567
=head2 ip

Returns the external IP this for this VM

=cut

sub ip {
    my $self = shift;

568
569
    my $name = ($self->public_ip or $self->host())
        or confess "this vm has no host name";
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
    my $ip = inet_ntoa(inet_aton($name)) ;

    return $ip if $ip && $ip !~ /^127\./;

    $name = Ravada::display_ip();

    if ($name) {
        if ($name =~ /^\d+\.\d+\.\d+\.\d+$/) {
            $ip = $name;
        } else {
            $ip = inet_ntoa(inet_aton($name));
        }
    }
    return $ip if $ip && $ip !~ /^127\./;

    $ip = $self->_interface_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."
Francesc Guasch's avatar
Francesc Guasch committed
589
        ." This virtual machine won't be available from the network." if $0 !~ /\.t$/;
590
591
592
593

    return '127.0.0.1';
}

Francesc Guasch's avatar
Francesc Guasch committed
594
595
596
597
598
599
=head2 nat_ip

Returns the IP of the VM when it is in a NAT environment

=cut

600
sub nat_ip($self) {
601
    return Ravada::nat_ip();
602
603
}

604
605
606
607
608
sub _interface_ip($self, $remote_ip=undef) {
    return '127.0.0.1' if $remote_ip && $remote_ip =~ /^127\./;
    my ($out, $err) = $self->run_command("/sbin/ip","route");
    my %route;
    my ($default_gw , $default_ip);
609

610
611
612
613
614
615
616
617
618
619
620
621
622
    my $remote_ip_addr = NetAddr::IP->new($remote_ip);

    for my $line ( split( /\n/, $out ) ) {
        if ( $line =~ m{^default via ([\d\.]+)} ) {
            $default_gw = NetAddr::IP->new($1);
        }
        if ( $line =~ m{^([\d\.\/]+).*src ([\d\.\/]+)} ) {
            my ($network, $ip) = ($1, $2);
            $route{$network} = $ip;

            my $netaddr = NetAddr::IP->new($network);
            return $ip if $remote_ip_addr->within($netaddr);

frankiejol's avatar
frankiejol committed
623
            $default_ip = $ip if !defined $default_ip && $ip !~ /^127\./;
624
625
            $default_ip = $ip if defined $default_gw && $default_gw->within($netaddr);
        }
626
    }
627
    return $default_ip;
628
629
}

630
sub listen_ip($self, $remote_ip=undef) {
631
    return Ravada::display_ip() if Ravada::display_ip();
632
    return $self->public_ip     if $self->public_ip;
633

634
635
    return $self->_interface_ip($remote_ip) if $remote_ip;

636
    return (
637
            $self->ip()
638
639
640
    );
}

641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
sub _check_memory {
    my $self = shift;
    my %args = @_;
    return if !exists $args{memory};

    die "ERROR: Low memory '$args{memory}' required ".int($MIN_MEMORY_MB/1024)." MB " if $args{memory} < $MIN_MEMORY_MB;
}

sub _check_disk {
    my $self = shift;
    my %args = @_;
    return if !exists $args{disk};

    die "ERROR: Low Disk '$args{disk}' required 1 Gb " if $args{disk} < 1024*1024;
}


sub _check_create_domain {
    my $self = shift;

    my %args = @_;

    $self->_check_readonly(@_);

    $self->_check_require_base(@_);
    $self->_check_memory(@_);
    $self->_check_disk(@_);

}

sub _check_require_base {
    my $self = shift;

    my %args = @_;
675
676
677
678
679
680

    my $id_base = delete $args{id_base} or return;
    my $request = delete $args{request};
    my $id_owner = delete $args{id_owner}
        or confess "ERROR: id_owner required ";

681
682
683
    delete $args{start};
    delete $args{remote_ip};

Francesc Guasch's avatar
Francesc Guasch committed
684
    delete @args{'_vm','name','vm', 'memory','description','id_iso','listen_ip','spice_password','from_pool'};
685
686
687
688
689

    confess "ERROR: Unknown arguments ".join(",",keys %args)
        if keys %args;

    my $base = Ravada::Domain->open($id_base);
690
691
692
693
694
695
    my %ignore_requests = map { $_ => 1 } qw(clone refresh_machine set_base_vm start_clones);
    my @requests;
    for my $req ( $base->list_requests ) {
        push @requests,($req) if !$ignore_requests{$req->command};
    }
    if (@requests) {
696
697
        confess "ERROR: Domain ".$base->name." has ".$base->list_requests
                            ." requests.\n"
Francesc Guasch's avatar
Francesc Guasch committed
698
                            .Dumper([$base->list_requests])
699
700
            unless scalar @requests == 1 && $request
                && $requests[0]->id eq $request->id;
701
702
    }

703

704
705
706
    die "ERROR: Domain ".$self->name." is not base"
            if !$base->is_base();

707
708
709
710
    my $user = Ravada::Auth::SQL->search_by_id($id_owner);

    die "ERROR: Base ".$base->name." is not public\n"
        unless $user->is_admin || $base->is_public;
711
712
}

713
714
715
716
717
718
719
720
721
722
723
=head2 id

Returns the id value of the domain. This id is used in the database
tables and is not related to the virtual machine engine.

=cut

sub id {
    return $_[0]->_data('id');
}

Francesc Guasch's avatar
Francesc Guasch committed
724
sub _data($self, $field, $value=undef) {
Francesc Guasch's avatar
Francesc Guasch committed
725
    if (defined $value && $self->store ) {
726
727
728
729
730
731
732
        $self->{_data}->{$field} = $value;
        my $sth = $$CONNECTOR->dbh->prepare(
            "UPDATE vms set $field=?"
            ." WHERE id=?"
        );
        $sth->execute($value, $self->id);
        $sth->finish;
733

734
735
        return $value;
    }
736
737
738

#    _init_connector();

739
    $self->_timed_data_cache()  if $self->{_data}->{$field} && $field ne 'name';
740
    return $self->{_data}->{$field} if exists $self->{_data}->{$field};
Francesc Guasch's avatar
Francesc Guasch committed
741
742
    return if !$self->store();

743
744
745
746
747
748
749
750
    $self->{_data} = $self->_select_vm_db( name => $self->name);

    confess "No DB info for VM ".$self->name    if !$self->{_data};
    confess "No field $field in vms"            if !exists$self->{_data}->{$field};

    return $self->{_data}->{$field};
}

751
sub _timed_data_cache($self) {
752
    return if !$self->{$FIELD_TIMEOUT} || time - $self->{$FIELD_TIMEOUT} < $CACHE_TIMEOUT;
753
754
755
756
    return _clean($self);
}

sub _clean($self) {
757
758
759
    my $name = $self->{_data}->{name};
    my $id = $self->{_data}->{id};
    delete $self->{_data};
760
    delete $self->{$FIELD_TIMEOUT};
761
762
    $self->{_data}->{name} = $name  if $name;
    $self->{_data}->{id} = $id      if $id;
763
    return $self;
764
765
}

766
767
768
769
sub _do_select_vm_db {
    my $self = shift;
    my %args = @_;

770
771
    _init_connector();

772
773
774
775
776
777
778
779
    if (!keys %args) {
        my $id;
        eval { $id = $self->id  };
        if ($id) {
            %args =( id => $id );
        }
    }

Francesc Guasch's avatar
Francesc Guasch committed
780
    confess Dumper(\%args) if !keys %args;
781
    my $sth = $$CONNECTOR->dbh->prepare(
782
        "SELECT * FROM vms WHERE ".join(" AND ",map { "$_=?" } sort keys %args )
783
784
785
786
    );
    $sth->execute(map { $args{$_} } sort keys %args);
    my $row = $sth->fetchrow_hashref;
    $sth->finish;
Francesc Guasch's avatar
Francesc Guasch committed
787
788
789

    return if !$row;

790
791
792
793
794
795
    return $row;
}

sub _select_vm_db {
    my $self = shift;

796
    my ($row) = ($self->_do_select_vm_db(@_) or $self->_insert_vm_db(@_));
797
798

    $self->{_data} = $row;
799
    $self->{$FIELD_TIMEOUT} = time if $row->{id};
800
801
802
803
804
    return $row if $row->{id};
}

sub _insert_vm_db {
    my $self = shift;
Francesc Guasch's avatar
Francesc Guasch committed
805
806
    return if !$self->store();

807
    my $sth = $$CONNECTOR->dbh->prepare(
808
809
        "INSERT INTO vms (name, vm_type, hostname, public_ip)"
        ." VALUES(?, ?, ?, ?)"
810
    );
811
812
813
    my %args = @_;
    my $name = ( delete $args{name} or $self->name);
    my $host = ( delete $args{hostname} or $self->host );
814
    my $public_ip = ( delete $args{public_ip} or '' );
815
    delete $args{vm_type};
816

817
818
    confess "Unknown args ".Dumper(\%args)  if keys %args;

819
    eval { $sth->execute($name,$self->type,$host, $public_ip) };
820
    confess $@ if $@;
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
    $sth->finish;

    return $self->_do_select_vm_db( name => $name);
}

=head2 default_storage_pool_name

Set the default storage pool name for this Virtual Machine Manager

    $vm->default_storage_pool_name('default');

=cut

sub default_storage_pool_name {
    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 default_storage=?"
            ." WHERE id=?"
        );
        $sth->execute($value,$id);
        $self->{_data}->{default_storage} = $value;
    }
Francesc Guasch's avatar
Francesc Guasch committed
848
    $self->_select_vm_db() if $self->store();
849
850
851
    return $self->_data('default_storage');
}

Francesc Guasch's avatar
Francesc Guasch committed
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
=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;
    }
874
    $self->_select_vm_db();
Francesc Guasch's avatar
Francesc Guasch committed
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
    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;
    }
900
    $self->_select_vm_db();
Francesc Guasch's avatar
Francesc Guasch committed
901
902
903
    return $self->_data('clone_storage');
}

904
905
906
907
908
909
910
911
912
913
914
=head2 min_free_memory

Returns the minimun free memory necessary to start a new virtual machine

=cut

sub min_free_memory {
    my $self = shift;
    return $self->_data('min_free_memory');
}

Francesc Guasch's avatar
Francesc Guasch committed
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
=head2 max_load 

Returns the maximum cpu load that the host can handle.

=cut

sub max_load {
    my $self = shift;
    return $self->_data('max_load');
}

=head2 active_limit

Returns the value of 'active_limit' in the BBDD

=cut

sub active_limit {
    my $self = shift;
    return $self->_data('active_limit');
}
936

Francesc Guasch's avatar
Francesc Guasch committed
937
938
939
940
941
942
943
944
945
946
947
948
949
=head2 list_drivers

Lists the drivers available for this Virtual Machine Manager

Arguments: Optional driver type

Returns a list of strings with the nams of the drivers.

    my @drivers = $vm->list_drivers();
    my @drivers = $vm->list_drivers('image');

=cut

Francesc Guasch's avatar
Francesc Guasch committed
950
951
952
sub list_drivers($self, $name=undef) {
    return Ravada::Domain::drivers(undef,$name,$self->type);
}
953

Francesc Guasch's avatar
Francesc Guasch committed
954
955
956
957
958
959
=head2 is_local

Returns wether this virtual manager is in the local host

=cut

960
sub is_local($self) {
961
    return 1 if $self->host eq 'localhost'
962
963
        || $self->host eq '127.0.0,1'
        || !$self->host;
964
    return 0;
965
966
}

967
968
969
970
971
972
973
974
975

=head2 list_nodes

Returns a list of virtual machine manager nodes of the same type as this.

    my @nodes = $self->list_nodes();

=cut

976
sub list_nodes($self) {
977
978
    return @{$self->{_nodes}} if $self->{_nodes};

979
980
981
982
983
984
985
986
987
988
    my $sth = $$CONNECTOR->dbh->prepare(
        "SELECT id FROM vms WHERE vm_type=?"
    );
    my @nodes;
    $sth->execute($self->type);

    while (my ($id) = $sth->fetchrow) {
        push @nodes,(Ravada::VM->open($id))
    }

989
    $self->{_nodes} = \@nodes;
990
991
    return @nodes;
}
992

993
994
995
996
997
998
=head2 ping

Returns if the virtual manager connection is available

=cut

999
1000
sub ping($self, $option=undef) {
    confess "ERROR: option unknown" if defined $option && $option ne 'debug';
Francesc Guasch's avatar
Francesc Guasch committed
1001
1002

    return 1 if $self->is_local();
1003
1004
1005
    my $debug = 0;
    $debug = 1 if defined $option && $option eq 'debug';

Francesc Guasch's avatar
Francesc Guasch committed
1006
1007
1008
1009
    return $self->_do_ping($self->host, $debug);
}

sub _do_ping($self, $host, $debug=0) {
1010

Francesc Guasch's avatar
Francesc Guasch committed
1011
    my $p = Net::Ping->new('tcp',2);
1012
    my $ping_ok;
Francesc Guasch's avatar
Francesc Guasch committed
1013
1014
1015
1016
1017
    eval { $ping_ok = $p->ping($host) };
    confess $@ if $@;
    warn "$@ pinging host $host" if $@;

    $self->_store_mac_address() if $ping_ok && $self;
1018
    return 1 if $ping_ok;
Francesc Guasch's avatar
Francesc Guasch committed
1019
1020
    $p->close();

1021
    return if $>; # icmp ping requires root privilege
1022
    warn "trying icmp"   if $debug;
Francesc Guasch's avatar
Francesc Guasch committed
1023
    $p= Net::Ping->new('icmp',2);
Francesc Guasch's avatar
Francesc Guasch committed
1024
    eval { $ping_ok = $p->ping($host) };
1025
    warn $@ if $@;
Francesc Guasch's avatar
Francesc Guasch committed
1026
    $self->_store_mac_address() if $ping_ok && $self;
1027
    return 1 if $ping_ok;
Francesc Guasch's avatar
Francesc Guasch committed
1028

1029
    return 0;
Francesc Guasch's avatar
Francesc Guasch committed
1030
1031
}

Francesc Guasch's avatar
Francesc Guasch committed
1032
1033
1034
1035
1036
1037
1038
1039
1040
sub _around_ping($orig, $self, $option=undef) {

    my $ping = $self->$orig($option);
    $self->_cached_active($ping);
    $self->_cached_active_time(time);

    return $ping;
}

1041
1042
=head2 is_active

1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
Returns if the domain is active. The active state is cached for some seconds.
Pass an optional true value to perform a real check.

Arguments: optional force mode

    if ($node->is_active) {
    }


    if ($node->is_active(1)) {
    }
1054
1055
1056

=cut

1057
1058
sub is_active($self, $force=0) {
    return $self->_do_is_active() if $self->is_local || $force;
1059

1060
    return $self->_cached_active if time - $self->_cached_active_time < 60;
1061
1062
1063
1064
    return $self->_do_is_active();
}

sub _do_is_active($self) {
1065
    my $ret = 0;
1066
1067
    if ( $self->is_local ) {
        $ret = 1 if $self->vm;
1068
    } else {
1069
1070
1071
1072
1073
1074
1075
        if ( !$self->ping() ) {
            $ret = 0;
        } else {
            if ( $self->is_alive ) {
                $ret = 1;
            }
        }
1076
1077
1078
1079
1080
1081
1082
    }
    $self->_cached_active($ret);
    $self->_cached_active_time(time);
    return $ret;
}

sub _cached_active($self, $value=undef) {
1083
    return $self->_data('is_active', $value);
1084
}
Francesc Guasch's avatar
Francesc Guasch committed
1085

1086
1087
sub _cached_active_time($self, $value=undef) {
    return $self->_data('cached_active_time', $value);
1088
}
Francesc Guasch's avatar
Francesc Guasch committed
1089

1090
1091
1092
1093
1094
1095
=head2 enabled

Returns if the domain is enabled.

=cut

1096
1097
sub enabled($self, $value=undef) {
    return $self->_data('enabled', $value);
1098
1099
}

1100
1101
sub is_enabled($self, $value=undef) {
    return $self->enabled($value);
1102
1103
}

1104
1105
1106
1107
sub public_ip($self, $value=undef) {
    return $self->_data('public_ip', $value);
}

1108
1109
1110
1111
1112
1113
=head2 remove

Remove the virtual machine manager.

=cut

Francesc Guasch's avatar
Francesc Guasch committed
1114
sub remove($self) {
1115
1116
    #TODO stop the active domains
    #
Francesc Guasch's avatar
Francesc Guasch committed
1117
1118
    delete $VM{$self->id};

Francesc Guasch's avatar
Francesc Guasch committed
1119
    $self->disconnect();
Francesc Guasch's avatar
Francesc Guasch committed
1120
1121
1122
1123
    my $sth = $$CONNECTOR->dbh->prepare("DELETE FROM vms WHERE id=?");
    $sth->execute($self->id);
}

1124
1125
1126
1127
1128
1129
1130
1131
=head2 run_command

Run a command on the node

    my @ls = $self->run_command("ls");

=cut

Francesc Guasch's avatar
Francesc Guasch committed
1132
sub run_command($self, @command) {
Francesc Guasch's avatar
Francesc Guasch committed
1133

Francesc Guasch's avatar
Francesc Guasch committed
1134
    return $self->_run_command_local(@command) if $self->is_local();
1135

Francesc Guasch's avatar
Francesc Guasch committed
1136
    my $chan = $self->_ssh_channel() or die "ERROR: No SSH channel to host ".$self->host;
1137

Francesc Guasch's avatar
Francesc Guasch committed
1138
    my $command = join(" ",@command);
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
    $chan->exec($command);# or $self->{_ssh}->die_with_error;

    $chan->send_eof();

    my ($out, $err) = ('', '');
    while (!$chan->eof) {
        if (my ($o, $e) = $chan->read2) {
            $out .= $o;
            $err .= $e;
        }
    }
    return ($out, $err);
}

Francesc Guasch's avatar
Francesc Guasch committed
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
sub run_command_nowait($self, @command) {

    return $self->_run_command_local(@command) if $self->is_local();

    my $chan = $self->_ssh_channel() or die "ERROR: No SSH channel to host ".$self->host;

    my $command = join(" ",@command);
    $chan->exec($command);# or $self->{_ssh}->die_with_error;

    $chan->send_eof();

    return;
}


Francesc Guasch's avatar
Francesc Guasch committed
1168
sub _run_command_local($self, @command) {
1169
    my ( $in, $out, $err);
1170
1171
    my ($exec) = $command[0];
    confess "ERROR: Missing command $exec"  if ! -e $exec;
Francesc Guasch's avatar
Francesc Guasch committed
1172
    run3(\@command, \$in, \$out, \$err);
1173
    return ($out, $err);
1174
1175
}

1176
1177
1178
1179
1180
1181
1182
1183
=head2 write_file

Writes a file to the node

    $self->write_file("filename.extension", $contents);

=cut

1184
sub write_file( $self, $file, $contents ) {
Francesc Guasch's avatar
Francesc Guasch committed
1185
1186
1187
1188
1189
1190
    return $self->_write_file_local($file, $contents )  if $self->is_local;

    my $chan = $self->_ssh_channel();
    $chan->exec("cat > $file");
    my $bytes = $chan->write($contents);
    $chan->send_eof();
1191
}
Francesc Guasch's avatar
Francesc Guasch committed
1192
1193

sub _write_file_local( $self, $file, $contents ) {
1194
1195
1196
    my ($path) = $file =~ m{(.*)/};
    make_path($path) or die "$! $path"
        if ! -e $path;
1197
    CORE::open(my $out,">",$file) or confess "$! $file";
1198
1199
    print $out $contents;
    close $out or die "$! $file";
Francesc Guasch's avatar
Francesc Guasch committed
1200
1201
}

1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
sub read_file( $self, $file ) {
    return $self->_read_file_local($file) if $self->is_local;

    my ($content, $err) = $self->run_command("cat $file");
    confess $err if $err;
    return $content;
}

sub _read_file_local( $self, $file ) {
    CORE::open my $in,'<',$file or die "$! $file";
    return join('',<$in>);
}

1215
1216
1217
sub file_exists( $self, $file ) {
    return -e $file if $self->is_local;

1218
1219
    # why should we force disconnect before ?
    $self->_connect_ssh();
1220
1221
1222
1223
    my ( $out, $err) = $self->run_command("/usr/bin/test",
        "-e $file ; echo \$?");

    chomp $out;
1224
1225
1226
1227
1228
    chomp $err;

    warn $self->name." ".$err if $err;

    return 1 if $out =~ /^0$/;
1229
1230
1231
1232
1233
    return 0;
}

sub remove_file( $self, $file ) {
    unlink $file if $self->is_local;
1234
    return $self->run_command("/bin/rm", $file);
1235
1236
}

1237
sub create_iptables_chain($self,$chain) {
1238
    my ($out, $err) = $self->run_command("/sbin/iptables","-n","-L",$chain);
1239

1240
1241
1242
1243
1244
1245
1246
1247
    $self->run_command("/sbin/iptables", '-N' => $chain)
        if $out !~ /^Chain $chain/;

    ($out, $err) = $self->run_command("/sbin/iptables","-n","-L",'INPUT');
    return if grep(/^RAVADA /, split(/\n/,$out));

    $self->run_command("/sbin/iptables", '-A','INPUT', '-j' => $chain);

1248
1249
1250
}

sub iptables($self, @args) {
1251
    my @cmd = ('/sbin/iptables');
1252
1253
1254
1255
1256
1257
1258
1259
    for ( ;; ) {
        my $key = shift @args or last;
        my $field = "