| 1 | package VM::EC2::Instance; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | VM::EC2::Instance - Object describing an Amazon EC2 instance | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use VM::EC2; | ||
| 10 | |||
| 11 | $ec2 = VM::EC2->new(...); | ||
| 12 | $instance = $ec2->describe_instances(-instance_id=>'i-12345'); | ||
| 13 | |||
| 14 | $instanceId = $instance->instanceId; | ||
| 15 | $ownerId = $instance->ownerId; | ||
| 16 | $reservationId = $instance->reservationId; | ||
| 17 | $imageId = $instance->imageId; | ||
| 18 | $state = $instance->instanceState; | ||
| 19 | @groups = $instance->groups; | ||
| 20 | $private_ip = $instance->privateIpAddress; | ||
| 21 | $public_ip = $instance->ipAddress; | ||
| 22 | $private_dns = $instance->privateDnsName; | ||
| 23 | $public_dns = $instance->dnsName; | ||
| 24 | $time = $instance->launchTime; | ||
| 25 | $status = $instance->current_status; | ||
| 26 | $tags = $instance->tags; | ||
| 27 | |||
| 28 | $stateChange = $instance->start(); | ||
| 29 | $stateChange = $instance->stop(); | ||
| 30 | $stateChange = $instance->reboot(); | ||
| 31 | $stateChange = $instance->terminate(); | ||
| 32 | |||
| 33 | $seconds = $instance->up_time; | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | This object represents an Amazon EC2 instance, and is returned by | ||
| 38 | VM::EC2->describe_instances(). In addition to methods to query the | ||
| 39 | instance's attributes, there are methods that allow you to manage the | ||
| 40 | instance's lifecycle, including start, stopping, and terminating it. | ||
| 41 | |||
| 42 | Note that the information about security groups and reservations that | ||
| 43 | is returned by describe_instances() is copied into each instance | ||
| 44 | before returning it, so there is no concept of a "reservation set" in | ||
| 45 | this interface. | ||
| 46 | |||
| 47 | =head1 METHODS | ||
| 48 | |||
| 49 | These object methods are supported: | ||
| 50 | |||
| 51 | instanceId -- ID of this instance. | ||
| 52 | |||
| 53 | imageId -- ID of the image used to launch this instance. | ||
| 54 | |||
| 55 | instanceState -- The current state of the instance at the time | ||
| 56 | that describe_instances() was called, as a | ||
| 57 | VM::EC2::Instance::State object. Also | ||
| 58 | see the status() method, which re-queries EC2 for | ||
| 59 | the current state of the instance. States are | ||
| 60 | represented in strings as "terminated", "running", | ||
| 61 | "stopped", "stopping",and "shutting-down". | ||
| 62 | |||
| 63 | privateDnsName -- The private DNS name assigned to the instance within | ||
| 64 | Amazon's EC2 network. This element is defined only | ||
| 65 | for running instances. | ||
| 66 | |||
| 67 | dnsName -- The public DNS name assigned to the instance, defined | ||
| 68 | only for running instances. | ||
| 69 | |||
| 70 | reason -- Reason for the most recent state transition, | ||
| 71 | if applicable. | ||
| 72 | |||
| 73 | keyName -- Name of the associated key pair, if applicable. | ||
| 74 | |||
| 75 | keyPair -- The VM::EC2::KeyPair object, derived from the keyName | ||
| 76 | |||
| 77 | amiLaunchIndex -- The AMI launch index, which can be used to find | ||
| 78 | this instance within the launch group. | ||
| 79 | |||
| 80 | productCodes -- A list of product codes that apply to this instance. | ||
| 81 | |||
| 82 | instanceType -- The instance type, such as "t1.micro". CHANGEABLE. | ||
| 83 | |||
| 84 | launchTime -- The time the instance launched. | ||
| 85 | |||
| 86 | placement -- The placement of the instance. Returns a | ||
| 87 | VM::EC2::Instance::Placement object, which when used | ||
| 88 | as a string is equal to the instance's | ||
| 89 | availability zone. | ||
| 90 | |||
| 91 | availabilityZone -- Same as placement. | ||
| 92 | |||
| 93 | kernelId -- ID of the instance's kernel. CHANGEABLE. | ||
| 94 | |||
| 95 | ramdiskId -- ID of the instance's RAM disk. CHANGEABLE. | ||
| 96 | |||
| 97 | platform -- Platform of the instance, either "windows" or empty. | ||
| 98 | |||
| 99 | monitoring -- State of monitoring for the instance. One of | ||
| 100 | "disabled", "enabled", or "pending". CHANGEABLE: | ||
| 101 | pass true or "enabled" to turn on monitoring. Pass | ||
| 102 | false or "disabled" to turn it off. | ||
| 103 | |||
| 104 | subnetId -- The Amazon VPC subnet ID in which the instance is | ||
| 105 | running, for Virtual Private Cloud instances only. | ||
| 106 | |||
| 107 | vpcId -- The Virtual Private Cloud ID for VPC instances. | ||
| 108 | |||
| 109 | privateIpAddress -- The private (internal Amazon) IP address assigned | ||
| 110 | to the instance. | ||
| 111 | |||
| 112 | ipAddress -- The public IP address of the instance. | ||
| 113 | |||
| 114 | sourceDestCheck -- Whether source destination checking is enabled on | ||
| 115 | this instance. This returns a Perl boolean rather than | ||
| 116 | the string "true". This method is used in conjunction | ||
| 117 | with VPC NAT functionality. See the Amazon VPC User | ||
| 118 | Guide for details. CHANGEABLE. | ||
| 119 | |||
| 120 | networkInterfaceSet -- Return list of VM::EC2::ElasticNetworkInterface objects | ||
| 121 | attached to this instance. | ||
| 122 | |||
| 123 | iamInstanceProfile -- The IAM instance profile (IIP) associated with this instance. | ||
| 124 | |||
| 125 | ebsOptimized -- True if instance is optimized for EBS I/O. | ||
| 126 | |||
| 127 | groupSet -- List of VM::EC2::Group objects indicating the VPC | ||
| 128 | security groups in which this instance resides. Not to be | ||
| 129 | confused with groups(), which returns the security groups | ||
| 130 | of non-VPC instances. | ||
| 131 | |||
| 132 | stateReason -- A VM::EC2::Instance::State::Reason object which | ||
| 133 | indicates the reason for the instance's most recent | ||
| 134 | state change. See http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-ItemType-StateReasonType.html | ||
| 135 | |||
| 136 | architecture -- The architecture of the image. Either "i386" or "x86_64". | ||
| 137 | |||
| 138 | rootDeviceType -- The type of the root device used by the instance. One of "ebs" | ||
| 139 | or "instance-store". | ||
| 140 | |||
| 141 | rootDeviceName -- The name of the the device used by the instance, such as /dev/sda1. | ||
| 142 | CHANGEABLE. | ||
| 143 | |||
| 144 | blockDeviceMapping -- The block device mappings for the instance, represented | ||
| 145 | as a list of L<VM::EC2::BlockDevice::Mapping> objects. | ||
| 146 | |||
| 147 | instanceLifeCycle -- "spot" if this instance is a spot instance, otherwise empty. | ||
| 148 | |||
| 149 | spotInstanceRequestId -- The ID of the spot instance request, if applicable. | ||
| 150 | |||
| 151 | virtualizationType -- Either "paravirtual" or "hvm". | ||
| 152 | |||
| 153 | clientToken -- The idempotency token provided at the time of the AMI launch, | ||
| 154 | if any. | ||
| 155 | |||
| 156 | hypervisor -- The instance's hypervisor type, either "ovm" or "xen". | ||
| 157 | |||
| 158 | userData -- User data passed to instance at launch. CHANGEABLE. | ||
| 159 | |||
| 160 | disableApiTermination -- True if the instance is protected from termination | ||
| 161 | via the console or command-line APIs. CHANGEABLE. | ||
| 162 | |||
| 163 | instanceInitiatedShutdownBehavior -- Action to take when the instance calls | ||
| 164 | shutdown or halt. One of "stop" or "terminate". CHANGEABLE. | ||
| 165 | |||
| 166 | sriovNetSupport -- Specifies whether enhanced networking is enabled. "simple" if so. | ||
| 167 | |||
| 168 | tagSet -- Tags for the instance as a hashref. CHANGEABLE via add_tags() | ||
| 169 | and delete_tags(). | ||
| 170 | |||
| 171 | The object also supports the tags() method described in | ||
| 172 | L<VM::EC2::Generic>: | ||
| 173 | |||
| 174 | print "ready for production\n" if $image->tags->{Released}; | ||
| 175 | |||
| 176 | All methods return read-only values except for those marked CHANGEABLE | ||
| 177 | in the list above. For these, you can change the instance attribute on | ||
| 178 | stopped instances by invoking the method with an appropriate new | ||
| 179 | value. For example, to change the instance type from "t1.micro" to | ||
| 180 | "m1.small", you can do this: | ||
| 181 | |||
| 182 | my @tiny_instances = $ec2->describe_instances(-filter=>{'instance-type'=>'t1.micro'}); | ||
| 183 | for my $i (@tiny_instances) { | ||
| 184 | next unless $i->instanceState eq 'stopped'; | ||
| 185 | $i->instanceType('m1.small') or die $ec2->error; | ||
| 186 | } | ||
| 187 | |||
| 188 | When you attempt to change an attribute of an instance, the method | ||
| 189 | will return true on success, false on failure. On failure, the | ||
| 190 | detailed error messages can be recovered from the VM::EC2 object's | ||
| 191 | error() method. | ||
| 192 | |||
| 193 | =head1 LIFECYCLE METHODS | ||
| 194 | |||
| 195 | In addition, the following convenience functions are provided | ||
| 196 | |||
| 197 | =head2 $state = $instance->current_status | ||
| 198 | |||
| 199 | This method queries AWS for the instance's current state and returns | ||
| 200 | it as a VM::EC2::Instance::State object. This enables you to | ||
| 201 | poll the instance until it is in the desired state: | ||
| 202 | |||
| 203 | while ($instance->current_status eq 'pending') { sleep 5 } | ||
| 204 | |||
| 205 | =head2 $state = $instance->current_state | ||
| 206 | |||
| 207 | An alias for current_status(). | ||
| 208 | |||
| 209 | =head2 $state_change = $instance->start([$wait]) | ||
| 210 | |||
| 211 | This method will start the current instance and returns a | ||
| 212 | VM::EC2::Instance::State::Change object that can be used to | ||
| 213 | monitor the status of the instance. By default the method returns | ||
| 214 | immediately, but you can pass a true value as an argument in order to | ||
| 215 | pause execution until the instance is in the "running" state. | ||
| 216 | |||
| 217 | Here's a polling example: | ||
| 218 | |||
| 219 | $state = $instance->start; | ||
| 220 | while ($state->status eq 'pending') { sleep 5 } | ||
| 221 | |||
| 222 | Here's an example that will pause until the instance is running: | ||
| 223 | |||
| 224 | $instance->start(1); | ||
| 225 | |||
| 226 | Attempting to start an already running instance, or one that is | ||
| 227 | in transition, will throw a fatal error. | ||
| 228 | |||
| 229 | =head2 $state_change = $instance->stop([$wait]) | ||
| 230 | |||
| 231 | This method is similar to start(), except that it can be used to | ||
| 232 | stop a running instance. | ||
| 233 | |||
| 234 | =head2 $state_change = $instance->terminate([$wait]) | ||
| 235 | |||
| 236 | This method is similar to start(), except that it can be used to | ||
| 237 | terminate an instance. It can only be called on instances that | ||
| 238 | are either "running" or "stopped". | ||
| 239 | |||
| 240 | =head2 $state_change = $instance->reboot() | ||
| 241 | |||
| 242 | Reboot the instance. Rebooting doesn't occur immediately; instead the | ||
| 243 | request is queued by the Amazon system and may be satisfied several | ||
| 244 | minutes later. For this reason, there is no "wait" argument. | ||
| 245 | |||
| 246 | =head2 $seconds = $instance->up_time() | ||
| 247 | |||
| 248 | Return the number of seconds since the instance was launched. Note | ||
| 249 | that this includes time that the instance was either in the "running" | ||
| 250 | or "stopped" state. | ||
| 251 | |||
| 252 | =head2 $result = $instance->associate_address($elastic_address) | ||
| 253 | |||
| 254 | Associate an elastic address with this instance. If you are | ||
| 255 | associating a VPC elastic IP address with the instance, the result | ||
| 256 | code will indicate the associationId. Otherwise it will be a simple | ||
| 257 | perl truth value ("1") if successful, undef if false. | ||
| 258 | |||
| 259 | In the case of an ordinary EC2 Elastic IP address, the first argument may | ||
| 260 | either be an ordinary string (xx.xx.xx.xx format) or a | ||
| 261 | VM::EC2::ElasticAddress object. However, if it is a VPC elastic | ||
| 262 | IP address, then the argument must be a VM::EC2::ElasticAddress | ||
| 263 | as returned by describe_addresses(). The reason for this is that the | ||
| 264 | allocationId must be retrieved from the object in order to use in the | ||
| 265 | call. | ||
| 266 | |||
| 267 | =head2 $bool = $instance->disassociate_address | ||
| 268 | |||
| 269 | Disassociate an elastic IP address from this instance. if any. The | ||
| 270 | result will be true if disassociation was successful. Note that for a | ||
| 271 | short period of time (up to a few minutes) after disassociation, the | ||
| 272 | instance will have no public IP address and will be unreachable from | ||
| 273 | the internet. | ||
| 274 | |||
| 275 | =head2 @list = $instance->network_interfaces | ||
| 276 | |||
| 277 | Return the network interfaces attached to this instance as a set of | ||
| 278 | VM::EC2::NetworkInterface objects (VPC only). | ||
| 279 | |||
| 280 | =head2 $instance->refresh | ||
| 281 | |||
| 282 | This method will refresh the object from AWS, updating all values to | ||
| 283 | their current ones. You can call it after starting an instance in | ||
| 284 | order to get its IP address. Note that refresh() is called | ||
| 285 | automatically for you if you call start(), stop() or terminate() with | ||
| 286 | a true $wait argument. | ||
| 287 | |||
| 288 | =head2 $text = $instance->console_output | ||
| 289 | |||
| 290 | Return the console output of the instance as a | ||
| 291 | VM::EC2::ConsoleOutput object. This object can be treated as a | ||
| 292 | string, or as an object with methods | ||
| 293 | |||
| 294 | =head1 CREATING IMAGES | ||
| 295 | |||
| 296 | The create_image() method provides a handy way of creating and | ||
| 297 | registering an AMI based on the current state of the instance. All | ||
| 298 | currently-associated block devices will be snapshotted and associated | ||
| 299 | with the image. | ||
| 300 | |||
| 301 | Note that this operation can take a long time to complete. You may | ||
| 302 | follow its progress by calling the returned image object's | ||
| 303 | current_status() method. | ||
| 304 | |||
| 305 | =head2 $imageId = $instance->create_image($name [,$description]) | ||
| 306 | |||
| 307 | =head2 $imageId = $instance->create_image(-name=>$name,-description=>$description,-no_reboot=>$boolean) | ||
| 308 | |||
| 309 | Create an image from this instance and return a VM::EC2::Image object. | ||
| 310 | The instance must be in the "stopped" or "running" state. In the | ||
| 311 | latter case, Amazon will stop the instance, create the image, and then | ||
| 312 | restart it unless the -no_reboot argument is provided. | ||
| 313 | |||
| 314 | Arguments: | ||
| 315 | |||
| 316 | -name Name for the image that will be created. (required) | ||
| 317 | -description Description of the new image. | ||
| 318 | -no_reboot If true, don't reboot the instance. | ||
| 319 | |||
| 320 | In the unnamed argument version you can provide the name and | ||
| 321 | optionally the description of the resulting image. | ||
| 322 | |||
| 323 | =head2 $boolean = $instance->confirm_product_code($product_code) | ||
| 324 | |||
| 325 | Return true if this instance is associated with the given product code. | ||
| 326 | |||
| 327 | =head1 VOLUME MANAGEMENT | ||
| 328 | |||
| 329 | =head2 $attachment = $instance->attach_volume($volume_id,$device) | ||
| 330 | |||
| 331 | =head2 $attachment = $instance->attach_volume(-volume_id=>$volume_id,-device=>$device) | ||
| 332 | |||
| 333 | Attach volume $volume_id to this instance using virtual device | ||
| 334 | $device. Both arguments are required. The result is a | ||
| 335 | VM::EC2::BlockDevice::Attachment object which you can monitor by | ||
| 336 | calling current_status(): | ||
| 337 | |||
| 338 | my $a = $instance->attach_volume('vol-12345'=>'/dev/sdg'); | ||
| 339 | while ($a->current_status ne 'attached') { | ||
| 340 | sleep 2; | ||
| 341 | } | ||
| 342 | print "volume is ready to go\n"; | ||
| 343 | |||
| 344 | =head2 $attachment = $instance->detach_volume($vol_or_device) | ||
| 345 | |||
| 346 | =head2 $attachment = $instance->detach_volume(-volume_id => $volume_id | ||
| 347 | -device => $device, | ||
| 348 | -force => $force); | ||
| 349 | |||
| 350 | Detaches the specified volume. In the single-argument form, you may | ||
| 351 | provide either a volume or a device name. In the named-argument form, | ||
| 352 | you may provide both the volume and the device as a check that you are | ||
| 353 | detaching exactly the volume you think you are. | ||
| 354 | |||
| 355 | Optional arguments: | ||
| 356 | |||
| 357 | -volume_id -- ID of the instance to detach from. | ||
| 358 | -device -- How the device is exposed to the instance. | ||
| 359 | -force -- Force detachment, even if previous attempts were | ||
| 360 | unsuccessful. | ||
| 361 | |||
| 362 | The result is a VM::EC2::BlockDevice::Attachment object which | ||
| 363 | you can monitor by calling current_status(): | ||
| 364 | |||
| 365 | my $a = $instance->detach_volume('/dev/sdg'); | ||
| 366 | while ($a->current_status ne 'detached') { | ||
| 367 | sleep 2; | ||
| 368 | } | ||
| 369 | print "volume is ready to go\n"; | ||
| 370 | |||
| 371 | =head1 NETWORK INTERFACE MANAGEMENT | ||
| 372 | |||
| 373 | =head2 $attachment_id = $instance->attach_network_interface($interface_id => $device) | ||
| 374 | |||
| 375 | =head2 $attachment_id = $instance->attach_network_interface(-network_interface_id=>$id, | ||
| 376 | -device_index => $device) | ||
| 377 | |||
| 378 | This method attaches a network interface to the current instance using | ||
| 379 | the indicated device index. You can use either an elastic network | ||
| 380 | interface ID, or a VM::EC2::NetworkInterface object. You may use an | ||
| 381 | integer for -device_index, or use the strings "eth0", "eth1" etc. | ||
| 382 | |||
| 383 | Required arguments: | ||
| 384 | |||
| 385 | -network_interface_id ID of the network interface to attach. | ||
| 386 | -device_index Network device number to use (e.g. 0 for eth0). | ||
| 387 | |||
| 388 | On success, this method returns the attachmentId of the new attachment | ||
| 389 | (not a VM::EC2::NetworkInterface::Attachment object, due to an AWS API | ||
| 390 | inconsistency). | ||
| 391 | |||
| 392 | =head2 $boolean = $instance->detach_network_interface($interface_id [,$force]) | ||
| 393 | |||
| 394 | This method detaches a network interface from the current instance. If | ||
| 395 | a true second argument is provided, then the detachment will be | ||
| 396 | forced, even if the interface is in use. | ||
| 397 | |||
| 398 | On success, this method returns a true value. | ||
| 399 | |||
| 400 | =head1 ACCESSING INSTANCE METADATA | ||
| 401 | |||
| 402 | =head2 $meta = $instance->metadata | ||
| 403 | |||
| 404 | B<For use on running EC2 instances only:> This method returns a | ||
| 405 | VM::EC2::Instance::Metadata object that will return information about | ||
| 406 | the currently running instance using the HTTP:// metadata fields | ||
| 407 | described at | ||
| 408 | http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instancedata-data-categories.html. This | ||
| 409 | is usually fastest way to get runtime information on the current | ||
| 410 | instance. | ||
| 411 | |||
| 412 | =head1 STRING OVERLOADING | ||
| 413 | |||
| 414 | When used in a string context, this object will interpolate the | ||
| 415 | instanceId. | ||
| 416 | |||
| 417 | =head1 SEE ALSO | ||
| 418 | |||
| 419 | L<VM::EC2> | ||
| 420 | L<VM::EC2::Generic> | ||
| 421 | L<VM::EC2::BlockDevice> | ||
| 422 | L<VM::EC2::Instance::State> | ||
| 423 | L<VM::EC2::Instance::State::Reason> | ||
| 424 | L<VM::EC2::Instance::Metadata> | ||
| 425 | L<VM::EC2::Instance::Placement> | ||
| 426 | L<VM::EC2::Tag> | ||
| 427 | |||
| 428 | =head1 AUTHOR | ||
| 429 | |||
| 430 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 431 | |||
| 432 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||
| 433 | |||
| 434 | This package and its accompanying libraries is free software; you can | ||
| 435 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 436 | version 1, or at your option, any later version) or the Artistic | ||
| 437 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 438 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 439 | |||
| 440 | =cut | ||
| 441 | |||
| 442 | use strict; | ||
| 443 | use base 'VM::EC2::Generic'; | ||
| 444 | use VM::EC2::Group; | ||
| 445 | use VM::EC2::Instance::State; | ||
| 446 | use VM::EC2::Instance::State::Reason; | ||
| 447 | use VM::EC2::BlockDevice::Mapping; | ||
| 448 | use VM::EC2::NetworkInterface; | ||
| 449 | use VM::EC2::Instance::Placement; | ||
| 450 | use VM::EC2::ProductCode; | ||
| 451 | use VM::EC2::Instance::IamProfile; | ||
| 452 | use MIME::Base64 qw(encode_base64 decode_base64); | ||
| 453 | use Carp 'croak'; | ||
| 454 | |||
| 455 | sub new { | ||
| 456 | my $self = shift; | ||
| 457 | my %args = @_; | ||
| 458 | return bless { | ||
| 459 | data => $args{-instance}, | ||
| 460 | reservation => $args{-reservation}, | ||
| 461 | requester => $args{-requester}, | ||
| 462 | owner => $args{-owner}, | ||
| 463 | groups => $args{-groups}, | ||
| 464 | aws => $args{-aws}, | ||
| 465 | xmlns => $args{-xmlns}, | ||
| 466 | requestId => $args{-requestId}, | ||
| 467 | },ref $self || $self; | ||
| 468 | } | ||
| 469 | |||
| 470 | sub reservationId {shift->{reservation} } | ||
| 471 | sub requesterId {shift->{requester} } | ||
| 472 | sub ownerId {shift->{owner} } | ||
| 473 | sub groups { | ||
| 474 | my $self = shift; | ||
| 475 | my $groups = $self->{groups}; | ||
| 476 | if (@_) { | ||
| 477 | return $self->aws->modify_instance_attribute($self,-group_id=>\@_); | ||
| 478 | } else { | ||
| 479 | return @$groups; | ||
| 480 | } | ||
| 481 | } | ||
| 482 | sub group {shift()->{groups}[0] } | ||
| 483 | sub primary_id {shift()->instanceId } | ||
| 484 | |||
| 485 | sub valid_fields { | ||
| 486 | my $self = shift; | ||
| 487 | return qw(instanceId | ||
| 488 | imageId | ||
| 489 | instanceState | ||
| 490 | privateDnsName | ||
| 491 | dnsName | ||
| 492 | reason | ||
| 493 | keyName | ||
| 494 | amiLaunchIndex | ||
| 495 | productCodes | ||
| 496 | instanceType | ||
| 497 | launchTime | ||
| 498 | placement | ||
| 499 | kernelId | ||
| 500 | ramdiskId | ||
| 501 | platform | ||
| 502 | monitoring | ||
| 503 | subnetId | ||
| 504 | vpcId | ||
| 505 | privateIpAddress | ||
| 506 | ipAddress | ||
| 507 | sourceDestCheck | ||
| 508 | networkInterfaceSet | ||
| 509 | iamInstanceProfile | ||
| 510 | ebsOptimized | ||
| 511 | groupSet | ||
| 512 | stateReason | ||
| 513 | architecture | ||
| 514 | rootDeviceType | ||
| 515 | rootDeviceName | ||
| 516 | blockDeviceMapping | ||
| 517 | instanceLifeCycle | ||
| 518 | spotInstanceRequestId | ||
| 519 | virtualizationType | ||
| 520 | clientToken | ||
| 521 | hypervisor | ||
| 522 | tagSet | ||
| 523 | sriovNetSupport | ||
| 524 | ); | ||
| 525 | } | ||
| 526 | |||
| 527 | sub keyPair { | ||
| 528 | my $self = shift; | ||
| 529 | my $name = $self->keyName or return; | ||
| 530 | return $self->aws->describe_key_pairs($name); | ||
| 531 | } | ||
| 532 | |||
| 533 | sub instanceState { | ||
| 534 | my $self = shift; | ||
| 535 | my $state = $self->SUPER::instanceState; | ||
| 536 | return VM::EC2::Instance::State->new($state); | ||
| 537 | } | ||
| 538 | |||
| 539 | sub sourceDestCheck { | ||
| 540 | my $self = shift; | ||
| 541 | my $check = $self->SUPER::sourceDestCheck; | ||
| 542 | if (@_) { | ||
| 543 | my $c = shift() ? 'true' : 'false'; | ||
| 544 | return $self->aws->modify_instance_attribute($self,-source_dest_check=>$c); | ||
| 545 | } | ||
| 546 | return $check eq 'true'; | ||
| 547 | } | ||
| 548 | |||
| 549 | sub ebsOptimized { | ||
| 550 | my $self = shift; | ||
| 551 | my $opt = $self->SUPER::ebsOptimized; | ||
| 552 | return $opt eq 'true'; | ||
| 553 | } | ||
| 554 | |||
| 555 | sub groupSet { | ||
| 556 | my $self = shift; | ||
| 557 | my $groupSet = $self->SUPER::groupSet; | ||
| 558 | return map {VM::EC2::Group->new($_,$self->aws,$self->xmlns,$self->requestId)} | ||
| 559 | @{$groupSet->{item}}; | ||
| 560 | } | ||
| 561 | |||
| 562 | sub placement { | ||
| 563 | my $self = shift; | ||
| 564 | my $p = $self->SUPER::placement or return; | ||
| 565 | return VM::EC2::Instance::Placement->new($p,$self->aws,$self->xmlns,$self->requestId); | ||
| 566 | } | ||
| 567 | |||
| 568 | sub availabilityZone { shift->placement } | ||
| 569 | |||
| 570 | sub monitoring { | ||
| 571 | my $self = shift; | ||
| 572 | if (@_) { | ||
| 573 | my $enable = shift; | ||
| 574 | if ($enable && $enable ne 'disabled') { | ||
| 575 | return $self->aws->monitor_instances($self); | ||
| 576 | } else { | ||
| 577 | return $self->aws->unmonitor_instances($self); | ||
| 578 | } | ||
| 579 | } | ||
| 580 | return $self->SUPER::monitoring->{state}; | ||
| 581 | } | ||
| 582 | |||
| 583 | sub blockDeviceMapping { | ||
| 584 | my $self = shift; | ||
| 585 | $self->refresh; | ||
| 586 | my $mapping = $self->SUPER::blockDeviceMapping or return; | ||
| 587 | my @mapping = map { VM::EC2::BlockDevice::Mapping->new($_,$self->aws)} @{$mapping->{item}}; | ||
| 588 | foreach (@mapping) { $_->instance($self) } | ||
| 589 | return @mapping; | ||
| 590 | } | ||
| 591 | |||
| 592 | sub blockDeviceMappings {shift->blockDeviceMapping} | ||
| 593 | |||
| 594 | sub stateReason { | ||
| 595 | my $self = shift; | ||
| 596 | my $reason = $self->SUPER::stateReason; | ||
| 597 | return VM::EC2::Instance::State::Reason->new($reason,$self->_object_args); | ||
| 598 | } | ||
| 599 | |||
| 600 | sub kernelId { | ||
| 601 | my $self = shift; | ||
| 602 | my $kernel = $self->SUPER::kernelId; | ||
| 603 | if (@_) { | ||
| 604 | return $self->aws->modify_instance_attribute($self,-kernel=>shift()); | ||
| 605 | } else { | ||
| 606 | return $kernel; | ||
| 607 | } | ||
| 608 | } | ||
| 609 | |||
| 610 | sub ramdiskId { | ||
| 611 | my $self = shift; | ||
| 612 | my $ramdisk = $self->SUPER::ramdiskId; | ||
| 613 | if (@_) { | ||
| 614 | return $self->aws->modify_instance_attribute($self,-ramdisk=>shift()); | ||
| 615 | } else { | ||
| 616 | return $ramdisk; | ||
| 617 | } | ||
| 618 | } | ||
| 619 | |||
| 620 | sub rootDeviceName { | ||
| 621 | my $self = shift; | ||
| 622 | my $root = $self->SUPER::rootDeviceName; | ||
| 623 | if (@_) { | ||
| 624 | return $self->aws->modify_instance_attribute($self,-root_device_name => shift()); | ||
| 625 | } else { | ||
| 626 | return $root; | ||
| 627 | } | ||
| 628 | } | ||
| 629 | |||
| 630 | sub instanceType { | ||
| 631 | my $self = shift; | ||
| 632 | return $self->aws->modify_instance_attribute($self, | ||
| 633 | -instance_type=>shift()) if @_; | ||
| 634 | return $self->SUPER::instanceType; | ||
| 635 | } | ||
| 636 | |||
| 637 | sub userData { | ||
| 638 | my $self = shift; | ||
| 639 | |||
| 640 | if (@_) { | ||
| 641 | my $encoded = encode_base64(shift); | ||
| 642 | return $self->aws->modify_instance_attribute($self,-user_data=>$encoded); | ||
| 643 | } | ||
| 644 | |||
| 645 | my $data = $self->aws->describe_instance_attribute($self,'userData') or return; | ||
| 646 | VM::EC2::Dispatch::load_module('MIME::Base64'); | ||
| 647 | return decode_base64($data); | ||
| 648 | } | ||
| 649 | |||
| 650 | sub disableApiTermination { | ||
| 651 | my $self = shift; | ||
| 652 | return $self->aws->modify_instance_attribute($self, | ||
| 653 | -disable_api_termination=>shift()) if @_; | ||
| 654 | return $self->aws->describe_instance_attribute($self,'disableApiTermination') eq 'true'; | ||
| 655 | } | ||
| 656 | |||
| 657 | sub instanceInitiatedShutdownBehavior { | ||
| 658 | my $self = shift; | ||
| 659 | return $self->aws->modify_instance_attribute($self, | ||
| 660 | -shutdown_behavior=>shift()) if @_; | ||
| 661 | return $self->aws->describe_instance_attribute($self,'instanceInitiatedShutdownBehavior'); | ||
| 662 | } | ||
| 663 | |||
| 664 | sub networkInterfaceSet { | ||
| 665 | my $self = shift; | ||
| 666 | my $set = $self->SUPER::networkInterfaceSet or return; | ||
| 667 | return map {VM::EC2::NetworkInterface->new($_,$self->aws)} @{$set->{item}}; | ||
| 668 | } | ||
| 669 | |||
| 670 | sub network_interfaces { shift->networkInterfaceSet } | ||
| 671 | |||
| 672 | sub iamInstanceProfile { | ||
| 673 | my $self = shift; | ||
| 674 | my $profile = $self->SUPER::iamInstanceProfile or return; | ||
| 675 | return VM::EC2::Instance::IamProfile->new($profile,$self->aws); | ||
| 676 | } | ||
| 677 | |||
| 678 | sub current_status { | ||
| 679 | my $self = shift; | ||
| 680 | my $retry = 0; | ||
| 681 | until ($self->refresh) { | ||
| 682 | if (++$retry > 10) { | ||
| 683 | croak "invalid instance: ",$self->instanceId; | ||
| 684 | } | ||
| 685 | sleep 2; | ||
| 686 | } | ||
| 687 | return $self->instanceState; | ||
| 688 | } | ||
| 689 | |||
| 690 | sub current_status_async { | ||
| 691 | my $self = shift; | ||
| 692 | my $to_caller = VM::EC2->condvar; | ||
| 693 | |||
| 694 | my $cv = $self->aws->describe_instances_async(-instance_id=>$self->instanceId); | ||
| 695 | |||
| 696 | $cv->cb(sub { | ||
| 697 | my $i = shift->recv; | ||
| 698 | $to_caller->send($i->instanceState) | ||
| 699 | }); | ||
| 700 | |||
| 701 | return $to_caller; | ||
| 702 | } | ||
| 703 | |||
| 704 | sub current_state { shift->current_status } # alias | ||
| 705 | sub status { shift->current_status } # legacy | ||
| 706 | |||
| 707 | sub start { | ||
| 708 | my $self = shift; | ||
| 709 | my $wait = shift; | ||
| 710 | |||
| 711 | my $s = $self->current_status; | ||
| 712 | croak "Can't start $self: run state=$s" unless $s eq 'stopped'; | ||
| 713 | my ($i) = $self->aws->start_instances($self) or return; | ||
| 714 | if ($wait) { | ||
| 715 | while ($i->current_status eq 'pending') { | ||
| 716 | sleep 5; | ||
| 717 | } | ||
| 718 | $self->refresh; | ||
| 719 | } | ||
| 720 | return $i; | ||
| 721 | } | ||
| 722 | |||
| 723 | sub stop { | ||
| 724 | my $self = shift; | ||
| 725 | my $wait = shift; | ||
| 726 | |||
| 727 | my $s = $self->current_status; | ||
| 728 | croak "Can't stop $self: run state=$s" unless $s eq 'running'; | ||
| 729 | |||
| 730 | my ($i) = $self->aws->stop_instances($self); | ||
| 731 | if ($wait) { | ||
| 732 | while ($i->current_status ne 'stopped') { | ||
| 733 | sleep 5; | ||
| 734 | } | ||
| 735 | $self->refresh; | ||
| 736 | } | ||
| 737 | return $i; | ||
| 738 | } | ||
| 739 | |||
| 740 | sub terminate { | ||
| 741 | my $self = shift; | ||
| 742 | my $wait = shift; | ||
| 743 | |||
| 744 | my $s = $self->current_status; | ||
| 745 | croak "Can't terminate $self: run state=$s" | ||
| 746 | unless $s eq 'running' or $s eq 'stopped'; | ||
| 747 | |||
| 748 | my $i = $self->aws->terminate_instances($self) or return; | ||
| 749 | if ($wait) { | ||
| 750 | while ($i->current_status ne 'terminated') { | ||
| 751 | sleep 5; | ||
| 752 | } | ||
| 753 | $self->refresh; | ||
| 754 | } | ||
| 755 | return $i; | ||
| 756 | } | ||
| 757 | |||
| 758 | sub reboot { | ||
| 759 | my $self = shift; | ||
| 760 | |||
| 761 | my $s = $self->current_status; | ||
| 762 | croak "Can't reboot $self: run state=$s"unless $s eq 'running'; | ||
| 763 | return $self->aws->reboot_instances($self); | ||
| 764 | } | ||
| 765 | |||
| 766 | sub upTime { | ||
| 767 | my $self = shift; | ||
| 768 | my $start = $self->launchTime; | ||
| 769 | VM::EC2::Dispatch::load_module('Date::Parse'); | ||
| 770 | my $sec = Date::Parse::str2time($start); | ||
| 771 | return time()-$sec; | ||
| 772 | } | ||
| 773 | |||
| 774 | sub up_time { shift->upTime } | ||
| 775 | |||
| 776 | sub associate_address { | ||
| 777 | my $self = shift; | ||
| 778 | my $addr = shift or croak "Usage: \$instance->associate_address(\$elastic_address)"; | ||
| 779 | my $r = $self->aws->associate_address($addr => $self->instanceId); | ||
| 780 | return $r; | ||
| 781 | } | ||
| 782 | |||
| 783 | sub disassociate_address { | ||
| 784 | my $self = shift; | ||
| 785 | my $addr = $self->aws->describe_addresses(-filter=>{'instance-id'=>$self->instanceId}); | ||
| 786 | $addr or croak "Instance $self is not currently associated with an elastic IP address"; | ||
| 787 | my $r = $self->aws->disassociate_address($addr); | ||
| 788 | return $r; | ||
| 789 | } | ||
| 790 | |||
| 791 | sub refresh { | ||
| 792 | my $self = shift; | ||
| 793 | my $i = shift; | ||
| 794 | local $self->aws->{raise_error} = 1; | ||
| 795 | ($i) = $self->aws->describe_instances_sync(-instance_id=>$self->instanceId) unless $i; | ||
| 796 | %$self = %$i if $i; | ||
| 797 | return defined $i; | ||
| 798 | } | ||
| 799 | |||
| 800 | sub console_output { | ||
| 801 | my $self = shift; | ||
| 802 | VM::EC2::Dispatch::load_module('VM::EC2::REST::general'); | ||
| 803 | my $output = $self->aws->get_console_output_sync(-instance_id=>$self->instanceId); | ||
| 804 | return $output->output; | ||
| 805 | } | ||
| 806 | |||
| 807 | sub create_image { | ||
| 808 | my $self = shift; | ||
| 809 | my %args; | ||
| 810 | if ($_[0] !~ /^-/) { | ||
| 811 | my ($name,$description) = @_; | ||
| 812 | $args{-name} = $name; | ||
| 813 | $args{-description} = $description if defined $description; | ||
| 814 | } else { | ||
| 815 | %args = @_; | ||
| 816 | } | ||
| 817 | $args{-name} or croak "Usage: create_image(\$image_name)"; | ||
| 818 | return $self->aws->create_image(%args,-instance_id=>$self->instanceId); | ||
| 819 | } | ||
| 820 | |||
| 821 | sub attach_volume { | ||
| 822 | my $self = shift; | ||
| 823 | my %args; | ||
| 824 | if (@_==2 && $_[0] !~ /^-/) { | ||
| 825 | my ($volume,$device) = @_; | ||
| 826 | $args{-volume_id} = $volume; | ||
| 827 | $args{-device} = $device; | ||
| 828 | } else { | ||
| 829 | %args = @_; | ||
| 830 | } | ||
| 831 | $args{-volume_id} && $args{-device} | ||
| 832 | or croak "usage: \$vol->attach(\$instance_id,\$device)"; | ||
| 833 | $args{-instance_id} = $self->instanceId; | ||
| 834 | |||
| 835 | my $result = $self->aws->attach_volume(%args); | ||
| 836 | |||
| 837 | if ($VM::EC2::ASYNC) { | ||
| 838 | $result->cb( | ||
| 839 | sub { | ||
| 840 | my $attach = shift->recv; | ||
| 841 | $self->refresh if $attach; | ||
| 842 | }); | ||
| 843 | } else { | ||
| 844 | $self->refresh if $result; | ||
| 845 | } | ||
| 846 | |||
| 847 | return $result; | ||
| 848 | } | ||
| 849 | |||
| 850 | sub detach_volume { | ||
| 851 | my $self = shift; | ||
| 852 | my %args; | ||
| 853 | |||
| 854 | if (@_ == 1 && $_[0] !~ /^-/) { | ||
| 855 | my $vol_or_device = shift; | ||
| 856 | $self->refresh; | ||
| 857 | my @mappings = $self->blockDeviceMapping; | ||
| 858 | my ($mapping) = grep {$_->deviceName eq $vol_or_device} @mappings; | ||
| 859 | if ($mapping) { | ||
| 860 | $args{-volume_id} = $mapping->volumeId; | ||
| 861 | $args{-device} = $mapping->deviceName; | ||
| 862 | } else { | ||
| 863 | $args{-volume_id} = $vol_or_device; | ||
| 864 | } | ||
| 865 | } else { | ||
| 866 | %args = @_; | ||
| 867 | } | ||
| 868 | $args{-instance_id} = $self->instanceId; | ||
| 869 | my $result = $self->aws->detach_volume(%args); | ||
| 870 | if ($VM::EC2::ASYNC) { | ||
| 871 | $result->cb( | ||
| 872 | sub { | ||
| 873 | my $attach = shift->recv; | ||
| 874 | $self->refresh if $attach; | ||
| 875 | }); | ||
| 876 | } else { | ||
| 877 | $self->refresh if $result; | ||
| 878 | } | ||
| 879 | |||
| 880 | return $result; | ||
| 881 | } | ||
| 882 | |||
| 883 | sub attach_network_interface { | ||
| 884 | my $self = shift; | ||
| 885 | my %args; | ||
| 886 | if (@_==2 && $_[0] !~ /^-/) { | ||
| 887 | @args{qw(-network_interface_id -device_index)} = @_; | ||
| 888 | } else { | ||
| 889 | %args = @_; | ||
| 890 | } | ||
| 891 | $args{-network_interface_id} && $args{-device_index} | ||
| 892 | or croak "usage: \$instance->attach_network_interface(\$network_interface_id,\$device_index)"; | ||
| 893 | |||
| 894 | $args{-instance_id} = $self->instanceId; | ||
| 895 | |||
| 896 | my $result = $self->aws->attach_network_interface(%args); | ||
| 897 | if ($VM::EC2::ASYNC) { | ||
| 898 | $result->cb( | ||
| 899 | sub { | ||
| 900 | my $attach = shift->recv; | ||
| 901 | if ($attach) { | ||
| 902 | $self->refresh; | ||
| 903 | eval {$args{-network_interface_id}->refresh}; | ||
| 904 | } | ||
| 905 | }); | ||
| 906 | } | ||
| 907 | elsif ($result) { | ||
| 908 | $self->refresh; | ||
| 909 | eval {$args{-network_interface_id}->refresh}; | ||
| 910 | } | ||
| 911 | |||
| 912 | return $result; | ||
| 913 | } | ||
| 914 | |||
| 915 | sub detach_network_interface { | ||
| 916 | my $self = shift; | ||
| 917 | my ($nid,$force) = @_; | ||
| 918 | $nid or croak "usage: \$instance=>detach_network_interface(\$network_interface_id [,\$force])"; | ||
| 919 | my ($attachment) = map {$_->attachment} grep { $_->networkInterfaceId eq $nid } $self->network_interfaces; | ||
| 920 | $attachment or croak "$self is not attached to $nid"; | ||
| 921 | my $result = $self->aws->detach_network_interface($attachment,$force); | ||
| 922 | |||
| 923 | if ($VM::EC2::ASYNC) { | ||
| 924 | $result->cb( | ||
| 925 | sub { | ||
| 926 | my $attach = shift->recv; | ||
| 927 | if ($attach) { | ||
| 928 | $self->refresh; | ||
| 929 | eval {$nid->refresh}; | ||
| 930 | } | ||
| 931 | }); | ||
| 932 | } | ||
| 933 | elsif ($result) { | ||
| 934 | $self->refresh; | ||
| 935 | eval {$nid->refresh} if $result; | ||
| 936 | } | ||
| 937 | |||
| 938 | return $result; | ||
| 939 | } | ||
| 940 | |||
| 941 | sub metadata { | ||
| 942 | my $self = shift; | ||
| 943 | return $self->aws->instance_metadata; | ||
| 944 | } | ||
| 945 | |||
| 946 | sub productCodes { | ||
| 947 | my $self = shift; | ||
| 948 | my $codes = $self->SUPER::productCodes or return; | ||
| 949 | return map {VM::EC2::ProductCode->new($_)} @{$codes->{item}}; | ||
| 950 | } | ||
| 951 | |||
| 952 | sub confirm_product_code { | ||
| 953 | my $self = shift; | ||
| 954 | my $code = shift; | ||
| 955 | return $self->aws->confirm_product_instance($self,$code); | ||
| 956 | } | ||
| 957 | |||
| 958 | 1; | ||
| 959 |