Domain.pm 5.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
package Ravada::Front::Domain;

use warnings;
use strict;

=head2 NAME

Ravada::Front::Domain - Frontent domain information for Ravada

=cut

use Carp qw(cluck confess croak);
Francesc Guasch's avatar
Francesc Guasch committed
13
use Data::Dumper;
Francesc Guasch's avatar
Francesc Guasch committed
14
use JSON::XS;
15
16
use Moose;

Francesc Guasch's avatar
Francesc Guasch committed
17
18
19
use Ravada::Front::Domain::KVM;
use Ravada::Front::Domain::Void;

20
21
22
23
24
no warnings "experimental::signatures";
use feature qw(signatures);

with 'Ravada::Domain';

25
26
###########################################################################
#
27
28
29
30
31
32
has 'readonly' => (
    isa => 'Int'
    ,is => 'ro'
    ,default => 1
);

33
our $CONNECTOR;
34
35
36
#
###########################################################################

37
38
39
40
41
sub _init_connector {
    $CONNECTOR= \$Ravada::CONNECTOR;
    $CONNECTOR= \$Ravada::Front::CONNECTOR   if !$$CONNECTOR;
}

42
sub BUILD($self, $arg) {
Francesc Guasch's avatar
Francesc Guasch committed
43
44
45
46
47
48
49
50
    my $id = $arg->{id};
    my $name = $arg->{name};

    $self->_select_domain_db( id => $id)    if defined $id;
    $self->_select_domain_db( name => $name)    if defined $name;

    $self->{_data}->{id} = $id      if defined $id;
    $self->{_data}->{name} = $name  if defined $name;
51

52
53
#    confess "ERROR: Domain '".$self->name." not found "
#        if $self->is_volatile && ! $self->is_active;
54
55
}

56
sub open($self, $id) {
Francesc Guasch's avatar
Francesc Guasch committed
57
    confess "Error: undefined id" if !defined $id;
Francesc Guasch's avatar
Francesc Guasch committed
58
59
60
61
62
63
    my $domain = Ravada::Front::Domain->new( id => $id );
    if ($domain->type eq 'KVM') {
        $domain = Ravada::Front::Domain::KVM->new( id => $id );
    } elsif ($domain->type eq 'Void') {
        $domain = Ravada::Front::Domain::Void->new( id => $id );
    }
Francesc Guasch's avatar
Francesc Guasch committed
64
    confess "ERROR: Unknown domain id: $id\n"
Francesc Guasch's avatar
Francesc Guasch committed
65
66
        unless exists $domain->{_data}->{name} && $domain->{_data}->{name};
    return $domain;
67
68
}

69
sub autostart($self )    { return $self->_data('autostart') }
70
sub _do_force_shutdown  { confess "TODO" }
Roberto P. Rubio's avatar
Roberto P. Rubio committed
71
sub _do_force_reboot    { confess "TODO" }
72
sub add_volume          { confess "TODO" }
Francesc Guasch's avatar
Francesc Guasch committed
73
sub remove_volume       { confess "TODO" }
74
75
76
sub clean_swap_volumes  { confess "TODO" }
sub disk_device         { confess "TODO" }
sub disk_size           { confess "TODO" }
77
78

sub display($self, $user) {
Francesc Guasch's avatar
Francesc Guasch committed
79
80
81
82
83
    my $info = $self->display_info($user);
    return $info->{display};
}

sub display_info($self, $user) {
84
85
86
87
    my $display = $self->_data('display');
    return {} if !$display;
    return decode_json($display);

Francesc Guasch's avatar
Francesc Guasch committed
88
89
90
91
}

sub display_file_tls($self, $user) {
    return $self->_data('display_file');
92
93
}

94
sub force_shutdown      { confess "TODO" }
Francesc Guasch's avatar
Francesc Guasch committed
95

Roberto P. Rubio's avatar
Roberto P. Rubio committed
96
97
sub force_reboot        { confess "TODO" }

Francesc Guasch's avatar
Francesc Guasch committed
98
99
100
101
102
sub get_info($self) {
     my $info = $self->_data('info');
     return {} if !$info;
     return decode_json($info);
}
103
sub hybernate           { confess "TODO" }
104
sub hibernate           { confess "TODO" }
105

106
sub internal_id($self) { return $self->_data('internal_id')}
107
108
109
110
111
112

sub is_active($self) {
    return 1 if $self->_data('status') eq 'active';
    return 0;
}

113
114
sub is_volatile_clones($self) { return $self->_data('volatile_clones')}

