This file is indexed.

/usr/share/perl5/VM/EC2/Image.pm is in libvm-ec2-perl 1.28-2build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package VM::EC2::Image;

=head1 NAME

VM::EC2::Image - Object describing an Amazon Machine Image (AMI)

=head1 SYNOPSIS

  use VM::EC2;

  $ec2     = VM::EC2->new(...);
  $image   = $ec2->describe_images(-image_id=>'ami-12345');

  $state   = $image->imageState;
  $owner   = $image->imageOwnerId;
  $rootdev = $image->rootDeviceName;
  @devices = $image->blockDeviceMapping;
  $tags    = $image->tags;

  @instances = $image->run_instances(-min_count=>10);

=head1 DESCRIPTION

This object represents an Amazon Machine Image (AMI), and is returned
by VM::EC2->describe_images(). In addition to methods to query the
image's attributes, the run_instances() method allows you to launch
and configure EC2 instances based on the AMI.

=head1 METHODS

These object methods are supported:

 imageId       -- AMI ID
 imageLocation -- Location of the AMI
 imageState    -- Current state of the AMI. One of "available",
                  "pending" or "failed". Only "available" AMIs
                  can be launched.
 imageOwnerId  -- AWS account ID of the image owner.
 isPublic      -- Returns true if this image has public launch
                  permissions. Note that this is a Perl boolean,
                  and not the string "true".
 productCodes  -- A list of product codes associated with the image.
 architecture  -- The architecture of the image.
 imageType     -- The image type (machine, kernel or RAM disk).
 kernelId      -- The kernel associated with the image.
 ramdiskId     -- The RAM disk associated with the image.
 platform      -- "Windows" for Windows AMIs, otherwise undef.
 sriovNetSupport -- Specifies whether enhanced networking is enabled.
                    "simple" if enabled.
 stateReason   -- Explanation of a "failed" imageState. This is
                  a VM::EC2::Instance::State::Reason
                  object.
 imageOwnerAlias -The AWS account alias (e.g. "self") or AWS
                  account ID that owns the AMI.
 name          -- Name of the AMI provided during image creation.
 description   -- Description of the AMI provided during image
                  creation.
 rootDeviceType -- The root device type. One of "ebs" or
                  "instance-store".
 rootDeviceName -- Name of the root device, e.g. "/dev/sda1"
 blockDeviceMapping -- List of block devices attached to this
                   image. Each element is a
                   VM::EC2::BlockDevice.
 virtualizationType -- One of "paravirtual" or "hvm".
 hypervisor     -- One of "ovm" or "xen"

In addition, the object supports the tags() method described in
L<VM::EC2::Generic>:

 print "ready for production\n" if $image->tags->{Released};

=head2 @instances = $image->run_instances(%args)

The run_instance() method will launch one or more instances based on
this AMI. The method takes all the arguments recognized by
VM::EC2->run_instances(), except for the -image_id argument. The
method returns a list of VM::EC2::Instance objects, which you may
monitor periodically until they are up and running.

