Request.pm 28.7 KB
Newer Older
1
2
3
4
5
package Ravada::Request;

use strict;
use warnings;

6
7
8
9
10
11
=head1 NAME

Ravada::Request - Requests library for Ravada

=cut

12
13
use Carp qw(confess);
use Data::Dumper;
14
use Hash::Util qw(lock_hash);
15
16
use JSON::XS;
use Hash::Util;
Francesc Guasch's avatar
Francesc Guasch committed
17
use Time::Piece;
18
19
20
21
22
use Ravada;
use Ravada::Front;

use vars qw($AUTOLOAD);

23
24
25
no warnings "experimental::signatures";
use feature qw(signatures);

26
27
28
29
30
31
=pod

Request a command to the ravada backend

=cut

Francesc Guasch's avatar
Francesc Guasch committed
32
my $COUNT = 0;
Francesc Guasch's avatar
Francesc Guasch committed
33
our %FIELD = map { $_ => 1 } qw(error output);
34
35
36
37
38
39
40
41
42
our %FIELD_RO = map { $_ => 1 } qw(id name);

our $args_manage = { name => 1 , uid => 1 };
our $args_prepare = { id_domain => 1 , uid => 1 };
our $args_remove_base = { id_domain => 1 , uid => 1 };
our $args_manage_iptables = {uid => 1, id_domain => 1, remote_ip => 1};

