blib/script/migrate-ebs-image.pl untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 81 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 =head1 NAME
4  
5 migrate-ebs-image.pl Copy an EBS-backed Amazon Image from one region to another
6  
7 =head1 SYNOPSYS
8  
9 % migrate-ebs-image.pl --from us-east-1 --to ap-southeast-1 ami-123456
10  
11 =head1 DESCRIPTION
12  
13 This script copies an EBS-backed Unix/Linux/windows AMI located in the
14 EC2 region indicated by --from to the region indicated by --to. All
15 associated volume snapshots, including LVM and RAID volumes, are
16 migrated as well.
17  
18 If --from is omitted, then the source region is derived from the
19 endpoint URL contained in the EC2_URL environment variable. The --to
20 option is required.
21  
22 This script works with any EBS backed image, including Linux PVM,
23 Linux HVM (cluster) and Windows images. It will B<not> work with any
24 instance-store backed image. To migrate such instances, please see one
25 of the recipes listed online, for example:
26  
27 http://www.dowdandassociates.com/content/howto-move-ec2-instance-store-ami-one-region-another
28  
29 =head1 COMMAND-LINE OPTIONS
30  
31 Options can be abbreviated. For example, you can use -l for
32 --list-regions and -b for --block-device-mapping:
33  
34 --from Region in which the AMI is currently located (e.g. "us-east-1")
35 --to Region to which the AMI is to be copied (e.g. "us-west-1") REQUIRED
36 --access_key EC2 access key
37 --secret_key EC2 secret key
38 --block_device_mapping
39 Add additional block devices to the image.
40 --endpoint EC2 URL (defaults to http://ec2.amazonaws.com/)
41 --kernel Force assignment of kernel in destination image.
42 --ramdisk Force assignment of ramdisk in destination image.
43 --quiet Quench status messages
44 --list_regions List the EC2 regions
45  
46 The --block-device-mapping (-b) option is used to add ephemeral
47 storage to the destination image. Amazon's API doesn't describe
48 ephemeral volumes that are associated with images, and so this
49 information is not copied from the source to the destination image,
50 requiring you to add it back manually. The value of the argument is a
51 block device mapping string in the same format as described for the
52 command line program ec2-register:
53  
54 migrate-ebs-image.pl -f us-east-1 -t ap-southeast-1 \
55 -b /dev/sdy=ephemeral0 \
56 ami-123456
57  
58 Ordinarily the script attempts to guess the correct matching kernel
59 and ramdisk for the destination image based on approximate string
60 matching. You can override these values by manually specifying the
61 kernel and/or ramdisk ID in the destination region. Note that no
62 checking is performed that the values you provide are correct.
63  
64 =head1 ENVIRONMENT VARIABLES
65  
66 The following environment variables are used if the corresponding
67 options are not present:
68  
69 EC2_ACCESS_KEY your access key
70 EC2_SECRET_KEY your secret key
71 EC2_URL the desired region endpoint
72  
73 =head1 INSTALLING THIS SCRIPT
74  
75 This script is part of the Perl VM::EC2 package. To install from the
76 command line:
77  
78 % perl -MCPAN -e 'install VM::EC2'
79 % migrate-ebs-image.pl --from us-east-1 --to ap-southeast-1 ami-123456
80  
81 =head1 IMPORTANT CAVEATS
82  
83 This script launches two "m1.small" instances, one each in the source
84 and destination regions. It also creates transient volumes in both
85 regions to hold the root volume and all other EBS snapshots associated
86 with the image. Running it will incur charges for instance run time
87 and data storage.
88  
89 In addition, this script will transfer data from one region to another
90 across the internet, incurring internet data out fees on the source
91 side, and internet data in fees on the destination side. Volumes that
92 contain a filesystem, such as ext4 or ntfs, are copied from source to
93 destination using rsync. Volumes that are part of a RAID or LVM volume
94 are copied at the block level using gzip and dd via the secure
95 shell. In general, rsync will be much faster and parsimonious of
96 network bandwidth than block copying!
97  
98 =head1 SEE ALSO
99  
100 L<VM::EC2>, L<VM::EC2::Staging::Manager>
101  
102 =head1 AUTHOR
103  
104 Lincoln Stein, lincoln.stein@gmail.com
105  
106 Copyright (c) 2012 Ontario Institute for Cancer Research
107
108 This package and its accompanying libraries is free software; you can
109 redistribute it and/or modify it under the terms of the GPL (either
110 version 1, or at your option, any later version) or the Artistic
111 License 2.0. Refer to LICENSE for the full license text. In addition,
112 please see DISCLAIMER.txt for disclaimers of warranty.
113  
114 =cut
115  
116 use strict;
117 use lib '../lib';
118  
119 use VM::EC2::Staging::Manager;
120 use File::Basename 'basename';
121 use Getopt::Long;
122  
123 my($From,$To,$Access_key,$Secret_key,$Endpoint,$Quiet,$List,$Kernel,$Ramdisk,@Block_devices);
124 my $Program_name = basename($0);
125  
126 GetOptions('from=s' => \$From,
127 'to=s' => \$To,
128 'access_key|access-key=s' => \$Access_key,
129 'secret_key|secret-key=s' => \$Secret_key,
130 'endpoint=s' => \$Endpoint,
131 'quiet' => \$Quiet,
132 'list_regions|list-regions' => \$List,
133 'block_device_mapping|block-device-mapping=s' => \@Block_devices,
134 'kernel' => \$Kernel,
135 'ramdisk' => \$Ramdisk,
136 ) or exec 'perldoc',$0;
137  
138 #setup defaults
139 $ENV{EC2_ACCESS_KEY} = $Access_key if defined $Access_key;
140 $ENV{EC2_SECRET_KEY} = $Secret_key if defined $Secret_key;
141 $ENV{EC2_URL} = $Endpoint if defined $Endpoint;
142 $Quiet ||= 0;
143  
144 my $ec2 = VM::EC2->new();
145 if ($List) {
146 print join("\n",sort $ec2->describe_regions),"\n";
147 exit 0;
148 }
149  
150 my $ami = shift or exec 'perldoc',$0;
151 $To or exec 'perldoc',$0;
152 $From ||= $ec2->region;
153  
154 unless ($From) {
155 my $endpoint = $ec2->endpoint;
156 ($From) = grep {$_->endpoint eq $ec2->endpoint} $ec2->describe_regions;
157 }
158  
159 my $source = eval {VM::EC2->new(-region => $From)->staging_manager(-on_exit=>'terminate',
160 -quiet => $Quiet)}
161 or die $@;
162  
163 my $dest = eval {VM::EC2->new(-region => $To)->staging_manager(-on_exit=>'terminate',
164 -quiet => $Quiet)}
165 or die $@;
166  
167 my @extra = @Block_devices ? (-block_devices=>\@Block_devices) : ();
168 push @extra,(-kernel_id => $Kernel) if $Kernel;
169 push @extra,(-ramdisk_id => $Ramdisk) if $Ramdisk;
170  
171 my $img = $source->copy_image($ami => $dest,@extra);
172  
173 undef $source;
174 undef $dest;
175  
176 print "New snapshot is now located in $To under $img.\n";
177  
178 exit 0;
179