All arguments are optional. See run_instances() in L<VM::EC2> for 
more information.

  -min_count         Minimum number of instances to launch [1]
  -max_count         Maximum number of instances to launch [1]
  -key_name          Name of the keypair to use
  -security_group_id Security group ID to use for this instance.
                     Use an arrayref for multiple group IDs
  -security_group    Security group name to use for this instance.
                     Use an arrayref for multiple values.
  -user_data         User data to pass to the instances. Do NOT base64
                     encode this. It will be done for you.
  -instance_type     Type of the instance to use. See below for a
                     list.
  -placement_zone    The availability zone you want to launch the
                     instance into. Call $ec2->regions for a list.
  -placement_group   An existing placement group to launch the
                     instance into. Applicable to cluster instances
                     only.
  -placement_tenancy Specify 'dedicated' to launch the instance on a
                     dedicated server. Only applicable for VPC
                     instances.
  -kernel_id         ID of the kernel to use for the instances,
                     overriding the kernel specified in the image.
  -ramdisk_id        ID of the ramdisk to use for the instances,
                     overriding the ramdisk specified in the image.
  -block_devices     Specify block devices to map onto the instances,
                     overriding the values specified in the image.
                     This can be a scalar string or an arrayref for
                     multiple mappings:
                     Example: 
                     ['/dev/sdb=ephemeral0','/dev/sdc=snap-12345:80:false']
  -monitoring        Pass a true value to enable detailed monitoring.
  -subnet_id         ID of the subnet to launch the instance
                     into. Only applicable for VPC instances.
  -termination_protection  Pass true to lock the instance so that it
                     cannot be terminated using the API. Use
                     modify_instance() to unset this if youu wish to
                     terminate the instance later.
  -disable_api_termination -- Same as above.
  -shutdown_behavior Pass "stop" (the default) to stop the instance
                     and save its disk state when "shutdown" is called
                     from within the instance. Stopped instances can
                     be restarted later. Pass "terminate" to
                     instead terminate the instance and discard its
                     state completely.
  -instance_initiated_shutdown_behavior -- Same as above.
  -private_ip_address Assign the instance to a specific IP address
                     from a VPC subnet (VPC only).
  -client_token      Unique identifier that you can provide to ensure
                     idempotency of the request. You can use
                     $ec2->token() to generate a suitable identifier.
                     See http://docs.amazonwebservices.com/AWSEC2/
                         latest/UserGuide/Run_Instance_Idempotency.html

Note that after launching an instance, you may need to wait a few
seconds before adding tags to it or performing other operations.

See L<VM::EC2> for details.

=head2 $boolean = $image->make_public($public)

Change the isPublic flag. Provide a true value to make the image
public, a false one to make it private.

=head2 $state  = $image->current_status

Refreshes the object and then calls imageState() to return one of
"pending", "available" or "failed." You can use this to monitor an
image_creation process in progress.

=head2 @user_ids = $image->launchPermissions

Returns a list of user IDs with launch permission for this
image. Note that the AWS API calls this
"launchPermission", but this module makes it plural to emphasize that
the result is a list.

=head2 @user_ids = $image->authorized_users

The same as launchPermissions.

=head2 $boolean = $image->add_authorized_users($id1,$id2,...)

=head2 $boolean = $image->remove_authorized_users($id1,$id2,...)

These methods add and remove user accounts which have launch
permissions for the image. The result code indicates whether the list
of user IDs were successfully added or removed.

=head2 $boolean = $image->add_authorized_users($id1,$id2,...)

=head2 $boolean = $image->remove_authorized_users($id1,$id2,...)

=head2 $boolean = $image->reset_authorized_users

These methods add and remove user accounts which have launch
permissions for the image. The result code indicates whether the list
of user IDs were successfully added or removed.

reset_authorized_users() resets the list users authored to launch
instances from this image to empty, effectively granting launch
privileges to the owner only.

See also authorized_users().

=head2 $copy = $image->copy(-region => $region, -name => $name, -description => $desc)

Copies the image to another region.

Required arguments:

 -region           The region to copy to

Optional arguments:

 -name             The name of the image.

 -description      The description of the image.

 -client_token     Unique identifier to ensure idempotency of the request

=head2 $image->refresh

This method will refresh the object from AWS, updating all values to
their current ones. You can call it after tagging or otherwise
changing image attributes.

=head1 STRING OVERLOADING

When used in a string context, this object will interpolate the
imageId.

=head1 SEE ALSO

L<VM::EC2>
L<VM::EC2::Generic>
L<VM::EC2::BlockDevice>
L<VM::EC2::StateReason>
L<VM::EC2::Instance>
L<VM::EC2::Tag>

=head1 AUTHOR

Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>.

Copyright (c) 2011 Ontario Institute for Cancer Research

This package and its accompanying libraries is free software; you can
redistribute it and/or modify it under the terms of the GPL (either
version 1, or at your option, any later version) or the Artistic
License 2.0.  Refer to LICENSE for the full license text. In addition,
please see DISCLAIMER.txt for disclaimers of warranty.

=cut

use strict;
use base 'VM::EC2::Generic';
use VM::EC2::BlockDevice;
use VM::EC2::Image::LaunchPermission;
use VM::EC2::Instance::State::Reason;
use VM::EC2::ProductCode;

