blib/lib/VM/EC2/Image.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 20 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 166 CC 7 SCAR 15.1
CC
7
Cov
0%
make_public
12.0
refresh
12.0
productCodes
6.0
CRAP 4.5
1 package VM::EC2::Image;
2  
3 =head1 NAME
4  
5 VM::EC2::Image - Object describing an Amazon Machine Image (AMI)
6  
7 =head1 SYNOPSIS
8  
9 use VM::EC2;
10  
11 $ec2 = VM::EC2->new(...);
12 $image = $ec2->describe_images(-image_id=>'ami-12345');
13  
14 $state = $image->imageState;
15 $owner = $image->imageOwnerId;
16 $rootdev = $image->rootDeviceName;
17 @devices = $image->blockDeviceMapping;
18 $tags = $image->tags;
19  
20 @instances = $image->run_instances(-min_count=>10);
21  
22 =head1 DESCRIPTION
23  
24 This object represents an Amazon Machine Image (AMI), and is returned
25 by VM::EC2->describe_images(). In addition to methods to query the
26 image's attributes, the run_instances() method allows you to launch
27 and configure EC2 instances based on the AMI.
28  
29 =head1 METHODS
30  
31 These object methods are supported:
32  
33 imageId -- AMI ID
34 imageLocation -- Location of the AMI
35 imageState -- Current state of the AMI. One of "available",
36 "pending" or "failed". Only "available" AMIs
37 can be launched.
38 imageOwnerId -- AWS account ID of the image owner.
39 isPublic -- Returns true if this image has public launch
40 permissions. Note that this is a Perl boolean,
41 and not the string "true".
42 productCodes -- A list of product codes associated with the image.
43 architecture -- The architecture of the image.
44 imageType -- The image type (machine, kernel or RAM disk).
45 kernelId -- The kernel associated with the image.
46 ramdiskId -- The RAM disk associated with the image.
47 platform -- "Windows" for Windows AMIs, otherwise undef.
48 sriovNetSupport -- Specifies whether enhanced networking is enabled.
49 "simple" if enabled.
50 stateReason -- Explanation of a "failed" imageState. This is
51 a VM::EC2::Instance::State::Reason
52 object.
53 imageOwnerAlias -The AWS account alias (e.g. "self") or AWS
54 account ID that owns the AMI.
55 name -- Name of the AMI provided during image creation.
56 description -- Description of the AMI provided during image
57 creation.
58 rootDeviceType -- The root device type. One of "ebs" or
59 "instance-store".
60 rootDeviceName -- Name of the root device, e.g. "/dev/sda1"
61 blockDeviceMapping -- List of block devices attached to this
62 image. Each element is a
63 VM::EC2::BlockDevice.
64 virtualizationType -- One of "paravirtual" or "hvm".
65 hypervisor -- One of "ovm" or "xen"
66  
67 In addition, the object supports the tags() method described in
68 L<VM::EC2::Generic>:
69  
70 print "ready for production\n" if $image->tags->{Released};
71  
72 =head2 @instances = $image->run_instances(%args)
73  
74 The run_instance() method will launch one or more instances based on
75 this AMI. The method takes all the arguments recognized by
76 VM::EC2->run_instances(), except for the -image_id argument. The
77 method returns a list of VM::EC2::Instance objects, which you may
78 monitor periodically until they are up and running.
79  
80 All arguments are optional. See run_instances() in L<VM::EC2> for
81 more information.
82  
83 -min_count Minimum number of instances to launch [1]
84 -max_count Maximum number of instances to launch [1]
85 -key_name Name of the keypair to use
86 -security_group_id Security group ID to use for this instance.
87 Use an arrayref for multiple group IDs
88 -security_group Security group name to use for this instance.
89 Use an arrayref for multiple values.
90 -user_data User data to pass to the instances. Do NOT base64
91 encode this. It will be done for you.
92 -instance_type Type of the instance to use. See below for a
93 list.
94 -placement_zone The availability zone you want to launch the
95 instance into. Call $ec2->regions for a list.
96 -placement_group An existing placement group to launch the
97 instance into. Applicable to cluster instances
98 only.
99 -placement_tenancy Specify 'dedicated' to launch the instance on a
100 dedicated server. Only applicable for VPC
101 instances.
102 -kernel_id ID of the kernel to use for the instances,
103 overriding the kernel specified in the image.
104 -ramdisk_id ID of the ramdisk to use for the instances,
105 overriding the ramdisk specified in the image.
106 -block_devices Specify block devices to map onto the instances,
107 overriding the values specified in the image.
108 This can be a scalar string or an arrayref for
109 multiple mappings:
110 Example:
111 ['/dev/sdb=ephemeral0','/dev/sdc=snap-12345:80:false']
112 -monitoring Pass a true value to enable detailed monitoring.
113 -subnet_id ID of the subnet to launch the instance
114 into. Only applicable for VPC instances.
115 -termination_protection Pass true to lock the instance so that it
116 cannot be terminated using the API. Use
117 modify_instance() to unset this if youu wish to
118 terminate the instance later.
119 -disable_api_termination -- Same as above.
120 -shutdown_behavior Pass "stop" (the default) to stop the instance
121 and save its disk state when "shutdown" is called
122 from within the instance. Stopped instances can
123 be restarted later. Pass "terminate" to
124 instead terminate the instance and discard its
125 state completely.
126 -instance_initiated_shutdown_behavior -- Same as above.
127 -private_ip_address Assign the instance to a specific IP address
128 from a VPC subnet (VPC only).
129 -client_token Unique identifier that you can provide to ensure
130 idempotency of the request. You can use
131 $ec2->token() to generate a suitable identifier.
132 See http://docs.amazonwebservices.com/AWSEC2/
133 latest/UserGuide/Run_Instance_Idempotency.html
134  
135 Note that after launching an instance, you may need to wait a few
136 seconds before adding tags to it or performing other operations.
137  
138 See L<VM::EC2> for details.
139  
140 =head2 $boolean = $image->make_public($public)
141  
142 Change the isPublic flag. Provide a true value to make the image
143 public, a false one to make it private.
144  
145 =head2 $state = $image->current_status
146  
147 Refreshes the object and then calls imageState() to return one of
148 "pending", "available" or "failed." You can use this to monitor an
149 image_creation process in progress.
150  
151 =head2 @user_ids = $image->launchPermissions
152  
153 Returns a list of user IDs with launch permission for this
154 image. Note that the AWS API calls this
155 "launchPermission", but this module makes it plural to emphasize that
156 the result is a list.
157  
158 =head2 @user_ids = $image->authorized_users
159  
160 The same as launchPermissions.
161  
162 =head2 $boolean = $image->add_authorized_users($id1,$id2,...)
163  
164 =head2 $boolean = $image->remove_authorized_users($id1,$id2,...)
165  
166 These methods add and remove user accounts which have launch
167 permissions for the image. The result code indicates whether the list
168 of user IDs were successfully added or removed.
169  
170 =head2 $boolean = $image->add_authorized_users($id1,$id2,...)
171  
172 =head2 $boolean = $image->remove_authorized_users($id1,$id2,...)
173  
174 =head2 $boolean = $image->reset_authorized_users
175  
176 These methods add and remove user accounts which have launch
177 permissions for the image. The result code indicates whether the list
178 of user IDs were successfully added or removed.
179  
180 reset_authorized_users() resets the list users authored to launch
181 instances from this image to empty, effectively granting launch
182 privileges to the owner only.
183  
184 See also authorized_users().
185  
186 =head2 $copy = $image->copy(-region => $region, -name => $name, -description => $desc)
187  
188 Copies the image to another region.
189  
190 Required arguments:
191  
192 -region The region to copy to
193  
194 Optional arguments:
195  
196 -name The name of the image.
197  
198 -description The description of the image.
199  
200 -client_token Unique identifier to ensure idempotency of the request
201  
202 =head2 $image->refresh
203  
204 This method will refresh the object from AWS, updating all values to
205 their current ones. You can call it after tagging or otherwise
206 changing image attributes.
207  
208 =head1 STRING OVERLOADING
209  
210 When used in a string context, this object will interpolate the
211 imageId.
212  
213 =head1 SEE ALSO
214  
215 L<VM::EC2>
216 L<VM::EC2::Generic>
217 L<VM::EC2::BlockDevice>
218 L<VM::EC2::StateReason>
219 L<VM::EC2::Instance>
220 L<VM::EC2::Tag>
221  
222 =head1 AUTHOR
223  
224 Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>.
225  
226 Copyright (c) 2011 Ontario Institute for Cancer Research
227  
228 This package and its accompanying libraries is free software; you can
229 redistribute it and/or modify it under the terms of the GPL (either
230 version 1, or at your option, any later version) or the Artistic
231 License 2.0. Refer to LICENSE for the full license text. In addition,
232 please see DISCLAIMER.txt for disclaimers of warranty.
233  
234 =cut
235  
236 use strict;
237 use base 'VM::EC2::Generic';
238 use VM::EC2::BlockDevice;
239 use VM::EC2::Image::LaunchPermission;
240 use VM::EC2::Instance::State::Reason;
241 use VM::EC2::ProductCode;
242  
243 use Carp 'croak';
244  
245 sub valid_fields {
246 my $self = shift;
247 return qw(imageId imageLocation imageState imageOwnerId isPublic
248 productCodes architecture imageType kernelId ramdiskId
249 platform sriovNetSupport stateReason imageOwnerAlias name
250 description rootDeviceType rootDeviceName blockDeviceMapping
251 virtualizationType tagSet hypervisor);
252 }
253  
254 sub primary_id { shift->imageId }
255  
256 sub stateReason {
257 my $self = shift;
258 my $state = $self->SUPER::stateReason;
259 return VM::EC2::Instance::State::Reason->new($state);
260  
261 }
262  
263 sub productCodes {
264 my $self = shift;
265 if (@_) {
266 $self->aws->modify_image_attribute($self,-product_code=>\@_);
267 } else {
268 my $codes = $self->SUPER::productCodes or return;
269 return map {VM::EC2::ProductCode->new($_)} @{$codes->{item}};
270 }
271 }
272  
273 sub blockDeviceMapping {
274 my $self = shift;
275 my $mapping = $self->SUPER::blockDeviceMapping or return;
276 return map { VM::EC2::BlockDevice->new($_,$self->aws)} @{$mapping->{item}};
277 }
278  
279 sub launchPermissions {
280 my $self = shift;
281 return map {VM::EC2::Image::LaunchPermission->new($_,$self->aws)}
282 $self->aws->describe_image_attribute($self->imageId,'launchPermission');
283 }
284  
285 sub isPublic {
286 my $self = shift;
287 return $self->SUPER::isPublic eq 'true';
288 }
289  
290 sub make_public {
291 my $self = shift;
292 @_ == 1 or croak "Usage: VM::EC2::Image->make_public(\$boolean)";
293 my $public = shift;
294 my @arg = $public ? (-launch_add_group=>'all') : (-launch_remove_group=>'all');
295 my $result = $self->aws->modify_image_attribute($self->imageId,@arg) or return;
296 $self->payload->{isPublic} = $public ? 'true' : 'false';
297 return $result
298 }
299  
300 sub authorized_users { shift->launchPermissions }
301  
302 sub add_authorized_users {
303 my $self = shift;
304 @_ or croak "Usage: VM::EC2::Image->add_authorized_users(\@userIds)";
305 return $self->aws->modify_image_attribute($self->imageId,-launch_add_user=>\@_);
306 }
307  
308 sub remove_authorized_users {
309 my $self = shift;
310 @_ or croak "Usage: VM::EC2::Image->remove_authorized_users(\@userIds)";
311 return $self->aws->modify_image_attribute($self->imageId,-launch_remove_user=>\@_);
312 }
313  
314 sub reset_authorized_users {
315 my $self = shift;
316 $self->aws->reset_image_attribute($self->imageId,'launchPermission');
317 }
318  
319 sub run_instances {
320 my $self = shift;
321 my %args = @_;
322 croak "$self is unavailable for launching because its state is ",$self->imageState
323 unless $self->imageState eq 'available';
324 $args{-image_id} = $self->imageId;
325 $self->aws->run_instances(%args);
326 }
327  
328 sub current_status {
329 my $self = shift;
330 $self->refresh;
331 return $self->imageState;
332 }
333  
334 sub current_status_async {
335 my $self = shift;
336 my $to_caller = VM::EC2->condvar;
337  
338 my $cv = $self->aws->describe_images_async(-image_id=>$self->imageId);
339  
340 $cv->cb(sub {
341 my $i = shift->recv;
342 $to_caller->send($i->imageState)
343 });
344  
345 return $to_caller;
346 }
347  
348  
349 sub refresh {
350 my $self = shift;
351 my $i = shift;
352 ($i) = $self->aws->describe_images(-image_id=>$self->imageId) unless $i;
353 %$self = %$i if $i;
354 return defined $i;
355 }
356  
357 sub copy {
358 my $self = shift;
359 my %args = @_;
360 my $image_id = $self->imageId;
361 my $name = $args{-name};
362 my $desc = $args{-description} || $args{-desc};
363 my $token = $args{-client_token} || $args{-token};
364 my $region = $args{-region} or croak "copy(): -region argument required";
365 my $orig_region = $self->aws->region;
366 # set region to dest region in ec2 object
367 $self->aws->region($region);
368 my $image = $self->aws->copy_image(-source_region=>$orig_region, -source_image_id=>$image_id, -name => $name, -description=>$desc, -client_token => $token);
369 # set region back
370 $self->aws->region($orig_region);
371 return $image;
372 }
373  
374 1;