115
116
117
118
119
120
121
122
123
124
sub is_hibernated($self) {
    return 1 if $self->_data('status') eq 'hibernated';
    return 0;
}

sub is_paused($self) {
    return 1 if $self->_data('status') eq 'paused';
    return 0;
}

Francesc Guasch's avatar
Francesc Guasch committed
125
sub is_removed          { return 0 }
Francesc Guasch's avatar
Francesc Guasch committed
126
sub list_volumes($self, $attribute=undef, $value=undef)
Francesc Guasch's avatar
Francesc Guasch committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
{
    _init_connector();
    my $sth = $$CONNECTOR->dbh->prepare(
        "SELECT * FROM volumes "
        ." WHERE id_domain=?"
        ." ORDER BY id"
    );
    $sth->execute($self->id);
    my @volumes;
    while (my $row = $sth->fetchrow_hashref) {
        $row->{info} = decode_json($row->{info}) if $row->{info};
        $row->{info}->{capacity} = Ravada::Utils::number_to_size($row->{info}->{capacity})
            if defined $row->{info}->{capacity} && $row->{info}->{capacity} =~ /^\d+$/;
        $row->{info}->{allocation} = Ravada::Utils::number_to_size($row->{info}->{allocation})
            if defined $row->{info}->{allocation} && $row->{info}->{allocation} =~ /^\d+$/;
Francesc Guasch's avatar
Francesc Guasch committed
142
143
144
145

        next if defined $attribute
        && ( !exists $row->{$attribute}
                || $row->{$attribute} != $value);
146
147
        $row->{info}->{file} = $row->{file} if $row->{file};
        push @volumes, (Ravada::Volume->new(file => $row->{file}, info => $row->{info}));
Francesc Guasch's avatar
Francesc Guasch committed
148
149
150
151
152
    }
    $sth->finish;
    return @volumes;
}

Francesc Guasch's avatar
Francesc Guasch committed
153
sub list_volumes_info($self, @args) { return $self->list_volumes(@args) }
Francesc Guasch's avatar
Francesc Guasch committed
154

155
sub migrate             { confess "TODO" }
156
157

sub name($self) {
Francesc Guasch's avatar
Francesc Guasch committed
158
    return $self->{_data}->{name}   if exists $self->{_data} && $self->{_data}->{name};
159
160
161
    return $self->_data('name') 
}

162
163
164
165
166
167
sub pause               { confess "TODO" }
sub prepare_base        { confess "TODO" }
sub remove              { confess "TODO" }
sub rename              { confess "TODO" }
sub resume              { confess "TODO" }
sub screenshot          { confess "TODO" }
168
169

sub search_domain($self,$name) {
170
    _init_connector();
171
172
173
174
    my $sth = $$CONNECTOR->dbh->prepare("SELECT id FROM domains WHERE name=?");
    $sth->execute($name);
    my ($id) = $sth->fetchrow;
    $sth->finish;
175
    return if !$id;
176
177
178
    return Ravada::Front::Domain->new(id => $id);
}

179
180
181
182
sub set_max_mem         { confess "TODO" }
sub set_memory          { confess "TODO" }
sub shutdown            { confess "TODO" }
sub shutdown_now        { confess "TODO" }
Roberto P. Rubio's avatar
Roberto P. Rubio committed
183
184
sub reboot              { confess "TODO" }
sub reboot_now          { confess "TODO" }
Francesc Guasch's avatar
Francesc Guasch committed
185
sub spinoff             { confess "TODO" }
186
sub start               { confess "TODO" }
187

Francesc Guasch's avatar
Francesc Guasch committed
188
189
sub dettach             { confess "TODO" }

Francesc Guasch's avatar
Francesc Guasch committed
190
sub get_driver {}
Francesc Guasch's avatar
Francesc Guasch committed
191
192
sub get_controller_by_name { }
sub list_controllers {}
Francesc Guasch's avatar
Francesc Guasch committed
193

Francesc Guasch's avatar
Francesc Guasch committed
194
195
sub set_controller {}
sub remove_controller {}
Francesc Guasch's avatar
Francesc Guasch committed
196
sub change_hardware { die "TODO" }
197
198
199
200
201

sub _get_controller_disk($self) {
    return map { $_->info } $self->list_volumes_info();
}

202
203
204
205
206
207
sub set_time($self) {
    Ravada::Request->set_time(uid => Ravada::Utils::user_daemon->id
        , id_domain => $self->id
        , retry => 10
    );
}
208
1;