Ravada.pm 30.7 KB
Newer Older
1
2
3
4
package Test::Ravada;
use strict;
use warnings;

5
use  Carp qw(carp confess);
Francesc Guasch's avatar
Francesc Guasch committed
6
use Data::Dumper;
7
use YAML qw(DumpFile);
8
9
use Hash::Util qw(lock_hash);
use IPC::Run3 qw(run3);
10
use  Test::More;
11
use YAML qw(LoadFile);
12

13
14
use feature qw(signatures);
no warnings "experimental::signatures";
15

Francesc Guasch's avatar
Francesc Guasch committed
16
17
18
no warnings "experimental::signatures";
use feature qw(signatures);

19
20
use Ravada;
use Ravada::Auth::SQL;
21
22
use Ravada::Domain::Void;

23
24
25
26
27
28
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);

require Exporter;

@ISA = qw(Exporter);

29
30
@EXPORT = qw(base_domain_name new_domain_name rvd_back remove_old_disks remove_old_domains create_user user_admin wait_request rvd_front init init_vm clean new_pool_name
create_domain
31
    test_chain_prerouting
32
    find_ip_rule
33
    search_id_iso
34
35
    flush_rules_node
    flush_rules
36
    arg_create_dom
Francesc Guasch's avatar
Francesc Guasch committed
37
    vm_names
38
    remote_config
39
    remote_config_nodes
Francesc Guasch's avatar
Francesc Guasch committed
40
    clean_remote_node
41
    arg_create_dom
Francesc Guasch's avatar
Francesc Guasch committed
42
    vm_names
Francesc Guasch's avatar
Francesc Guasch committed
43
    search_iptable_remote
44
    clean_remote
45
    start_node shutdown_node remove_node hibernate_node
46
    start_domain_internal   shutdown_domain_internal
47
    hibernate_domain_internal
48
    remote_node
49
    add_ubuntu_minimal_iso
Francesc Guasch's avatar
Francesc Guasch committed
50
    create_ldap_user
Francesc Guasch's avatar
Francesc Guasch committed
51
    connector
Francesc Guasch's avatar
Francesc Guasch committed
52
    create_ldap_user
53
);
54
55

our $DEFAULT_CONFIG = "t/etc/ravada.conf";
56
57
our $FILE_CONFIG_REMOTE = "t/etc/remote_vm.conf";

58
59
$Ravada::Front::Domain::Void = "/var/tmp/test/rvd_void/".getpwuid($>);

60
our ($CONNECTOR, $CONFIG , $FILE_CONFIG_TMP);
Francesc Guasch's avatar
Francesc Guasch committed
61
our $DEFAULT_DB_CONFIG = "t/etc/sql.conf";
62
63

our $CONT = 0;
64
our $CONT_POOL= 0;
65
our $USER_ADMIN;
66
our $CHAIN = 'RAVADA';
67

Francesc Guasch's avatar
Francesc Guasch committed
68
69
our $RVD_BACK;

Francesc Guasch's avatar
Francesc Guasch committed
70
71
72
73
our %ARG_CREATE_DOM = (
    KVM => []
    ,Void => []
);
74

75
76
77
78
our %VM_VALID = ( KVM => 1
    ,Void => 0
);

79
sub user_admin {
80
81
82
83
84
85
86
87
88
89
90
91
92

    return $USER_ADMIN if $USER_ADMIN;

    my $login;
    my $admin_name = base_domain_name();
    my $admin_pass = "$$ $$";
    eval {
        $login = Ravada::Auth::SQL->new(name => $admin_name );
    };
    $USER_ADMIN = $login if $login && $login->id;
    $USER_ADMIN = create_user($admin_name, $admin_pass,1)
        if !$USER_ADMIN;

93
94
    return $USER_ADMIN;
}
95

96
97
98
99
100
101
102
sub arg_create_dom {
    my $vm_name = shift;
    confess "Unknown vm $vm_name"
        if !$ARG_CREATE_DOM{$vm_name};
    return @{$ARG_CREATE_DOM{$vm_name}};
}

103
104
105
106
107
108
109
110
111
112
113
114
115
sub add_ubuntu_minimal_iso {
    my %info = ('bionic_minimal' => {
        name => 'Ubuntu Bionic Minimal'
        ,url => 'http://archive.ubuntu.com/ubuntu/dists/bionic/main/installer-i386/current/images/netboot/mini.iso'
        ,xml => 'bionic-i386.xml'
        ,xml_volume => 'bionic32-volume.xml'
        ,rename_file => 'ubuntu_bionic_mini.iso'
        ,arch => 'i386'
        ,md5 => 'c7b21dea4d2ea037c3d97d5dac19af99'
    });
    $RVD_BACK->_update_table('iso_images','name',\%info);
}

Francesc Guasch's avatar
Francesc Guasch committed
116
117
118
119
sub vm_names {
    return sort keys %ARG_CREATE_DOM;
}

