nypl_ruby_util 0.0.1 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a102fc9301f667018c0ae2c9f2fc26c5525f8ac2fad91b35b980da52731b67b2
4
- data.tar.gz: 4a3ac1f699931ca2432196a3c4997948ded44e6e470ec3bd958cb639b0731eca
3
+ metadata.gz: bd76f2db6e4d52927503aec7dbe8a5e50c5e0fea01bafe5b7caf184e814e3ca4
4
+ data.tar.gz: 2c59a7d630e18ee839780b450eab83370b32d9231e5c23c244ffe795a0323c3f
5
5
  SHA512:
6
- metadata.gz: 9cb6c355ffc6d948bd800e7e6b69267115f67f49cb644aa14212d296556a0e95acdd2d8004e88f31f9671e38d4eca6a8ce349a7a18bc52fe694c990e6f6a561c
7
- data.tar.gz: 59e711f7bebcacfe8dc24520ec8c2bcde349d2bffabc1ad40386622cf55096bdde87ee7aff4f088f70408d76466e67d1a0be634f7d96457ba5265b3304b15e70
6
+ metadata.gz: 79f01aa5a487725cc37793f1b5b4656bf9e969a8e0e234bf2c6d9927c008107c71f161630e1539037a1974663c4df95d5f415d80089aaaaa19512257b771a3dd
7
+ data.tar.gz: 3d3d398b64516e59736d254ff2c709aec0980b2682401216e945f78be77bd6f2f1b8f3162025bf177fc85c6d631caf08da5ff42560861caa00d1d08a9c37c0a4
@@ -0,0 +1,152 @@
1
+ require 'aws-sdk-lambda'
2
+ require 'aws-sdk-cloudwatchevents'
3
+ require 'yaml'
4
+
5
+ # Utility class for running rake methods
6
+ class DeployHelper
7
+ attr_reader(
8
+ :travis_branch,
9
+ :aws_access_key_id,
10
+ :aws_secret_access_key,
11
+ :aws_configuration,
12
+ :region,
13
+ :lambda_client,
14
+ :yaml,
15
+ :lambda_config,
16
+ :function_name,
17
+ :event
18
+ )
19
+
20
+ def initialize
21
+ @travis_branch = ENV['TRAVIS_BRANCH'].upcase
22
+ @travis_branch = ['MAIN', 'MASTER'].include?(@travis_branch) ? 'PRODUCTION' : @travis_branch
23
+ @aws_access_key_id = ENV["AWS_ACCESS_KEY_ID_#{travis_branch}"]
24
+ @aws_secret_access_key = ENV["AWS_SECRET_ACCESS_KEY_#{travis_branch}"]
25
+ @yaml = YAML.safe_load(File.read('.travis.yml'))
26
+ @lambda_config = yaml['deploy'].find { |conf| name_matches_branch?(conf['function_name'], travis_branch) }
27
+ @region = @lambda_config['region']
28
+ @function_name = @lambda_config['function_name']
29
+ @aws_configuration = {
30
+ region: region,
31
+ access_key_id: aws_access_key_id,
32
+ secret_access_key: aws_secret_access_key
33
+ }
34
+ p 'using configuration: ', aws_configuration
35
+ p 'lambda config: ', lambda_config
36
+ @lambda_client = Aws::Lambda::Client.new(aws_configuration) if configured?
37
+ end
38
+
39
+ def configured?
40
+ aws_access_key_id && aws_secret_access_key && region
41
+ end
42
+
43
+ def name_matches_branch?(name, branch)
44
+ downcase_name = name.downcase
45
+ downcase_branch = branch.downcase
46
+ variants = [
47
+ ['dev', 'development'],
48
+ ['qa'],
49
+ ['main', 'master', 'production', 'prod'],
50
+ ]
51
+ variants.any? do |group|
52
+ group.any? { |variant| downcase_name.include? variant }\
53
+ && group.any? { |variant| downcase_branch.include?(variant) }
54
+ end
55
+ end
56
+
57
+ def update_lambda_configuration
58
+ unless configured? && lambda_config
59
+ p 'insufficient configuration'
60
+ return nil
61
+ end
62
+
63
+ updated_lambda_configuration = {
64
+ function_name: function_name,
65
+ vpc_config: lambda_config['vpc_config'],
66
+ environment: lambda_config['environment'],
67
+ layers: lambda_config['layers']
68
+ }
69
+ updated_lambda_configuration[:function_name] = function_name
70
+ p 'updating_function_configuration with: ', updated_lambda_configuration
71
+ update_configuration_resp = lambda_client.update_function_configuration(updated_lambda_configuration)
72
+ p 'update_configuration_resp: ', update_configuration_resp
73
+ end
74
+
75
+ def update_event
76
+ unless lambda_config['event']
77
+ p 'no event config'
78
+ return nil
79
+ end
80
+
81
+ @event = lambda_config['event']
82
+ if event['event_source_arn']
83
+ add_event_source
84
+ elsif event['schedule_expression']
85
+ add_cron
86
+ end
87
+ end
88
+
89
+ def add_event_source
90
+ existing_events = lambda_client.list_event_source_mappings({
91
+ function_name: function_name
92
+ }).event_source_mappings
93
+
94
+ existing_events.each do |existing_event|
95
+ p 'deleting event with uuid: ', existing_event.uuid, 'and arn: ', existing_event.event_source_arn
96
+ lambda_client.delete_event_source_mapping({ uuid: existing_event.uuid })
97
+ end
98
+ event_to_create = event.map { |k, v| [k.to_sym, v] }.to_h
99
+ event_to_create[:function_name] = function_name
100
+ p 'creating event: ', event_to_create
101
+ create_resp = lambda_client.create_event_source_mapping(event_to_create)
102
+ p 'created: ', create_resp
103
+ end
104
+
105
+ def add_cron
106
+ ## create the event
107
+ events_client = Aws::CloudWatchEvents::Client.new(aws_configuration)
108
+ schedule_expression = event['schedule_expression']
109
+ rule_name = "#{function_name}-rule"
110
+ p 'rule_name: ', rule_name, 'schedule_expression: ', schedule_expression
111
+ events_client.put_rule(name: rule_name, schedule_expression: schedule_expression)
112
+
113
+ ## next we have to connect the event to the lambda
114
+ ## the first step is to get the lambda
115
+
116
+ return 'missing function_name' unless function_name
117
+
118
+ p 'getting lambda with function name', function_name
119
+ lambda_resp = lambda_client.get_function(function_name: function_name).configuration
120
+ arn = lambda_resp.function_arn
121
+
122
+ ## next figure out if the lambda already has granted cloudwatch
123
+ ## permission to invoke
124
+ begin
125
+ policy_resp = lambda_client.get_policy(function_name: function_name)
126
+ unless policy_resp.policy.include?("#{function_name}-permission")
127
+ add_policy = true
128
+ else
129
+ p 'lambda already has permission'
130
+ end
131
+ rescue Aws::Lambda::Errors::ResourceNotFoundException
132
+ add_policy = true
133
+ p 'no policy'
134
+ end
135
+
136
+ ## if not, add permission to invoke
137
+ if add_policy
138
+ permission = lambda_client.add_permission({
139
+ function_name: function_name,
140
+ principal: 'events.amazonaws.com',
141
+ statement_id: "#{function_name}-permission",
142
+ action: 'lambda:InvokeFunction'
143
+ })
144
+ p 'permission: ', permission
145
+ end
146
+
147
+ ## finally we can tell the event to invoke the lambda
148
+ target_id = "#{function_name}-lambda"
149
+ p 'putting targets ', 'rule: ', rule_name, 'target_id: ', target_id, 'arn: ', arn
150
+ events_client.put_targets(rule: rule_name, targets: [{ id: target_id, arn: arn }])
151
+ end
152
+ end
@@ -18,9 +18,8 @@ class KinesisClient
18
18
  end