our %VALID_ARG = (
    create_domain => {
frankiejol's avatar
frankiejol committed
43
              vm => 2
44
45
           ,name => 1
           ,swap => 2
Francesc Guasch's avatar
Francesc Guasch committed
46
         ,id_iso => 2
47
         ,iso_file => 2
Francesc Guasch's avatar
Francesc Guasch committed
48
        ,id_base => 2
49
       ,id_owner => 1
Francesc Guasch's avatar
Francesc Guasch committed
50
    ,id_template => 2
51
52
         ,memory => 2
           ,disk => 2
Francesc Guasch's avatar
Francesc Guasch committed
53
           #        ,network => 2
54
55
      ,remote_ip => 2
          ,start => 2
56
57
58
59
60
61
62
    }
    ,open_iptables => $args_manage_iptables
      ,remove_base => $args_remove_base
     ,prepare_base => $args_prepare
     ,pause_domain => $args_manage
    ,resume_domain => {%$args_manage, remote_ip => 1 }
    ,remove_domain => $args_manage
63
64
    ,shutdown_domain => { name => 2, id_domain => 2, uid => 1, timeout => 2, at => 2
                       , id_vm => 2 }
65
    ,force_shutdown_domain => { id_domain => 1, uid => 1, at => 2, id_vm => 2 }
66
    ,screenshot_domain => { id_domain => 1, filename => 2 }
Francesc Guasch's avatar
Francesc Guasch committed
67
    ,domain_autostart => { id_domain => 1 , uid => 1, value => 2 }
68
    ,copy_screenshot => { id_domain => 1, filename => 2 }
Francesc Guasch's avatar
Francesc Guasch committed
69
    ,start_domain => {%$args_manage, remote_ip => 2, name => 2, id_domain => 2 }
70
    ,start_clones => { id_domain => 1, uid => 1, remote_ip => 1 }
71
    ,rename_domain => { uid => 1, name => 1, id_domain => 1}
72
    ,dettach => { uid => 1, id_domain => 1 }
73
    ,set_driver => {uid => 1, id_domain => 1, id_option => 1}
74
    ,hybernate=> {uid => 1, id_domain => 1}
75
    ,download => {uid => 2, id_iso => 1, id_vm => 2, verbose => 2, delay => 2}
76
    ,refresh_storage => { id_vm => 2 }
77
    ,set_base_vm=> {uid => 1, id_vm=> 1, id_domain => 1, value => 2 }
78
    ,cleanup => { }
Francesc Guasch's avatar
Francesc Guasch committed
79
80
81
82
    ,clone => { uid => 1, id_domain => 1, name => 2, memory => 2, number => 2, is_pool => 2
                ,start => 2, no_pool => 2
                ,remote_ip => 2
    }
Joel Alarcón's avatar
Joel Alarcón committed
83
    ,change_owner => {uid => 1, id_domain => 1}
Francesc Guasch's avatar
Francesc Guasch committed
84
    ,add_hardware => {uid => 1, id_domain => 1, name => 1, number => 2, data => 2 }
85
    ,remove_hardware => {uid => 1, id_domain => 1, name => 1, index => 1}
Francesc Guasch's avatar
Francesc Guasch committed
86
    ,change_hardware => {uid => 1, id_domain => 1, hardware => 1, index => 1, data => 1 }
JanFontanet's avatar
JanFontanet committed
87
    ,change_max_memory => {uid => 1, id_domain => 1, ram => 1}
88
    ,change_curr_memory => {uid => 1, id_domain => 1, ram => 1}
89
    ,enforce_limits => { timeout => 2, _force => 2 }
Francesc Guasch's avatar
Francesc Guasch committed
90
    ,refresh_machine => { id_domain => 1, uid => 1 }
91
    ,rebase_volumes => { uid => 1, id_base => 1, id_domain => 1 }
Francesc Guasch's avatar
Francesc Guasch committed
92
93
94
95
    # ports
    ,expose => { uid => 1, id_domain => 1, port => 1, name => 2, restricted => 2, id_port => 2}
    ,remove_expose => { uid => 1, id_domain => 1, port => 1}
    ,open_exposed_ports => {uid => 1, id_domain => 1 }
Francesc Guasch's avatar
Francesc Guasch committed
96
    # Virtual Managers or Nodes
Francesc Guasch's avatar
Francesc Guasch committed
97
    ,refresh_vms => { _force => 2, timeout_shutdown => 2 }
Francesc Guasch's avatar
Francesc Guasch committed
98
99
100
101
102

    ,shutdown_node => { id_node => 1, at => 2 }
    ,start_node => { id_node => 1, at => 2 }
    ,connect_node => { backend => 2, hostname => 2, id_node =>2, timeout => 2 }

Francesc Guasch's avatar
Francesc Guasch committed
103
    #users
104
    ,post_login => { user => 1, locale => 2 }
Francesc Guasch's avatar
Francesc Guasch committed
105

Francesc Guasch's avatar
Francesc Guasch committed
106
107
108
    #networks
    ,list_network_interfaces => { uid => 1, vm_type => 1, type => 2 }

Fernando Verdugo's avatar
Fernando Verdugo committed
109
110
111
    #isos
    ,list_isos => { vm_type => 1 }

Francesc Guasch's avatar
Francesc Guasch committed
112
    ,manage_pools => { uid => 2, id_domain => 2 }
113
    ,ping_backend => {}
114
115
116
);

our %CMD_SEND_MESSAGE = map { $_ => 1 }
117
    qw( create start shutdown prepare_base remove remove_base rename_domain screenshot download
118
            set_base_vm remove_base_vm
Francesc Guasch's avatar
Francesc Guasch committed
119
            domain_autostart hibernate hybernate
Francesc Guasch's avatar
Francesc Guasch committed
120
            change_owner
Francesc Guasch's avatar
Francesc Guasch committed
121
            change_max_memory change_curr_memory
Francesc Guasch's avatar
Francesc Guasch committed
122
            add_hardware remove_hardware set_driver change_hardware
123
            set_base_vm
Francesc Guasch's avatar
Francesc Guasch committed
124
            shutdown_node start_node
125
    );
126

Francesc Guasch's avatar
Francesc Guasch committed
127
128
our $TIMEOUT_SHUTDOWN = 120;

129
130
our $CONNECTOR;