120
121
122
sub create_domain {
    my $vm_name = shift;
    my $user = (shift or $USER_ADMIN);
123
    my $id_iso = (shift or 'Alpine');
124

125
126
    $vm_name = 'KVM' if $vm_name eq 'qemu';

127
128
129
130
131
    if ( $id_iso && $id_iso !~ /^\d+$/) {
        my $iso_name = $id_iso;
        $id_iso = search_id_iso($iso_name);
        warn "I can't find iso $iso_name" if !defined $id_iso;
    }
132
133
134
135
136
137
138
139
    my $vm;
    if (ref($vm_name)) {
        $vm = $vm_name;
        $vm_name = $vm->type;
    } else {
        $vm = rvd_back()->search_vm($vm_name);
        ok($vm,"Expecting VM $vm_name, got ".$vm->type) or return;
    }
140

141
142
    confess "ERROR: Domains can only be created at localhost"
        if $vm->host ne 'localhost';
143
    confess "Missing id_iso" if !defined $id_iso;
144
145
146

    my $name = new_domain_name();

147
    my %arg_create = (id_iso => $id_iso);
148
149
150
151

    my $domain;
    eval { $domain = $vm->create_domain(name => $name
                    , id_owner => $user->id
152
153
                    , %arg_create
                    , active => 0
154
                    , memory => 256*1024
155
156
157
158
159
160
161
162
           );
    };
    is($@,'');

    return $domain;

}

163
sub base_domain_name {
164
165
    my ($name) = $0 =~ m{.*?/(.*)\.t};
    die "I can't find name in $0"   if !$name;
166
167
    $name =~ s{/}{_}g;

168
    return "tst_$name";
169
170
}

171
172
173
174
175
sub base_pool_name {
    my ($name) = $0 =~ m{.*?/(.*)\.t};
    die "I can't find name in $0"   if !$name;
    $name =~ s{/}{_}g;

Francesc Guasch's avatar
Francesc Guasch committed
176
    return "tst_pool_$name";
177
178
}

179
sub new_domain_name {
Francesc Guasch's avatar
Francesc Guasch committed
180
181
    my $post = (shift or '');
    $post = $post."_" if $post;
182
183
    my $cont = $CONT++;
    $cont = "0$cont"    if length($cont)<2;
Francesc Guasch's avatar
Francesc Guasch committed
184
    return base_domain_name()."_$post".$cont;
185
186
}

187
188
189
190
sub new_pool_name {
    return base_pool_name()."_".$CONT_POOL++;
}

Francesc Guasch's avatar
Francesc Guasch committed
191
sub rvd_back($config=undef) {
Francesc Guasch's avatar
Francesc Guasch committed
192

Francesc Guasch's avatar
Francesc Guasch committed
193
194
195
    return $RVD_BACK            if $RVD_BACK && !$config;

    $RVD_BACK = 1;
196
    init($config or $DEFAULT_CONFIG);
197

198
    my $rvd = Ravada->new(
Francesc Guasch's avatar
Francesc Guasch committed
199
200
            connector => connector()
                , config => ( $config or $DEFAULT_CONFIG)
201
                , warn_error => 0
202
    );
203
    $rvd->_install();
204

205
    user_admin();
206
207
    $ARG_CREATE_DOM{KVM} = [ id_iso => search_id_iso('Alpine') ];

208
    $RVD_BACK = $rvd;
209
    return $rvd;
210
211
}

Francesc Guasch's avatar
Francesc Guasch committed
212
sub rvd_front() {
213

214
    return Ravada::Front->new(
215
216
            connector => $CONNECTOR
                , config => ( $CONFIG or $DEFAULT_CONFIG)
217
    );
218
219
}

220
sub init($config=undef) {
221

222
223
224
225
226
    if ($config && ! ref($config) && $config =~ /[A-Z][a-z]+$/) {
        $config = { vm => [ $config ] };
    }

    if ($config && ref($config) ) {
227
228
229
230
231
232
233
        $FILE_CONFIG_TMP = "/tmp/ravada_".base_domain_name()."_$$.conf";
        DumpFile($FILE_CONFIG_TMP, $config);
        $CONFIG = $FILE_CONFIG_TMP;
    } else {
        $CONFIG = $config;
    }

234
235
236
237
238
    if ( $RVD_BACK && ref($RVD_BACK) ) {
        clean();
        # clean removes the temporary config file, so we dump it again
        DumpFile($FILE_CONFIG_TMP, $config) if $config && ref($config);
    }
239

240
    $Ravada::CONNECTOR = connector();
241
242
    Ravada::Auth::SQL::_init_connector($CONNECTOR);

243
    $Ravada::Domain::MIN_FREE_MEMORY = 512*1024;
244

Francesc Guasch's avatar
Francesc Guasch committed
245
    rvd_back($config)  if !$RVD_BACK;
Francesc Guasch's avatar
Francesc Guasch committed
246
    $Ravada::VM::KVM::VERIFY_ISO = 0;
247
248
}