19
19
 
20
20
  def <<(json_message)
21
- p '<< ', config[:schema_string], avro
22
21
  if config[:schema_string]
23
- message = avro.encode(json_message)
22
+ message = avro.encode(json_message, false)
24
23
  else
25
24
  message = json_message
26
25
  end
@@ -39,7 +38,7 @@ class KinesisClient
39
38
  if resp.successful?
40
39
  return_hash["code"] = "200"
41
40
  return_hash["message"] = json_message, resp
42
- $logger.info("Message sent to HoldRequestResult #{json_message}, #{resp}") if $logger
41
+ $logger.info("Message sent to #{config[:stream_name]} #{json_message}, #{resp}") if $logger
43
42
  else
44
43
  $logger.error("message" => "FAILED to send message to HoldRequestResult #{json_message}, #{resp}.") if $logger
45
44
  raise NYPLError.new json_message, resp
data/lib/nypl_avro.rb CHANGED
@@ -32,6 +32,7 @@ class NYPLAvro
32
32
  def encode(decoded_data, base64 = true)
33
33
  bin_encoder = Avro::IO::DatumWriter.new(@schema)
34
34
  buffer = StringIO.new
35
+ buffer.set_encoding('UTF-8')
35
36
  encoder = Avro::IO::BinaryEncoder.new(buffer)
36
37
 
37
38
  begin
@@ -6,6 +6,7 @@ require_relative 'directory'
6
6
  require_relative 'nypl_avro'
7
7
  require_relative 'errors'
8
8
  require_relative 'kinesis_client'
9
+ require_relative 'deploy_helper'
9
10
 
10
11
  class NYPLRubyUtil
11
12
  class SierraApiClient < SierraApiClient
@@ -25,4 +26,7 @@ class NYPLRubyUtil
25
26
 
26
27
  class KinesisClient < KinesisClient
27
28
  end
29
+
30
+ class DeployHelper < DeployHelper
31
+ end
28
32
  end
metadata CHANGED
@@ -1,21 +1,120 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nypl_ruby_util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Appel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-13 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2020-09-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: avro
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.10.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.10.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-kinesis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.26.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.26.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk-kms
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.36.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.36.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: aws-sdk-lambda
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: aws-sdk-cloudwatchevents
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: nypl_log_formatter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.1.3
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.1.3
97
+ - !ruby/object:Gem::Dependency
98
+ name: nypl_sierra_api_client
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 1.0.3
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 1.0.3
13
111
  description: A repository of common utilities for NYPL Ruby application
14
112
  email:
15
113
  executables: []
16
114
  extensions: []
17
115
  extra_rdoc_files: []
18
116
  files:
117
+ - lib/deploy_helper.rb
19
118
  - lib/directory.rb
20
119
  - lib/errors.rb
21
120
  - lib/kinesis_client.rb
@@ -23,7 +122,7 @@ files:
23
122
  - lib/nypl_avro.rb
24
123
  - lib/nypl_ruby_util.rb
25
124
  - lib/platform_api_client.rb
26
- homepage:
125
+ homepage: https://github.com/NYPL/NYPLRubyUtil
27
126
  licenses:
28
127
  - MIT
29
128
  metadata: {}
@@ -42,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
141
  - !ruby/object:Gem::Version
43
142
  version: '0'
44
143
  requirements: []
45
- rubygems_version: 3.0.3
144
+ rubygems_version: 3.2.3
46
145
  signing_key:
47
146
  specification_version: 4
48
147
  summary: A repository of common utilities for NYPL Ruby application