Class: DeployHelper
- Inherits:
-
Object
- Object
- DeployHelper
- Defined in:
- lib/deploy_helper.rb
Overview
Utility class for running rake methods
Direct Known Subclasses
Instance Attribute Summary collapse
-
#aws_access_key_id ⇒ Object
readonly
Returns the value of attribute aws_access_key_id.
-
#aws_configuration ⇒ Object
readonly
Returns the value of attribute aws_configuration.
-
#aws_secret_access_key ⇒ Object
readonly
Returns the value of attribute aws_secret_access_key.
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#function_name ⇒ Object
readonly
Returns the value of attribute function_name.
-
#lambda_client ⇒ Object
readonly
Returns the value of attribute lambda_client.
-
#lambda_config ⇒ Object
readonly
Returns the value of attribute lambda_config.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#travis_branch ⇒ Object
readonly
Returns the value of attribute travis_branch.
-
#yaml ⇒ Object
readonly
Returns the value of attribute yaml.
Instance Method Summary collapse
- #add_cron ⇒ Object
- #add_event_source ⇒ Object
- #configured? ⇒ Boolean
-
#initialize ⇒ DeployHelper
constructor
A new instance of DeployHelper.
- #name_matches_branch?(name, branch) ⇒ Boolean
- #update_event ⇒ Object
- #update_lambda_configuration ⇒ Object
Constructor Details
#initialize ⇒ DeployHelper
Returns a new instance of DeployHelper.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/deploy_helper.rb', line 20 def initialize @travis_branch = ENV["TRAVIS_BRANCH"].upcase @travis_branch = ["MAIN", "MASTER"].include?(@travis_branch) ? "PRODUCTION" : @travis_branch @aws_access_key_id = ENV["AWS_ACCESS_KEY_ID_#{travis_branch}"] @aws_secret_access_key = ENV["AWS_SECRET_ACCESS_KEY_#{travis_branch}"] @yaml = YAML.safe_load(File.read(".travis.yml")) @lambda_config = yaml["deploy"].find { |conf| name_matches_branch?(conf["function_name"], travis_branch) } @region = @lambda_config["region"] @function_name = @lambda_config["function_name"] @aws_configuration = { region: region, access_key_id: aws_access_key_id, secret_access_key: aws_secret_access_key } p("using configuration: ", aws_configuration) p("lambda config: ", lambda_config) @lambda_client = Aws::Lambda::Client.new(aws_configuration) if configured? end |
Instance Attribute Details
#aws_access_key_id ⇒ Object (readonly)
Returns the value of attribute aws_access_key_id.
7 8 9 |
# File 'lib/deploy_helper.rb', line 7 def aws_access_key_id @aws_access_key_id end |
#aws_configuration ⇒ Object (readonly)
Returns the value of attribute aws_configuration.
7 8 9 |
# File 'lib/deploy_helper.rb', line 7 def aws_configuration @aws_configuration end |
#aws_secret_access_key ⇒ Object (readonly)
Returns the value of attribute aws_secret_access_key.
7 8 9 |
# File 'lib/deploy_helper.rb', line 7 def aws_secret_access_key @aws_secret_access_key end |
#event ⇒ Object (readonly)
Returns the value of attribute event.
7 8 9 |
# File 'lib/deploy_helper.rb', line 7 def event @event end |
#function_name ⇒ Object (readonly)
Returns the value of attribute function_name.
7 8 9 |
# File 'lib/deploy_helper.rb', line 7 def function_name @function_name end |
#lambda_client ⇒ Object (readonly)
Returns the value of attribute lambda_client.
7 8 9 |
# File 'lib/deploy_helper.rb', line 7 def lambda_client @lambda_client end |
#lambda_config ⇒ Object (readonly)
Returns the value of attribute lambda_config.
7 8 9 |
# File 'lib/deploy_helper.rb', line 7 def lambda_config @lambda_config end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
7 8 9 |
# File 'lib/deploy_helper.rb', line 7 def region @region end |
#travis_branch ⇒ Object (readonly)
Returns the value of attribute travis_branch.
7 8 9 |
# File 'lib/deploy_helper.rb', line 7 def travis_branch @travis_branch end |
#yaml ⇒ Object (readonly)
Returns the value of attribute yaml.
7 8 9 |
# File 'lib/deploy_helper.rb', line 7 def yaml @yaml end |
Instance Method Details
#add_cron ⇒ Object
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 |
# File 'lib/deploy_helper.rb', line 105 def add_cron ## create the event events_client = Aws::CloudWatchEvents::Client.new(aws_configuration) schedule_expression = event["schedule_expression"] rule_name = "#{function_name}-rule" p("rule_name: ", rule_name, "schedule_expression: ", schedule_expression) events_client.put_rule(name: rule_name, schedule_expression: schedule_expression) ## next we have to connect the event to the lambda ## the first step is to get the lambda return "missing function_name" unless function_name p("getting lambda with function name", function_name) lambda_resp = lambda_client.get_function(function_name: function_name).configuration arn = lambda_resp.function_arn ## next figure out if the lambda already has granted cloudwatch ## permission to invoke begin policy_resp = lambda_client.get_policy(function_name: function_name) if policy_resp.policy.include?("#{function_name}-permission") p("lambda already has permission") else add_policy = true end rescue Aws::Lambda::Errors::ResourceNotFoundException add_policy = true p("no policy") end ## if not, add permission to invoke if add_policy = lambda_client.({ function_name: function_name, principal: "events.amazonaws.com", statement_id: "#{function_name}-permission", action: "lambda:InvokeFunction" }) p("permission: ", ) end ## finally we can tell the event to invoke the lambda target_id = "#{function_name}-lambda" p("putting targets ", "rule: ", rule_name, "target_id: ", target_id, "arn: ", arn) events_client.put_targets(rule: rule_name, targets: [{ id: target_id, arn: arn }]) end |
#add_event_source ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/deploy_helper.rb', line 89 def add_event_source existing_events = lambda_client.list_event_source_mappings({ function_name: function_name }).event_source_mappings existing_events.each do |existing_event| p("deleting event with uuid: ", existing_event.uuid, "and arn: ", existing_event.event_source_arn) lambda_client.delete_event_source_mapping({ uuid: existing_event.uuid }) end event_to_create = event.map { |k, v| [k.to_sym, v] }.to_h event_to_create[:function_name] = function_name p("creating event: ", event_to_create) create_resp = lambda_client.create_event_source_mapping(event_to_create) p("created: ", create_resp) end |
#configured? ⇒ Boolean
39 40 41 |
# File 'lib/deploy_helper.rb', line 39 def configured? aws_access_key_id && aws_secret_access_key && region end |
#name_matches_branch?(name, branch) ⇒ Boolean
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/deploy_helper.rb', line 43 def name_matches_branch?(name, branch) downcase_name = name.downcase downcase_branch = branch.downcase variants = [ ["dev", "development"], ["qa"], ["main", "master", "production", "prod"], ] variants.any? do |group| group.any? { |variant| downcase_name.include?(variant) }\ && group.any? { |variant| downcase_branch.include?(variant) } end end |
#update_event ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/deploy_helper.rb', line 75 def update_event unless lambda_config["event"] p("no event config") return nil end @event = lambda_config["event"] if event["event_source_arn"] add_event_source elsif event["schedule_expression"] add_cron end end |
#update_lambda_configuration ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/deploy_helper.rb', line 57 def update_lambda_configuration unless configured? && lambda_config p("insufficient configuration") return nil end updated_lambda_configuration = { function_name: function_name, vpc_config: lambda_config["vpc_config"], environment: lambda_config["environment"], layers: lambda_config["layers"] } updated_lambda_configuration[:function_name] = function_name p("updating_function_configuration with: ", updated_lambda_configuration) update_configuration_resp = lambda_client.update_function_configuration(updated_lambda_configuration) p("update_configuration_resp: ", update_configuration_resp) end |