-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from fac/refactor
Complete refactor
- Loading branch information
Showing
15 changed files
with
660 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Metrics/BlockLength: | ||
Enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
source 'https://rubygems.org' do | ||
gem 'aws-sdk-states' | ||
end | ||
|
||
group :test do | ||
gem 'nokogiri' | ||
gem 'rspec' | ||
gem 'rspec-mocks' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
|
||
cd /usr/src/app && bundle exec ruby app.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require 'deployment/deployment' | ||
require 'deployment/events/fail_state_entered' | ||
require 'deployment/events/lambda_function_failed' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module Deployment | ||
class Deployment | ||
def initialize(aws_client, execution_arn) | ||
@aws_client = aws_client | ||
@execution_arn = execution_arn | ||
end | ||
|
||
def status | ||
@aws_client.describe_execution({ execution_arn: @execution_arn }).status | ||
end | ||
|
||
def running? | ||
status == 'RUNNING' | ||
end | ||
|
||
def succeeded? | ||
status == 'SUCCEEDED' | ||
end | ||
|
||
def aborted? | ||
status == 'ABORTED' | ||
end | ||
|
||
def timed_out? | ||
status == 'TIMED_OUT' | ||
end | ||
|
||
def failed? | ||
status == 'FAILED' | ||
end | ||
|
||
def failure_reason | ||
raise 'Deploy still runnning' if running? | ||
raise 'Deploy succeeded' if succeeded? | ||
|
||
event_type = fail_event.type | ||
|
||
case event_type | ||
when 'LambdaFunctionFailed' | ||
Events::LambdaFunctionFailed.new(fail_event).error | ||
when 'FailStateEntered' | ||
Events::FailStateEntered.new(fail_event, @execution_arn).error | ||
else | ||
"Uncaught failure. Please investigate here https://eu-west-1.console.aws.amazon.com/states/home?region=eu-west-1#/executions/details/#{@execution_arn}" | ||
end | ||
end | ||
|
||
private | ||
|
||
def fail_event | ||
# The penultimate event holds the failure reason | ||
@aws_client.get_execution_history( | ||
{ | ||
execution_arn: @execution_arn, | ||
max_results: 2, | ||
reverse_order: true | ||
} | ||
).events.last | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Deployment | ||
module Events | ||
class FailStateEntered | ||
def initialize(event, execution_arn) | ||
@event = event | ||
@execution_arn = execution_arn | ||
end | ||
|
||
def error | ||
if sidekiq_error? | ||
'Sidekiq workers failed to start or failed to stabilise.' | ||
else | ||
"Failure message: #{error_message}. Please investigate further here if required https://eu-west-1.console.aws.amazon.com/states/home?region=eu-west-1#/executions/details/#{@execution_arn}" | ||
end | ||
end | ||
|
||
private | ||
|
||
def error_message | ||
@error_message ||= JSON.parse(JSON.parse(@event.state_entered_event_details.input)['Error']['Cause'])['errorMessage'] | ||
end | ||
|
||
def in_progress? | ||
error_message.include? 'IN_PROGRESS' | ||
end | ||
|
||
def ecs_error? | ||
error_message.include? 'ECS' | ||
end | ||
|
||
def sidekiq_error? | ||
ecs_error? && in_progress? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Deployment | ||
module Events | ||
class LambdaFunctionFailed | ||
def initialize(event) | ||
@event = event | ||
end | ||
|
||
def error | ||
if preflight_checks_failed? && forward_deploy_check_failed? | ||
'Forward deploy check FAILED. No need to panic! '\ | ||
'This likely means your commit has already been deployed as part of a previous deploy. '\ | ||
'To confirm you can check whether your SHA is a parent commit to the currently deployed SHA. '\ | ||
'You can figure out the currently deployed SHA by following this guide https://www.notion.so/freeagent/Deployment-Runbooks-29796221387e40b7abbb217d7d33c4ac?pvs=4#3bfa2ab5d3ab4c33b7a46522027f94bb' | ||
else | ||
error_message | ||
end | ||
end | ||
|
||
private | ||
|
||
def error_message | ||
@error_message ||= JSON.parse(@event.lambda_function_failed_event_details.cause)['errorMessage'] | ||
end | ||
|
||
def preflight_checks_output | ||
error_message.lines[1] | ||
end | ||
|
||
def preflight_checks_failed? | ||
error_message.include? 'Pre flight checks failed' | ||
end | ||
|
||
def forward_deploy_check_result | ||
preflight_checks_output.match(/ForwardDeployCheck=>"(.*?)"/i).captures[0] | ||
end | ||
|
||
def forward_deploy_check_failed? | ||
forward_deploy_check_result == 'FAILED' | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.