| 1 | package CCfnX::CloudFormationDeployer { | ||
| 2 | use Moose::Role; | ||
| 3 | use Paws; | ||
| 4 | use JSON; | ||
| 5 | |||
| 6 | requires 'origin'; | ||
| 7 | has region => (is => 'rw', isa => 'Str', required => 1, lazy => 1, default => sub { $_[0]->origin->params->region }); | ||
| 8 | |||
| 9 | with 'CCfnX::DeployerResourceTypeHandler' => { | ||
| 10 | resource_type_prefixes=> [ 'AWS' ] | ||
| 11 | }; | ||
| 12 | |||
| 13 | has cfn => (is => 'ro', | ||
| 14 | isa => 'Paws::CloudFormation', | ||
| 15 | lazy => 1, | ||
| 16 | default => sub { | ||
| 17 | Paws->service('CloudFormation', | ||
| 18 | region => $_[0]->region, | ||
| 19 | ) | ||
| 20 | } | ||
| 21 | ); | ||
| 22 | |||
| 23 | before undeploy => sub { | ||
| 24 | my $self = shift; | ||
| 25 | |||
| 26 | eval { | ||
| 27 | $self->cfn->DeleteStack(StackName => $self->name); | ||
| 28 | }; | ||
| 29 | if ($@){ | ||
| 30 | warn "CloudFormation threw an error trying to delete the stack: $@\n\nPlease review that the stack was correctly deleted, because we're going to deregister it"; | ||
| 31 | } | ||
| 32 | |||
| 33 | my $start_time = time(); | ||
| 34 | print "Undeploying stack\n"; | ||
| 35 | |||
| 36 | # WaitForStack has a problem: when a stack disappears, the API will return an Error: Stack doesn't exist and die | ||
| 37 | # Tried to look at other API calls to detect if a stack doesn't exist anymore... | ||
| 38 | my $result; | ||
| 39 | eval { | ||
| 40 | $result = $self->WaitForStack; | ||
| 41 | }; | ||
| 42 | |||
| 43 | my $end_time = time(); | ||
| 44 | my $elapsed_time = $end_time - $start_time; | ||
| 45 | if (not defined $result) { | ||
| 46 | print "Stack undeployed\n"; | ||
| 47 | print "Undeployed in $elapsed_time seconds.\n"; | ||
| 48 | } else { | ||
| 49 | die "Stack wasn't completely deleted. Now in state: " . $result->StackStatus; | ||
| 50 | } | ||
| 51 | }; | ||
| 52 | |||
| 53 | before redeploy => sub { | ||
| 54 | my $self = shift; | ||
| 55 | |||
| 56 | $self->get_from_mongo; | ||
| 57 | $self->deploy_to_cloudformation; | ||
| 58 | }; | ||
| 59 | |||
| 60 | before deploy => sub { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | unless (defined $self->origin->params->{onlysnapshot} and $self->origin->params->onlysnapshot) { | ||
| 64 | $self->deploy_to_cloudformation; | ||
| 65 | } | ||
| 66 | }; | ||
| 67 | |||
| 68 | sub deploy_to_cloudformation { | ||
| 69 | my $self = shift; | ||
| 70 | my $result; | ||
| 71 | my $origin = $self->origin; | ||
| 72 | |||
| 73 | $origin->addResource('ForceUpdate', 'AWS::IAM::User') if ($ENV{CLOUDDEPLOY_FORCE_UPDATE}); | ||
| 74 | |||
| 75 | my $parameters_for_cfn = []; | ||
| 76 | foreach my $atto ($origin->params->meta->get_all_attributes) { | ||
| 77 | my $att = $atto->name; | ||
| 78 | if ($origin->params->meta->find_attribute_by_name($att)->does('CCfnX::Meta::Attribute::Trait::StackParameter')) { | ||
| 79 | my $val = $origin->params->$att; | ||
| 80 | $val = '' if (not defined $val); | ||
| 81 | push @$parameters_for_cfn, { ParameterKey => $att, ParameterValue => $val }; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | my $cfn_method = $origin->params->update ? 'UpdateStack' : 'CreateStack'; | ||
| 86 | |||
| 87 | $result = $self->cfn->$cfn_method(StackName => $self->name, | ||
| 88 | Capabilities => [ 'CAPABILITY_IAM' ], | ||
| 89 | TemplateBody => $origin->as_json, | ||
| 90 | Parameters => $parameters_for_cfn, | ||
| 91 | ); | ||
| 92 | |||
| 93 | my $start_time = time(); | ||
| 94 | print "Polling cfn for stack status\n"; | ||
| 95 | |||
| 96 | $result = $self->WaitForStack; | ||
| 97 | |||
| 98 | my $end_time = time(); | ||
| 99 | my $elapsed_time = $end_time - $start_time; | ||
| 100 | if ($result->StackStatus eq 'CREATE_COMPLETE' or $result->StackStatus eq 'UPDATE_COMPLETE') { | ||
| 101 | print "Stack Complete\n"; | ||
| 102 | print "Deployed in $elapsed_time seconds.\n"; | ||
| 103 | $self->outputs($self->get_stack_outputs); | ||
| 104 | } else { | ||
| 105 | die "Can't continue: Stack status is: " . $result->StackStatus ; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | sub get_stack_outputs { | ||
| 110 | my $self = shift; | ||
| 111 | my $result = $self->cfn->DescribeStacks(StackName => $self->name); | ||
| 112 | my $outputs = $result->Stacks->[0]->Outputs; | ||
| 113 | my $mappings = $self->origin->output_mappings; | ||
| 114 | return if (not $outputs); | ||
| 115 | |||
| 116 | # Get outputs from cloudfront | ||
| 117 | my $out = {}; | ||
| 118 | foreach my $output (@$outputs) { | ||
| 119 | my $real_key = $mappings->{$output->OutputKey}; | ||
| 120 | if ($real_key) { | ||
| 121 | $out->{$real_key} = $output->OutputValue; | ||
| 122 | } else { | ||
| 123 | $out->{$output->OutputKey} = $output->OutputValue; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | my $origin = $self->origin; | ||
| 128 | foreach my $atto ($origin->meta->get_all_attributes) { | ||
| 129 | my $att = $atto->name; | ||
| 130 | if ($origin->meta->find_attribute_by_name($att)->does('CCfnX::Meta::Attribute::Trait::PostOutput')) { | ||
| 131 | my $stack_res = $self->cfn->DescribeStackResources(PhysicalResourceId => $out->{$att}); | ||
| 132 | my ($resource) = grep { $_->PhysicalResourceId eq $out->{$att} } @{ $stack_res->StackResources }; | ||
| 133 | my $params = { | ||
| 134 | StackName => $self->name, | ||
| 135 | LogicalResourceId => $resource->LogicalResourceId | ||
| 136 | }; | ||
| 137 | my $res = $self->cfn->DescribeStackResource(%$params); | ||
| 138 | $out->{ $att } = from_json($res->StackResourceDetail->Metadata); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | return $out; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub WaitForStack { | ||
| 146 | my $self = shift; | ||
| 147 | my $stackname = $self->name; | ||
| 148 | my $result = $self->cfn->DescribeStacks(StackName => $stackname); | ||
| 149 | my $stack_status = $result->Stacks->[0]->StackStatus; | ||
| 150 | print "Stack Status: $stack_status\n"; | ||
| 151 | |||
| 152 | while ($stack_status =~ m/IN_PROGRESS$/){ | ||
| 153 | $result = $self->cfn->DescribeStacks(StackName => $stackname); | ||
| 154 | $stack_status = $result->Stacks->[0]->StackStatus; | ||
| 155 | print "Stack Status: $stack_status\n"; | ||
| 156 | sleep 15; | ||
| 157 | } | ||
| 158 | return $result->Stacks->[0]; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub assert_stack_version_ok { | ||
| 162 | my $self = shift; | ||
| 163 | my $cfn = Cfn->from_json($self->cfn->GetTemplate(StackName => $self->name)->TemplateBody); | ||
| 164 | |||
| 165 | die "No stack version found in deployment class. 'stack_version' statement is required!\n" unless ($self->origin->can('StackVersion')); | ||
| 166 | |||
| 167 | if ($cfn->MetadataItem('StackVersion')) { | ||
| 168 | if ($self->origin->StackVersion->Value < $cfn->MetadataItem('StackVersion')->Value) { | ||
| 169 | die "Your stack looks outdated! Are you using a version that is older than what is deployed to production?\nHas someone deployed a new version while you were working?\n"; | ||
| 170 | } | ||
| 171 | |||
| 172 | if ($self->origin->StackVersion->Value == $cfn->MetadataItem('StackVersion')->Value) { | ||
| 173 | die "You're almost done! But have not updated stack_version. Increase the version number and try again.\n"; | ||
| 174 | } | ||
| 175 | |||
| 176 | my $recommended = $cfn->MetadataItem('StackVersion')->Value + 1; | ||
| 177 | |||
| 178 | if ($self->origin->StackVersion->Value > $recommended) { | ||
| 179 | die "Ooops! stack_version is too ahead in the future. You should be using: " . $recommended . "\n"; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | else { | ||
| 183 | die "Ooops! stack_version is too ahead in the future. You should be using: 1\n" if ($self->origin->StackVersion->Value != 1); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | sub reset_stack_version { | ||
| 188 | my $self = shift; | ||
| 189 | |||
| 190 | if ($self->origin->can('StackVersion')) { | ||
| 191 | $self->origin->meta->remove_attribute('StackVersion'); | ||
| 192 | delete $self->origin->Metadata->{StackVersion}; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | 1; | ||
| 198 |