CCfnX/MakeAMI.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 92 CC 3 SCAR 19.5
CC
3
Cov
0%
register_image
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 7.0
1 package CCfnX::MakeAMI {
2 use Moose::Role;
3 use CloudDeploy::AMIDB;
4  
5 has amidb => (is => 'ro', lazy => 1, default => sub {
6 CloudDeploy::AMIDB->new
7 });
8  
9 before deploy => sub {
10 my ($self) = @_;
11  
12 return if (not defined $self->origin->params->amitag);
13  
14 my @list = $self->amidb->search(
15 Name => $self->origin->params->name,
16 Region => $self->origin->params->region,
17 Tags => $self->origin->params->amitag,
18 );
19 if (@list > 0) {
20 die sprintf "Aborting AMI creation: you already have an image called %s in the region %s with amitag %s", $self->origin->params->name, $self->origin->params->region, $self->origin->params->amitag;
21 }
22 };
23  
24 around deploy => sub {
25 my ($orig, $self) = @_;
26 if ($self->origin->params->onlysnapshot) {
27 print "Loading deployment " . $self->name . "\n";
28 $self->get_from_mongo;
29 die "Error: previous deployment with same name was not run with --devel option" unless ($self->params->{devel});
30 } else {
31 $self->$orig();
32 }
33  
34 if (not $self->origin->params->devel or $self->origin->params->onlysnapshot) {
35 use Paws;
36 my $ec2 = Paws->service('EC2',
37 region => $self->region
38 );
39  
40 # This won't work until the persistence is functional
41 my $instance_id = $self->outputs->{'InstanceID'};
42  
43 if ($self->origin->params->os_family eq 'linux') {
44 $ec2->StopInstances(InstanceIds => [ $instance_id ]);
45 }
46  
47 sleep 2;
48 my $instance_state = $ec2->DescribeInstances(InstanceIds => [ $instance_id ]);
49 while ($instance_state->Reservations->[0]->Instances->[0]->State->Name ne 'stopped') {
50 sleep 5;
51 $instance_state = $ec2->DescribeInstances(InstanceIds => [ $instance_id ]);
52 }
53  
54 my $ami = $ec2->CreateImage(
55 InstanceId => $instance_id,
56 Name => sprintf("%s %d", $self->name, time()),
57 Description => sprintf("%s %s", $self->name, scalar(localtime)),
58 ) || die "An error occurred when creating the AMI of $instance_id\n";
59 my $ami_id = $ami->ImageId;
60  
61 print "CREATING AMI: $ami_id\n";
62  
63 sleep 5;
64  
65 my $image = $ec2->DescribeImages(ImageIds => [ $ami_id ])->Images->[0];
66 my $image_state = $image->State;
67  
68 print "WAITING FOR SNAPSHOT TO FINISH: $ami_id\n";
69  
70 while ($image_state eq 'pending') {
71 sleep 10;
72 $image = $ec2->DescribeImages(ImageIds => [ $ami_id ])->Images->[0];
73 $image_state = $image->State;
74 }
75  
76 print "CREATED AMI: $ami_id\n";
77  
78 $self->outputs->{'ImageId'} = $ami_id;
79  
80 if ($image_state eq 'available') {
81 $self->register_image($image);
82 $self->undeploy;
83 } else {
84 die "Failed creating AMI:" . Dumper($image_state);
85 }
86 }
87 };
88  
89 sub register_image {
90 my ($self, $image) = @_;
91
92 my $type = $image->VirtualizationType;
93 $type = 'pv' if ($type eq 'paravirtual');
94  
95 $self->amidb->add(
96 Account => $self->account,
97 Name => $self->name,
98 Arch => $image->Architecture,
99 Root => $image->RootDeviceType,
100 Region => $self->params->{ region },
101 ImageId => $image->ImageId,
102 Type => $type,
103 Class => $self->type,
104 Date => $image->CreationDate,
105 OriginImageId => $self->params->{ ami },
106 (defined $self->origin->params->amitag)?(Tags => [ $self->origin->params->amitag ]):(),
107 );
108 }
109  
110 }
111  
112 1;