131
our %COMMAND = (
Francesc Guasch's avatar
Francesc Guasch committed
132
133
134
135
    long => {
        limit => 4
        ,priority => 4
    } #default
136
137
138
    ,huge => {
        limit => 1
        ,commands => ['download']
Francesc Guasch's avatar
Francesc Guasch committed
139
        ,priority => 5
140
    }
141
142
    ,disk => {
        limit => 1
143
144
        ,commands => ['prepare_base','remove_base','set_base_vm','rebase_volumes'
                    , 'manage_pools']
145
146
        ,priority => 6
    }
Francesc Guasch's avatar
Francesc Guasch committed
147
    ,important=> {
148
        limit => 20
Francesc Guasch's avatar
Francesc Guasch committed
149
        ,priority => 1
Fernando Verdugo's avatar
Fernando Verdugo committed
150
        ,commands => ['clone','start','start_clones','create','open_iptables','list_network_interfaces','list_isos']
151
    }
Francesc Guasch's avatar
Francesc Guasch committed
152
153
154
155
156
    ,secondary => {
        limit => 50
        ,priority => 2
        ,commands => ['shutdown','shutdown_now']
    }
157
158
159
);
lock_hash %COMMAND;

160
161
162
163
164
165
166
167
168
169
sub _init_connector {
    $CONNECTOR = \$Ravada::CONNECTOR;
    $CONNECTOR = \$Ravada::Front::CONNECTOR   if !$$CONNECTOR;

}

=head2 BUILD

    Internal object builder, do not call

170
=cut
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

sub BUILD {
    _init_connector();
}

sub _request {
    my $proto = shift;
    my $class=ref($proto) || $proto;

    my $self = {};
    bless ($self, $class);
    return $self;
}

=head2 open

Opens the information of a previous request by id

  my $req = Ravada::Request->open($id);

=cut

sub open {
    my $proto = shift;
    my $class = ref($proto) || $proto;

    my $id = shift or confess "Missing request id";

    _init_connector()   if !$CONNECTOR || !$$CONNECTOR;

    my $sth = $$CONNECTOR->dbh->prepare("SELECT * FROM requests "
        ." WHERE id=?");
    $sth->execute($id);
    my $row = $sth->fetchrow_hashref;

    confess "I can't find id=$id " if !defined $row;
    $sth->finish;

209
210
    my $args = {};
    $args = decode_json($row->{args}) if $row->{args};
211
212
213
214

    $row->{args} = $args;

    bless ($row, $class);
Francesc Guasch's avatar
Francesc Guasch committed
215
216
    $row->{priority} = $row->_set_priority();

217
218
219
    return $row;
}

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
=head2 info

Returns information of the request

=cut

sub info {
    my $self = shift;
    my $user = shift;
    confess "USER ".$user->name." not authorized"
        unless $user->is_admin
            || ($self->defined_arg('uid') && $user->id == $self->args('uid'))
            || ($self->defined_arg('id_owner') && $user->id == $self->args('id_owner'));

    return {
Francesc Guasch's avatar
Francesc Guasch committed
235
236
        id => $self->id
        ,status => $self->status
237
238
239
240
241
        ,error => $self->error
        ,id_domain => $self->id_domain
    }
}

242
243
=head2 create_domain

Francesc Guasch's avatar
Francesc Guasch committed
244
245
    my $req = Ravada::Request->create_domain(
                        name => 'bla'
246
247
248
249
250
251
252
253
254
255
256
257
                    , id_iso => 1
    );


=cut

sub create_domain {
    my $proto = shift;
    my $class=ref($proto) || $proto;

    my %args = @_;

Francesc Guasch's avatar
Francesc Guasch committed
258
    my $args = _check_args('create_domain', @_ );
259

260
261
262
    confess "ERROR: Argument vm required without id_base"
        if !exists $args->{vm} && !exists $args->{id_base};

263
    my $self = {};
Francesc Guasch's avatar
Francesc Guasch committed
264
265
    if ($args->{network}) {
        $args->{network} = JSON::XS->new->convert_blessed->encode($args->{network});
266
267
268
    }

    bless($self,$class);
Francesc Guasch's avatar
Francesc Guasch committed
269
    return $self->_new_request(command => 'create' , args => encode_json($args));
270
271
272
273
}

=head2 remove_domain