249
250
251
sub remote_config {
    my $vm_name = shift;
    return { } if !-e $FILE_CONFIG_REMOTE;
252

253
254
255
    my $conf;
    eval { $conf = LoadFile($FILE_CONFIG_REMOTE) };
    is($@,'',"Error in $FILE_CONFIG_REMOTE\n".$@) or return;
256

257
258
259
260
    my $remote_conf = $conf->{$vm_name} or do {
        diag("SKIPPED: No $vm_name section in $FILE_CONFIG_REMOTE");
        return ;
    };
Francesc Guasch's avatar
Francesc Guasch committed
261
    for my $field ( qw(host user password security public_ip name)) {
262
263
        delete $remote_conf->{$field};
    }
Francesc Guasch's avatar
Francesc Guasch committed
264
    die "Unknown fields in remote_conf $vm_name, valids are : host user password name\n"
265
        .Dumper($remote_conf)   if keys %$remote_conf;
266

267
    $remote_conf = LoadFile($FILE_CONFIG_REMOTE);
268
269
270
271
272
273
    ok($remote_conf->{public_ip} ne $remote_conf->{host},
            "Public IP must be different from host at $FILE_CONFIG_REMOTE")
        if defined $remote_conf->{public_ip};

    $remote_conf->{public_ip} = '' if !exists $remote_conf->{public_ip};

274
275
    lock_hash(%$remote_conf);
    return $remote_conf->{$vm_name};
276
277
}

278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
sub remote_config_nodes {
    my $file_config = shift;
    confess "Missing file $file_config" if !-e $file_config;

    my $conf;
    eval { $conf = LoadFile($file_config) };
    is($@,'',"Error in $file_config\n".($@ or ''))  or return;

    lock_hash((%$conf));

    for my $name (keys %$conf) {
        if ( !$conf->{$name}->{host} ) {
            warn "ERROR: Missing host section in ".Dumper($conf->{$name})
                ."at $file_config\n";
            next;
        }
    }
    return $conf;
}

298
299
300
sub _remove_old_domains_vm {
    my $vm_name = shift;

301

302
    my $domain;
303
304

    my $vm;
Francesc Guasch's avatar
Francesc Guasch committed
305
306
307
308

    if (ref($vm_name)) {
        $vm = $vm_name;
    } else {
309
        return if !$VM_VALID{$vm_name};
Francesc Guasch's avatar
Francesc Guasch committed
310
        eval {
311
312
        my $rvd_back=rvd_back();
        return if !$rvd_back;
313
        confess if $rvd_back eq 1;
314
        $vm = $rvd_back->search_vm($vm_name);
Francesc Guasch's avatar
Francesc Guasch committed
315
        };
316
        diag($@) if $@ && $@ !~ /Missing qemu-img/;
317

318
319
320
321
        if ( !$vm ) {
            $VM_VALID{$vm_name} = 0;
            return;
        }
Francesc Guasch's avatar
Francesc Guasch committed
322
    }
323
    my $base_name = base_domain_name();
324

325
326
    my @domains;
    eval { @domains = $vm->list_domains() };
Francesc Guasch's avatar
Francesc Guasch committed
327
328
    for my $domain ( sort { $b->name cmp $a->name }  @domains) {
        next if $domain->name !~ /^$base_name/i;
329
330
331
332

        eval { $domain->shutdown_now($USER_ADMIN); };
        warn "Error shutdown ".$domain->name." $@" if $@ && $@ !~ /No DB info/i;

333
        $domain = $vm->search_domain($domain->name);
334
        eval {$domain->remove( $USER_ADMIN ) }  if $domain;
335
336
337
338
339
340
        if ( $@ && $@ =~ /No DB info/i ) {
            eval { $domain->domain->undefine() if $domain->domain };
        }

    }

Francesc Guasch's avatar
Francesc Guasch committed
341
    _remove_old_domains_kvm($vm)    if $vm->type =~ /qemu|kvm/i;
342
343
344
345
346
347
    _remove_old_domains_void($vm)    if $vm->type =~ /void/i;
}

sub _remove_old_domains_void {
    my $vm = shift;
    return _remove_old_domains_void_remote($vm) if !$vm->is_local;
Francesc Guasch's avatar
Francesc Guasch committed
348
    my $base_name = base_domain_name();
349
350
351

    opendir my $dir, $vm->dir_img or return;
    while ( my $file = readdir($dir) ) {
Francesc Guasch's avatar
Francesc Guasch committed
352
        next if $file !~ /^$base_name/;
353
354
355
356
357
358
359
360
        my $path = $vm->dir_img."/".$file;
        next if ! -f $path
            || $path !~ m{\.(yml|qcow|img)$};
        unlink $path or die "$! $path";
    }
    closedir $dir;
}

