ruby_lambda 0.3.2 → 0.4.0

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: fa6c0abf94783204e4b49dc597fca614b23a6286764235f0841434ae30ae02dc
4
- data.tar.gz: 95d98d3d9c3f176f78a2671a4b8f454af042c976879d35e5343ff59981b58738
3
+ metadata.gz: 3d14b2ea8a2f48a998ee212328e73e8a74a2f4512cce87e1297abd074d33d71f
4
+ data.tar.gz: 5a158755424e2b161fc90ff60369c3ac6922d304655ec8e34dc3753ed8dd68c0
5
5
  SHA512:
6
- metadata.gz: 59a9afabde3113b52213058f3108c6a336ce892b47f4aef335b534d452c73c714ebe69062f3b6c90d61522022e5582787c240419140ccaa9854a783da3872a06
7
- data.tar.gz: 2ae2922aad65e325ca29c3de58cd2098fe40e6a0584d2493585b6f90f7b7b9ede838211efdd8fc661dca0553977d8bff69c4cdde2cf976e5378f7a16ffaa1b0e
6
+ metadata.gz: 5c4ccbe29359ea91eaed3b7950cef7d83eff68fe0b7fa49a55e743e164b17eda481ca7b530c660ec5359c481dda7a6d6dd41ae02fa4f666dd1f78e0f428b1679
7
+ data.tar.gz: 03dce8db39c13a16fa93f7c3ac5f768a2d97c96e719fd870b06218d228b845d7b807f1412eecdd7632f90e6ac9bc7184220339da8718ba5cd6c1e4588878b9fc
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby_lambda (0.3.2)
4
+ ruby_lambda (0.4.0)
5
5
  awesome_print (~> 1.8.0)
6
+ rubyzip (~> 1.2)
6
7
  thor (~> 0.19)
7
8
 
8
9
  GEM
@@ -60,6 +61,7 @@ GEM
60
61
  rspec-support (~> 3.8.0)
61
62
  rspec-support (3.8.0)
62
63
  ruby_dep (1.5.0)
64
+ rubyzip (1.2.2)
63
65
  shellany (0.0.1)
64
66
  simplecov (0.16.1)
65
67
  docile (~> 1.1)
data/README.md CHANGED
@@ -12,15 +12,15 @@ RubyLambda is a toolset for developing and deploying serverless Ruby apps in AWS
12
12
  The main available commands.
13
13
 
14
14
  ```
15
- $ ruby-lambda init
16
- $ ruby-lambda execute
17
- $ ruby-lambda build
15
+ $ ruby-lambda init # Within the current directory scaffold files needed for a baisc lambda function
16
+ $ ruby-lambda execute # Invokes the function locally offline
17
+ $ ruby-lambda build # Build your function into a local ready to deploy zip file
18
18
  $ ruby-lambda deploy
19
19
  ```
20
20
 
21
21
  ### Commands
22
22
 
23
- #### ruby-lambda init
23
+ #### ruby-lambda init
24
24
  ```
25
25
  $ ruby-lambda init
26
26
  ```
@@ -30,9 +30,9 @@ Initializes the `.gitignore`, `config.yml`, `env`, `event.json`, `lambda_functio
30
30
  * `config.yml` contains some default configuration for your function.
31
31
  * `env` will be renamed to `.env` after the init command runs, it will contain `AWS_ACCESS_KEY` and `AWS_SECRET_ACCESS_KEY`. You will need these to be able to deploy your function.
32
32
 
33
- Please have a read of the `config.yml` and update any of the default configuration to better suit your function.
33
+ Please have a read of the `config.yml` and update any of the default configuration to better suit your function to AWS.
34
34
 
35
- #### ruby-lambda execute
35
+ #### ruby-lambda execute
36
36
  ```
37
37
  $ ruby-lambda execute
38
38
  ```
@@ -48,7 +48,7 @@ Options:
48
48
  * `$ ruby-lambda execute -c=config.yml`
49
49
  * `$ ruby-lambda execute -H=lambda_function.handler`
50
50
 
51
- The handler function is the function AWS Lambda will invoke / run in response to an event. AWS Lambda uses the event argument to pass in event data to the handler. If the `handler` flas is passed with execute, this will take precedence over the handler function defined within the `config.yml`
51
+ The handler function is the function AWS Lambda will invoke / run in response to an event. AWS Lambda uses the event argument to pass in event data to the handler. If the `handler` flag is passed with execute, this will take precedence over the handler function defined within the `config.yml`
52
52
 
53
53
  ```ruby