Francesc Guasch's avatar
Francesc Guasch committed
274
275
    my $req = Ravada::Request->remove_domain(
                     name => 'bla'
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
                    , uid => $user->id
    );


=cut


sub remove_domain {
    my $proto = shift;
    my $class=ref($proto) || $proto;

    my %args = @_;
    confess "Missing domain name"   if !$args{name};
    confess "Name is not scalar"    if ref($args{name});
    confess "Missing uid"           if !$args{uid};

    for (keys %args) {
        confess "Invalid argument $_" if !$VALID_ARG{'remove_domain'}->{$_};
    }

    my $self = {};
    bless($self,$class);

299
    return $self->_new_request(command => 'remove' , args => \%args);
300
301
302
303
304
305
306

}

=head2 start_domain

Requests to start a domain

Francesc Guasch's avatar
Francesc Guasch committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  my $req = Ravada::Request->start_domain(
     name => 'name'
    , uid => $user->id
  );

Mandatory arguments: one of those must be passed:

=over

=item * name or id_domain

=item * uid: user id

=item * remote_ip: [optional] IP of the remote client that requested to start the domain

=back
323
324
325
326
327
328
329
330
331

=cut

sub start_domain {
    my $proto = shift;
    my $class=ref($proto) || $proto;

    my $args = _check_args('start_domain', @_);

332
333
334
    confess "ERROR: choose either id_domain or name "
        if $args->{id_domain} && $args->{name};

335
336
337
    my $self = {};
    bless($self,$class);

338
    return $self->_new_request(command => 'start' , args => $args);
339
340
}

341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
=head2 start_clones

Requests to start the clones of a base

  my $req = Ravada::Request->start_clones( name => 'name', uid => $user->id );

=cut

sub start_clones {
    my $proto = shift;
    my $class=ref($proto) || $proto;

    my $args = _check_args('start_clones', @_);

    my $self = {};
    bless($self,$class);

358
    return $self->_new_request(command => 'start_clones' , args => $args);
359
360
}

361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
=head2 pause_domain

Requests to pause a domain

  my $req = Ravada::Request->pause_domain( name => 'name', uid => $user->id );

=cut

sub pause_domain {
    my $proto = shift;
    my $class=ref($proto) || $proto;

    my $args = _check_args('pause_domain', @_);

    my $self = {};
    bless($self,$class);

378
    return $self->_new_request(command => 'pause' , args => $args);
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
}

=head2 resume_domain

Requests to pause a domain

  my $req = Ravada::Request->resume_domain( name => 'name', uid => $user->id );

=cut

sub resume_domain {
    my $proto = shift;
    my $class=ref($proto) || $proto;

    my $args = _check_args('resume_domain', @_);

    my $self = {};
    bless($self,$class);

398
    return $self->_new_request(command => 'resume' , args => $args);
399
400
401
402
403
404
}



sub _check_args {
    my $sub = shift;
Francesc Guasch's avatar
Francesc Guasch committed
405
    confess "Odd number of elements ".Dumper(\@_)   if scalar(@_) % 2;
406
407
408
    my $args = { @_ };

    my $valid_args = $VALID_ARG{$sub};
409
410
411
    for (qw(at after_request)) {
        $valid_args->{$_}=2 if !exists $valid_args->{$_};
    }
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

    confess "Unknown method $sub" if !$valid_args;
    for (keys %{$args}) {
        confess "Invalid argument $_ , valid args ".Dumper($valid_args)
            if !$valid_args->{$_};
    }

    for (keys %{$VALID_ARG{$sub}}) {
        next if $VALID_ARG{$sub}->{$_} == 2; # optional arg
        confess "Missing argument $_"   if !exists $args->{$_};
    }

    return $args;
}

=head2 force_shutdown_domain

Requests to stop a domain now !

  my $req = Ravada::Request->shutdown_domain( name => 'name' , uid => $user->id );

=cut

