Ovirt/Display.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 125 CC 8 SCAR 23.3
CC
8
Cov
0%
get_display_info
20.0
generate
12.0
set_ticket
12.0
CRAP 10.2
1 package Ovirt::Display;
2  
3 use Ovirt::VM;
4 use Carp;
5 use Moo;
6  
7 with 'Ovirt';
8 our $VERSION = '0.01';
9  
10 =head1 NAME
11  
12 Ovirt::Display - Generate display config for remote viewer
13  
14 =head1 VERSION
15  
16 Version 0.01
17  
18 =cut
19  
20 =head1 SYNOPSIS
21  
22 use Ovirt::Display;
23  
24 my %con = (
25 username => 'admin',
26 password => 'password',
27 manager => 'ovirt-mgr.example.com',
28 id => 'e20cc93c-f526-4682-a250-924fa953f57b',
29 expiry => 300,
30 );
31  
32 my $display = Ovirt::Display->new(%con);
33 print $display->generate();
34  
35 sample spice configuration output :
36 [virt-viewer]
37 type=spice
38 host=192.168.1.152
39 port=-1
40 password=+cnsq458Oq6T
41 # Password is valid for 300 seconds.
42 tls-port=5902
43 fullscreen=0
44 title=C1 : %d - Press SHIFT+F12 to Release Cursor
45 enable-smartcard=0
46 enable-usb-autoshare=1
47 delete-this-file=1
48 usb-filter=-1,-1,-1,-1,0
49 tls-ciphers=DEFAULT
50 host-subject=O=example.com,CN=192.168.1.152
51 ca=-----BEGIN CERTIFICATE-----\n -- output removed -- S2fE=\n-----END CERTIFICATE-----\n
52 toggle-fullscreen=shift+f11
53 release-cursor=shift+f12
54 secure-attention=ctrl+alt+end
55 secure-channels=main;inputs;cursor;playback;record;display;usbredir;smartcard
56  
57 you can save it to a file then use remote viewer to open it:
58 $ remote-viewer [your saved file].vv
59  
60 =head1 Attributes
61  
62 Other attributes is also inherited from Ovirt.pm
63 Check 'perldoc Ovirt' for detail
64
65 notes :
66 ro = read only, can be specified only during initialization
67 rw = read write, user can set this attribute
68 rwp = read write protected, for internal class
69
70 expiry = (rw) specify password expiry in seconds, default is 120s
71 ca = (rwp) store ca contents
72 =cut
73  
74 has 'expiry' => ( is => 'rw', default => 120 );
75 has 'ca' => ( is => 'rwp' );
76  
77 =head1 SUBROUTINES/METHODS
78  
79 =head2 BUILD
80  
81 The Constructor, build logging, call pass_log_obj method from Ovirt.pm
82 get and set ca certificate
83
84 =cut
85  
86 sub BUILD {
87 my $self = shift;
88
89 $self->pass_log_obj;
90
91 my $ca_url = $self->base_url . "/ca.crt";
92 my $ua = LWP::UserAgent->new();
93 my $ca = $ua->get($ca_url);
94
95 $ca = $ca->decoded_content;
96 my @line = split '\n' => $ca;
97
98 $ca = join '\n' => @line;
99
100 $ca =~ s/-----END CERTIFICATE-----/-----END CERTIFICATE-----\\n/;
101 chomp $ca;
102
103 $self->_set_ca($ca);
104 }
105  
106 =head2 generate
107  
108 $display->generate($expire, $vmid);
109 we can also set it during initialization (it's recommended to specify id for efficiency)
110 my %con = (
111 username => 'admin',
112 password => 'password',
113 manager => 'ovirt-mgr.example.com',
114 expiry => 300,
115 id => 'e20cc93c-f526-4682-a250-924fa953f57b'
116 );
117
118 my $display = Ovirt::Display->new(%con);
119 $display->generate();
120
121 =cut
122  
123 sub generate {
124 my $self = shift;
125
126 my $expiry = shift || $self->expiry;
127 my $vmid = $self->id || shift || croak "vm id required";
128 $self->log->debug("expiry: $expiry, vmid: $vmid");
129
130 my $displayinfo = $self->get_display_info($vmid);
131
132 my $vmname = @$displayinfo[1];
133 my $display_type = @$displayinfo[3];
134 my $display_address = @$displayinfo[4];
135 my $display_port = @$displayinfo[5];
136 my $display_host_subject = @$displayinfo[6];
137
138 my $password = $self->set_ticket($expiry, $vmid);
139
140 if ($display_type eq 'spice') {
141 return $self->get_spice_template($vmname,$password,$expiry,$display_address,$display_port,$display_host_subject);
142 }
143 elsif ($display_type eq 'vnc') {
144 return $self->get_vnc_template($vmname,$password,$expiry,$display_address,$display_port);
145 }
146 else {
147 croak "display type not supported\n";
148 }
149 }
150  
151 =head2 get_vnc_template
152  
153 get_vnc_template($vmname,$password,$expiry,$display_address,$display_port);
154 internal method, will return vnc template for Remote Viewer
155 =cut
156  
157 sub get_vnc_template {
158 my $self = shift;
159
160 my ($vmname,$password,$expiry,$display_address,$display_port) = @_;
161
162 my $template = <<EOF;
163 [virt-viewer]
164 type=vnc
165 host=$display_address
166 port=$display_port
167 password=$password
168 # Password is valid for $expiry seconds.
169 delete-this-file=1
170 title=$vmname:%d - Press SHIFT+F12 to Release Cursor
171 toggle-fullscreen=shift+f11
172 release-cursor=shift+f12
173 secure-attention=ctrl+alt+end
174 EOF
175  
176 return $template;
177 }
178  
179 =head2 get_spice_template
180  
181 get_spice_template($vmname,$password,$expiry,$display_address,$display_port,$display_host_subject);
182 internal method, will return spice template for Remote Viewer
183  
184 =cut
185  
186 sub get_spice_template {
187 my $self = shift;
188
189 my ($vmname,$password,$expiry,$display_address,$display_port,$display_host_subject) = @_;
190
191 my $ca = $self->ca;
192 my $template = <<EOF;
193 [virt-viewer]
194 type=spice
195 host=$display_address
196 port=-1
197 password=$password
198 # Password is valid for $expiry seconds.
199 tls-port=$display_port
200 fullscreen=0
201 title=$vmname : %d - Press SHIFT+F12 to Release Cursor
202 enable-smartcard=0
203 enable-usb-autoshare=1
204 delete-this-file=1
205 usb-filter=-1,-1,-1,-1,0
206 tls-ciphers=DEFAULT
207 host-subject=$display_host_subject
208 ca=$ca
209 toggle-fullscreen=shift+f11
210 release-cursor=shift+f12
211 secure-attention=ctrl+alt+end
212 secure-channels=main;inputs;cursor;playback;record;display;usbredir;smartcard
213 EOF
214  
215 return $template;
216 }
217  
218 =head2 set_ticket
219  
220 set_ticket($expire, $vmid);
221 this method will set the password expiry and will return the password
222  
223 =cut
224  
225 sub set_ticket {
226 my $self = shift;
227 my ($expiry, $vmid) = @_;
228
229 my $ticketurl = $self->base_url . "/api/vms/$vmid/ticket";
230 my $xml = <<EOF;
231 <action>
232 <ticket>
233 <expiry>$expiry</expiry>
234 </ticket>
235 </action>
236 EOF
237  
238 $self->log->debug("ticket url : $ticketurl");
239  
240 # set user agent
241 my $ua = LWP::UserAgent->new();
242 my $action = $ua->post($ticketurl, Content_Type => 'application/xml', Content => $xml);
243
244 my $parser = XML::LibXML->new();
245 my $output = $parser->parse_string($action->decoded_content);
246
247 $self->log->debug($output);
248
249 my $status = $output->findnodes('/action/status/state');
250 if ($action->is_success) {
251 croak "status not completed"
252 unless $status eq 'complete';
253
254 my $password = $output->findnodes('/action/ticket/value');
255 return $password;
256 }
257 else {
258 $self->log->debug("LWP Error : " . $action->status_line);
259
260 return $status;
261 }
262 }
263  
264 =head2 get_display_info
265  
266 get_display_info($vmid);
267
268 internal method to query vm display information.
269 this will croak if vm state is not up.
270 By default will return vmid,name,state,display_type,display_address,display_port,display_host_subject.
271 check Ovirt::VM for more information
272  
273 =cut
274  
275 sub get_display_info {
276 my $self = shift;
277
278 my $vmid = shift;
279
280 my %vmcon = (
281 username => $self->username,
282 password => $self->password,
283 manager => $self->manager,
284 id => $vmid,
285 vm_output_attrs => 'id,name,state,display_type,display_address,display_port,display_host_subject',
286 );
287
288 my $vminfo = Ovirt::VM->new(%vmcon);
289
290 my $attrs = $vminfo->list;
291 chomp $attrs;
292
293 $self->log->debug("Attributes: " . $attrs);
294
295 # if state is not up, not interested
296 $self->log->debug("Delimiter is " . $vminfo->vm_output_delimiter() );
297
298 my $delimiter;
299 if ($vminfo->vm_output_delimiter() eq '|') {
300 $delimiter = '\|';
301 }
302 elsif ($vminfo->vm_output_delimiter() eq '||') {
303 $delimiter = '\|\|';
304 }
305 else {
306 $delimiter = $vminfo->vm_output_delimiter();
307 }
308
309 my @state = split $delimiter => $attrs;
310 for my $attr (@state) {
311 $self->log->debug($attr);
312 }
313
314 if ($state[2] ne 'up') {
315 croak "vm state is $state[2], it need to be in up state";
316 }
317
318 return \@state;
319 }
320  
321 =head1 AUTHOR
322  
323 "Heince Kurniawan", C<< <"heince at cpan.org"> >>
324  
325 =head1 BUGS
326  
327 Please report any bugs or feature requests to C<bug-ovirt at rt.cpan.org>, or through
328 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Ovirt::Display>. I will be notified, and then you'll
329 automatically be notified of progress on your bug as I make changes.
330  
331 =head1 SUPPORT
332  
333 You can find documentation for this module with the perldoc command.
334  
335 perldoc Ovirt::Display
336  
337 You can also look for information at:
338  
339 =head1 ACKNOWLEDGEMENTS
340  
341  
342 =head1 LICENSE AND COPYRIGHT
343  
344 Copyright 2015 "Heince Kurniawan".
345  
346 This program is free software; you can redistribute it and/or modify it
347 under the terms of the the Artistic License (2.0). You may obtain a
348 copy of the full license at:
349  
350 L<http://www.perlfoundation.org/artistic_license_2_0>
351  
352 Any use, modification, and distribution of the Standard or Modified
353 Versions is governed by this Artistic License. By using, modifying or
354 distributing the Package, you accept this license. Do not use, modify,
355 or distribute the Package, if you do not accept this license.
356  
357 If your Modified Version has been derived from a Modified Version made
358 by someone other than you, you are nevertheless required to ensure that
359 your Modified Version complies with the requirements of this license.
360  
361 This license does not grant you the right to use any trademark, service
362 mark, tradename, or logo of the Copyright Holder.
363  
364 This license includes the non-exclusive, worldwide, free-of-charge
365 patent license to make, have made, use, offer to sell, sell, import and
366 otherwise transfer the Package with respect to any patent claims
367 licensable by the Copyright Holder that are necessarily infringed by the
368 Package. If you institute patent litigation (including a cross-claim or
369 counterclaim) against any party alleging that the Package constitutes
370 direct or contributory patent infringement, then this Artistic License
371 to you shall terminate on the date that such litigation is filed.
372  
373 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
374 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
375 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
376 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
377 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
378 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
379 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
380 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
381  
382  
383 =cut
384  
385 1;