-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathFront.pm
More file actions
2182 lines (1666 loc) · 54 KB
/
Front.pm
File metadata and controls
2182 lines (1666 loc) · 54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
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
1000
package Ravada::Front;
use strict;
use warnings;
=head1 NAME
Ravada::Front - Web Frontend library for Ravada
=cut
use Carp qw(carp);
use DateTime;
use DateTime::Format::DateParse;
use Hash::Util qw(lock_hash);
use IPC::Run3 qw(run3);
use JSON::XS;
use Moose;
use Storable qw(dclone);
use Ravada;
use Ravada::Auth::LDAP;
use Ravada::Front::Domain;
use Ravada::Front::Domain::KVM;
use Ravada::Route;
use feature qw(signatures);
no warnings "experimental::signatures";
use Data::Dumper;
has 'config' => (
is => 'ro'
,isa => 'Str'
);
has 'connector' => (
is => 'rw'
);
has 'backend' => (
is => 'ro',
isa => 'Ravada'
);
has 'fork' => (
is => 'rw'
,isa => 'Int'
,default => 1
);
our $CONNECTOR;# = \$Ravada::CONNECTOR;
our $TIMEOUT = 20;
our @VM_TYPES = ('KVM');
our %VM;
our %VM_ID;
our $PID_FILE_BACKEND = '/var/run/rvd_back.pid';
our $LOCAL_TZ = DateTime::TimeZone->new(name => 'local');
###########################################################################
#
# method modifiers
#
around 'list_machines' => \&_around_list_machines;
=head2 BUILD
Internal constructor
=cut
sub BUILD {
my $self = shift;
if ($self->connector) {
$CONNECTOR = $self->connector;
} else {
Ravada::_init_config($self->config()) if $self->config;
$CONNECTOR = Ravada::_connect_dbh();
}
Ravada::_init_config($self->config()) if $self->config;
Ravada::Auth::init($Ravada::CONFIG);
$CONNECTOR->dbh();
@VM_TYPES = @{$Ravada::CONFIG->{vm}};
}
=head2 list_bases
Returns a list of the base domains as a listref
my $bases = $rvd_front->list_bases();
=cut
sub list_bases($self, %args) {
$args{is_base} = 1;
my $query = "SELECT name, id, is_base, id_owner FROM domains "
._where(%args)
." ORDER BY name";
my $sth = $CONNECTOR->dbh->prepare($query);
$sth->execute(map { $args{$_} } sort keys %args);
my @bases = ();
while ( my $row = $sth->fetchrow_hashref) {
my $domain;
eval { $domain = $self->search_domain($row->{name}) };
next if !$domain;
$row->{has_clones} = $domain->has_clones;
$row->{is_locked} = 0 if !exists $row->{is_locked};
delete $row->{spice_password};
push @bases, ($row);
}
$sth->finish;
return \@bases;
}
=head2 list_machines_user
Returns a list of machines available to the user
If the user has ever clone the base, it shows this information. It show the
base data if not.
Arguments: user
Returns: listref of machines
=cut
sub list_machines_user($self, $user, $access_data={}) {
my $sth = $CONNECTOR->dbh->prepare(
"SELECT id,name,alias,is_public, description, screenshot, id_owner, is_base, date_changed, show_clones"
." FROM domains "
." WHERE ( is_base=1 OR ( id_base IS NULL AND id_owner=?))"
." ORDER BY alias"
);
my ($id, $name, $alias, $is_public, $description, $screenshot, $id_owner, $is_base, $date_changed, $show_clones);
$sth->execute($user->id);
$sth->bind_columns(\($id, $name, $alias, $is_public, $description, $screenshot, $id_owner, $is_base, $date_changed,$show_clones));
my $bookings_enabled = $self->setting('/backend/bookings');
my @list;
while ( $sth->fetch ) {
# check if enabled settings and this user not allowed
next if $bookings_enabled && !Ravada::Front::Domain->open($id)->allowed_booking($user);
my @clones = $self->search_clone(
id_owner =>$user->id
,id_base => $id
);
push @clones,$self->_search_shared($id, $user->id);
my ($clone) = ($clones[0] or undef);
next unless
$clone && $show_clones && $user->allowed_access_group($id)
|| $user->is_admin
|| ($is_public && $user->allowed_access($id))
|| ($id_owner == $user->id);
$name = $alias if defined $alias;
my $base = { id => $id, name => Encode::decode_utf8($name)
, alias => Encode::decode_utf8($alias or $name)
, is_public => ($is_public or 0)
, screenshot => ($screenshot or '')
, description => ($description or '')
, id_clone => undef
, name_clone => undef
, is_base => $is_base
, can_prepare_base => 0
};
next unless $self->_access_allowed($id, $base->{id_clone}, $access_data) || ($id_owner == $user->id);
_copy_clone_info($user, $base, \@clones);
if (!$is_base) {
$base = _get_clone_info($user, $base);
$base->{is_public} = 0;
$base->{is_base} = 0;
$base->{list_clones} = [];
$base->{can_prepare_base} = 1 if $user->can_create_base();
}
lock_hash(%$base);
push @list,($base);
}
$sth->finish;
return \@list;
}
sub _copy_clone_info($user, $base, $clones) {
my @list;
for my $clone (@$clones) {
my $c = _get_clone_info($user, $base, $clone);
push @list,($c);
}
$base->{list_clones} = \@list;
}
sub _get_clone_info($user, $base, $clone = Ravada::Front::Domain->open($base->{id})) {
my $c = {id => $clone->id
,name => $clone->name
,alias => $clone->alias
,is_active => $clone->is_active
,screenshot => $clone->_data('screenshot')
,date_changed => $clone->_data('date_changed')
,autostart=> $clone->_data('autostart')
};
$c->{can_hibernate} = ($clone->is_active && !$clone->is_volatile);
$c->{can_shutdown} = $clone->is_active;
$c->{is_locked} = $clone->is_locked;
$c->{description} = ( $clone->_data('description')
or $base->{description});
$c->{can_remove} = ( $user->can_remove() && $user->id == $clone->_data('id_owner'));
$c->{can_remove} = 0 if !$c->{can_remove};
if ($clone->is_active && !$clone->is_locked
&& $user->can_screenshot) {
my $req = Ravada::Request->screenshot(
id_domain => $clone->id
);
}
return $c;
}
sub _access_allowed($self, $id_base, $id_clone, $access_data) {
if ($id_clone) {
my $clone = Ravada::Front::Domain->open($id_clone);
my $allowed = $clone->access_allowed(%$access_data);
return $allowed if $allowed;
}
my $base = Ravada::Front::Domain->open($id_base);
my $allowed = $base->access_allowed(%$access_data);
return 1 if !defined $allowed;
return $allowed;
}
sub list_machines($self, $user, @filter) {
return $self->list_domains(@filter) if $user->can_list_machines();
my @list = ();
push @list,(@{filter_base_without_clones($self->list_domains(@filter))}) if $user->can_list_clones();
push @list,(@{$self->_list_own_clones($user)}) if $user->can_list_clones_from_own_base();
push @list,(@{$self->_list_own_machines($user)}) if $user->can_list_own_machines();
return [@list] if scalar @list < 2;
my %uniq = map { $_->{name} => $_ } @list;
return [sort { $a->{name} cmp $b->{name} } values %uniq];
}
sub _init_available_actions($user, $m) {
eval { $m->{can_shutdown} = $user->can_shutdown($m->{id}) };
$m->{can_start} = 0;
$m->{can_start} = 1 if $m->{id_owner} == $user->id || $user->is_admin
|| $user->_machine_shared($m->{id})
;
$m->{can_reboot} = $m->{can_shutdown} && $m->{can_start};
$m->{can_view} = 0;
$m->{can_view} = 1 if $m->{id_owner} == $user->id || $user->is_admin
|| $user->_machine_shared($m->{id})
;
$m->{can_manage} = ( $user->can_manage_machine($m->{id}) or 0);
eval {
$m->{can_change_settings} = ( $user->can_change_settings($m->{id}) or 0);
};
#may have been deleted just now
next if $@ && $@ =~ /Unknown domain/;
die $@ if $@;
$m->{can_hibernate} = 0;
$m->{can_hibernate} = 1 if $user->can_shutdown($m->{id})
&& !$m->{is_volatile};
warn $@ if $@;
}
sub _around_list_machines($orig, $self, $user, @filter) {
my $machines = $self->$orig($user, @filter);
for my $m (@$machines) {
_init_available_actions($user, $m);
$m->{id_base} = undef if !exists $m->{id_base};
lock_hash(%$m);
}
return $machines;
}
=pod
sub search_clone_data {
my $self = shift;
my %args = @_;
my $query = "SELECT * FROM domains WHERE "
.(join(" AND ", map { "$_ = ? " } sort keys %args));
my $sth = $CONNECTOR->dbh->prepare($query);
$sth->execute( map { $args{$_} } sort keys %args );
my $row = $sth->fetchrow_hashref;
return ( $row or {});
}
=cut
=head2 list_domains
Returns a list of the domains as a listref
my $bases = $rvd_front->list_domains();
=cut
sub list_domains($self, %args) {
my $query = "SELECT d.name,d.alias, d.id, id_base, is_base, id_vm, status, is_public "
." ,vms.name as node , is_volatile, client_status, id_owner "
." ,comment, is_pool, show_clones"
." ,d.has_clones, d.is_locked"
." ,d.client_status, d.date_status_change, d.autostart "
." ,d.date_changed"
." FROM domains d LEFT JOIN vms "
." ON d.id_vm = vms.id ";
my ($where, $values) = $self->_create_where(\%args);
my $sth = $CONNECTOR->dbh->prepare("$query $where ORDER BY d.id");
$sth->execute(@$values);
my @domains = ();
while ( my $row = $sth->fetchrow_hashref) {
for (qw(is_hibernated is_paused)) {
$row->{$_} = 0;
}
$row->{remote_ip} = undef;
$row->{name}=Encode::decode_utf8($row->{alias})
if defined $row->{alias} && length($row->{alias});
$row->{is_active} = 0;
$row->{remote_ip} = undef;
{
if ($row->{status} =~ /active|starting/) {
$row->{is_active} = 1;
$row->{is_hibernated} = 0;
$row->{is_paused} = 0;
$row->{remote_ip} = Ravada::Domain::remote_ip($row->{id});
} else {
$row->{is_active} = 0;
$row->{is_hibernated} = 1 if $row->{status} eq 'hibernated';
$row->{is_paused} = 1 if $row->{status} eq 'paused';
}
$row->{node} = $self->_node_name($row->{id_vm});
if (defined $row->{client_status}) {
($row->{remote_ip}) = $row->{client_status} =~ /onnected.*?\((.*)\)/;
$row->{remote_ip} = $row->{client_status} if ! $row->{remote_ip};
}
if (!$row->{status} ) {
if ($row->{is_active}) {
$row->{status} = 'active';
} elsif ($row->{is_hibernated}) {
$row->{status} = 'hibernated';
} else {
$row->{status} = 'down';
}
}
$row->{date_status_change} = Ravada::Domain::_date_status_change($row->{date_status_change});
}
delete $row->{spice_password};
push @domains, ($row);
}
$sth->finish;
return \@domains;
}
sub _create_where($self, $args) {
my $where = '';
my @values;
my $date_changed = delete $args->{date_changed};
for my $field ( sort keys %$args ) {
$where .= " OR " if $where;
if (!defined $args->{$field}) {
$where .= " $field IS NULL ";
next;
}
my $operation = "=";
$operation = ">=" if $field eq 'date_changed';
$operation = "like" if $field eq 'name';
if (!ref($args->{$field})) {
$where .= " d.$field $operation ?";
if ($field eq 'name') {
push @values,('%'.$args->{$field}.'%');
} else {
push @values,($args->{$field});
}
} else {
my $option = '';
for my $value ( @{$args->{$field}} ) {
$option .= " OR " if $option;
if (!defined $value) {
$option .= " d.$field IS NULL ";
next;
}
$option .= " d.$field=? ";
push @values,($value);
}
$where .= " ($option) ";
}
}
if ($date_changed) {
$where = " ( $where ) AND " if $where ;
$where .= " d.date_changed >= ? ";
push @values, ($date_changed);
}
$where = "WHERE $where" if $where;
return ($where,\@values);
}
sub _node_name($self, $id_vm) {
return $self->{_node_name}->{$id_vm}
if $self->{_node_name}->{$id_vm};
my $sth = $self->_dbh->prepare("SELECT name FROM vms "
." WHERE id=?"
);
$sth->execute($id_vm);
my ($name) = $sth->fetchrow;
$self->{_node_name}->{$id_vm} = $name;
return $name;
}
=head2 filter_base_without_clones
filters the list of domains and drops all machines that are unacessible and
bases with 0 machines accessible
=cut
sub filter_base_without_clones($domains) {
my @list;
my $size_domains = scalar(@$domains);
for (my $i = 0; $i < $size_domains; ++$i) {
if (@$domains[$i]->{is_base}) {
for (my $j = 0; $j < $size_domains; ++$j) {
if ($j != $i && !($domains->[$j]->{is_base})
&& defined $domains->[$j]->{id_base}
&& $domains->[$j]->{id_base} eq $domains->[$i]->{id}) {
push @list, ($domains->[$i]);
last;
}
}
}
else {
push @list, (@$domains[$i]);
}
}
return \@list;
}
sub _list_own_clones($self, $user) {
my $machines = $self->list_bases( id_owner => $user->id );
for my $base (@$machines) {
confess "ERROR: BAse without id ".Dumper($base) if !$base->{id};
push @$machines,@{$self->list_domains( id_base => $base->{id} )};
}
return $machines;
}
sub _list_own_machines($self, $user) {
my $machines = $self->list_domains(id_owner => $user->id);
for my $clone (@$machines) {
next if !$clone->{id_base};
push @$machines,@{$self->list_domains( id => $clone->{id_base} )};
}
return $machines;
}
sub _where(%args) {
my $where = '';
for my $field ( sort keys %args ) {
$where .= " AND " if $where;
$where .= " $field=?"
}
$where = "WHERE $where" if $where;
return $where;
}
=head2 list_clones
Returns a list of the domains that are clones as a listref
my $clones = $rvd_front->list_clones();
=cut
sub list_clones {
my $self = shift;
my %args = @_;
my $domains = $self->list_domains();
my @clones;
for (@$domains ) {
if($_->{id_base}) { push @clones, ($_); }
}
return \@clones;
}
sub _remove_domain_db($self, $id) {
my $sth = $CONNECTOR->dbh->prepare("DELETE FROM domains WHERE id=?");
$sth->execute($id);
$sth->finish;
}
=head2 domain_info
Returns information of a domain
my $info = $rvd_front->domain_info( id => $id);
my $info = $rvd_front->domain_info( name => $name);
=cut
sub domain_info {
my $self = shift;
my $domains = $self->list_domains(@_);
return $domains->[0];
}
=head2 domain_exists
Returns true if the domain name exists
if ($rvd->domain_exists('domain_name')) {
...
}
=cut
sub domain_exists {
my $self = shift;
my $name = shift;
my $sth = $self->_dbh->prepare(
"SELECT id FROM domains "
." WHERE (name=? OR alias=?) "
." AND ( is_volatile = 0 "
." OR is_volatile=1 AND status = 'active' "
." ) "
);
$sth->execute($name,$name);
my ($id) = $sth->fetchrow;
$sth->finish;
return 0 if !defined $id;
return 1;
}
=head2 node_exists
Returns true if the node name exists
if ($rvd->node('node_name')) {
...
}
=cut
sub node_exists {
my $self = shift;
my $name = shift;
my $sth = $CONNECTOR->dbh->prepare(
"SELECT id FROM vms"
." WHERE name=? "
);
$sth->execute($name);
my ($id) = $sth->fetchrow;
$sth->finish;
return 0 if !defined $id;
return 1;
}
=head2 list_vm_types
Returns a reference to a list of Virtual Machine Managers known by the system
=cut
sub list_vm_types {
my $self = shift;
return $self->{cache}->{vm_types} if $self->{cache}->{vm_types};
my $result = [sort @VM_TYPES];
$self->{cache}->{vm_types} = $result if $result->[0];
return $result;
}
=head2 list_vms
Returns a list of Virtual Managers
=cut
sub list_vms($self, $type=undef) {
my $sql = "SELECT id,name,hostname,is_active, vm_type, enabled FROM vms ";
my @args = ();
if ($type) {
$sql .= "WHERE (vm_type=? or vm_type=?)";
my $type2 = $type;
$type2 = 'qemu' if $type eq 'KVM';
@args = ( $type, $type2);
}
my $sth = $CONNECTOR->dbh->prepare($sql." ORDER BY vm_type,name");
$sth->execute(@args);
my @list;
while (my $row = $sth->fetchrow_hashref) {
$row->{bases}= $self->_list_bases_vm($row->{id});
$row->{machines}= $self->_list_machines_vm($row->{id});
$row->{type} = $row->{vm_type};
$row->{action_remove} = 'disabled' if length defined $row->{machines}[0];
$row->{action_remove} = 'disabled' if $row->{hostname} eq 'localhost';
$row->{action_remove} = 'disabled' if length defined $row->{bases}[0];
$row->{is_local} = 0;
$row->{is_local} = 1 if $row->{hostname} =~ /^(localhost|127)/;
delete $row->{vm_type};
lock_hash(%$row);
push @list,($row);
}
$sth->finish;
return @list;
}
=head2 list_nodes_by_id
Returns a list of Nodes by id
=cut
sub list_nodes_by_id($self, $type=undef) {
my $sql = "SELECT id,name,hostname,is_active, vm_type, enabled FROM vms ";
my @args = ();
if ($type) {
$sql .= "WHERE (vm_type=? or vm_type=?)";
my $type2 = $type;
$type2 = 'qemu' if $type eq 'KVM';
@args = ( $type, $type2);
}
my $sth = $CONNECTOR->dbh->prepare($sql." ORDER BY vm_type,name");
$sth->execute(@args);
my %list;
while (my $row = $sth->fetchrow_hashref) {
$list{$row->{id}}= $row->{name};
}
$sth->finish;
return \%list;
}
sub _list_bases_vm($self, $id_node) {
my $sth = $CONNECTOR->dbh->prepare(
"SELECT d.id FROM domains d,bases_vm bv"
." WHERE d.is_base=1"
." AND d.id = bv.id_domain "
." AND bv.id_vm=?"
." AND bv.enabled=1"
);
my @bases;
$sth->execute($id_node);
while ( my ($id_domain) = $sth->fetchrow ) {
push @bases,($id_domain);
}
$sth->finish;
return \@bases;
}
sub _list_bases_vm_all($self, $id_node) {
my $sth = $CONNECTOR->dbh->prepare(
"SELECT d.id, d.name FROM domains d, vms v "
." WHERE is_base=? AND vm=v.vm_type"
." AND d.vm =v.vm_type"
." AND v.id=?"
." ORDER BY d.name "
);
$sth->execute(1, $id_node);
my $sth_bv = $CONNECTOR->dbh->prepare(
"SELECT bv.enabled FROM bases_vm bv, domains d "
." WHERE bv.id_domain=? AND bv.id_vm=?"
." AND d.id = bv.id_domain "
);
my ($id_domain, $name_domain);
$sth->bind_columns(\($id_domain, $name_domain));
my $sth_clones = $CONNECTOR->dbh->prepare(
"SELECT count(*) FROM domain_instances "
." WHERE id_vm=? AND id_domain IN (SELECT id FROM domains WHERE id_base=?) "
);
my @bases;
while ( $sth->fetch ) {
$sth_bv->execute($id_domain, $id_node);
my ($enabled) = $sth_bv->fetchrow;
$sth_clones->execute($id_node,$id_domain);
my ($n_clones) = $sth_clones->fetchrow();
push @bases,{
id => $id_domain
,name => $name_domain
,clones => $n_clones
,enabled => ( $enabled or 0)
};
}
return \@bases;
}
sub _list_machines_vm($self, $id_node) {
my $sth = $CONNECTOR->dbh->prepare(
"SELECT d.id, name FROM domains d"
." WHERE d.status='active'"
." AND d.id_vm=?"
);
my @bases;
$sth->execute($id_node);
while ( my ($id_domain, $name) = $sth->fetchrow ) {
push @bases,({ id => $id_domain, name => $name });
}
$sth->finish;
return \@bases;
}
=head2 list_iso_images
Returns a reference to a list of the ISO images known by the system
=cut
sub list_iso_images {
my $self = shift;
my $vm_name = shift;
my $vm;
my @iso;
my $sth = $CONNECTOR->dbh->prepare(
"SELECT * FROM iso_images ORDER BY name"
);
$sth->execute;
while (my $row = $sth->fetchrow_hashref) {
$row->{options} = decode_json($row->{options})
if $row->{options};
$row->{min_ram} = 0.2 if !$row->{min_ram};
$row->{min_swap_size} = 0 if !$row->{min_swap_size};
push @iso,($row);
}
$sth->finish;
return \@iso;
}
=head2 iso_file
Returns a reference to a list of the ISOs known by the system
=cut
sub iso_file ($self, $vm_type) {
my $cache = $self->_cache_get("list_isos");
return $cache if $cache;
my $req = Ravada::Request->list_isos(
vm_type => $vm_type
);
return [] if !$req;
$self->wait_request($req);
return [] if $req->status ne 'done';
my $isos = [];
$isos = decode_json($req->output()) if $req->output;
$self->_cache_store("list_isos",$isos);
return $isos;
}
=head2 list_lxc_templates
Returns a reference to a list of the LXC templates known by the system
=cut
sub list_lxc_templates {
my $self = shift;
my @template;
my $sth = $CONNECTOR->dbh->prepare(
"SELECT * FROM lxc_templates ORDER BY name"
);
$sth->execute;
while (my $row = $sth->fetchrow_hashref) {
push @template,($row);
}
$sth->finish;
return \@template;
}
=head2 list_users
Returns a reference to a list of the users
=cut
sub list_users($self,$name=undef) {
my $sth = $CONNECTOR->dbh->prepare("SELECT id, name FROM users ");
$sth->execute();
my @users = ();
while ( my $row = $sth->fetchrow_hashref) {
next if defined $name && $row->{name} !~ /$name/;
push @users, ($row);
}
$sth->finish;
return \@users;
}
=head2 list_bases_network
Returns a reference to a list to all the bases in a network
my $list = $rvd_front->list_bases_network($id_network);
=cut
sub list_bases_network($self, $id_network) {
my $sth = $CONNECTOR->dbh->prepare("SELECT * FROM networks where name = 'default'");
$sth->execute;
my $default = $sth->fetchrow_hashref();
$sth->finish;
lock_hash(%$default);
warn "Warning: all_domains and no_domains both true for default network ".Dumper($default)
if $default->{all_domains} && $default->{no_domains};
my $sth_nd = $CONNECTOR->dbh->prepare("SELECT id,allowed,anonymous FROM domains_network"
." WHERE id_domain=? AND id_network=? "
);
$sth = $CONNECTOR->dbh->prepare("SELECT * FROM domains where is_base=1 "
." ORDER BY name");
$sth->execute();
my @bases;
while (my $row = $sth->fetchrow_hashref) {
$sth_nd->execute($row->{id}, $id_network);
my ($id,$allowed, $anonymous) = $sth_nd->fetchrow;
$row->{anonymous} = ( $anonymous or 0);
if (defined $allowed) {
$row->{allowed} = $allowed;
} else {
$row->{allowed} = $default->{all_domains};
$row->{allowed} = 0 if $default->{no_domains};
}
lock_hash(%$row);
push @bases,($row);
}
return \@bases;
}
=head2 create_domain
Request the creation of a new domain or virtual machine
# TODO: document the args here
my $req = $rvd_front->create_domain( ... );
=cut
sub create_domain {
my $self = shift;
return Ravada::Request->create_domain(@_);
}
=head2 wait_request
Waits for a request for some seconds.
=head3 Arguments
=over
=item * request
=item * timeout (optional defaults to $Ravada::Front::TIMEOUT
=back
Returns: the request
=cut
sub wait_request {
my $self = shift;
my $req = shift or confess "Missing request";
my $timeout = ( shift or $TIMEOUT );
if ( $self->backend ) {
if ($self->fork ) {
$self->backend->process_requests();
} else {
$self->backend->_process_requests_dont_fork();
}
}
for ( 1 .. $timeout ) {
last if $req->status eq 'done';
sleep 1;
}
$req->status("timeout")
if $req->status eq 'working';
$req->refresh();
return $req;
}
=head2 ping_backend
Checks if the backend is alive.
Return true if alive, false otherwise.
=cut
sub ping_backend {
my $self = shift;
my $req = Ravada::Request->ping_backend();
$self->wait_request($req, 2);
return 1 if $req->status() eq 'done';
return 0;
}
sub _ping_backend_localhost {
my $self = shift;
return 1 if -e $PID_FILE_BACKEND;
# TODO check the process with pid $PID_FILE_BACKEND is really alive
return;
}
=head2 open_vm
Connects to a Virtual Machine Manager ( or VMM ( or VM )).
Returns a read-only connection to the VM.
my $vm = $front->open_vm('KVM');
=cut
sub open_vm {
my $self = shift;
my $type = shift or confess "I need vm type";
my $class = "Ravada::VM::$type";