361
sub _remove_old_domains_void_remote($vm) {
362
363
364
    return if !$vm->ping;
    eval { $vm->connect };
    warn $@ if $@;
365
    return if !$vm->_do_is_active;
Francesc Guasch's avatar
Francesc Guasch committed
366
367
368
369
370

    my $base_name = base_domain_name();
    $vm->run_command("rm -f ".$vm->dir_img."/$base_name*yml "
                    .$vm->dir_img."/$base_name*qcow "
                    .$vm->dir_img."/$base_name*img"
371
    );
372
}
373
374

sub _remove_old_domains_kvm {
375
    return if !$VM_VALID{'KVM'};
Francesc Guasch's avatar
Francesc Guasch committed
376
    my $vm = shift;
377

Francesc Guasch's avatar
Francesc Guasch committed
378
379
    if (!$vm) {
        eval {
380
381
            my $rvd_back = rvd_back();
            $vm = $rvd_back->search_vm('KVM');
Francesc Guasch's avatar
Francesc Guasch committed
382
383
384
385
        };
        diag($@) if $@;
        return if !$vm;
    }
386
    return if !$vm->vm;
Francesc Guasch's avatar
Francesc Guasch committed
387
    _activate_storage_pools($vm);
388

389
    my $base_name = base_domain_name();
390
391
392
393
394
395

    my @domains;
    eval { @domains = $vm->vm->list_all_domains() };
    return if $@ && $@ =~ /connect to host/;
    is($@,'') or return;

396
    for my $domain ( $vm->vm->list_all_domains ) {
397
        next if $domain->get_name !~ /^$base_name/;
398
        my $domain_name = $domain->get_name;
399
        eval { 
400
401
402
403
404
405
406
407
408
409
410
            $domain->shutdown();
            sleep 1 if $domain->is_active;
        };
        warn "WARNING: error $@ trying to shutdown ".$domain_name." on ".$vm->name
            if $@ && $@ !~ /error code: (42|55),/;

        eval { $domain->destroy() if $domain->is_active };
        warn $@ if $@;

        warn "WARNING: error $@ trying to shutdown ".$domain_name." on ".$vm->name
            if $@ && $@ !~ /error code: (42|55),/;
411

412
413
414
415
416
        eval {
            $domain->managed_save_remove()
                if $domain->has_managed_save_image();
        };
        warn $@ if $@ && $@ !~ /error code: 42,/;
417

418
        eval { $domain->undefine };
419
        warn $@ if $@ && $@ !~ /error code: 42,/;
420
421
422
    }
}

423
sub remove_old_domains {
424
425
    _remove_old_domains_vm('KVM');
    _remove_old_domains_vm('Void');
426
    _remove_old_domains_kvm();
427
428
}

Francesc Guasch's avatar
Francesc Guasch committed
429
430
431
432
433
434
435
sub _activate_storage_pools($vm) {
    for my $sp ($vm->vm->list_all_storage_pools()) {
        next if $sp->is_active;
        diag("Activating sp ".$sp->get_name." on ".$vm->name);
        $sp->create();
    }
}
436
sub _remove_old_disks_kvm {
437
    return if !$VM_VALID{'KVM'};
438
    my $vm = shift;
439

440
441
    my $name = base_domain_name();
    confess "Unknown base domain name " if !$name;
442

443
    if (!$vm) {
444
445
446
447
        my $rvd_back = rvd_back();
        $vm = $rvd_back->search_vm('KVM');
    }

448
    if (!$vm || !$vm->vm) {
449
450
        return;
    }
451
#    ok($vm,"I can't find a KVM virtual manager") or return;
452

453
    eval { $vm->_refresh_storage_pools() };
454
    return if $@ && $@ =~ /Cannot recv data/;
455

456
457
    ok(!$@,"Expecting error = '' , got '".($@ or '')."'"
        ." after refresh storage pool") or return;
458

459
460
461
462
463
    for my $pool( $vm->vm->list_all_storage_pools ) {
        for my $volume  ( $pool->list_volumes ) {
            next if $volume->get_name !~ /^${name}_\d+.*\.(img|raw|ro\.qcow2|qcow2)$/;
            $volume->delete();
        }
464
465
466
    }
    $vm->storage_pool->refresh();
}
467
468
469
470
471
472
473
sub _remove_old_disks_void($node=undef){
    if (! defined $node || $node->is_local) {
       _remove_old_disks_void_local();
    } else {
       _remove_old_disks_void_remote($node);
    }
}
474

475
476
sub _remove_old_disks_void_remote($node) {
    confess "Remote node must be defined"   if !defined $node;
477
478
    return if !$node->ping;

479
    my $cmd = "rm -rfv ".$node->dir_img."/".base_domain_name().'_*';
480
    $node->run_command($cmd);
481
}
482

