Front.pm 4.38 KB
Newer Older
1
2
3
4
5
package Ravada::Front;

use strict;
use warnings;

Francesc Guasch's avatar
Francesc Guasch committed
6
use Hash::Util qw(lock_hash);
7
use JSON::XS;
8
9
10
use Moose;
use Ravada;

11
12
use Data::Dumper;

13
14
15
16
17
18
19
20
has 'config' => (
    is => 'ro'
    ,isa => 'Str'
    ,default => $Ravada::FILE_CONFIG
);
has 'connector' => (
        is => 'rw'
);
21
22
23
24
25
has 'backend' => (
    is => 'ro',
    isa => 'Ravada'

);
26
27


Francesc Guasch's avatar
Francesc Guasch committed
28
our $CONNECTOR;# = \$Ravada::CONNECTOR;
29
our $TIMEOUT = 5;
30
31
32
33
34
35
36
37
38

=head2 BUILD

Internal constructor

=cut

sub BUILD {
    my $self = shift;
39
    if ($self->connector) {
Francesc Guasch's avatar
Francesc Guasch committed
40
        $CONNECTOR = $self->connector;
41
42
43
44
    } else {
        Ravada::_init_config($self->config());
        $CONNECTOR = Ravada::_connect_dbh();
    }
45
46
47
48
}

sub list_bases {
    my $self = shift;
Francesc Guasch's avatar
Francesc Guasch committed
49
    my $sth = $CONNECTOR->dbh->prepare("SELECT * FROM domains where is_base='y'");
50
51
52
53
54
55
56
57
58
59
60
61
62
    $sth->execute();
    
    my @bases = ();
    while ( my $row = $sth->fetchrow_hashref) {
        push @bases, ($row);
    }
    $sth->finish;

    return \@bases;
}

sub list_domains {
    my $self = shift;
Francesc Guasch's avatar
Francesc Guasch committed
63
    my $sth = $CONNECTOR->dbh->prepare("SELECT * FROM domains ");
64
65
66
67
68
69
70
71
72
    $sth->execute();
    
    my @domains = ();
    while ( my $row = $sth->fetchrow_hashref) {
        push @domains, ($row);
    }
    $sth->finish;

    return \@domains;
73
74
75
76
}

sub list_vm_types {
    my $self = shift;
77

78
    my $req = Ravada::Request->list_vm_types();
Francesc Guasch's avatar
Francesc Guasch committed
79
    $self->wait_request($req);
80
81
82
83
84

    die "ERROR: Timeout waiting for request ".$req->id
        if $req->status() eq 'timeout';

    return $req->result();
85
86
}

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
sub list_iso_images {
    my $self = shift;

    my @iso;
    my $sth = $CONNECTOR->dbh->prepare(
        "SELECT * FROM iso_images ORDER BY name"
    );
    $sth->execute;
    while (my $row = $sth->fetchrow_hashref) {
        push @iso,($row);
    }
    $sth->finish;
    return \@iso;
}

Francesc Guasch's avatar
Francesc Guasch committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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;

}

118
119
120
121
122
sub create_domain {
    my $self = shift;
    return Ravada::Request->create_domain(@_);
}

Francesc Guasch's avatar
Francesc Guasch committed
123
sub wait_request {
124
    my $self = shift;
125
    my $req = shift;
126

127
128
    my $timeout = ( shift or $TIMEOUT );

129
    $self->backend->process_requests() if $self->backend;
130

131
132
133
134
135
136
137
138
139
140
141
142
    for ( 1 .. $TIMEOUT ) {
        last if $req->status eq 'done';
        sleep 1;
    }
    $req->status("timeout")
        if $req->status eq 'working';
    return $req;

}

sub ping_backend {
    my $self = shift;
143

144
    my $req = Ravada::Request->ping_backend();
Francesc Guasch's avatar
Francesc Guasch committed
145
    $self->wait_request($req, 2);
146

147
148
149
150
    return 1 if $req->status() eq 'done';
    return 0;
}

Francesc Guasch's avatar
Francesc Guasch committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
sub search_domain {
    my $self = shift;

    my $name = shift;

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

    my $row = $sth->fetchrow_hashref;

    return if !keys %$row;

    lock_hash(%$row);
    return $row;
}
Francesc Guasch's avatar
Francesc Guasch committed
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

=head2 list_requests

Returns a list of ruquests : ( id , domain_name, status, error )

=cut

sub list_requests {
    my $self = shift;
    my $sth = $CONNECTOR->dbh->prepare("SELECT id, command, args, date_changed, status, error "
        ." FROM requests "
        ." ORDER BY date_changed DESC LIMIT 4"
    );
    $sth->execute;
    my @reqs;
    my ($id, $command, $j_args, $date_changed, $status, $error);
    $sth->bind_columns(\($id, $command, $j_args, $date_changed, $status, $error));

    while ( $sth->fetch) {
        my $args = decode_json($j_args) if $j_args;

        push @reqs,{ id => $id,  command => $command, date_changed => $date_changed, status => $status, error => $error , name => $args->{name}};
    }
    $sth->finish;
    return \@reqs;
}

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
=head2 search_domain_by_id

  my $domain = $ravada->search_domain_by_id($id);

=cut

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

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

    my $row = $sth->fetchrow_hashref;

    return if !keys %$row;

    lock_hash(%$row);
211

212
    return $row;
213
214
215
216
217
218
219
}

sub domdisplay {
    my $self = shift;
    my $name = shift;

    my $req = Ravada::Request->domdisplay($name);
220
    $self->wait_request($req, 10);
221

222
223
224
225
    return if $req->status() ne 'done';

    my $result = $req->result();
    return $result->{display};
226
227
}

228
229
230
231
232
233
sub start_domain {
    my $self = shift;
    my $name = shift;

    return Ravada::Request->start_domain($name);
}
234

235
1;