sub force_shutdown_domain {
    my $proto = shift;
    my $class=ref($proto) || $proto;

    my $args = _check_args('force_shutdown_domain', @_ );

    my $self = {};
    bless($self,$class);

    return $self->_new_request(command => 'force_shutdown' , args => $args);
}

=head2 shutdown_domain

Requests to stop a domain

  my $req = Ravada::Request->shutdown_domain( name => 'name' , uid => $user->id );
  my $req = Ravada::Request->shutdown_domain( name => 'name' , uid => $user->id
                                            ,timeout => $timeout );

=cut

sub shutdown_domain {
    my $proto = shift;
    my $class=ref($proto) || $proto;

    my $args = _check_args('shutdown_domain', @_ );

Francesc Guasch's avatar
Francesc Guasch committed
463
    $args->{timeout} = $TIMEOUT_SHUTDOWN if !exists $args->{timeout};
464

Francesc Guasch's avatar
Francesc Guasch committed
465
466
467
    confess "ERROR: You must supply either id_domain or name ".Dumper($args)
        if !$args->{id_domain} && !$args->{name};

468
469
470
    my $self = {};
    bless($self,$class);

471
    return $self->_new_request(command => 'shutdown' , args => $args);
472
473
}

Francesc Guasch's avatar
Francesc Guasch committed
474
475
476
477
478
479
480
481
482
sub new_request($self, $command, @args) {
    die "Error: Unknown request '$command'" if !$VALID_ARG{$command};
    return _new_request(
        $self
        ,command => $command
           ,args => _check_args($command, @args)
    );
}

Francesc Guasch's avatar
Francesc Guasch committed
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
sub _duplicated_request($command, $args) {
    return if !$args;
    my $args_d = decode_json($args);
    delete $args_d->{uid};
    my $sth = $$CONNECTOR->dbh->prepare(
        "SELECT id,args FROM requests WHERE status='requested'"
        ." AND command=?"
    );
    $sth->execute($command);
    while (my ($id,$args_found) = $sth->fetchrow) {

        my $args_found_d = decode_json($args_found);
        delete $args_found_d->{uid};

        next if join(".",sort keys %$args_d) ne join(".",sort keys %$args_found_d);
        my $args_d_s = join(".",map { $args_d->{$_} } sort keys %$args_d);
        my $args_found_s = join(".",map {$args_found_d->{$_} } sort keys %$args_found_d);
        next if $args_d_s ne $args_found_s;

        warn "$id\n$args_d_s\n$args_found_s\n"
            if $command eq 'clone' && $args =~ /a-1/;
        return $id;
    }
    return 0;
}

509
510
sub _new_request {
    my $self = shift;
511
512
513
    if ( !ref($self) ) {
        my $proto = $self ;
        my $class = ref($proto) || $proto;
Francesc Guasch's avatar
Francesc Guasch committed
514
515
516
        $self = {};
        bless ($self, $class);
    }
517
518
519
520
521
522
523
524
525
526
527
528
    my %args = @_;

    $args{status} = 'requested';

    if ($args{name}) {
        $args{domain_name} = $args{name};
        delete $args{name};
    }
    if ( ref $args{args} ) {
        $args{args}->{uid} = $args{args}->{id_owner}
            if !exists $args{args}->{uid};
        $args{at_time} = $args{args}->{at} if exists $args{args}->{at};
529
530
531
532
533
534
        my $id_domain_args = $args{args}->{id_domain};

        if ($id_domain_args) {
            confess "ERROR: Different id_domain: ".Dumper(\%args)
                if $args{id_domain} && $args{id_domain} ne $id_domain_args;
            $args{id_domain} = $id_domain_args;
535
536
537
            $args{after_request} = delete $args{args}->{after_request}
                if exists $args{args}->{after_request};

538
        }
539
540
541
        $args{args} = encode_json($args{args});
    }
    _init_connector()   if !$CONNECTOR || !$$CONNECTOR;
Francesc Guasch's avatar
Francesc Guasch committed
542
543
544
    if ($args{command} =~ /^(clone|manage_pools)$/) {
        if ( _duplicated_request($args{command}, $args{args})
            || ( $args{command} ne 'clone' && done_recently(undef, 60, $args{command}))) {
frankiejol's avatar
frankiejol committed
545
            #            warn "Warning: duplicated request for $args{command} $args{args}";
Francesc Guasch's avatar
Francesc Guasch committed
546
547
548
            return;
        }
    }
549
550
551
552
553
554
555
556
557
558
559
560

    my $sth = $$CONNECTOR->dbh->prepare(
        "INSERT INTO requests (".join(",",sort keys %args).")"
        ."  VALUES ( "
                .join(",", map { '?' } keys %args)
                ." )"
    );
    $sth->execute(map { $args{$_} } sort keys %args);
    $sth->finish;

    $self->{id} = $self->_last_insert_id();

Francesc Guasch's avatar
Francesc Guasch committed
561
562
563
564
565
    $sth = $$CONNECTOR->dbh->prepare(
    "UPDATE requests set date_req=date_changed"
    ." WHERE id=?");
    $sth->execute($self->{id});

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
    return $self->open($self->{id});
}

