| 1 | package VM::EC2::REST::ami; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use VM::EC2 ''; # important not to import anything! | ||
| 5 | package VM::EC2; # add methods to VM::EC2 | ||
| 6 | |||
| 7 | VM::EC2::Dispatch->register( | ||
| 8 | DescribeImages => 'fetch_items,imagesSet,VM::EC2::Image', | ||
| 9 | CreateImage => sub { shift->{imageId} }, | ||
| 10 | RegisterImage => sub { shift->{imageId} }, | ||
| 11 | DeregisterImage => 'boolean', | ||
| 12 | ModifyImageAttribute => 'boolean', | ||
| 13 | ResetImageAttribute => 'boolean', | ||
| 14 | CopyImage => sub { shift->{imageId} }, | ||
| 15 | ); | ||
| 16 | |||
| 17 | =head1 NAME VM::EC2::REST::ami | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use VM::EC2 ':standard'; | ||
| 22 | |||
| 23 | =head1 METHODS | ||
| 24 | |||
| 25 | These are methods that allow you to fetch and manipulate Amazon Machine Images. | ||
| 26 | |||
| 27 | Implemented: | ||
| 28 | CopyImage | ||
| 29 | CreateImage | ||
| 30 | DeregisterImage | ||
| 31 | DescribeImageAttribute | ||
| 32 | DescribeImages | ||
| 33 | ModifyImageAttribute | ||
| 34 | RegisterImage | ||
| 35 | ResetImageAttribute | ||
| 36 | |||
| 37 | Unimplemented: | ||
| 38 | (none) | ||
| 39 | |||
| 40 | =head1 EC2 AMAZON MACHINE IMAGES | ||
| 41 | |||
| 42 | The methods in this section allow you to query and manipulate Amazon | ||
| 43 | machine images (AMIs). See L<VM::EC2::Image>. | ||
| 44 | |||
| 45 | =head2 @i = $ec2->describe_images(@image_ids) | ||
| 46 | |||
| 47 | =head2 @i = $ec2->describe_images(-image_id=>\@id,-executable_by=>$id, | ||
| 48 | -owner=>$id, -filter=>\%filters) | ||
| 49 | |||
| 50 | Return a series of VM::EC2::Image objects, each describing an | ||
| 51 | AMI. Optional arguments: | ||
| 52 | |||
| 53 | -image_id The id of the image, either a string scalar or an | ||
| 54 | arrayref. | ||
| 55 | |||
| 56 | -executable_by Filter by images executable by the indicated user account, or | ||
| 57 | one of the aliases "self" or "all". | ||
| 58 | |||
| 59 | -owner Filter by owner account number or one of the aliases "self", | ||
| 60 | "aws-marketplace", "amazon" or "all". | ||
| 61 | |||
| 62 | -filter Tags and other filters to apply | ||
| 63 | |||
| 64 | If there are no other arguments, you may omit the -filter argument | ||
| 65 | name and call describe_images() with a single hashref consisting of | ||
| 66 | the search filters you wish to apply. | ||
| 67 | |||
| 68 | The full list of image filters can be found at: | ||
| 69 | http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeImages.html | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub describe_images { | ||
| 74 | my $self = shift; | ||
| 75 | my %args = $self->args(-image_id=>@_); | ||
| 76 | my @params; | ||
| 77 | push @params,$self->list_parm($_,\%args) foreach qw(ExecutableBy ImageId Owner); | ||
| 78 | push @params,$self->filter_parm(\%args); | ||
| 79 | return $self->call('DescribeImages',@params); | ||
| 80 | } | ||
| 81 | |||
| 82 | =head2 $image = $ec2->create_image(-instance_id=>$id,-name=>$name,%other_args) | ||
| 83 | |||
| 84 | Create an image from an EBS-backed instance and return a | ||
| 85 | VM::EC2::Image object. The instance must be in the "stopped" or | ||
| 86 | "running" state. In the latter case, Amazon will stop the instance, | ||
| 87 | create the image, and then restart it unless the -no_reboot argument | ||
| 88 | is provided. | ||
| 89 | |||
| 90 | Arguments: | ||
| 91 | |||
| 92 | -instance_id ID of the instance to create an image from. (required) | ||
| 93 | -name Name for the image that will be created. (required) | ||
| 94 | -description Description of the new image. | ||
| 95 | -no_reboot If true, don't reboot the instance. | ||
| 96 | -block_device_mapping | ||
| 97 | Block device mapping as a scalar or array ref. See | ||
| 98 | run_instances() for the syntax. | ||
| 99 | -block_devices Alias of the above | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub create_image { | ||
| 104 | my $self = shift; | ||
| 105 | my %args = @_; | ||
| 106 | $args{-instance_id} && $args{-name} | ||
| 107 | or croak "Usage: create_image(-instance_id=>\$id,-name=>\$name)"; | ||
| 108 | $args{-block_device_mapping} ||= $args{-block_devices}; | ||
| 109 | my @param = $self->single_parm('InstanceId',\%args); | ||
| 110 | push @param,$self->single_parm('Name',\%args); | ||
| 111 | push @param,$self->single_parm('Description',\%args); | ||
| 112 | push @param,$self->boolean_parm('NoReboot',\%args); | ||
| 113 | push @param,$self->block_device_parm($args{-block_device_mapping}); | ||
| 114 | |||
| 115 | my $cv = $self->condvar; | ||
| 116 | { | ||
| 117 | local $VM::EC2::ASYNC = 1; | ||
| 118 | $self->call('CreateImage',@param)->cb( | ||
| 119 | sub { | ||
| 120 | my $img_id = shift->recv; | ||
| 121 | my $timer; $timer = AnyEvent->timer(after => 0.5, | ||
| 122 | interval => 1, | ||
| 123 | cb => sub { | ||
| 124 | my $di = $self->describe_images_async($img_id); | ||
| 125 | $di->cb(sub { | ||
| 126 | if (my $img = shift->recv) { | ||
| 127 | $cv->send($img); | ||
| 128 | undef $timer; | ||
| 129 | }}); | ||
| 130 | }); | ||
| 131 | }); | ||
| 132 | } | ||
| 133 | return $VM::EC2::ASYNC ? $cv : $cv->recv; | ||
| 134 | } | ||
| 135 | |||
| 136 | =head2 $image = $ec2->register_image(-name=>$name,%other_args) | ||
| 137 | |||
| 138 | Register an image, creating an AMI. This can be used to create an AMI | ||
| 139 | from a S3-backed instance-store bundle, or to create an AMI from a | ||
| 140 | snapshot of an EBS-backed root volume. | ||
| 141 | |||
| 142 | Required arguments: | ||
| 143 | |||
| 144 | -name Name for the image that will be created. | ||
| 145 | |||
| 146 | Arguments required for an EBS-backed image: | ||
| 147 | |||
| 148 | -root_device_name The root device name, e.g. /dev/sda1 | ||
| 149 | -block_device_mapping The block device mapping strings, including the | ||
| 150 | snapshot ID for the root volume. This can | ||
| 151 | be either a scalar string or an arrayref. | ||
| 152 | See run_instances() for a description of the | ||
| 153 | syntax. | ||
| 154 | -block_devices Alias of the above. | ||
| 155 | |||
| 156 | Arguments required for an instance-store image: | ||
| 157 | |||
| 158 | -image_location Full path to the AMI manifest in Amazon S3 storage. | ||
| 159 | |||
| 160 | Common optional arguments: | ||
| 161 | |||
| 162 | -description Description of the AMI | ||
| 163 | -architecture Architecture of the image ("i386" or "x86_64") | ||
| 164 | -kernel_id ID of the kernel to use | ||
| 165 | -ramdisk_id ID of the RAM disk to use | ||
| 166 | -virtualization_type Type of virtualization ("paravirtual" or "hvm") | ||
| 167 | Default is "paravirtual" | ||
| 168 | -sriov_net_support Set to "simple" to enable enhanced networking for the AMI | ||
| 169 | |||
| 170 | While you do not have to specify the kernel ID, it is strongly | ||
| 171 | recommended that you do so. Otherwise the kernel will have to be | ||
| 172 | specified for run_instances(). | ||
| 173 | |||
| 174 | Note: Immediately after registering the image you can add tags to it | ||
| 175 | and use modify_image_attribute to change launch permissions, etc. | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | sub register_image { | ||
| 180 | my $self = shift; | ||
| 181 | my %args = @_; | ||
| 182 | |||
| 183 | $args{-name} or croak "register_image(): -name argument required"; | ||
| 184 | $args{-block_device_mapping} ||= $args{-block_devices}; | ||
| 185 | if (!$args{-image_location}) { | ||
| 186 | $args{-root_device_name} && $args{-block_device_mapping} | ||
| 187 | or croak "register_image(): either provide -image_location to create an instance-store AMI\nor both the -root_device_name && -block_device_mapping arguments to create an EBS-backed AMI."; | ||
| 188 | } | ||
| 189 | |||
| 190 | my @param; | ||
| 191 | for my $a (qw(Name RootDeviceName ImageLocation Description | ||
| 192 | Architecture KernelId RamdiskId VirtualizationType | ||
| 193 | SriovNetSupport)) { | ||
| 194 | push @param,$self->single_parm($a,\%args); | ||
| 195 | } | ||
| 196 | push @param,$self->block_device_parm($args{-block_devices} || $args{-block_device_mapping}); | ||
| 197 | |||
| 198 | return $self->call('RegisterImage',@param); | ||
| 199 | } | ||
| 200 | |||
| 201 | =head2 $result = $ec2->deregister_image($image_id) | ||
| 202 | |||
| 203 | Deletes the registered image and returns true if successful. | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | sub deregister_image { | ||
| 208 | my $self = shift; | ||
| 209 | my %args = $self->args(-image_id => @_); | ||
| 210 | my @param = $self->single_parm(ImageId=>\%args); | ||
| 211 | return $self->call('DeregisterImage',@param); | ||
| 212 | } | ||
| 213 | |||
| 214 | =head2 @data = $ec2->describe_image_attribute($image_id,$attribute) | ||
| 215 | |||
| 216 | This method returns image attributes. Only one attribute can be | ||
| 217 | retrieved at a time. The following is the list of attributes that can be | ||
| 218 | retrieved: | ||
| 219 | |||
| 220 | description -- scalar | ||
| 221 | kernel -- scalar | ||
| 222 | ramdisk -- scalar | ||
| 223 | launchPermission -- list of scalar | ||
| 224 | productCodes -- array | ||
| 225 | blockDeviceMapping -- list of hashref | ||
| 226 | sriovNetSupport -- scalar | ||
| 227 | |||
| 228 | All of these values can be retrieved more conveniently from the | ||
| 229 | L<VM::EC2::Image> object returned from describe_images(), so there is | ||
| 230 | no attempt to parse the results of this call into Perl objects. In | ||
| 231 | particular, 'blockDeviceMapping' is returned as a raw hashrefs (there | ||
| 232 | also seems to be an AWS bug that causes fetching this attribute to return an | ||
| 233 | AuthFailure error). | ||
| 234 | |||
| 235 | Please see the VM::EC2::Image launchPermissions() and | ||
| 236 | blockDeviceMapping() methods for more convenient ways to get this | ||
| 237 | data. | ||
| 238 | |||
| 239 | =cut | ||
| 240 | |||
| 241 | sub describe_image_attribute { | ||
| 242 | my $self = shift; | ||
| 243 | @_ == 2 or croak "Usage: describe_image_attribute(\$instance_id,\$attribute_name)"; | ||
| 244 | my ($instance_id,$attribute) = @_; | ||
| 245 | my @param = (ImageId=>$instance_id,Attribute=>$attribute); | ||
| 246 | my $result = $self->call('DescribeImageAttribute',@param); | ||
| 247 | return $result && $result->attribute($attribute); | ||
| 248 | } | ||
| 249 | |||
| 250 | |||
| 251 | =head2 $boolean = $ec2->modify_image_attribute($image_id,-$attribute_name=>$value) | ||
| 252 | |||
| 253 | This method changes image attributes. The first argument is the image | ||
| 254 | ID, and this is followed by the attribute name and the value to change | ||
| 255 | it to. | ||
| 256 | |||
| 257 | The following is the list of attributes that can be set: | ||
| 258 | |||
| 259 | -launch_add_user -- scalar or arrayref of UserIds to grant launch permissions to | ||
| 260 | -launch_add_group -- scalar or arrayref of Groups to remove launch permissions from | ||
| 261 | (only currently valid value is "all") | ||
| 262 | -launch_remove_user -- scalar or arrayref of UserIds to remove from launch permissions | ||
| 263 | -launch_remove_group -- scalar or arrayref of Groups to remove from launch permissions | ||
| 264 | -product_code -- scalar or array of product codes to add | ||
| 265 | -description -- scalar new description | ||
| 266 | |||
| 267 | You can abbreviate the launch permission arguments to -add_user, | ||
| 268 | -add_group, -remove_user, -remove_group, etc. | ||
| 269 | |||
| 270 | Only one attribute can be changed in a single request. | ||
| 271 | |||
| 272 | For example: | ||
| 273 | |||
| 274 | $ec2->modify_image_attribute('ami-12345',-product_code=>['abcde','ghijk']); | ||
| 275 | |||
| 276 | The result code is true if the attribute was successfully modified, | ||
| 277 | false otherwise. In the latter case, $ec2->error() will provide the | ||
| 278 | error message. | ||
| 279 | |||
| 280 | To make an image public, specify -launch_add_group=>'all': | ||
| 281 | |||
| 282 | $ec2->modify_image_attribute('i-12345',-launch_add_group=>'all'); | ||
| 283 | |||
| 284 | Also see L<VM::EC2::Image> for shortcut methods. For example: | ||
| 285 | |||
| 286 | $image->add_authorized_users(1234567,999991); | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | sub modify_image_attribute { | ||
| 291 | my $self = shift; | ||
| 292 | my $image_id = shift or croak "Usage: modify_image_attribute(\$imageId,%param)"; | ||
| 293 | my %args = @_; | ||
| 294 | |||
| 295 | # shortcuts | ||
| 296 | foreach (qw(add_user remove_user add_group remove_group)) { | ||
| 297 | $args{"-launch_$_"} ||= $args{"-$_"}; | ||
| 298 | } | ||
| 299 | |||
| 300 | my @param = (ImageId=>$image_id); | ||
| 301 | push @param,$self->value_parm('Description',\%args); | ||
| 302 | push @param,$self->list_parm('ProductCode',\%args); | ||
| 303 | push @param,$self->launch_perm_parm('Add','UserId', $args{-launch_add_user}); | ||
| 304 | push @param,$self->launch_perm_parm('Remove','UserId',$args{-launch_remove_user}); | ||
| 305 | push @param,$self->launch_perm_parm('Add','Group', $args{-launch_add_group}); | ||
| 306 | push @param,$self->launch_perm_parm('Remove','Group', $args{-launch_remove_group}); | ||
| 307 | return $self->call('ModifyImageAttribute',@param); | ||
| 308 | } | ||
| 309 | |||
| 310 | =head2 $boolean = $ec2->reset_image_attribute($image_id,$attribute_name) | ||
| 311 | |||
| 312 | This method resets an attribute of the given snapshot to its default | ||
| 313 | value. The valid attributes are: | ||
| 314 | |||
| 315 | launchPermission | ||
| 316 | |||
| 317 | |||
| 318 | =cut | ||
| 319 | |||
| 320 | sub reset_image_attribute { | ||
| 321 | my $self = shift; | ||
| 322 | @_ == 2 or | ||
| 323 | croak "Usage: reset_image_attribute(\$imageId,\$attribute_name)"; | ||
| 324 | my ($image_id,$attribute) = @_; | ||
| 325 | my %valid = map {$_=>1} qw(launchPermission); | ||
| 326 | $valid{$attribute} or croak "attribute to reset must be one of ",join(' ',map{"'$_'"} keys %valid); | ||
| 327 | return $self->call('ResetImageAttribute', | ||
| 328 | ImageId => $image_id, | ||
| 329 | Attribute => $attribute); | ||
| 330 | } | ||
| 331 | |||
| 332 | =head2 $image = $ec2->copy_image(-source_region => $src, | ||
| 333 | -source_image_id => $id, | ||
| 334 | -name => $name, | ||
| 335 | -description => $desc, | ||
| 336 | -client_token => $token) | ||
| 337 | |||
| 338 | Initiates the copy of an AMI from the specified source region to the | ||
| 339 | region in which the API call is executed. | ||
| 340 | |||
| 341 | Required arguments: | ||
| 342 | |||
| 343 | -source_region -- The ID of the AWS region that contains the AMI to be | ||
| 344 | copied (source). | ||
| 345 | |||
| 346 | -source_image_id -- The ID of the Amazon EC2 AMI to copy. | ||
| 347 | |||
| 348 | Optional arguments: | ||
| 349 | |||
| 350 | -name -- The name of the new EC2 AMI in the destination region. | ||
| 351 | |||
| 352 | -description -- A description of the new AMI in the destination region. | ||
| 353 | |||
| 354 | -client_token -- Unique, case-sensitive identifier you provide to ensure | ||
| 355 | idempotency of the request. | ||
| 356 | |||
| 357 | Returns a L<VM::EC2::Image> object on success; | ||
| 358 | |||
| 359 | =cut | ||
| 360 | |||
| 361 | sub copy_image { | ||
| 362 | my $self = shift; | ||
| 363 | my %args = @_; | ||
| 364 | $args{-description} ||= $args{-desc}; | ||
| 365 | $args{-source_region} ||= $args{-region}; | ||
| 366 | $args{-source_image_id} ||= $args{-image_id}; | ||
| 367 | $args{-source_region} or croak "copy_image(): -source_region argument required"; | ||
| 368 | $args{-source_image_id} or croak "copy_image(): -source_image_id argument required"; | ||
| 369 | my @params; | ||
| 370 | push @params, $self->single_parm($_,\%args) | ||
| 371 | foreach qw(SourceRegion SourceImageId Name Description ClientToken); | ||
| 372 | my $image_id = $self->call('CopyImage',@params) or return; | ||
| 373 | return eval { | ||
| 374 | my $image; | ||
| 375 | local $SIG{ALRM} = sub {die "timeout"}; | ||
| 376 | alarm(60); | ||
| 377 | until ($image = $self->describe_images($image_id)) { sleep 1 } | ||
| 378 | alarm(0); | ||
| 379 | $image; | ||
| 380 | }; | ||
| 381 | } | ||
| 382 | |||
| 383 | |||
| 384 | =head1 SEE ALSO | ||
| 385 | |||
| 386 | L<VM::EC2> | ||
| 387 | |||
| 388 | =head1 AUTHOR | ||
| 389 | |||
| 390 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 391 | |||
| 392 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||
| 393 | |||
| 394 | This package and its accompanying libraries is free software; you can | ||
| 395 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 396 | version 1, or at your option, any later version) or the Artistic | ||
| 397 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 398 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 399 | |||
| 400 | =cut | ||
| 401 | |||
| 402 | |||
| 403 | 1; |