483
sub _remove_old_disks_void_local {
484
485
    my $name = base_domain_name();

486
    my $dir_img =  Ravada::Front::Domain::Void::_config_dir();
487
488
    opendir my $ls,$dir_img or return;
    while (my $file = readdir $ls ) {
489
        next if $file !~ /^${name}_\d/;
490
491
492
493
494
495
496
497
498
499
500

        my $disk = "$dir_img/$file";
        next if ! -f $disk;

        unlink $disk or die "I can't remove $disk";

    }
    closedir $ls;
}

sub remove_old_disks {
501
502
    _remove_old_disks_void();
    _remove_old_disks_kvm();
503
504
}

505
506
sub create_user {
    my ($name, $pass, $is_admin) = @_;
507

508
    Ravada::Auth::SQL::add_user(name => $name, password => $pass, is_admin => $is_admin);
509
510
511
512
513
514
515
516
517

    my $user;
    eval {
        $user = Ravada::Auth::SQL->new(name => $name, password => $pass);
    };
    die $@ if !$user;
    return $user;
}

Francesc Guasch's avatar
Francesc Guasch committed
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
sub create_ldap_user($name, $password) {

    if ( Ravada::Auth::LDAP::search_user($name) ) {
        diag("Removing $name");
        Ravada::Auth::LDAP::remove_user($name)  
    }

    my $user = Ravada::Auth::LDAP::search_user($name);
    ok(!$user,"I shouldn't find user $name in the LDAP server") or return;

    my $user_db = Ravada::Auth::SQL->new( name => $name);
    $user_db->remove();
    # check for the user in the SQL db, he shouldn't be  there
    #
    my $sth = $CONNECTOR->dbh->prepare("SELECT * FROM users WHERE name=?");
    $sth->execute($name);
    my $row = $sth->fetchrow_hashref;
    $sth->finish;
    ok(!$row->{name},"I shouldn't find $name in the SQL db ".Dumper($row));

    eval { $user = Ravada::Auth::LDAP::add_user($name,$password) };
    is($@,'') or return;

    my @user = Ravada::Auth::LDAP::search_user($name);
    return $user[0];
}

545
546
sub wait_request {
    my $req = shift;
547
548
549
    for my $cnt ( 0 .. 10 ) {
        diag("Request ".$req->id." ".$req->command." ".$req->status." ".localtime(time))
            if $cnt > 2;
550
551
552
553
554
555
        last if $req->status eq 'done';
        sleep 2;
    }

}

Francesc Guasch's avatar
Francesc Guasch committed
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
sub init_vm {
    my $vm = shift;
    return if $vm->type =~ /void/i;
    _qemu_storage_pool($vm) if $vm->type =~ /qemu/i;
}

sub _exists_storage_pool {
    my ($vm, $pool_name) = @_;
    for my $pool ($vm->vm->list_storage_pools) {
        return 1 if $pool->get_name eq $pool_name;
    }
    return;
}

sub _qemu_storage_pool {
    my $vm = shift;

573
574
575
576
    my $pool_name = new_pool_name();

    if ( _exists_storage_pool($vm, $pool_name)) {
        $vm->default_storage_pool_name($pool_name);
Francesc Guasch's avatar
Francesc Guasch committed
577
578
579
580
581
        return;
    }

    my $uuid = Ravada::VM::KVM::_new_uuid('68663afc-aaf4-4f1f-9fff-93684c260942');

582
    my $dir = "/var/tmp/$pool_name";
Francesc Guasch's avatar
Francesc Guasch committed
583
584
585
586
    mkdir $dir if ! -e $dir;

    my $xml =
"<pool type='dir'>
587
  <name>$pool_name</name>
Francesc Guasch's avatar
Francesc Guasch committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
  <uuid>$uuid</uuid>
  <capacity unit='bytes'></capacity>
  <allocation unit='bytes'></allocation>
  <available unit='bytes'></available>
  <source>
  </source>
  <target>
    <path>$dir</path>
    <permissions>
      <mode>0711</mode>
      <owner>0</owner>
      <group>0</group>
    </permissions>
  </target>
</pool>"
;
    my $pool;
    eval { $pool = $vm->vm->create_storage_pool($xml) };
    ok(!$@,"Expecting \$@='', got '".($@ or '')."'") or return;
    ok($pool,"Expecting a pool , got ".($pool or ''));

609
    $vm->default_storage_pool_name($pool_name);
Francesc Guasch's avatar
Francesc Guasch committed
610
611
612
}

sub remove_qemu_pools {
613
614
615
616
617
618
619
620
    return if !$VM_VALID{'KVM'} || $>;
    my $vm;
    eval { $vm = rvd_back->search_vm('kvm') };
    if ($@ && $@ !~ /Missing qemu-img/) {
        warn $@;
    }
    if  ( !$vm ) {
        $VM_VALID{'KVM'} = 0;
621
        return;
622
    }
623

Francesc Guasch's avatar
Francesc Guasch committed
624
    my $base = base_pool_name();
625
    for my $pool  ( $vm->vm->list_all_storage_pools) {
626
        my $name = $pool->get_name;
Francesc Guasch's avatar
Francesc Guasch committed
627
        next if $name !~ qr/^$base/;
628
        diag("Removing ".$pool->get_name." storage_pool");
629
630
631
632
        for my $vol ( $pool->list_volumes ) {
            diag("Removing ".$pool->get_name." vol ".$vol->get_name);
            $vol->delete();
        }
633
634
        $pool->destroy();
        eval { $pool->undefine() };
635
        warn $@ if$@ && $@ !~ /libvirt error code: 49,/;
636
637
        ok(!$@ or $@ =~ /Storage pool not found/i);
    }
Francesc Guasch's avatar
Francesc Guasch committed
638
639
640
641
642
643
644
645

}

