line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package VM::JiffyBox::Box; |
2
|
|
|
|
|
|
|
$VM::JiffyBox::Box::VERSION = '0.031'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Representation of a Virtual Machine in JiffyBox |
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
16
|
use strict; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
103
|
|
6
|
4
|
|
|
4
|
|
12
|
use warnings; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
108
|
|
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
13
|
use Moo; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
46
|
|
9
|
4
|
|
|
4
|
|
979
|
use JSON; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
25
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has id => (is => 'ro', required => 1); |
12
|
|
|
|
|
|
|
has hypervisor => (is => 'ro', required => 1); |
13
|
|
|
|
|
|
|
has name => (is => 'rwp'); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has last => (is => 'rw'); |
16
|
|
|
|
|
|
|
has backup_cache => (is => 'rw'); |
17
|
|
|
|
|
|
|
has details_cache => (is => 'rw'); |
18
|
|
|
|
|
|
|
has start_cache => (is => 'rw'); |
19
|
|
|
|
|
|
|
has stop_cache => (is => 'rw'); |
20
|
|
|
|
|
|
|
has delete_cache => (is => 'rw'); |
21
|
|
|
|
|
|
|
has freeze_cache => (is => 'rw'); |
22
|
|
|
|
|
|
|
has thaw_cache => (is => 'rw'); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub get_backups { |
25
|
2
|
|
|
2
|
1
|
350
|
my $self = shift; |
26
|
|
|
|
|
|
|
|
27
|
2
|
|
|
|
|
5
|
my $url = $self->{hypervisor}->base_url . '/backups/' . $self->id; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# POSSIBLE EXIT |
30
|
2
|
50
|
|
|
|
9
|
return { url => $url } if ($self->{hypervisor}->test_mode); |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
0
|
my $response = $self->{hypervisor}->ua->get($url); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# POSSIBLE EXIT |
35
|
0
|
0
|
|
|
|
0
|
unless ($response->is_success) { |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
0
|
$self->last ( $response->status_line ); |
38
|
0
|
|
|
|
|
0
|
return; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
0
|
my $backup_info = from_json($response->decoded_content); |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
0
|
$self->last ($backup_info); |
44
|
0
|
|
|
|
|
0
|
$self->backup_cache ($backup_info); |
45
|
0
|
|
|
|
|
0
|
return $backup_info ; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub get_details { |
50
|
3
|
|
|
3
|
1
|
397
|
my $self = shift; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# add method specific stuff to the URL |
53
|
3
|
|
|
|
|
8
|
my $url = $self->{hypervisor}->base_url . '/jiffyBoxes/' . $self->id; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# POSSIBLE EXIT |
56
|
|
|
|
|
|
|
# return the URL if we are using test_mode |
57
|
3
|
50
|
|
|
|
14
|
return { url => $url } if ($self->{hypervisor}->test_mode); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# send the request and return the response |
60
|
0
|
|
|
|
|
0
|
my $response = $self->{hypervisor}->ua->get($url); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# POSSIBLE EXIT |
63
|
0
|
0
|
|
|
|
0
|
unless ($response->is_success) { |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
0
|
$self->last ( $response->status_line ); |
66
|
0
|
|
|
|
|
0
|
return; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
0
|
my $details = from_json($response->decoded_content); |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
0
|
$self->last ($details); |
72
|
0
|
|
|
|
|
0
|
$self->details_cache ($details); |
73
|
0
|
|
|
|
|
0
|
return $details ; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub clone { |
77
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
78
|
0
|
|
|
|
|
0
|
my $args = shift; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# POSSIBLE EXIT (DIE) |
81
|
0
|
0
|
|
|
|
0
|
die 'name needed' unless $args->{name}; |
82
|
0
|
0
|
|
|
|
0
|
die 'planid needed' unless $args->{planid}; |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
0
|
my $url = $self->{hypervisor}->base_url . '/jiffyBoxes/' . $self->id; |
85
|
|
|
|
|
|
|
my $json = to_json( { |
86
|
|
|
|
|
|
|
name => $args->{name}, |
87
|
|
|
|
|
|
|
planid => $args->{planid}, |
88
|
|
|
|
|
|
|
metadata => $args->{metadata} || {}, |
89
|
0
|
|
0
|
|
|
0
|
}); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# POSSIBLE EXIT |
92
|
|
|
|
|
|
|
return { url => $url, json => $json } |
93
|
0
|
0
|
|
|
|
0
|
if ($self->{hypervisor}->test_mode); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# send the request with method specific json content |
96
|
0
|
|
|
|
|
0
|
my $response = $self->{hypervisor}->ua->post( $url, Content => $json ); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# POSSIBLE EXIT |
99
|
0
|
0
|
|
|
|
0
|
unless ($response->is_success) { |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
0
|
$self->last ( $response->status_line ); |
102
|
0
|
|
|
|
|
0
|
return; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
0
|
my $clone_info = from_json($response->decoded_content); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $clone_box = __PACKAGE__->new( |
108
|
|
|
|
|
|
|
id => $clone_info->{result}->{id}, |
109
|
|
|
|
|
|
|
name => $args->{name}, |
110
|
0
|
|
|
|
|
0
|
hypervisor => $self, |
111
|
|
|
|
|
|
|
); |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
0
|
return $clone_box; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub _status_action { |
117
|
8
|
|
|
8
|
|
7
|
my $self = shift; |
118
|
8
|
|
|
|
|
7
|
my $action = shift; |
119
|
|
|
|
|
|
|
|
120
|
8
|
|
|
|
|
10
|
my $status = uc $action; |
121
|
8
|
100
|
|
|
|
19
|
$status = 'SHUTDOWN' if 'stop' eq lc $action; |
122
|
|
|
|
|
|
|
|
123
|
8
|
|
|
|
|
19
|
my $url = $self->{hypervisor}->base_url . '/jiffyBoxes/' . $self->id; |
124
|
8
|
|
|
|
|
26
|
my $json = to_json( { status => $status } ); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# POSSIBLE EXIT |
127
|
|
|
|
|
|
|
return { url => $url, json => $json } |
128
|
8
|
50
|
|
|
|
124
|
if ($self->{hypervisor}->test_mode); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# send the request with method specific json content |
131
|
0
|
|
|
|
|
0
|
my $response = $self->{hypervisor}->ua->put( $url, Content => $json ); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# POSSIBLE EXIT |
134
|
0
|
0
|
|
|
|
0
|
unless ($response->is_success) { |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
0
|
$self->last ( $response->status_line ); |
137
|
0
|
|
|
|
|
0
|
return; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
0
|
my $status_info = from_json($response->decoded_content); |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
0
|
my $cache_sub = $self->can( $action . '_cache' ); |
143
|
0
|
|
|
|
|
0
|
$self->last ($status_info); |
144
|
0
|
0
|
|
|
|
0
|
$cache_sub->($status_info) if $cache_sub; |
145
|
0
|
|
|
|
|
0
|
return $status_info ; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub freeze { |
149
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
0
|
return $self->_status_action( 'freeze' ); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub thaw { |
155
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
156
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
0
|
return $self->_status_action( 'thaw' ); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub start { |
161
|
4
|
|
|
4
|
1
|
745
|
my $self = shift; |
162
|
|
|
|
|
|
|
|
163
|
4
|
|
|
|
|
9
|
return $self->_status_action( 'start' ); |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub stop { |
167
|
4
|
|
|
4
|
1
|
1079
|
my $self = shift; |
168
|
|
|
|
|
|
|
|
169
|
4
|
|
|
|
|
7
|
return $self->_status_action( 'stop' ); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub delete { |
173
|
2
|
|
|
2
|
1
|
366
|
my $self = shift; |
174
|
|
|
|
|
|
|
|
175
|
2
|
|
|
|
|
6
|
my $url = $self->{hypervisor}->base_url . '/jiffyBoxes/' . $self->id; |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
# POSSIBLE EXIT |
178
|
2
|
50
|
|
|
|
10
|
return { url => $url } if ($self->{hypervisor}->test_mode); |
179
|
|
|
|
|
|
|
|
180
|
0
|
|
|
|
|
|
my $response = $self->{hypervisor}->ua->delete($url); |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
# POSSIBLE EXIT |
183
|
0
|
0
|
|
|
|
|
unless ($response->is_success) { |
184
|
|
|
|
|
|
|
|
185
|
0
|
|
|
|
|
|
$self->last ( $response->status_line ); |
186
|
0
|
|
|
|
|
|
return; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
0
|
|
|
|
|
|
my $delete_info = from_json($response->decoded_content); |
190
|
|
|
|
|
|
|
|
191
|
0
|
|
|
|
|
|
$self->last ($delete_info); |
192
|
0
|
|
|
|
|
|
$self->delete_cache ($delete_info); |
193
|
0
|
|
|
|
|
|
return $delete_info ; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
1; |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
__END__ |