| 1 | package VM::EC2::REST::spot_instance; | ||
| 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 | CancelSpotInstanceRequests => 'fetch_items,spotInstanceRequestSet,VM::EC2::Spot::InstanceRequest', | ||
| 9 | CreateSpotDatafeedSubscription => 'fetch_one,spotDatafeedSubscription,VM::EC2::Spot::DatafeedSubscription', | ||
| 10 | DeleteSpotDatafeedSubscription => 'boolean', | ||
| 11 | DescribeSpotDatafeedSubscription => 'fetch_one,spotDatafeedSubscription,VM::EC2::Spot::DatafeedSubscription', | ||
| 12 | DescribeSpotInstanceRequests => 'fetch_items,spotInstanceRequestSet,VM::EC2::Spot::InstanceRequest', | ||
| 13 | DescribeSpotPriceHistory => 'fetch_items_iterator,spotPriceHistorySet,VM::EC2::Spot::PriceHistory,spot_price_history', | ||
| 14 | RequestSpotInstances => 'fetch_items,spotInstanceRequestSet,VM::EC2::Spot::InstanceRequest', | ||
| 15 | ); | ||
| 16 | |||
| 17 | =head1 NAME VM::EC2::REST::spot_instance | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use VM::EC2 ':misc'; | ||
| 22 | |||
| 23 | =head1 METHODS | ||
| 24 | |||
| 25 | These methods allow you to request spot instances and manipulate spot | ||
| 26 | data feed subscriptions. | ||
| 27 | |||
| 28 | Implemented: | ||
| 29 | CancelSpotInstanceRequests | ||
| 30 | CreateSpotDatafeedSubscription | ||
| 31 | DeleteSpotDatafeedSubscription | ||
| 32 | DescribeSpotDatafeedSubscription | ||
| 33 | DescribeSpotInstanceRequests | ||
| 34 | DescribeSpotPriceHistory | ||
| 35 | RequestSpotInstances | ||
| 36 | |||
| 37 | Unimplemented: | ||
| 38 | (none) | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | =head2 $subscription = $ec2->create_spot_datafeed_subscription($bucket,$prefix) | ||
| 43 | |||
| 44 | This method creates a spot datafeed subscription. Provide the method with the | ||
| 45 | name of an S3 bucket associated with your account, and a prefix to be appended | ||
| 46 | to the files written by the datafeed. Spot instance usage logs will be written | ||
| 47 | into the requested bucket, and prefixed with the desired prefix. | ||
| 48 | |||
| 49 | If no prefix is specified, it defaults to "SPOT_DATAFEED_"; | ||
| 50 | |||
| 51 | On success, a VM::EC2::Spot::DatafeedSubscription object is returned; | ||
| 52 | |||
| 53 | Only one datafeed is allowed per account; | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub create_spot_datafeed_subscription { | ||
| 58 | my $self = shift; | ||
| 59 | my ($bucket,$prefix) = @_; | ||
| 60 | $bucket or croak "Usage: create_spot_datafeed_subscription(\$bucket,\$prefix)"; | ||
| 61 | $prefix ||= 'SPOT_DATAFEED_'; | ||
| 62 | my @param = (Bucket => $bucket, | ||
| 63 | Prefix => $prefix); | ||
| 64 | return $self->call('CreateSpotDatafeedSubscription',@param); | ||
| 65 | } | ||
| 66 | |||
| 67 | =head2 $boolean = $ec2->delete_spot_datafeed_subscription() | ||
| 68 | |||
| 69 | This method delete's the current account's spot datafeed | ||
| 70 | subscription, if any. It takes no arguments. | ||
| 71 | |||
| 72 | On success, it returns true. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub delete_spot_datafeed_subscription { | ||
| 77 | my $self = shift; | ||
| 78 | return $self->call('DeleteSpotDatafeedSubscription'); | ||
| 79 | } | ||
| 80 | |||
| 81 | =head2 $subscription = $ec2->describe_spot_datafeed_subscription() | ||
| 82 | |||
| 83 | This method describes the current account's spot datafeed | ||
| 84 | subscription, if any. It takes no arguments. | ||
| 85 | |||
| 86 | On success, a VM::EC2::Spot::DatafeedSubscription object is returned; | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | sub describe_spot_datafeed_subscription { | ||
| 91 | my $self = shift; | ||
| 92 | return $self->call('DescribeSpotDatafeedSubscription'); | ||
| 93 | } | ||
| 94 | |||
| 95 | =head2 @spot_price_history = $ec2->describe_spot_price_history(@filters) | ||
| 96 | |||
| 97 | This method applies the specified filters to spot instances and | ||
| 98 | returns a list of instances, timestamps and their price at the | ||
| 99 | indicated time. Each spot price history point is represented as a | ||
| 100 | VM::EC2::Spot::PriceHistory object. | ||
| 101 | |||
| 102 | Option arguments are: | ||
| 103 | |||
| 104 | -start_time Start date and time of the desired history | ||
| 105 | data, in the form yyyy-mm-ddThh:mm:ss (GMT). | ||
| 106 | The Perl DateTime module provides a convenient | ||
| 107 | way to create times in this format. | ||
| 108 | |||
| 109 | -end_time End date and time of the desired history | ||
| 110 | data. | ||
| 111 | |||
| 112 | -instance_type The instance type, e.g. "m1.small", can be | ||
| 113 | a scalar value or an arrayref. | ||
| 114 | |||
| 115 | -product_description The product description. One of "Linux/UNIX", | ||
| 116 | "SUSE Linux" or "Windows". Can be a scalar value | ||
| 117 | or an arrayref. | ||
| 118 | |||
| 119 | -availability_zone A single availability zone, such as "us-east-1a". | ||
| 120 | |||
| 121 | -max_results Maximum number of rows to return in a single | ||
| 122 | call. | ||
| 123 | |||
| 124 | -next_token Specifies the next set of results to return; used | ||
| 125 | internally. | ||
| 126 | |||
| 127 | -filter Hashref containing additional filters to apply, | ||
| 128 | |||
| 129 | The following filters are recognized: "instance-type", | ||
| 130 | "product-description", "spot-price", "timestamp", | ||
| 131 | "availability-zone". The '*' and '?' wildcards can be used in filter | ||
| 132 | values, but numeric comparison operations are not supported by the | ||
| 133 | Amazon API. Note that wildcards are not generally allowed in the | ||
| 134 | standard options. Hence if you wish to get spot price history in all | ||
| 135 | availability zones in us-east, this will work: | ||
| 136 | |||
| 137 | $ec2->describe_spot_price_history(-filter=>{'availability-zone'=>'us-east*'}) | ||
| 138 | |||
| 139 | but this will return an invalid parameter error: | ||
| 140 | |||
| 141 | $ec2->describe_spot_price_history(-availability_zone=>'us-east*') | ||
| 142 | |||
| 143 | If you specify -max_results, then the list of history objects returned | ||
| 144 | may not represent the complete result set. In this case, the method | ||
| 145 | more_spot_prices() will return true. You can then call | ||
| 146 | describe_spot_price_history() repeatedly with no arguments in order to | ||
| 147 | retrieve the remainder of the results. When there are no more results, | ||
| 148 | more_spot_prices() will return false. | ||
| 149 | |||
| 150 | my @results = $ec2->describe_spot_price_history(-max_results => 20, | ||
| 151 | -instance_type => 'm1.small', | ||
| 152 | -availability_zone => 'us-east*', | ||
| 153 | -product_description=>'Linux/UNIX'); | ||
| 154 | print_history(\@results); | ||
| 155 | while ($ec2->more_spot_prices) { | ||
| 156 | @results = $ec2->describe_spot_price_history | ||
| 157 | print_history(\@results); | ||
| 158 | } | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub more_spot_prices { | ||
| 163 | my $self = shift; | ||
| 164 | return $self->{spot_price_history_token} && | ||
| 165 | !$self->{spot_price_history_stop}; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub describe_spot_price_history { | ||
| 169 | my $self = shift; | ||
| 170 | my @parms; | ||
| 171 | |||
| 172 | if (!@_ && $self->{spot_price_history_token} && $self->{price_history_args}) { | ||
| 173 | @parms = (@{$self->{price_history_args}},NextToken=>$self->{spot_price_history_token}); | ||
| 174 | } | ||
| 175 | |||
| 176 | else { | ||
| 177 | my %args = $self->args('-filter',@_); | ||
| 178 | push @parms,$self->single_parm($_,\%args) | ||
| 179 | foreach qw(StartTime EndTime MaxResults AvailabilityZone); | ||
| 180 | push @parms,$self->list_parm($_,\%args) | ||
| 181 | foreach qw(InstanceType ProductDescription); | ||
| 182 | push @parms,$self->filter_parm(\%args); | ||
| 183 | |||
| 184 | if ($args{-max_results}) { | ||
| 185 | $self->{spot_price_history_token} = 'xyzzy'; # dummy value | ||
| 186 | $self->{price_history_args} = \@parms; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | return $self->call('DescribeSpotPriceHistory',@parms); | ||
| 191 | } | ||
| 192 | |||
| 193 | =head2 @requests = $ec2->request_spot_instances(%args) | ||
| 194 | |||
| 195 | This method will request one or more spot instances to be launched | ||
| 196 | when the current spot instance run-hour price drops below a preset | ||
| 197 | value and terminated when the spot instance run-hour price exceeds the | ||
| 198 | value. | ||
| 199 | |||
| 200 | On success, will return a series of VM::EC2::Spot::InstanceRequest | ||
| 201 | objects, one for each instance specified in -instance_count. | ||
| 202 | |||
| 203 | =over 4 | ||
| 204 | |||
| 205 | =item Required arguments: | ||
| 206 | |||
| 207 | -spot_price The desired spot price, in USD. | ||
| 208 | |||
| 209 | -image_id ID of an AMI to launch | ||
| 210 | |||
| 211 | -instance_type Type of the instance(s) to launch, such as "m1.small" | ||
| 212 | |||
| 213 | =item Optional arguments: | ||
| 214 | |||
| 215 | -instance_count Maximum number of instances to launch (default 1) | ||
| 216 | |||
| 217 | -type Spot instance request type; one of "one-time" or "persistent" | ||
| 218 | |||
| 219 | -valid_from Date/time the request becomes effective, in format | ||
| 220 | yyyy-mm-ddThh:mm:ss. Default is immediately. | ||
| 221 | |||
| 222 | -valid_until Date/time the request expires, in format | ||
| 223 | yyyy-mm-ddThh:mm:ss. Default is to remain in | ||
| 224 | effect indefinitely. | ||
| 225 | |||
| 226 | -launch_group Name of the launch group. Instances in the same | ||
| 227 | launch group are started and terminated together. | ||
| 228 | Default is to launch instances independently. | ||
| 229 | |||
| 230 | -availability_zone_group If specified, all instances that are given | ||
| 231 | the same zone group name will be launched into the | ||
| 232 | same availability zone. This is independent of | ||
| 233 | the -availability_zone argument, which specifies | ||
| 234 | a particular availability zone. | ||
| 235 | |||
| 236 | -key_name Name of the keypair to use | ||
| 237 | |||
| 238 | -security_group_id Security group ID to use for this instance. | ||
| 239 | Use an arrayref for multiple group IDs | ||
| 240 | |||
| 241 | -security_group Security group name to use for this instance. | ||
| 242 | Use an arrayref for multiple values. | ||
| 243 | |||
| 244 | -user_data User data to pass to the instances. Do NOT base64 | ||
| 245 | encode this. It will be done for you. | ||
| 246 | |||
| 247 | -availability_zone The availability zone you want to launch the | ||
| 248 | instance into. Call $ec2->regions for a list. | ||
| 249 | -zone Short version of -availability_aone. | ||
| 250 | |||
| 251 | -placement_group An existing placement group to launch the | ||
| 252 | instance into. Applicable to cluster instances | ||
| 253 | only. | ||
| 254 | -placement_tenancy Specify 'dedicated' to launch the instance on a | ||
| 255 | dedicated server. Only applicable for VPC | ||
| 256 | instances. | ||
| 257 | |||
| 258 | -kernel_id ID of the kernel to use for the instances, | ||
| 259 | overriding the kernel specified in the image. | ||
| 260 | |||
| 261 | -ramdisk_id ID of the ramdisk to use for the instances, | ||
| 262 | overriding the ramdisk specified in the image. | ||
| 263 | |||
| 264 | -block_devices Specify block devices to map onto the instances, | ||
| 265 | overriding the values specified in the image. | ||
| 266 | See run_instances() for the syntax of this argument. | ||
| 267 | |||
| 268 | -block_device_mapping Alias for -block_devices. | ||
| 269 | |||
| 270 | -network_interfaces Same as the -network_interfaces option in run_instances(). | ||
| 271 | |||
| 272 | -monitoring Pass a true value to enable detailed monitoring. | ||
| 273 | |||
| 274 | -subnet The ID of the Amazon VPC subnet in which to launch the | ||
| 275 | spot instance (VPC only). | ||
| 276 | |||
| 277 | -subnet_id deprecated | ||
| 278 | |||
| 279 | -addressing_type Deprecated and undocumented, but present in the | ||
| 280 | current EC2 API documentation. | ||
| 281 | |||
| 282 | -iam_arn The Amazon resource name (ARN) of the IAM Instance Profile (IIP) | ||
| 283 | to associate with the instances. | ||
| 284 | |||
| 285 | -iam_name The name of the IAM instance profile (IIP) to associate with the | ||
| 286 | instances. | ||
| 287 | |||
| 288 | -ebs_optimized If true, request an EBS-optimized instance (certain | ||
| 289 | instance types only). | ||
| 290 | |||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | sub request_spot_instances { | ||
| 295 | my $self = shift; | ||
| 296 | my %args = @_; | ||
| 297 | $args{-spot_price} or croak "-spot_price argument missing"; | ||
| 298 | $args{-image_id} or croak "-image_id argument missing"; | ||
| 299 | $args{-instance_type} or croak "-instance_type argument missing"; | ||
| 300 | |||
| 301 | $args{-availability_zone} ||= $args{-zone}; | ||
| 302 | $args{-availability_zone} ||= $args{-placement_zone}; | ||
| 303 | |||
| 304 | my @p = map {$self->single_parm($_,\%args)} | ||
| 305 | qw(SpotPrice InstanceCount Type ValidFrom ValidUntil LaunchGroup AvailabilityZoneGroup Subnet); | ||
| 306 | |||
| 307 | # oddly enough, the following args need to be prefixed with "LaunchSpecification." | ||
| 308 | my @launch_spec = map {$self->single_parm($_,\%args)} | ||
| 309 | qw(ImageId KeyName UserData AddressingType InstanceType KernelId RamdiskId SubnetId); | ||
| 310 | push @launch_spec, map {$self->list_parm($_,\%args)} qw(SecurityGroup SecurityGroupId); | ||
| 311 | push @launch_spec, ('EbsOptimized'=>'true') if $args{-ebs_optimized}; | ||
| 312 | push @launch_spec, $self->block_device_parm($args{-block_devices}||$args{-block_device_mapping}); | ||
| 313 | push @launch_spec, $self->iam_parm(\%args); | ||
| 314 | push @launch_spec, $self->network_interface_parm(\%args); | ||
| 315 | |||
| 316 | while (my ($key,$value) = splice(@launch_spec,0,2)) { | ||
| 317 | push @p,("LaunchSpecification.$key" => $value); | ||
| 318 | } | ||
| 319 | |||
| 320 | # a few more oddballs | ||
| 321 | push @p,('LaunchSpecification.Placement.AvailabilityZone'=> $args{-availability_zone}) | ||
| 322 | if $args{-availability_zone}; | ||
| 323 | push @p,('Placement.GroupName' =>$args{-placement_group}) if $args{-placement_group}; | ||
| 324 | push @p,('LaunchSpecification.Monitoring.Enabled' => 'true') if $args{-monitoring}; | ||
| 325 | push @p,('LaunchSpecification.UserData' => | ||
| 326 | encode_base64($args{-user_data},'')) if $args{-user_data}; | ||
| 327 | return $self->call('RequestSpotInstances',@p); | ||
| 328 | } | ||
| 329 | |||
| 330 | =head2 @requests = $ec2->cancel_spot_instance_requests(@request_ids) | ||
| 331 | |||
| 332 | This method cancels the pending requests. It does not terminate any | ||
| 333 | instances that are already running as a result of the requests. It | ||
| 334 | returns a list of VM::EC2::Spot::InstanceRequest objects, whose fields | ||
| 335 | will be unpopulated except for spotInstanceRequestId and state. | ||
| 336 | |||
| 337 | =cut | ||
| 338 | |||
| 339 | sub cancel_spot_instance_requests { | ||
| 340 | my $self = shift; | ||
| 341 | my %args = $self->args('-spot_instance_request_id',@_); | ||
| 342 | my @parm = $self->list_parm('SpotInstanceRequestId',\%args); | ||
| 343 | # possible bug in AWS: some cancelled instances repeated in list | ||
| 344 | my %c = map {$_=>$_} $self->call('CancelSpotInstanceRequests',@parm); | ||
| 345 | return values %c; | ||
| 346 | } | ||
| 347 | |||
| 348 | |||
| 349 | =head2 @requests = $ec2->describe_spot_instance_requests(@spot_instance_request_ids) | ||
| 350 | |||
| 351 | =head2 @requests = $ec2->describe_spot_instance_requests(\%filters) | ||
| 352 | |||
| 353 | =head2 @requests = $ec2->describe_spot_instance_requests(-spot_instance_request_id=>\@ids,-filter=>\%filters) | ||
| 354 | |||
| 355 | This method will return information about current spot instance | ||
| 356 | requests as a list of VM::EC2::Spot::InstanceRequest objects. | ||
| 357 | |||
| 358 | Optional arguments: | ||
| 359 | |||
| 360 | -spot_instance_request_id -- Scalar or arrayref of request Ids. | ||
| 361 | |||
| 362 | -filter -- Tags and other filters to apply. | ||
| 363 | |||
| 364 | There are many filters available, described fully at | ||
| 365 | http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-ItemType-SpotInstanceRequestSetItemType.html: | ||
| 366 | |||
| 367 | availability-zone-group | ||
| 368 | create-time | ||
| 369 | fault-code | ||
| 370 | fault-message | ||
| 371 | instance-id | ||
| 372 | launch-group | ||
| 373 | launch.block-device-mapping.delete-on-termination | ||
| 374 | launch.block-device-mapping.device-name | ||
| 375 | launch.block-device-mapping.snapshot-id | ||
| 376 | launch.block-device-mapping.volume-size | ||
| 377 | launch.block-device-mapping.volume-type | ||
| 378 | launch.group-id | ||
| 379 | launch.image-id | ||
| 380 | launch.instance-type | ||
| 381 | launch.kernel-id | ||
| 382 | launch.key-name | ||
| 383 | launch.monitoring-enabled | ||
| 384 | launch.ramdisk-id | ||
| 385 | launch.network-interface.network-interface-id | ||
| 386 | launch.network-interface.device-index | ||
| 387 | launch.network-interface.subnet-id | ||
| 388 | launch.network-interface.description | ||
| 389 | launch.network-interface.private-ip-address | ||
| 390 | launch.network-interface.delete-on-termination | ||
| 391 | launch.network-interface.group-id | ||
| 392 | launch.network-interface.group-name | ||
| 393 | launch.network-interface.addresses.primary | ||
| 394 | product-description | ||
| 395 | spot-instance-request-id | ||
| 396 | spot-price | ||
| 397 | state | ||
| 398 | status-code | ||
| 399 | status-message | ||
| 400 | tag-key | ||
| 401 | tag-value | ||
| 402 | tag:<key> | ||
| 403 | type | ||
| 404 | launched-availability-zone | ||
| 405 | valid-from | ||
| 406 | valid-until | ||
| 407 | |||
| 408 | =cut | ||
| 409 | |||
| 410 | |||
| 411 | sub describe_spot_instance_requests { | ||
| 412 | my $self = shift; | ||
| 413 | my %args = $self->args('-spot_instance_request_id',@_); | ||
| 414 | my @params = $self->list_parm('SpotInstanceRequestId',\%args); | ||
| 415 | push @params,$self->filter_parm(\%args); | ||
| 416 | return $self->call('DescribeSpotInstanceRequests',@params); | ||
| 417 | } | ||
| 418 | |||
| 419 | |||
| 420 | =head1 SEE ALSO | ||
| 421 | |||
| 422 | L<VM::EC2> | ||
| 423 | |||
| 424 | =head1 AUTHOR | ||
| 425 | |||
| 426 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 427 | |||
| 428 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||
| 429 | |||
| 430 | This package and its accompanying libraries is free software; you can | ||
| 431 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 432 | version 1, or at your option, any later version) or the Artistic | ||
| 433 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 434 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 435 | |||
| 436 | =cut | ||
| 437 | |||
| 438 | |||
| 439 | 1; |