sub remove_old_pools {
    remove_qemu_pools();
}

sub clean {
646
    my $file_remote_config = shift;
Francesc Guasch's avatar
Francesc Guasch committed
647
648
649
    remove_old_domains();
    remove_old_disks();
    remove_old_pools();
Francesc Guasch's avatar
Francesc Guasch committed
650

651
652
653
654
655
656
657

    if ($file_remote_config) {
        my $config;
        eval { $config = LoadFile($file_remote_config) };
        warn $@ if $@;
        _clean_remote_nodes($config)    if $config;
    }
Francesc Guasch's avatar
Francesc Guasch committed
658
    _clean_db();
659
    _clean_file_config();
Francesc Guasch's avatar
Francesc Guasch committed
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
}

sub _clean_db {
    my $sth = $CONNECTOR->dbh->prepare(
        "DELETE FROM vms "
    );
    $sth->execute;
    $sth->finish;

    $sth = $CONNECTOR->dbh->prepare(
        "DELETE FROM domains"
    );
    $sth->execute;
    $sth->finish;

Francesc Guasch's avatar
Francesc Guasch committed
675
676
}

677
sub clean_remote {
Francesc Guasch's avatar
Francesc Guasch committed
678
679
680
681
682
683
    return if ! -e $FILE_CONFIG_REMOTE;

    my $conf;
    eval { $conf = LoadFile($FILE_CONFIG_REMOTE) };
    return if !$conf;
    for my $vm_name (keys %$conf) {
684
685
686
        my $vm;
        eval { $vm = rvd_back->search_vm($vm_name) };
        warn $@ if $@;
Francesc Guasch's avatar
Francesc Guasch committed
687
688
689
690
        next if !$vm;

        my $node;
        eval { $node = $vm->new(%{$conf->{$vm_name}}) };
691
        next if ! $node;
692
        if ( !$node->_do_is_active ) {
693
694
695
            $node->remove;
            next;
        }
Francesc Guasch's avatar
Francesc Guasch committed
696
697

        clean_remote_node($node);
Francesc Guasch's avatar
Francesc Guasch committed
698
        _remove_old_domains_vm($node);
699
        _remove_old_disks_kvm($node) if $vm_name =~ /^kvm/i;
700
        $node->remove();
Francesc Guasch's avatar
Francesc Guasch committed
701
    }
Francesc Guasch's avatar
Francesc Guasch committed
702
}
703

704
705
706
707
708
709
710
711
sub _clean_remote_nodes {
    my $config = shift;
    for my $name (keys %$config) {
        diag("Cleaning $name");
        my $node;
        my $vm = rvd_back->search_vm($config->{$name}->{type});
        eval { $node = $vm->new($config->{$name}) };
        warn $@ if $@;
712
        next if !$node || !$node->_do_is_active;
Francesc Guasch's avatar
Francesc Guasch committed
713
714

        clean_remote_node($node);
715
716
717
718

    }
}

Francesc Guasch's avatar
Francesc Guasch committed
719
720
721
722
sub clean_remote_node {
    my $node = shift;

    _remove_old_domains_vm($node);
723
    _remove_old_disks($node);
724
    flush_rules_node($node)  if !$node->is_local();
725
726
727
728
729
730
731
732
733
734
735
}

sub _remove_old_disks {
    my $node = shift;
    if ( $node->type eq 'KVM' ) {
        _remove_old_disks_kvm($node);
    }elsif ($node->type eq 'Void') {
        _remove_old_disks_void($node);
    }   else {
        die "I don't know how to remove ".$node->type." disks";
    }
Francesc Guasch's avatar
Francesc Guasch committed
736
}
737

Francesc Guasch's avatar
Francesc Guasch committed
738
739
740
741
742
743
sub remove_old_user {
    $USER_ADMIN->remove if $USER_ADMIN;
    confess "Undefined connector" if !defined $CONNECTOR;
    my $sth = $CONNECTOR->dbh->prepare("DELETE FROM users WHERE name=?");
    $sth->execute(base_domain_name());
}
744
745
sub search_id_iso {
    my $name = shift;
Francesc Guasch's avatar
Francesc Guasch committed
746
    connector() if !$CONNECTOR;
747
748
749
750
751
752
753
754
755
    my $sth = $CONNECTOR->dbh->prepare("SELECT id FROM iso_images "
        ." WHERE name like ?"
    );
    $sth->execute("$name%");
    my ($id) = $sth->fetchrow;
    die "There is no iso called $name%" if !$id;
    return $id;
}