sub _last_insert_id {
    my $driver = $$CONNECTOR->dbh->{Driver}->{Name};

    if ( $driver =~ /sqlite/i ) {
        return _last_insert_id_sqlite(@_);
    } elsif ( $driver =~ /mysql/i ) {
        return _last_insert_id_mysql(@_);
    } else {
        confess "I don't know how to get last_insert_id for $driver";
    }
}

sub _last_insert_id_mysql {
    my $self = shift;
    my $sth = $$CONNECTOR->dbh->prepare("SELECT last_insert_id()");
    $sth->execute;
    my ($id) = $sth->fetchrow;
    $sth->finish;
    return $id;

}

sub _last_insert_id_sqlite {
    my $self = shift;

    my $sth = $$CONNECTOR->dbh->prepare("SELECT last_insert_rowid()");
    $sth->execute;
    my ($id) = $sth->fetchrow;
    $sth->finish;
    return $id;
}

=head2 status

Returns or sets the status of a request

  $req->status('done');

  my $status = $req->status();

=cut

sub status {
    my $self = shift;
    my $status = shift;
    my $message = shift;

    if (!defined $status) {
        my $sth = $$CONNECTOR->dbh->prepare("SELECT * FROM requests "
            ." WHERE id=?");
        $sth->execute($self->{id});
        my $row = $sth->fetchrow_hashref;
        $sth->finish;

        return ($row->{status} or 'unknown');
    }

    my $sth = $$CONNECTOR->dbh->prepare("UPDATE requests set status=? "
            ." WHERE id=?");
628
629

    $status = substr($status,0,64);
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
    $sth->execute($status, $self->{id});
    $sth->finish;

    $self->_send_message($status, $message)
        if $CMD_SEND_MESSAGE{$self->command} || $self->error ;
    return $status;
}