use Carp 'croak';

sub valid_fields {
    my $self = shift;
    return qw(imageId imageLocation imageState imageOwnerId isPublic
              productCodes architecture imageType kernelId ramdiskId
              platform sriovNetSupport stateReason imageOwnerAlias name
              description rootDeviceType rootDeviceName blockDeviceMapping
              virtualizationType tagSet hypervisor);
}

sub primary_id { shift->imageId }

sub stateReason {
    my $self  = shift;
    my $state = $self->SUPER::stateReason;
    return VM::EC2::Instance::State::Reason->new($state);

}

sub productCodes {
    my $self = shift;
    if (@_) {
	$self->aws->modify_image_attribute($self,-product_code=>\@_);
    } else {
	my $codes = $self->SUPER::productCodes or return;
	return map {VM::EC2::ProductCode->new($_)} @{$codes->{item}};
    }
}

sub blockDeviceMapping {
    my $self = shift;
    my $mapping = $self->SUPER::blockDeviceMapping or return;
    return map { VM::EC2::BlockDevice->new($_,$self->aws)} @{$mapping->{item}};
}

sub launchPermissions {
    my $self = shift;
    return map {VM::EC2::Image::LaunchPermission->new($_,$self->aws)}
        $self->aws->describe_image_attribute($self->imageId,'launchPermission');
}

sub isPublic {
    my $self = shift;
    return $self->SUPER::isPublic eq 'true';
}

sub make_public {
    my $self = shift;
    @_ == 1 or croak "Usage: VM::EC2::Image->make_public(\$boolean)";
    my $public = shift;
    my @arg    = $public ? (-launch_add_group=>'all') : (-launch_remove_group=>'all');
    my $result = $self->aws->modify_image_attribute($self->imageId,@arg) or return;
    $self->payload->{isPublic} = $public ? 'true' : 'false';
    return $result
}

sub authorized_users { shift->launchPermissions }

sub add_authorized_users {
    my $self = shift;
    @_ or croak "Usage: VM::EC2::Image->add_authorized_users(\@userIds)";
    return $self->aws->modify_image_attribute($self->imageId,-launch_add_user=>\@_);
}

sub remove_authorized_users {
    my $self = shift;
    @_ or croak "Usage: VM::EC2::Image->remove_authorized_users(\@userIds)";
    return $self->aws->modify_image_attribute($self->imageId,-launch_remove_user=>\@_);
}

sub reset_authorized_users {
    my $self = shift;
    $self->aws->reset_image_attribute($self->imageId,'launchPermission');
}

sub run_instances {
    my $self = shift;
    my %args = @_;
    croak "$self is unavailable for launching because its state is ",$self->imageState
	      unless $self->imageState eq 'available';
    $args{-image_id} = $self->imageId;
    $self->aws->run_instances(%args);
}

sub current_status {
    my $self = shift;
    $self->refresh;
    return $self->imageState;
}

sub current_status_async {
    my $self = shift;
    my $to_caller = VM::EC2->condvar;

    my $cv = $self->aws->describe_images_async(-image_id=>$self->imageId);

    $cv->cb(sub {
	my $i = shift->recv;
	$to_caller->send($i->imageState)
	    });

    return $to_caller;
}


sub refresh {
    my $self = shift;
    my $i   = shift;
    ($i) = $self->aws->describe_images(-image_id=>$self->imageId) unless $i;
    %$self  = %$i if $i;
    return defined $i;
}

sub copy {
    my $self = shift;
    my %args = @_;
    my $image_id = $self->imageId;
    my $name = $args{-name};
    my $desc = $args{-description} || $args{-desc};
    my $token = $args{-client_token} || $args{-token};
    my $region = $args{-region} or croak "copy(): -region argument required";
    my $orig_region = $self->aws->region;
    # set region to dest region in ec2 object
    $self->aws->region($region);
    my $image = $self->aws->copy_image(-source_region=>$orig_region, -source_image_id=>$image_id, -name => $name, -description=>$desc, -client_token => $token);
    # set region back
    $self->aws->region($orig_region);
    return $image;
}

1;