lambit 0.0.1

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.
@@ -0,0 +1,62 @@
1
+ # encoding: UTF-8
2
+
3
+ module Lambit
4
+ module Aws
5
+ module CloudWatchEvents
6
+ class Rule
7
+ OPTIONS = [
8
+ :name,
9
+ :rule,
10
+ :schedule_expression,
11
+ :event_pattern,
12
+ :state,
13
+ :description,
14
+ :role_arn,
15
+ :targets
16
+ ].each { |option| attr_reader option }
17
+
18
+ attr_reader :client
19
+ attr_reader :options
20
+
21
+ def initialize(config)
22
+ @client = ::Aws::CloudWatchEvents::Client.new
23
+
24
+ OPTIONS.each do |option|
25
+ if config.has_key?(option)
26
+ instance_variable_set("@#{option}", config[option])
27
+ end
28
+ end
29
+ end
30
+
31
+ def options
32
+ options = {}
33
+
34
+ OPTIONS.each do |option|
35
+ value = self.send(option)
36
+ options[option] = value unless value.nil?
37
+ end
38
+ options
39
+ end
40
+
41
+ def put_rule
42
+ self.client.put_rule options
43
+ end
44
+
45
+ def delete_rule
46
+ opts = {name: options[:name]}
47
+ self.client.delete_rule opts
48
+ end
49
+
50
+ def put_targets
51
+ opts = {rule: options[:rule], targets: options[:targets]}
52
+ self.client.put_targets opts
53
+ end
54
+
55
+ def remove_targets
56
+ opts = {rule: options[:rule], ids: options[:targets].map{|i| i.map{|k,v| v if k == :id}.compact}.flatten}
57
+ self.client.remove_targets opts
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: UTF-8
2
+
3
+ module Lambit
4
+ module Aws
5
+ module Lambda
6
+ class Function
7
+ OPTIONS = [
8
+ :function_name,
9
+ :runtime,
10
+ :role,
11
+ :handler,
12
+ :code,
13
+ :description,
14
+ :timeout,
15
+ :memory_size,
16
+ :publish,
17
+ :vpc_config
18
+ ].each { |option| attr_reader option }
19
+
20
+ attr_reader :client
21
+
22
+ def initialize(config)
23
+ @client = ::Aws::Lambda::Client.new
24
+
25
+ OPTIONS.each do |option|
26
+ if config.has_key?(option)
27
+ instance_variable_set("@#{option}", config[option])
28
+ end
29
+ end
30
+ end
31
+
32
+ def options
33
+ options = {}
34
+
35
+ OPTIONS.each do |option|
36
+ value = self.send(option)
37
+ options[option] = value unless value.nil?
38
+ end
39
+ options
40
+ end
41
+
42
+ def create_function
43
+ self.client.create_function options
44
+ end
45
+
46
+ def delete_function
47
+ opts = {function_name: options[:function_name]}
48
+ self.client.delete_function opts
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,52 @@
1
+ # encoding: UTF-8
2
+
3
+ module Lambit
4
+ module Aws
5
+ module Lambda
6
+ class Permission
7
+ OPTIONS = [
8
+ :function_name,
9
+ :statement_id,
10
+ :action,
11
+ :principal,
12
+ :source_arn,
13
+ :source_account,
14
+ :event_source_token,
15
+ :qualifier
16
+ ].each { |option| attr_reader option }
17
+
18
+ attr_reader :client
19
+ attr_reader :options
20
+
21
+ def initialize(config)
22
+ @client = ::Aws::Lambda::Client.new
23
+
24
+ OPTIONS.each do |option|
25
+ if config.has_key?(option)
26
+ instance_variable_set("@#{option}", config[option])
27
+ end
28
+ end
29
+ end
30
+
31
+ def options
32
+ options = {}
33
+
34
+ OPTIONS.each do |option|
35
+ value = self.send(option)
36
+ options[option] = value unless value.nil?
37
+ end
38
+ options
39
+ end
40
+
41
+ def add
42
+ self.client.add_permission options
43
+ end
44
+
45
+ def remove
46
+ opts = {function_name: options[:function_name], statement_id: options[:statement_id]}
47
+ self.client.remove_permission opts
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,81 @@
1
+ # encoding: UTF-8
2
+
3
+ using Lambit::Common::HashHelper
4
+
5
+ module Lambit
6
+ module Aws
7
+ module S3
8
+ class BucketNotificationConfiguration
9
+ OPTIONS = [
10
+ :bucket,
11
+ :notification_configuration,
12
+ :use_accelerate_endpoint
13
+ ].each { |option| attr_reader option }
14
+
15
+ attr_reader :client
16
+ attr_reader :options
17
+
18
+ def initialize(config)
19
+ @client = ::Aws::S3::Client.new
20
+
21
+ OPTIONS.each do |option|
22
+ if config.has_key?(option)
23
+ instance_variable_set("@#{option}", config[option])
24
+ end
25
+ end
26
+ end
27
+
28
+ def options
29
+ options = {}
30
+
31
+ OPTIONS.each do |option|
32
+ value = self.send(option)
33
+ options[option] = value unless value.nil?
34
+ end
35
+ options
36
+ end
37
+
38
+ def notification_configuration_types
39
+ types = [
40
+ :topic_configurations,
41
+ :lambda_function_configurations,
42
+ :queue_configurations
43
+ ]
44
+ end
45
+
46
+ def add
47
+ config = self.client.get_bucket_notification_configuration(:bucket => options[:bucket])
48
+
49
+ notification_configuration_types.each do |type|
50
+ options_hash = options[:notification_configuration][type].to_a.map { |r| [r[:id], r] }.to_h
51
+ config_hash = config.send(type).to_a.map { |r| [r[:id], r.to_hash] }.to_h
52
+ options[:notification_configuration][type] = config_hash.merge(options_hash).values
53
+ end
54
+
55
+ put(options)
56
+ end
57
+
58
+ def remove
59
+ config = self.client.get_bucket_notification_configuration(:bucket => options[:bucket])
60
+
61
+ notification_configuration_types.each do |type|
62
+ options_hash = options[:notification_configuration][type].to_a.map { |r| [r[:id], r] }.to_h
63
+ config_hash = config.send(type).to_a.map { |r| [r[:id], r.to_hash] }.to_h
64
+ options[:notification_configuration][type] = config_hash.difference(options_hash).values
65
+ end
66
+
67
+ put(options)
68
+ end
69
+
70
+ def put(opts)
71
+ r = self.client.put_bucket_notification_configuration(opts)
72
+ notification_configuration_types.each do |type|
73
+ puts self.client.get_bucket_notification_configuration(:bucket => opts[:bucket]).send type
74
+ end if Lambit.is_debug?
75
+
76
+ r
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative 'commands/common'
4
+ require_relative 'commands/subcommand'
5
+ require_relative 'commands/build'
6
+ require_relative 'commands/lambdas'
@@ -0,0 +1,128 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'json'
4
+ require 'fileutils'
5
+
6
+ using Lambit::Common::StringHelper
7
+
8
+ module Lambit::Commands
9
+ module Build
10
+ def self.run(cmd, opts, args)
11
+ handler = handler_for cmd
12
+ output, exitstatus = handler.run opts, args
13
+
14
+ # Send Subcommand output to STDOUT if :debug
15
+ if Lambit.is_debug?
16
+ puts output unless output.nil? || output.empty?
17
+ end
18
+
19
+ # Return exit status
20
+ exitstatus
21
+ end
22
+
23
+ def self.normalize_cmd(cmd)
24
+ cmd.to_s.downcase
25
+ end
26
+
27
+ def self.handler_for(cmd)
28
+ if @handlers
29
+ @handlers[normalize_cmd(cmd)]
30
+ else
31
+ raise 'Uh Oh!'
32
+ end
33
+ end
34
+
35
+ def self.register_handler_for(cmd, handler=nil)
36
+ normalized_cmd = normalize_cmd(cmd)
37
+ handler = self.const_get(normalized_cmd.classify) if handler.nil?
38
+
39
+ @handlers ||= {}
40
+ @handlers[normalized_cmd] = handler
41
+ end
42
+
43
+ class PackageTemplatesCommand < Lambit::Commands::Common::Base
44
+ attr_reader :workspace
45
+ attr_reader :project_path
46
+ attr_reader :tmpl_name
47
+ attr_reader :tmpl_config
48
+
49
+ def initialize(opts, args)
50
+ super
51
+ @workspace = opts['workspace']
52
+ @project_path = opts['project_path']
53
+ @tmpl_name = args[0]
54
+ @tmpl_config = args[1]
55
+ end
56
+
57
+ def command
58
+ tmpl = JSON.parse(File.read("#{self.project_path}/templates/#{self.tmpl_name}"))
59
+ tmpl.merge!(self.tmpl_config) unless self.tmpl_config.nil?
60
+
61
+ begin
62
+ File.open("#{self.workspace}/#{self.tmpl_name}",'w') do |f|
63
+ f.write(tmpl.to_json)
64
+ end
65
+ @exitstatus = 0
66
+ rescue Exception => e
67
+ if Lambit.is_debug?
68
+ Lambit.logger.error e
69
+ else
70
+ Lambit.logger.error e.message
71
+ end
72
+ @exitstatus = 1
73
+ end
74
+
75
+ return '', self.exitstatus
76
+ end
77
+ end
78
+
79
+ register_handler_for :package_templates_command
80
+
81
+ class Base < Lambit::Commands::Subcommands::Base
82
+ attr_reader :workspace
83
+
84
+ def initialize(opts, args)
85
+ super
86
+ @workspace = opts['workspace']
87
+ end
88
+ end
89
+
90
+ class PackagePipCommand < Base
91
+ attr_reader :pip
92
+
93
+ def initialize(opts, args)
94
+ super
95
+ @pip = args[0]
96
+ end
97
+
98
+ def subcommand
99
+ @subcommand = "pip install #{self.pip} -q -t #{workspace}/."
100
+ end
101
+ end
102
+
103
+ register_handler_for :package_pip_command
104
+
105
+ class PackageSourceCommand < Base
106
+ attr_reader :project_path
107
+
108
+ def initialize(opts, args)
109
+ super
110
+ @project_path = opts['project_path']
111
+ end
112
+
113
+ def subcommand
114
+ @subcommand = "cp -r #{project_path}/function/* #{workspace}"
115
+ end
116
+ end
117
+
118
+ register_handler_for :package_source_command
119
+
120
+ class CompressPackageCommand < Base
121
+ def subcommand
122
+ @subcommand = "cd #{workspace} && zip -r package.zip *"
123
+ end
124
+ end
125
+
126
+ register_handler_for :compress_package_command
127
+ end
128
+ end
@@ -0,0 +1,92 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'time'
4
+
5
+ using Lambit::Common::StringHelper
6
+
7
+ module Lambit::Commands
8
+ module Common
9
+ def self.run(cmd, opts, args)
10
+ handler = handler_for cmd
11
+ output, exitstatus = handler.run opts, args
12
+
13
+ # Send Subcommand output to STDOUT if :debug
14
+ if Lambit.is_debug?
15
+ puts output unless output.nil? || output.empty?
16
+ end
17
+
18
+ # Return exit status
19
+ exitstatus
20
+ end
21
+
22
+ def self.normalize_cmd(cmd)
23
+ cmd.to_s.downcase
24
+ end
25
+
26
+ def self.handler_for(cmd)
27
+ if @handlers
28
+ @handlers[normalize_cmd(cmd)]
29
+ else
30
+ raise 'Uh Oh!'
31
+ end
32
+ end
33
+
34
+ def self.register_handler_for(cmd, handler=nil)
35
+ normalized_cmd = normalize_cmd(cmd)
36
+ handler = self.const_get(normalized_cmd.classify) if handler.nil?
37
+
38
+ @handlers ||= {}
39
+ @handlers[normalized_cmd] = handler
40
+ end
41
+
42
+ class Base
43
+ attr_reader :run
44
+ attr_reader :dry_run
45
+ attr_reader :exitstatus
46
+
47
+ def initialize(opts, args)
48
+ @dry_run = opts['dry-run']
49
+ @exitstatus = 1
50
+ end
51
+
52
+ def self.run(opts, args)
53
+ output, exitstatus = self.new(opts, args).execute
54
+ return output, exitstatus
55
+ end
56
+
57
+ def command
58
+ return '', self.exitstatus
59
+ end
60
+
61
+ def execute
62
+ command
63
+ end
64
+ end
65
+
66
+ class CreateWorkspaceCommand < Base
67
+ attr_reader :workspace
68
+
69
+ def initialize(opts, args)
70
+ super
71
+ @workspace = opts['workspace']
72
+ end
73
+
74
+ def command
75
+ begin
76
+ FileUtils.mkdir_p self.workspace
77
+ @exitstatus = 0
78
+ rescue Exception => e
79
+ if Lambit.is_debug?
80
+ Lambit.logger.error e
81
+ else
82
+ Lambit.logger.error e.message
83
+ end
84
+ @exitstatus = 1
85
+ end
86
+ return '', self.exitstatus
87
+ end
88
+ end
89
+
90
+ register_handler_for :create_workspace_command
91
+ end
92
+ end