sub _search_domain_name {
    my $self = shift;
    my $domain_id = shift;

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

sub _send_message {
    my $self = shift;
    my $status = shift;
    my $message = ( shift or $self->error );

    my $uid;

    $uid = $self->args('id_owner') if $self->defined_arg('id_owner');
    $uid = $self->args('uid')      if !$uid && $self->defined_arg('uid');
    return if !$uid;

    my $domain_name = $self->defined_arg('name');
    if (!$domain_name) {
        my $domain_id = $self->defined_arg('id_domain');
        $domain_name = $self->_search_domain_name($domain_id)   if $domain_id;
        $domain_name = '' if !defined $domain_name;
    }
    $domain_name = "$domain_name "  if length $domain_name;

    $self->_remove_unnecessary_messages() if $self->status eq 'done';

    my $subject = $self->command." $domain_name ".$self->status;
    $subject = $message if $message && $self->status eq 'done'
            && length ($message)<60;

    my $sth = $$CONNECTOR->dbh->prepare(
        "INSERT INTO messages ( id_user, id_request, subject, message, date_shown ) "
        ." VALUES ( ?,?,?,?, NULL)"
    );
    $sth->execute($uid, $self->id,$subject, $message);
    $sth->finish;
}

sub _remove_unnecessary_messages {
    my $self = shift;

    my $uid;
    $uid = $self->defined_arg('id_owner');
    $uid = $self->defined_arg('uid')        if !$uid;
    return if !$uid;


    my $sth = $$CONNECTOR->dbh->prepare(
        "DELETE FROM messages WHERE id_user=? AND id_request=? "
        ." AND (message='' OR message IS NULL)"
    );

    $sth->execute($uid, $self->id);
    $sth->finish;

}

699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
sub _remove_messages {
    my $self = shift;

    my $uid;
    $uid = $self->defined_arg('id_owner');
    $uid = $self->defined_arg('uid')        if !$uid;
    return if !$uid;


    my $sth = $$CONNECTOR->dbh->prepare(
        "DELETE FROM messages WHERE id_user=? AND id_request=? "
    );

    $sth->execute($uid, $self->id);
    $sth->finish;

}


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

=head2 result

  Returns the result of the request if any

  my $result = $req->result;

=cut

sub result {
    my $self = shift;

    my $value = shift;

    if (defined $value ) {
        my $sth = $$CONNECTOR->dbh->prepare("UPDATE requests set result=? "
            ." WHERE id=?");
        $sth->execute(encode_json($value), $self->{id});
        $sth->finish;

    } else {
        my $sth = $$CONNECTOR->dbh->prepare("SELECT result FROM requests where id=? ");
        $sth->execute($self->{id});
        ($value) = $sth->fetchrow;
        $value = decode_json($value)    if defined $value;
        $sth->finish;

    }

    return $value;
}

=head2 command

Returns the requested command

=cut

sub command {
    my $self = shift;
    return $self->{command};
}

=head2 args

Returns the requested command

  my $command = $req->command;

=cut


=head2 args

Returns the arguments of a request or the value of one argument field

  my $args = $request->args();
  print $args->{name};

  print $request->args('name');

=cut


sub args {
    my $self = shift;
    my $name = shift;
    return $self->{args}    if !$name;

    confess "Unknown argument $name ".Dumper($self->{args})
        if !exists $self->{args}->{$name};
    return $self->{args}->{$name};
}

Francesc Guasch's avatar
Francesc Guasch committed
792
793
794
795
796
797
798
799
800
801
sub arg($self, $name, $value) {

    confess "Unknown argument $name ".Dumper($self->{args})
        if !exists $self->{args}->{$name} && !defined $value;

    $self->{args}->{$name} = $value if defined $value;
    return $self->{args}->{$name};
}


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
=head2 defined_arg

Returns if an argument is defined

=cut

sub defined_arg {
    my $self = shift;
    my $name = shift;
    confess "ERROR: missing arg name" if !defined $name;
    return $self->{args}->{$name};
}

=head2 screenshot_domain

Request the screenshot of a domain.

Arguments:

- optional filename , defaults to "storage_path/$id_domain.png"

Returns a Ravada::Request;

=cut

sub screenshot_domain {
    my $proto = shift;
    my $class=ref($proto) || $proto;

    my $args = _check_args('screenshot_domain', @_ );

    $args->{filename} = '' if !exists $args->{filename};

    my $self = {};
    bless($self,$class);

    return $self->_new_request(command => 'screenshot' , id_domain => $args->{id_domain}
        ,args => $args);

}

843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
=head2 copy_screenshot

Request to copy a screenshot from a domain to another

=cut

sub copy_screenshot {
  my $proto = shift;
  my $class=ref($proto) || $proto;

  my $args = _check_args('copy_screenshot', @_ );

  $args->{filename} = '' if !exists $args->{filename};

  my $self = {};
  bless($self,$class);

  return $self->_new_request(
       command => 'copy_screenshot'
      ,id_domain => $args->{id_domain}
      ,args => $args
      );
Joel Alarcón's avatar
Joel Alarcón committed
865

866
867
}

Francesc Guasch's avatar
Francesc Guasch committed
868
869
=head2 refresh_vms

870
Refreshes the Virtual Mangers
Francesc Guasch's avatar
Francesc Guasch committed
871
872
873
874
875
876
877
878

=cut

sub refresh_vms {
    my $proto = shift;
    my $class = ref($proto) || $proto;

    my $args = _check_args('refresh_vms', @_ );
879
880
881
    if  (!$args->{_force} ) {
          return if done_recently(undef,60,'refresh_vms') || _requested('refresh_vms');
    }
Francesc Guasch's avatar
Francesc Guasch committed
882

Francesc Guasch's avatar
Francesc Guasch committed
883
884
    $args->{timeout} = 120 if ! $args->{timeout};

Francesc Guasch's avatar
Francesc Guasch committed
885
886
887
    my $self = {};
    bless($self,$class);

888
    _init_connector();
Francesc Guasch's avatar
Francesc Guasch committed
889
890
891
892
893
894
    return $self->_new_request(
        command => 'refresh_vms'
        , args => $args
    );


895
896
897
898
899
900
901
902
903
904
905
906
907
}

=head2 set_base_vm

Enables a base in a Virtual Manager

=cut

sub set_base_vm {
    my $proto = shift;
    my $class = ref($proto) || $proto;

    my $args = _check_args('set_base_vm', @_ );
908
    $args->{value} = 1 if !exists $args->{value};
909
910

    my $self = {};
911
    bless ($self, $class);
912
913

    return $self->_new_request(
914
            command => 'set_base_vm'
915
             , args => $args
916
917
    );

918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
}

=head2 remove_base_vm

Disables a base in a Virtual Manager

=cut

sub remove_base_vm {
    my $proto = shift;
    my $class = ref($proto) || $proto;

    my $args = _check_args('set_base_vm', @_ );
    $args->{value} = 0;

    my $self = {};
    bless($self,$class);

    return $self->_new_request(
Francesc Guasch's avatar
Francesc Guasch committed
937
            command => 'remove_base_vm'
938
             , args => $args
939
    );
940
941
942

}

943
944
945
946
947
948
949

=head2 type

Returns the type of the request

=cut

950
sub type($self) {
951
952
    my $command = $self;
    $command = $self->command if ref($self);
953
954
955
956
957
958
    for my $type ( keys %COMMAND ) {
        return $type if grep /^$command$/, @{$COMMAND{$type}->{commands}};
    }
    return 'long';
}

Francesc Guasch's avatar
Francesc Guasch committed
959
960
961
962
963
964
965
966
967
968
969
sub _set_priority ($self) {
    my $command = $self;
    $command = $self->command if ref($self);
    for my $type ( keys %COMMAND ) {
        next if  !grep /^$command$/, @{$COMMAND{$type}->{commands}};
        return $COMMAND{$type}->{priority} if exists $COMMAND{$type}->{priority};
        return 10;
    }
}


970
971
972
973
974
975
976
977
978
979
=head2 working_requests

Returns the number of working requests of the same type

    my $n = $request->working_requests();

=cut

sub count_requests($self) {
    my $sth = $$CONNECTOR->dbh->prepare(
980
        "SELECT command FROM requests"
981
982
983
        ." WHERE status = 'working' "
    );
    $sth->execute();
984
985
986
987
    my $n = 0;
    while ( my $command = $sth->fetchrow ) {
        $n++ if type($command) eq $self->type;
    }
988
989
990
    return $n;
}

991
=head2 requests_limit
Francesc Guasch's avatar
Francesc Guasch committed
992

993
    Returns the limit of requests of a type.
Francesc Guasch's avatar
Francesc Guasch committed
994
995
996

=cut

997
998
999
sub requests_limit($self, $type = $self->type) {
    confess "Error: no requests of type $type" if !exists $COMMAND{$type};
    return $COMMAND{$type}->{limit};
JanFontanet's avatar
JanFontanet committed
1000
}
For faster browsing, not all history is shown. View entire blame