Skip to content

Commit

Permalink
Merge pull request #48 from navarasu/fix_36_requires_all
Browse files Browse the repository at this point in the history
#36 Added example and test for bundler.require all. Include Gemfile and Gemfile.lock in package if lock file is present
  • Loading branch information
navarasu committed May 3, 2021
2 parents d99b545 + 05fe853 commit 973d462
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Using the custom configuration, the plugin can be utilized for below cases,
* Using Dockerfile for gems which with other OS Linux image or system libraries and utilities - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/use-docker-file) - [Docs](https://navarasu.github.io/serverless-ruby-layer/#/use_docker_file)
* Include / Exclude specific functions from layer configuration - [Include Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/include-functions) , [Exclude Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/exclude-functions) - [Docs](https://navarasu.github.io/serverless-ruby-layer/#/include_exclude)
* Exclude test and development related gems from layer - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/exclude-dev-test-gems) - [Docs](https://navarasu.github.io/serverless-ruby-layer/#/exclude_dev_test_gems)
* Using `Bundler.require(:default)` to require all gems in handler.rb by respecting Gemfile.lock - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/bundler-require-all) - [Docs](https://navarasu.github.io/serverless-ruby-layer/#/bundler_require_all)

## Contributing

Expand Down
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
* [Using Custom Dockerfile](use_docker_file)
* [Include / Exclude functions](include_exclude)
* [Exclude test gems / development gems](exclude_dev_test_gems)
* [Bundler.require all gems](bundler_require_all)
* [Configuration](/configuration.md)
* [Release Notes](/release_notes.md)
27 changes: 17 additions & 10 deletions docs/home.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<h1><div style="height:75px" class="side-bar-logo">&nbsp;</div></h1>


[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) [![npm](https://img.shields.io/npm/v/serverless-ruby-layer.svg)](https://www.npmjs.com/package/serverless-ruby-layer) [![Build Status](https://img.shields.io/circleci/build/github/navarasu/serverless-ruby-layer)](https://circleci.com/gh/navarasu/serverless-ruby-layer) [![Coverage Status](https://coveralls.io/repos/github/navarasu/serverless-ruby-layer/badge.svg?branch=master)](https://coveralls.io/github/navarasu/serverless-ruby-layer?branch=master) [![MIT License](https://img.shields.io/npm/l/serverless-ruby-layer)](https://github.com/navarasu/serverless-ruby-layer/blob/master/LICENSE)

A Serverless Plugin to bundle ruby gems from Gemfile and deploy it to the lambda layer automatically while running `serverless deploy`.
A Serverless Plugin which bundles ruby gems from Gemfile and deploys them to the lambda layer automatically while running `serverless deploy`.

It auto-configures the AWS lambda layer and RUBY_PATH to all the functions.

## Install

Install ⚡️ [serverless](https://www.serverless.com/). Refer [here](https://www.serverless.com/framework/docs/getting-started/) for serverless installation instructions.

Navigate to your serverless project and install the plugin

```
sls plugin install -n serverless-ruby-layer
```
Expand Down Expand Up @@ -52,7 +55,7 @@ functions:
gem 'httparty'
```

Running `serverless deploy` automatically deploys the required gems as in Gemfile to AWS lambda layer and make the gems available to the `RUBY_PATH` of the functions `hello.handler`
Running `sls deploy` automatically deploys the required gems as in Gemfile to AWS lambda layer and make the gems available to the `RUBY_PATH` of the functions `hello.handler`

Refer [example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/basic) amd [docs](https://navarasu.github.io/serverless-ruby-layer) for more details

Expand All @@ -68,24 +71,28 @@ custom:
use_docker: true
```

For more details, refer the docs [here](https://navarasu.github.io/serverless-ruby-layer/configuration)
For more detailse refer the docs [here](https://navarasu.github.io/serverless-ruby-layer/#/configuration)


## Usage

Using the custom configuration, the plugin can be utilized for below cases,
* Using locallly installed bundler for gems without any native extensions - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/basic) - [Docs](https://navarasu.github.io/serverless-ruby-layer/use_docker)
* Using Docker for gems with OS native C extensions or system libraries like `http`, `Nokogiri` - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/use_docker) - [Docs](https://navarasu.github.io/serverless-ruby-layer/use_docker)
* Preinstall OS packages (yum packages) for gems which requires OS native system libraries like `pg`, `mysql`, `RMagick` - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/use_docker_with_yums) - [Docs](https://navarasu.github.io/serverless-ruby-layer/use_docker_with_yums)
* Using Dockerfile for gems which requires other system libraries and configuration - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/use_docker_file) - [Docs](https://navarasu.github.io/serverless-ruby-layer/use_docker_file)

* Using locally installed bundler for gems without any native extensions - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/basic) - [Docs](https://navarasu.github.io/serverless-ruby-layer/#/use_local_bundler)
* Using Docker for gems with OS native C extensions or system libraries like `http`, `Nokogiri` - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/use-docker) - [Docs](https://navarasu.github.io/serverless-ruby-layer/#/use_docker)
* Preinstall OS packages (yum packages) for gems which requires OS native system libraries like `pg`, `mysql`, `RMagick` - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/use-docker-with-yums) - [Docs](https://navarasu.github.io/serverless-ruby-layer/#/use_docker_with_yums)
* Using Dockerfile for gems which with other OS Linux image or system libraries and utilities - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/use-docker-file) - [Docs](https://navarasu.github.io/serverless-ruby-layer/#/use_docker_file)
* Include / Exclude specific functions from layer configuration - [Include Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/include-functions) , [Exclude Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/exclude-functions) - [Docs](https://navarasu.github.io/serverless-ruby-layer/#/include_exclude)
* Exclude test and development related gems from layer - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/exclude-dev-test-gems) - [Docs](https://navarasu.github.io/serverless-ruby-layer/#/exclude_dev_test_gems)
* Using `Bundler.require(:default)` to require all gems in handler.rb by respecting Gemfile.lock - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/bundler-require-all) - [Docs](https://navarasu.github.io/serverless-ruby-layer/#/bundler_require_all)

## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update the tests as appropriate.

Refer [Guidelines](https://github.com/navarasu/serverless-ruby-layer/blob/master/CONTRIBUTING.md) for more information.

## License

[MIT](https://choosealicense.com/licenses/mit/)
[MIT](https://choosealicense.com/licenses/mit/)
3 changes: 2 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
'/use_docker_with_yums': '/usage_examples/use_docker_with_yums',
'/use_docker_file': '/usage_examples/use_docker_file',
'/include_exclude': '/usage_examples/include_exclude',
'/exclude_dev_test_gems': '/usage_examples/exclude_dev_test_gems'
'/exclude_dev_test_gems': '/usage_examples/exclude_dev_test_gems',
'/bundler_require_all': '/usage_examples/bundler_require_all'
},
search: {
noData: {
Expand Down
112 changes: 112 additions & 0 deletions docs/usage_examples/bundler_require_all.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@

## Using `Bundler.require(:default)` to require all gems in handler.rb by respecting Gemfile.lock

<!-- tabs:start -->

#### ** handler.rb **

```ruby
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)

def hello(event:, context:)
body = HTTP.get('https://github.com').body.to_s
doc = Nokogiri::HTML(body)

{ statusCode: 200, body: doc.title }
end


```

#### ** serverless.yml **

```yml
service: bundler_require_all

plugins:
- serverless-ruby-layer

custom:
rubyLayer:
use_docker: true

provider:
name: aws
runtime: ruby2.5

functions:
hello:
handler: handler.hello

```

#### ** Gemfile **

```ruby
source 'https://rubygems.org'
gem 'http'
gem 'nokogiri'
```

#### ** Gemfile.lock **

```ruby
GEM
remote: https://rubygems.org/
specs:
httparty (0.18.1)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2020.1104)
mini_portile2 (2.5.1)
multi_xml (0.6.0)
nokogiri (1.11.3)
mini_portile2 (~> 2.5.0)
racc (~> 1.4)
racc (1.5.2)

PLATFORMS
ruby

DEPENDENCIES
httparty
nokogiri

BUNDLED WITH
2.1.4
```

<!-- tabs:end -->


#### Checkout above Example here


```bash
git clone https://github.com/navarasu/serverless-ruby.git
```
```bash
cd serverless-ruby/bundler_require_all

sls plugin install -n serverless-ruby-layer

```

#### Deploy to AWS

```bash
sls deploy
```

Running `sls deploy` automatically deploys the required gems as in Gemfile to AWS lambda layer and make the gems available to the `RUBY_PATH` of the functions `hello.handler`


#### Test it

```bash
sls invoke -f hello
```
1 change: 1 addition & 0 deletions docs/usage_examples/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Using the custom configuration, the plugin can be utilized for below cases,
* Preinstall OS packages (yum packages) for gems which requires OS native system libraries like `pg`, `mysql`, `RMagick` - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/use_docker_with_yums) - [Docs](use_docker_with_yums)
* Using Dockerfile for gems which requires other system libraries and configuration - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/use_docker_file) - [Docs](use_docker_file)
* Exclude test and development related gems from layer - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/exclude-dev-test-gems) - [Docs](https://navarasu.github.io/serverless-ruby-layer/#/exclude_dev_test_gems)
* Using `Bundler.require(:default)` to require all gems in handler.rb by respecting Gemfile.lock - [Example](https://github.com/navarasu/serverless-ruby-layer/blob/master/examples/bundler-require-all) - [Docs](https://navarasu.github.io/serverless-ruby-layer/#/bundler_require_all)
3 changes: 3 additions & 0 deletions examples/bundler-require-all/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'
gem 'httparty'
gem 'nokogiri'
25 changes: 25 additions & 0 deletions examples/bundler-require-all/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
GEM
remote: https://rubygems.org/
specs:
httparty (0.18.1)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2020.1104)
mini_portile2 (2.5.1)
multi_xml (0.6.0)
nokogiri (1.11.3)
mini_portile2 (~> 2.5.0)
racc (~> 1.4)
racc (1.5.2)

PLATFORMS
ruby

DEPENDENCIES
httparty
nokogiri

BUNDLED WITH
2.1.4
10 changes: 10 additions & 0 deletions examples/bundler-require-all/handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)

def hello(event:, context:)
body = HTTParty.get("https://github.com").body
doc = Nokogiri::HTML(body)

{ statusCode: 200, body: doc.title }
end
15 changes: 15 additions & 0 deletions examples/bundler-require-all/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
service: bundler-require-all

plugins:
- serverless-ruby-layer
provider:
name: aws
runtime: ruby2.5

custom:
rubyLayer:
use_docker: true

functions:
hello:
handler: handler.hello
12 changes: 8 additions & 4 deletions lib/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ function bundleInstall(){

fs.copySync(path.join(this.servicePath,'Gemfile'), path.join(this.ruby_layer,'Gemfile') )
const gem_file_lock_path = path.join(this.servicePath,'Gemfile.lock')
const gem_file_lock_exists = fs.pathExistsSync(gem_file_lock_path)
if (!this.options.ignore_gemfile_lock && gem_file_lock_exists){
if (this.use_gemfile_lock){
fs.copySync(gem_file_lock_path, path.join(this.ruby_layer,'Gemfile.lock') )
}

Expand Down Expand Up @@ -102,7 +101,7 @@ function bundleInstall(){
this.docker_gem_path = '/var/gem_build'
docker(['exec', docker_id, 'mkdir', this.docker_gem_path], options, this.cli)
docker(['cp', '-L', 'Gemfile', container_id+':'+this.docker_gem_path], options,this.cli)
if (!this.options.ignore_gemfile_lock && gem_file_lock_exists) {
if (this.use_gemfile_lock){
docker(['cp', '-L', 'Gemfile.lock', container_id+':'+this.docker_gem_path], options,this.cli)
const data = fs.readFileSync(gem_file_lock_path).toString().match(/BUNDLED WITH[\r\n]+([^\r\n]+)/)
docker(['exec','-w',this.docker_gem_path, docker_id].concat(['gem','install','bundler:'+ data[data.length - 1].trim()]),options,this.cli)
Expand Down Expand Up @@ -207,7 +206,12 @@ function excludePackage(){
this.serverless.service.package["exclude"] = Object.assign([])
}
this.serverless.service.package["exclude"].push("node_modules/**", "package-lock.json", "package.json",
"vendor/**", "Gemfile", "Gemfile.lock")
"vendor/**")
this.use_gemfile_lock = !this.options.ignore_gemfile_lock && fs.pathExistsSync(path.join(this.servicePath,'Gemfile.lock'))

if (!this.use_gemfile_lock) {
this.serverless.service.package["exclude"].push("Gemfile", "Gemfile.lock")
}
if (this.options.docker_file) {
this.serverless.service.package["exclude"].push(this.options.docker_file)
}
Expand Down
7 changes: 4 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ let test_data = [
function_files: ['handler.rb'], include_functions: ['Hello'], exclude_functions:[] },

{ folder: 'basic-with-gemfile-lock', gem_zip_dirs: ['/','/bin/','/build_info/','/doc/','/extensions/', '/gems/', '/specifications/','/gems/httparty-0.18.1/', '/gems/mime-types-data-3.2020.1104/', '/gems/multi_xml-0.6.0/', '/gems/mime-types-3.3.1/'],
function_files: ['handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },
function_files: ['Gemfile', 'Gemfile.lock', 'handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },

{ folder: 'use-docker-with-gemfile-lock', gem_zip_dirs: ['/','/bin/','/build_info/','/doc/','/extensions/', '/gems/', '/specifications/','/gems/httparty-0.18.1/', '/gems/mime-types-data-3.2020.1104/', '/gems/multi_xml-0.6.0/', '/gems/mime-types-3.3.1/'],
function_files: ['handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },
function_files: ['Gemfile', 'Gemfile.lock', 'handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },

{ folder: 'basic-ignore-gemfile-lock', gem_zip_dirs: ['/','/bin/','/build_info/','/doc/','/extensions/', '/gems/', '/specifications/','/gems/httparty-0.18.1/', '/gems/mime-types-data-3.2021.0225/', '/gems/multi_xml-0.6.0/', '/gems/mime-types-3.3.1/'],
function_files: ['handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },

{ folder: 'use-docker-ignore-gemfile-lock', gem_zip_dirs: ['/','/bin/','/build_info/','/doc/','/extensions/', '/gems/', '/specifications/','/gems/httparty-0.18.1/', '/gems/mime-types-data-3.2021.0225/', '/gems/multi_xml-0.6.0/', '/gems/mime-types-3.3.1/'],
function_files: ['handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },


{ folder: 'bundler-require-all', gem_zip_dirs: ['/','/bin/','/build_info/','/doc/','/extensions/', '/gems/', '/specifications/','/gems/httparty-0.18.1/', '/gems/mime-types-data-3.2020.1104/', '/gems/multi_xml-0.6.0/', '/gems/mime-types-3.3.1/','/gems/nokogiri-1.11.3-x86_64-linux/', '/gems/racc-1.5.2/', '/extensions/x86_64-linux/', '/gems/mini_portile2-2.5.1/' ],
function_files: ['Gemfile', 'Gemfile.lock', 'handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },

]

Expand Down

0 comments on commit 973d462

Please sign in to comment.