Francesc Guasch's avatar
Francesc Guasch committed
756
757
758
759
760
761
762
sub search_iptable_remote {
    my %args = @_;
    my $node = delete $args{node};
    my $remote_ip = delete $args{remote_ip};
    my $local_ip = delete $args{local_ip};
    my $local_port= delete $args{local_port};
    my $jump = (delete $args{jump} or 'ACCEPT');
763
    my $iptables = $node->iptables_list();
Francesc Guasch's avatar
Francesc Guasch committed
764
765
766
767
768
769
770
771
772
773
774

    $remote_ip .= "/32" if defined $remote_ip && $remote_ip !~ m{/};
    $local_ip .= "/32"  if defined $local_ip && $local_ip !~ m{/};

    my @found;

    my $count = 0;
    for my $line (@{$iptables->{filter}}) {
        my %args = @$line;
        next if $args{A} ne $CHAIN;
        $count++;
775
776
777
778
779
780
781

        if(
              (!defined $jump      || exists $args{j} && $args{j} eq $jump )
           && (!defined $remote_ip || exists $args{s} && $args{s} eq $remote_ip )
           && (!defined $local_ip  || exists $args{d} && $args{d} eq $local_ip )
           && (!defined $local_port|| exists $args{dport} && $args{dport} eq $local_port)
        ){
Francesc Guasch's avatar
Francesc Guasch committed
782
783
784
785
786
787
788
789
790

            push @found,($count);
        }
    }
    return @found   if wantarray;
    return if !scalar@found;
    return $found[0];
}

791
sub flush_rules_node($node) {
792
    $node->create_iptables_chain($CHAIN);
793
794
    $node->run_command("/sbin/iptables","-F", $CHAIN);
    $node->run_command("/sbin/iptables","-X", $CHAIN);
Francesc Guasch's avatar
Francesc Guasch committed
795
796
}

797
sub flush_rules {
Francesc Guasch's avatar
Francesc Guasch committed
798
    return if $>;
799
800
801
802
803

    my @cmd = ('iptables','-t','nat','-F','PREROUTING');
    my ($in,$out,$err);
    run3(\@cmd, \$in, \$out, \$err);
    die $err if $err;
Francesc Guasch's avatar
Francesc Guasch committed
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819

    @cmd = ('iptables','-L','INPUT');
    run3(\@cmd, \$in, \$out, \$err);

    my $count = -2;
    my @found;
    for my $line ( split /\n/,$out ) {
        $count++;
        next if $line !~ /^RAVADA /;
        push @found,($count);
    }
    @cmd = ('iptables','-D','INPUT');
    for my $n (reverse @found) {
        run3([@cmd, $n], \$in, \$out, \$err);
        warn $err if $err;
    }
820
821
}

Francesc Guasch's avatar
Francesc Guasch committed
822
823
sub _domain_node($node) {
    my $vm = rvd_back->search_vm('KVM','localhost');
824
    ok($vm) or die Dumper(rvd_back->_create_vm);
Francesc Guasch's avatar
Francesc Guasch committed
825
826
827
828
829
830
831
    my $domain = $vm->search_domain($node->name);
    $domain = rvd_back->import_domain(name => $node->name
            ,user => user_admin->name
            ,vm => 'KVM'
            ,spinoff_disks => 0
    )   if !$domain || !$domain->is_known;

832
    ok($domain->id,"Expecting an ID for domain ") or exit;
Francesc Guasch's avatar
Francesc Guasch committed
833
834
835
836
    $domain->_set_vm($vm, 'force');
    return $domain;
}

837
838
839
840
841
842
843
844
845
846
847
sub hibernate_node($node) {
    diag("hibernate node ".$node->type." ".$node->name);
    if ($node->is_active) {
        for my $domain ($node->list_domains()) {
            diag("Shutting down ".$domain->name." on node ".$node->name);
            $domain->shutdown_now(user_admin);
        }
    }
    $node->disconnect;

    my $domain_node = _domain_node($node);
848
    $domain_node->hibernate( user_admin );
849
850
851
852
853
854
855
856
857
858
859
860

    my $max_wait = 30;
    my $ping;
    for ( 1 .. $max_wait ) {
        diag("Waiting for node ".$node->name." to be inactive ...")  if !($_ % 10);
        $ping = $node->ping;
        last if !$ping;
        sleep 1;
    }
    is($ping,0, "Expecting node ".$node->name." hibernated not pingable");
}

