line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package VM::JiffyBox::Box; |
2
|
|
|
|
|
|
|
$VM::JiffyBox::Box::VERSION = '0.032'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Representation of a Virtual Machine in JiffyBox |
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
15
|
use strict; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
89
|
|
6
|
4
|
|
|
4
|
|
13
|
use warnings; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
94
|
|
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
14
|
use Moo; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
44
|
|
9
|
4
|
|
|
4
|
|
936
|
use JSON; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
22
|
|
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
|
343
|
my $self = shift; |
26
|
|
|
|
|
|
|
|
27
|
2
|
|
|
|
|
6
|
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
|
390
|
my $self = shift; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# add method specific stuff to the URL |
53
|
3
|
|
|
|
|
7
|
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
|
|
6
|
my $self = shift; |
118
|
8
|
|
|
|
|
7
|
my $action = shift; |
119
|
8
|
|
|
|
|
7
|
my $params = shift; |
120
|
|
|
|
|
|
|
|
121
|
8
|
50
|
|
|
|
17
|
return if !$action; |
122
|
|
|
|
|
|
|
|
123
|
8
|
|
|
|
|
9
|
my $status = uc $action; |
124
|
8
|
100
|
|
|
|
17
|
$status = 'SHUTDOWN' if 'stop' eq lc $action; |
125
|
|
|
|
|
|
|
|
126
|
8
|
|
|
|
|
8
|
my %opts; |
127
|
8
|
50
|
|
|
|
10
|
if ( $status eq 'THAW' ) { |
128
|
0
|
|
|
|
|
0
|
$opts{planid} = $params->{planid}; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
8
|
|
|
|
|
19
|
my $url = $self->{hypervisor}->base_url . '/jiffyBoxes/' . $self->id; |
132
|
8
|
|
|
|
|
31
|
my $json = to_json( { %opts, status => $status } ); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# POSSIBLE EXIT |
135
|
|
|
|
|
|
|
return { url => $url, json => $json } |
136
|
8
|
50
|
|
|
|
164
|
if ($self->{hypervisor}->test_mode); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# send the request with method specific json content |
139
|
0
|
|
|
|
|
0
|
my $response = $self->{hypervisor}->ua->put( $url, Content => $json ); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# POSSIBLE EXIT |
142
|
0
|
0
|
|
|
|
0
|
unless ($response->is_success) { |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
0
|
$self->last ( $response->status_line ); |
145
|
0
|
|
|
|
|
0
|
return; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
0
|
my $status_info = from_json($response->decoded_content); |
149
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
0
|
my $cache_sub = $self->can( $action . '_cache' ); |
151
|
0
|
|
|
|
|
0
|
$self->last ($status_info); |
152
|
0
|
0
|
|
|
|
0
|
$cache_sub->($status_info) if $cache_sub; |
153
|
0
|
|
|
|
|
0
|
return $status_info ; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub freeze { |
157
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
0
|
return $self->_status_action( 'freeze' ); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub thaw { |
163
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
0
|
return $self->_status_action( 'thaw', shift ); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub start { |
169
|
4
|
|
|
4
|
1
|
1042
|
my $self = shift; |
170
|
|
|
|
|
|
|
|
171
|
4
|
|
|
|
|
9
|
return $self->_status_action( 'start' ); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub stop { |
175
|
4
|
|
|
4
|
1
|
784
|
my $self = shift; |
176
|
|
|
|
|
|
|
|
177
|
4
|
|
|
|
|
8
|
return $self->_status_action( 'stop' ); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub delete { |
181
|
2
|
|
|
2
|
1
|
371
|
my $self = shift; |
182
|
|
|
|
|
|
|
|
183
|
2
|
|
|
|
|
6
|
my $url = $self->{hypervisor}->base_url . '/jiffyBoxes/' . $self->id; |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# POSSIBLE EXIT |
186
|
2
|
50
|
|
|
|
10
|
return { url => $url } if ($self->{hypervisor}->test_mode); |
187
|
|
|
|
|
|
|
|
188
|
0
|
|
|
|
|
|
my $response = $self->{hypervisor}->ua->delete($url); |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# POSSIBLE EXIT |
191
|
0
|
0
|
|
|
|
|
unless ($response->is_success) { |
192
|
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
|
$self->last ( $response->status_line ); |
194
|
0
|
|
|
|
|
|
return; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
0
|
|
|
|
|
|
my $delete_info = from_json($response->decoded_content); |
198
|
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
$self->last ($delete_info); |
200
|
0
|
|
|
|
|
|
$self->delete_cache ($delete_info); |
201
|
0
|
|
|
|
|
|
return $delete_info ; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
1; |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
__END__ |