54
54
  def handler(event:, context:)
@@ -58,6 +58,31 @@ end
58
58
 
59
59
  The `execute` command gets the values stored in the `event.json` file and passes them to your handler function.
60
60
 
61
+ #### ruby-lambda build
62
+ ```
63
+ $ ruby-lambda build
64
+ ```
65
+ This command will create a zipped file ready to be published on Lambda
66
+
67
+ ```
68
+ Options:
69
+ -n, [--native-extensions], [--no-native-extensions]
70
+ -q, [--quiet], [--no-quiet]
71
+ ```
72
+
73
+ All output zipped will in the builds folder within the project root - the build folder will be created if one does not already exists.
74
+
75
+
76
+ ## Roadmap
77
+ Below is the roadmap to version 1
78
+
79
+ - [ ] Add a way to deploy directly to AWS lambda
80
+ - [x] Add ablility to execute the function offline
81
+ - [x] Add json file or options to be passed to execute function
82
+ - [x] Add a way to build files in to zips ready to be deployed to lambda
83
+ - [x] Add the building and zipping of native extentions ready for the lambda environment using docker
84
+
85
+
61
86
  ## Development
62
87
 
63
88
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -74,4 +99,5 @@ The gem is available as open source under the terms of the [MIT License](https:/
74
99
 
75
100
  ## Code of Conduct
76
101
 
77
- Everyone interacting in the RubyLambda project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/cookieshq/ruby_lambda/blob/master/CODE_OF_CONDUCT.md).
102
+ Everyone interacting in the RubyLambda project’s codebases and issue trackers is expected to follow the [code of conduct](https://github.com/cookieshq/ruby_lambda/blob/master/CODE_OF_CONDUCT.md).
103
+
data/bin/ruby-lambda CHANGED
@@ -3,18 +3,23 @@
3
3
  require "#{::File.dirname(__FILE__)}/../lib/ruby_lambda"
4
4
 
5
5
  class RubyLambdaCLI < Thor
6
+ class_option :quiet, type: :boolean, default: false, aliases: :q
7
+
6
8
  map '-v' => :version, '--version' => :version
7
9
  desc '-v (--versions)', 'Outputs the version of Ruby_Lambda.'
8
10
  def version
9
11
  puts RubyLambda::VERSION
10
12
  end
11
13
 
12
- desc 'init', 'Uses the current directory as a lambda ruby project (creates `main.rb`, `.ruby-version`, and `Gemfile`).'
14
+ desc 'init', 'Within the current directory scaffold files needed for a baisc lambda function'
15
+ long_desc <<-LONGDESC
16
+ Uses the current directory as a lambda ruby project (creates `main.rb`, `.ruby-version`, and `Gemfile`).
17
+ LONGDESC
13
18
  def init
14
- RubyLambda::Init.new(FileUtils.pwd).run
19
+ RubyLambda::Init.new(FileUtils.pwd).run(mute: options[:quiet])
15
20
  end
16
21
 
17
- desc 'execute', 'Invokes the function locally'
22
+ desc 'execute', 'Invokes the function locally offline'
18
23
  long_desc <<-LONGDESC
19
24
  This command is used to invoke / run the function locally
20
25
 
@@ -28,6 +33,20 @@ class RubyLambdaCLI < Thor
28
33
  def execute
29
34
  RubyLambda::Execute.new(FileUtils.pwd, options).run
30
35
  end
36
+
37
+ desc 'build', 'Builds your function into a local ready to deploy zip file'
38
+ long_desc <<-LONGDESC
39
+ This command will create a zipped file ready to be published on Lambda
40
+
41
+ All output zips will be in the folder builds
42
+
43
+ With -n / --native-extensions flag to pass build gems with native extensions
44
+ LONGDESC
45
+
46
+ option :native_extensions, type: :boolean, default: false, aliases: :n
47
+ def build
48
+ RubyLambda::Build.new(FileUtils.pwd, options).run(mute: options[:quiet])
49
+ end
31
50
  end
32
51
 
33
52
  RubyLambdaCLI.start(ARGV)
data/lib/ruby_lambda.rb CHANGED
@@ -3,11 +3,13 @@ require 'thor'
3
3
  require 'fileutils'
4
4
  require 'awesome_print'
5
5
  require 'yaml'
6
+ require 'zip'
6
7
 
7
8
  require 'ruby_lambda/version'
8
9
  require 'ruby_lambda/error'
9
10
  require 'ruby_lambda/init'
10
11
  require 'ruby_lambda/execute'
12
+ require 'ruby_lambda/build'
11
13
 
12
14
  module RubyLambda
13
15
  end
@@ -0,0 +1,120 @@
1
+ module RubyLambda
2
+ class Build
3
+ def initialize(current_directory, options = {"native_extensions"=>false})
4
+ @current_directory = current_directory
5
+ @shell = Thor::Base.shell.new
6
+ @options = options
7
+ end
8
+
9
+ def run(mute: false)
10
+ @mute = mute
11
+
12
+ if @options[:native_extensions]
13
+ unless which('docker')
14
+ @shell.say 'Can not find docker, you need to install docker if you want to build with native extensions', :red
15
+ return
16
+ end
17
+
18
+ setup_gems_with_native_extensions
19
+ else
20
+ setup_gems
21
+ end
22
+
23
+ @shell.say('Setting up files to build', :yellow) unless @mute
24
+ FileUtils.mkdir_p "#{@current_directory}/builds"
25
+
26
+ @output_file = "#{@current_directory}/#{generate_build_name}"
27
+
28
+ @shell.say('Adding files to zip', :yellow) unless @mute
29
+ write
30
+ end
31
+
32
+ private
33
+
34
+ def setup_gems
35
+ Dir.chdir(@current_directory) do
36
+ Bundler.with_clean_env do
37
+ @shell.say('Installing gems for deployment', :yellow) unless @mute
38
+ `bundle install`
39
+ `bundle install --deployment`
40
+ end
41
+ end
42
+ end
43
+
44
+ def setup_gems_with_native_extensions
45
+
46
+ Dir.chdir(@current_directory) do
47
+ Bundler.with_clean_env do
48
+ @shell.say('Installing gems for deployment', :yellow) unless @mute
49
+ `docker run -v #{@current_directory}:#{@current_directory} -w #{@current_directory} -i -t lambci/lambda:build-ruby2.5 bundle install --deployment`
50
+ end
51
+ end
52
+ end
53
+
54
+ def generate_build_name
55
+ new_build_number = Dir["#@current_directory/builds"].length - 1
56
+
57
+ new_file_name = ''
58
+
59
+ begin
60
+ new_build_number = new_build_number + 1
61
+
62
+ new_file_name = "builds/build_#{new_build_number}.zip"
63
+ end while File.exist?("#@current_directory/#{new_file_name}")
64
+
65
+ new_file_name
66
+ end
67
+
68
+ def write
69
+ git_ignore_path = "#@current_directory/.gitignore"
70
+
71
+ files_to_ignore = %w(. .. .bundle builds tmp .gitignore .ruby-version)
72
+
73
+ if File.exist?(git_ignore_path)
74
+ files_to_ignore << File.readlines(git_ignore_path).map { |a| a.strip }
75
+ end
76
+
77
+ entries = Dir.entries(@current_directory) - files_to_ignore.flatten
78
+
79
+ ::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|
80
+ write_entries entries, '', zipfile
81
+ end
82
+ end
83
+
84
+ def write_entries(entries, path, zipfile)
85
+ entries.each do |e|
86
+ zipfile_path = path == '' ? e : File.join(path, e)
87
+ disk_file_path = File.join(@current_directory, zipfile_path)
88
+
89
+ if File.directory? disk_file_path
90
+ recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
91
+ else
92
+ put_into_archive(disk_file_path, zipfile, zipfile_path)
93
+ end
94
+ end
95
+ end
96
+
97
+ def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
98
+ zipfile.mkdir zipfile_path
99
+ subdir = Dir.entries(disk_file_path) - %w(. ..)
100
+ write_entries subdir, zipfile_path, zipfile
101
+ end
102
+
103
+ def put_into_archive(disk_file_path, zipfile, zipfile_path)
104
+ zipfile.get_output_stream(zipfile_path) do |f|
105
+ f.write(File.open(disk_file_path, 'rb').read)
106
+ end
107
+ end
108
+
109
+ def which(cmd)
110
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
111
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
112
+ exts.each { |ext|
113
+ exe = File.join(path, "#{cmd}#{ext}")
114
+ return exe if File.executable?(exe) && !File.directory?(exe)
115
+ }
116
+ end
117
+ return nil
118
+ end
119
+ end
120
+ end
@@ -8,9 +8,7 @@ module RubyLambda
8
8
  @options = options
9
9
  end
10
10
 
11
- def run(mute: false)
12
- @mute = mute
13
-
11
+ def run
14
12
  begin
15
13
  if @options.has_key?('handler')
16
14
 
@@ -27,13 +25,9 @@ module RubyLambda
27
25
 
28
26
  context = LambdaContext.new()
29
27
 
30
- if mute
31
- send(:"#{lambda_handler}", event: event, context: context)
32
- else
33
- ap send(:"#{lambda_handler}", event: event, context: context), options = { :indent => -2 }
34
- end
28
+ ap send(:"#{lambda_handler}", event: event, context: context), options = { :indent => -2 }
35
29
  rescue LoadError
36
- @shell.say('Handler file or function, can not be found', :red) unless @mute
30
+ @shell.say('Handler file or function, can not be found', :red)
37
31
 
38
32
  exit 1
39
33
  end
@@ -49,11 +43,11 @@ module RubyLambda
49
43
  rescue Errno::ENOENT
50
44
  no_config_file_message = 'Config file missing, create a config.yml file or use the -c / --config flag to use a different config file. Alternativly you can use the -H flag to pass the name of the handler that should be executed'
51
45
 
52
- @shell.say(no_config_file_message, :red) unless @mute
46
+ @shell.say(no_config_file_message, :red)
53
47
 
54
48
  exit 1
55
49
  rescue RubyLambda::ExecuteError
56
- @shell.say('Invalid config file', :red) unless @mute
50
+ @shell.say('Invalid config file', :red)
57
51
 
58
52
  exit 1
59
53
  end
@@ -63,7 +57,7 @@ module RubyLambda
63
57
  if lambda_handler.nil? || lambda_handler.nil?
64
58
  no_defined_handler_message = 'No config or handler function defined, create a config.yml file or use the -c / --config flag to use a different config file. Alternativly you can use the -H flag to pass the name of the handler that should be executed'
65
59
 
66
- @shell.say(no_defined_handler_message, :red) unless @mute
60
+ @shell.say(no_defined_handler_message, :red)
67
61
 
68
62
  exit 1
69
63
  end
@@ -9,7 +9,6 @@ module RubyLambda
9
9
  end
10
10
 
11
11
  def run(mute: false)
12
-
13
12
  @mute = mute
14
13
 
15
14
  unless File.writable?(@current_directory)
@@ -1,4 +1,4 @@
1
1
  /tmp/
2
2
  .env
3
- build/
3
+ builds/
4
4
  /.bundle/
@@ -1,3 +1,3 @@
1
1
  module RubyLambda
2
- VERSION = '0.3.2'
2
+ VERSION = '0.4.0'
3
3
  end
data/ruby_lambda.gemspec CHANGED
@@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
30
30
 
31
31
  spec.add_runtime_dependency 'thor', '~> 0.19'
32
32
  spec.add_runtime_dependency 'awesome_print', '~> 1.8.0'
33
+ spec.add_runtime_dependency 'rubyzip', '~> 1.2'
33
34
 
34
35
  spec.add_development_dependency 'bundler', '~> 1.16'
35
36
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_lambda
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - cookieshq
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-02-26 00:00:00.000000000 Z
12
+ date: 2019-05-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
@@ -39,6 +39,20 @@ dependencies:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
41
  version: 1.8.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: rubyzip
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.2'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.2'
42
56
  - !ruby/object:Gem::Dependency
43
57
  name: bundler
44
58
  requirement: !ruby/object:Gem::Requirement
@@ -147,6 +161,7 @@ files:
147
161
  - bin/ruby-lambda
148
162
  - bin/setup
149
163
  - lib/ruby_lambda.rb
164
+ - lib/ruby_lambda/build.rb
150
165
  - lib/ruby_lambda/error.rb
151
166
  - lib/ruby_lambda/execute.rb
152
167
  - lib/ruby_lambda/init.rb