861
862
sub shutdown_node($node) {

863
    diag("shutdown node ".$node->type." ".$node->name);
864
    if ($node->is_active) {
865
866
        $node->run_command("service lightdm stop");
        $node->run_command("service gdm stop");
867
868
869
870
871
872
873
874
875
876
877
878
        for my $domain ($node->list_domains()) {
            diag("Shutting down ".$domain->name." on node ".$node->name);
            $domain->shutdown_now(user_admin);
        }
    }
    $node->disconnect;

    my $domain_node = _domain_node($node);
    eval {
        $domain_node->shutdown(user => user_admin);# if !$domain_node->is_active;
    };
    sleep 2 if !$node->ping;
Francesc Guasch's avatar
Francesc Guasch committed
879

880
881
    my $max_wait = 120;
    for ( 1 .. $max_wait / 2 ) {
Francesc Guasch's avatar
Francesc Guasch committed
882
        diag("Waiting for node ".$node->name." to be inactive ...")  if !($_ % 10);
883
884
885
886
887
888
889
890
        last if !$node->ping;
        sleep 1;
    }
    is($node->ping,0);
}

sub start_node($node) {

891
    confess "Undefined node"    if !defined $node;
892
    diag("start node ".$node->type." ".$node->name);
893
894
895
    confess "Undefined node " if!$node;

    $node->disconnect;
896
    if ( $node->_do_is_active ) {
897
898
899
900
901
902
903
904
905
906
        $node->connect && return;
        warn "I can't connect";
    }

    my $domain = _domain_node($node);

    ok($domain->_vm->host eq 'localhost');

    $domain->start(user => user_admin, remote_ip => '127.0.0.1')  if !$domain->is_active;

907
    for ( 1 .. 30 ) {
908
909
        last if $node->ping ;
        sleep 1;
910
        diag("Waiting for ping node ".$node->name." ".$node->ip." $_") if !($_ % 10);
911
912
    }

913
    is($node->ping('debug'),1,"[".$node->type."] Expecting ping node ".$node->name) or exit;
914
915

    for ( 1 .. 20 ) {
916
        last if $node->_do_is_active;
917
        sleep 1;
Francesc Guasch's avatar
Francesc Guasch committed
918
        diag("Waiting for active node ".$node->name." $_") if !($_ % 10);
919
920
    }

921
    is($node->_do_is_active,1,"Expecting active node ".$node->name) or exit;
922
923

    my $connect;
924
    for ( 1 .. 20 ) {
925
        eval { $connect = $node->connect };
926
        warn $@ if $@;
927
928
929
930
931
932
933
        last if $connect;
        sleep 1;
        diag("Waiting for connection to node ".$node->name." $_") if !($_ % 5);
    }
    is($connect,1
            ,"[".$node->type."] "
                .$node->name." Expecting connection") or exit;
934

935
936
    eval { $node->run_command("hwclock","--hctosys") };
    is($@,'',"Expecting no error setting clock on ".$node->name." ".($@ or ''));
937
938
}

Francesc Guasch's avatar
Francesc Guasch committed
939
940
941
942
943
944
945
946
947
948
sub remove_node($node) {
    eval { $node->remove() };
    is(''.$@,'');

    my $node2;
    eval { $node2 = Ravada::VM->open($node->id) };
    like($@,qr"can't find VM");
    ok(!$node2, "Expecting no node ".$node->id);
}

949
950
951
952
953
954
955
956
957
958
959
sub hibernate_domain_internal($domain) {
    start_domain_internal($domain)  if !$domain->is_active;
    if ($domain->type eq 'KVM') {
        $domain->domain->managed_save();
    } elsif ($domain->type eq 'Void') {
        $domain->_store(is_hibernated => 1 );
    } else {
        confess "ERROR: I don't know how to hibernate internal domain of type ".$domain->type;
    }
}

960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
sub _iptables_list {
    my ($in, $out, $err);
    run3(['/sbin/iptables-save'], \$in, \$out, \$err);
    my ( %tables, $ret );

    my ($current_table);
    for my $line (split /\n/, $out) {
        chomp $line;

        next if ( $line eq "COMMIT" );
        next if ( $line =~ m/^#/ );
        next if ( $line =~ m/^:/ );

        if ( $line =~ m/^\*([a-z]+)$/ ) {
            $current_table = $1;
            $tables{$current_table} = [];
            next;
        }

        #my @parts = grep { ! /^\s+$/ && ! /^$/ } split (/(\-\-?[^\s]+\s[^\s]+)/i, $line);
        my @parts = grep { !/^\s+$/ && !/^$/ } split( /^\-\-?|\s+\-\-?/i, $line );

        my @option = ();
        for my $part (@parts) {
            my ( $key, $value ) = split( /\s/, $part, 2 );
            push( @option, $key => $value );
        }

        push( @{ $ret->{$current_table} }, \@option );

    }

    return $ret;
}

sub find_ip_rule {
    my %args = @_;
    my $remote_ip = delete $args{remote_ip};
    my $local_ip = delete $args{local_ip};
    my $local_port= delete $args{local_port};
1000
    my $jump = ( delete $args{jump} or 'ACCEPT');
For faster browsing, not all history is shown. View entire blame