| 1 | package VM::EC2::Snapshot; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | VM::EC2::Snapshot - Object describing an Amazon EBS snapshot | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use VM::EC2; | ||
| 10 | |||
| 11 | $ec2 = VM::EC2->new(...); | ||
| 12 | @snap = $ec2->describe_snapshots; | ||
| 13 | for my $snap (@snapshots) { | ||
| 14 | $id = $snap->snapshotId; | ||
| 15 | $vol = $snap->volumeId; | ||
| 16 | $state = $snap->status; | ||
| 17 | $time = $snap->startTime; | ||
| 18 | $progress = $snap->progress; | ||
| 19 | $size = $snap->volumeSize; | ||
| 20 | $description = $snap->description; | ||
| 21 | $tags = $snap->tags; | ||
| 22 | } | ||
| 23 | |||
| 24 | # use a snapshot as the root device for a new AMI | ||
| 25 | $ami = $snap->register_image(-name => 'My new image', | ||
| 26 | -kernel_id => 'aki-407d9529', | ||
| 27 | -architecture => 'i386'); | ||
| 28 | |||
| 29 | #create a volume from the snapshot | ||
| 30 | $vol = $snap->create_volume(-zone => 'us-east-1a'); | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | This object is used to describe an Amazon EBS snapshot. | ||
| 35 | |||
| 36 | =head1 METHODS | ||
| 37 | |||
| 38 | The following object methods are supported: | ||
| 39 | |||
| 40 | snapshotId -- ID of this snapshot | ||
| 41 | ownerId -- Owner of this snapshot | ||
| 42 | volumeId -- ID of the volume snapshot was taken from | ||
| 43 | status -- Snapshot state, one of "pending", "completed" or "error" | ||
| 44 | startTime -- Timestamp for when snapshot was begun. | ||
| 45 | progress -- The progress of the snapshot, in percent. | ||
| 46 | volumeSize -- Size of the volume, in gigabytes. | ||
| 47 | description -- Description of the snapshot | ||
| 48 | ownerAlias -- AWS account alias, such as "self". | ||
| 49 | encrypted -- True if snapshot is encrypted | ||
| 50 | tags -- Hashref containing tags associated with this group. | ||
| 51 | See L<VM::EC2::Generic>. | ||
| 52 | |||
| 53 | In addition, this class provides several convenience functions: | ||
| 54 | |||
| 55 | =head2 $vol = $snap->from_volume | ||
| 56 | |||
| 57 | Returns the VM::EC2::Volume object that this snapshot was originally | ||
| 58 | derived from. If the original volume no longer exists because it has | ||
| 59 | been deleted, this will return undef; if -raise_error was passed to | ||
| 60 | the VM::EC2 object, this will raise an exception. | ||
| 61 | |||
| 62 | =head2 @vol = $snap->to_volumes | ||
| 63 | |||
| 64 | Returns all VM::EC2::Volume objects that were derived from this | ||
| 65 | snapshot. If no volumes currently exist that satisfy this criteria, | ||
| 66 | returns an empty list, but will not raise an error. | ||
| 67 | |||
| 68 | =head2 $image = $snap->register_image(%args) | ||
| 69 | |||
| 70 | Register a new AMI using this snapshot as the root device. By default, | ||
| 71 | the root device will be mapped to /dev/sda1 and will delete on | ||
| 72 | instance termination. You can modify this behavior and add additional | ||
| 73 | block devices. | ||
| 74 | |||
| 75 | Arguments: | ||
| 76 | |||
| 77 | -name Name for this image (required) | ||
| 78 | |||
| 79 | -description Description of this image | ||
| 80 | |||
| 81 | -kernel_id Kernel for this image (recommended) | ||
| 82 | |||
| 83 | -ramdisk_id Ramdisk for this image | ||
| 84 | |||
| 85 | -architecture Architecture ("i386" or "x86_64") | ||
| 86 | |||
| 87 | -root_device_name Specify the root device based | ||
| 88 | on this snapshot (/dev/sda1). | ||
| 89 | |||
| 90 | -root_size Size of the root volume (defaults | ||
| 91 | to size of the snapshot). | ||
| 92 | |||
| 93 | -root_delete_on_termination True value (default) to delete | ||
| 94 | the root volume after the instance | ||
| 95 | terminates. False value to keep the | ||
| 96 | EBS volume available. | ||
| 97 | |||
| 98 | -block_device_mapping Additional block devices you wish to | ||
| 99 | incorporate into the image. | ||
| 100 | |||
| 101 | -block_devices Same as above. | ||
| 102 | |||
| 103 | See L<VM::EC2> for information on the syntax of the | ||
| 104 | -block_device_mapping argument. If the root device is explicitly | ||
| 105 | included in the block device mapping argument, then the arguments | ||
| 106 | specified in -root_size, and -root_delete_on_termination will be | ||
| 107 | ignored, and the current snapshot will not automatically be used as | ||
| 108 | the root device. | ||
| 109 | |||
| 110 | The return value is a L<VM::EC2::Image>. You can call its | ||
| 111 | current_status() method to poll its availability: | ||
| 112 | |||
| 113 | $snap = $ec2->describe_snapshots('snap-123456'); | ||
| 114 | $ami = $snap->register_image(-name => 'My new image', | ||
| 115 | -kernel_id => 'aki-407d9529', | ||
| 116 | -architecture => 'i386', | ||
| 117 | -block_devices => '/dev/sdc=ephemeral0' | ||
| 118 | ) or die $ec2->error_str; | ||
| 119 | |||
| 120 | while ($ami->current_status eq 'pending') { | ||
| 121 | print "$ami: ",$ami->current_status,"\n" | ||
| 122 | sleep 30; # takes a long time to register some images | ||
| 123 | } | ||
| 124 | |||
| 125 | print "$ami is ready to go\n"; | ||
| 126 | |||
| 127 | =head2 $volume = $snap->create_volume(%args) | ||
| 128 | |||
| 129 | Create a new volume from this snapshot. Arguments are: | ||
| 130 | |||
| 131 | -availability_zone -- An availability zone from | ||
| 132 | describe_availability_zones (required) | ||
| 133 | |||
| 134 | -size -- Size of the volume, in GB (between 1 and 1024). | ||
| 135 | |||
| 136 | If -size is not provided, then the new volume will have the same size as | ||
| 137 | the snapshot. | ||
| 138 | |||
| 139 | Optional Arguments: | ||
| 140 | |||
| 141 | -volume_type -- The volume type. standard or io1, default is | ||
| 142 | standard | ||
| 143 | |||
| 144 | -iops -- The number of I/O operations per second (IOPS) that | ||
| 145 | the volume supports. Range is 100 to 4000. Required | ||
| 146 | when volume type is io1. IOPS must be 30-to-1 ratio | ||
| 147 | to size. ie: 3000 IOPS volume must be at least 100GB. | ||
| 148 | |||
| 149 | On success, the returned value is a L<VM::EC2::Volume> object. | ||
| 150 | |||
| 151 | =head2 $status = $snap->current_status | ||
| 152 | |||
| 153 | Refreshes the snapshot and returns its current status. | ||
| 154 | |||
| 155 | =head2 $boolean = $snapshot->is_public | ||
| 156 | |||
| 157 | Return true if the snapshot's createVolume permissions allow the "all" | ||
| 158 | group to create volumes from the snapshot. | ||
| 159 | |||
| 160 | =head2 $boolean = $snapshot->make_public($public) | ||
| 161 | |||
| 162 | Modify the createVolumePermission attribute to allow the "all" group | ||
| 163 | to create volumes from this snapshot. Provide a true value to make the | ||
| 164 | snapshot public, a false one to make it private. | ||
| 165 | |||
| 166 | =head2 @user_ids = $snap->createVolumePermissions() | ||
| 167 | |||
| 168 | =head2 @user_ids = $snap->authorized_users | ||
| 169 | |||
| 170 | Returns a list of user IDs with createVolume permissions for this | ||
| 171 | snapshot. The result is a list of L<VM::EC2::Snapshot::CreateVolumePermission> | ||
| 172 | objects, which interpolate as strings corresponding to either the | ||
| 173 | user ID, or the group named "all." | ||
| 174 | |||
| 175 | The two methods are aliases of each other. | ||
| 176 | |||
| 177 | =head2 $boolean = $snap->add_authorized_users($id1,$id2,...) | ||
| 178 | |||
| 179 | =head2 $boolean = $snap->remove_authorized_users($id1,$id2,...) | ||
| 180 | |||
| 181 | =head2 $boolean = $snap->reset_authorized_users | ||
| 182 | |||
| 183 | These methods add and remove user accounts which have createVolume | ||
| 184 | permissions for the snapshot. The result code indicates whether the | ||
| 185 | list of user IDs were successfully added or removed. To add the "all" | ||
| 186 | group, use make_public(). | ||
| 187 | |||
| 188 | reset_authorized_users() resets the list users authored to create | ||
| 189 | volumes from this snapshot to empty, effectively granting volume | ||
| 190 | creation to the owner only. | ||
| 191 | |||
| 192 | See also authorized_users(). | ||
| 193 | |||
| 194 | =head2 $size = $snap->size | ||
| 195 | |||
| 196 | Alias to volumeSize, provided for consistency with | ||
| 197 | VM::EC2::Volume->size. | ||
| 198 | |||
| 199 | =head2 $snap->refresh | ||
| 200 | |||
| 201 | Refreshes the snapshot from information provided by AWS. Use before | ||
| 202 | checking progress or other changeable elements. | ||
| 203 | |||
| 204 | =head2 $snapshot_copy = $snapshot->copy(-region=>$dest_region, -description=>$desc) | ||
| 205 | |||
| 206 | Copies the snapshot to the same or different region. | ||
| 207 | |||
| 208 | Required Argument: | ||
| 209 | -region The region to copy the snapshot to | ||
| 210 | |||
| 211 | Optional Argument: | ||
| 212 | -description Description of the new snapshot | ||
| 213 | |||
| 214 | Returns a VM::EC2::Snapshot object if successful. | ||
| 215 | |||
| 216 | =head1 STRING OVERLOADING | ||
| 217 | |||
| 218 | When used in a string context, this object will interpolate the | ||
| 219 | snapshotId. | ||
| 220 | |||
| 221 | =head1 SEE ALSO | ||
| 222 | |||
| 223 | L<VM::EC2> | ||
| 224 | L<VM::EC2::Generic> | ||
| 225 | L<VM::EC2::Instance> | ||
| 226 | L<VM::EC2::Volume> | ||
| 227 | |||
| 228 | =head1 AUTHOR | ||
| 229 | |||
| 230 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 231 | |||
| 232 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||
| 233 | |||
| 234 | This package and its accompanying libraries is free software; you can | ||
| 235 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 236 | version 1, or at your option, any later version) or the Artistic | ||
| 237 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 238 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 239 | |||
| 240 | =cut | ||
| 241 | |||
| 242 | use strict; | ||
| 243 | use base 'VM::EC2::Generic'; | ||
| 244 | use VM::EC2::Snapshot::CreateVolumePermission; | ||
| 245 | use VM::EC2::ProductCode; | ||
| 246 | use Carp 'croak'; | ||
| 247 | |||
| 248 | sub valid_fields { | ||
| 249 | my $self = shift; | ||
| 250 | return $self->SUPER::valid_fields, | ||
| 251 | qw(snapshotId | ||
| 252 | volumeId | ||
| 253 | status | ||
| 254 | startTime | ||
| 255 | progress | ||
| 256 | ownerId | ||
| 257 | volumeSize | ||
| 258 | description | ||
| 259 | encrypted | ||
| 260 | ownerAlias); | ||
| 261 | } | ||
| 262 | |||
| 263 | sub primary_id { shift->snapshotId } | ||
| 264 | |||
| 265 | sub size { shift->volumeSize } | ||
| 266 | |||
| 267 | sub from_volume { | ||
| 268 | my $self = shift; | ||
| 269 | my $vid = $self->volumeId or return; | ||
| 270 | # may throw an error if volume no longer exists | ||
| 271 | return $self->aws->describe_volumes(-volume_id=>$vid); | ||
| 272 | } | ||
| 273 | |||
| 274 | sub to_volumes { | ||
| 275 | my $self = shift; | ||
| 276 | return $self->aws->describe_volumes(-filter=>{'snapshot-id'=>$self->snapshotId}); | ||
| 277 | } | ||
| 278 | |||
| 279 | sub refresh { | ||
| 280 | my $self = shift; | ||
| 281 | local $self->aws->{raise_error} = 1; | ||
| 282 | my $s = $self->aws->describe_snapshots($self); | ||
| 283 | %$self = %$s if $s; | ||
| 284 | return defined $s | ||
| 285 | } | ||
| 286 | |||
| 287 | sub register_image { | ||
| 288 | my $self = shift; | ||
| 289 | my %args = @_; | ||
| 290 | $args{-name} or croak "register_image(): -name argument required"; | ||
| 291 | $args{-root_device_name} ||= '/dev/sda1'; | ||
| 292 | |||
| 293 | my $block_devices = $args{-block_devices} || $args{-block_device_mapping} || []; | ||
| 294 | $block_devices = [$block_devices] unless ref $block_devices && ref $block_devices eq 'ARRAY'; | ||
| 295 | |||
| 296 | # See if the root device is on the block device mapping list. | ||
| 297 | # If it is not, then create a /dev/sda1 entry for it from this snapshot. | ||
| 298 | my $rd = $args{-root_device_name}; | ||
| 299 | unless (grep {/$rd=/} @$block_devices) { | ||
| 300 | my $root_size = $args{-root_size} || ''; | ||
| 301 | $args{-root_delete_on_termination} = 1 unless defined $args{-root_delete_on_termination}; | ||
| 302 | my $root_delete = $args{-root_delete_on_termination} ? 'true' : 'false'; | ||
| 303 | my $snap_id = $self->snapshotId; | ||
| 304 | unshift @$block_devices,"$rd=$snap_id:$root_size:$root_delete" | ||
| 305 | } | ||
| 306 | |||
| 307 | $args{-block_device_mapping} = $block_devices; | ||
| 308 | |||
| 309 | # just cleaning up, not really necessary | ||
| 310 | delete $args{-root_size}; | ||
| 311 | delete $args{-root_delete_on_termination}; | ||
| 312 | |||
| 313 | return $self->aws->register_image(%args); | ||
| 314 | } | ||
| 315 | |||
| 316 | sub create_volume { | ||
| 317 | my $self = shift; | ||
| 318 | my @args = @_; | ||
| 319 | return $self->ec2->create_volume(@args,-snapshot_id=>$self->snapshotId); | ||
| 320 | } | ||
| 321 | |||
| 322 | sub current_status { | ||
| 323 | my $self = shift; | ||
| 324 | $self->refresh; | ||
| 325 | return $self->status; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub current_status_async { | ||
| 329 | my $self = shift; | ||
| 330 | my $to_caller = VM::EC2->condvar; | ||
| 331 | |||
| 332 | my $cv = $self->aws->describe_snapshots_async(-snapshot_id=>$self->snapshotId); | ||
| 333 | |||
| 334 | $cv->cb(sub { | ||
| 335 | my $i = shift->recv; | ||
| 336 | $to_caller->send($i->status) | ||
| 337 | }); | ||
| 338 | |||
| 339 | return $to_caller; | ||
| 340 | } | ||
| 341 | |||
| 342 | |||
| 343 | sub createVolumePermissions { | ||
| 344 | my $self = shift; | ||
| 345 | return map {VM::EC2::Snapshot::CreateVolumePermission->new($_,$self->aws)} | ||
| 346 | $self->aws->describe_snapshot_attribute($self->snapshotId,'createVolumePermission'); | ||
| 347 | } | ||
| 348 | |||
| 349 | sub is_public { | ||
| 350 | my $self = shift; | ||
| 351 | my @users = $self->createVolumePermissions; | ||
| 352 | my $count = grep {$_->group eq 'all'} @users; | ||
| 353 | return $count > 0; | ||
| 354 | } | ||
| 355 | |||
| 356 | sub make_public { | ||
| 357 | my $self = shift; | ||
| 358 | @_ == 1 or croak "Usage: VM::EC2::Snapshot->make_public(\$boolean)"; | ||
| 359 | my $public = shift; | ||
| 360 | my @arg = $public ? (-add_group=>'all') : (-remove_group=>'all'); | ||
| 361 | my $result = $self->aws->modify_snapshot_attribute($self->snapshotId,@arg) or return; | ||
| 362 | return $result | ||
| 363 | } | ||
| 364 | |||
| 365 | sub authorized_users { shift->createVolumePermissions } | ||
| 366 | |||
| 367 | sub add_authorized_users { | ||
| 368 | my $self = shift; | ||
| 369 | @_ or croak "Usage: VM::EC2::Snapshot->add_authorized_users(\@userIds)"; | ||
| 370 | return $self->aws->modify_snapshot_attribute($self->snapshotId,-add_user=>\@_); | ||
| 371 | } | ||
| 372 | |||
| 373 | sub remove_authorized_users { | ||
| 374 | my $self = shift; | ||
| 375 | @_ or croak "Usage: VM::EC2::Snapshot->remove_authorized_users(\@userIds)"; | ||
| 376 | return $self->aws->modify_snapshot_attribute($self->snapshotId,-remove_user=>\@_); | ||
| 377 | } | ||
| 378 | |||
| 379 | sub reset_authorized_users { | ||
| 380 | my $self = shift; | ||
| 381 | $self->aws->reset_snapshot_attribute($self->snapshotId,'createVolumePermission'); | ||
| 382 | } | ||
| 383 | |||
| 384 | |||
| 385 | sub product_codes { | ||
| 386 | my $self = shift; | ||
| 387 | my @codes = $self->aws->describe_snapshot_attribute($self,'productCodes'); | ||
| 388 | return map {VM::EC2::ProductCode->new($_,$self->aws)} @codes; | ||
| 389 | } | ||
| 390 | |||
| 391 | sub copy { | ||
| 392 | my $self = shift; | ||
| 393 | my %args = @_; | ||
| 394 | my $snap_id = $self->snapshotId; | ||
| 395 | my $desc = $args{-description} || $args{-desc}; | ||
| 396 | my $region = $args{-region} or croak "copy(): -region argument required"; | ||
| 397 | my $orig_region = $self->aws->region; | ||
| 398 | |||
| 399 | # create a new EC2 object for the destination | ||
| 400 | my $dest_aws = $self->aws->clone; | ||
| 401 | $dest_aws->region($region); | ||
| 402 | my $snapshot = $dest_aws->copy_snapshot(-source_region=>$orig_region, -source_snapshot_id=>$snap_id, -description=>$desc); | ||
| 403 | return $snapshot; | ||
| 404 | } | ||
| 405 | |||
| 406 | sub encrypted { | ||
| 407 | my $self = shift; | ||
| 408 | my $enc = $self->SUPER::encrypted; | ||
| 409 | return $enc eq 'true'; | ||
| 410 | } | ||
| 411 | |||
| 412 | 1; |