lambda-layer-cake 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -3
- data/build_env/build_ruby.rb +7 -6
- data/lib/lambda-layer-cake.rb +0 -67
- data/lib/lambda_layer_cake/rake_helper.rb +121 -0
- data/lib/lambda_layer_cake/version.rb +1 -1
- data/lib/tasks/layer_cake.rake +7 -84
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0929fa88af212b2adbd2434aa1f75518fa50990c7b0ec5e1145153d76fde16b8'
|
4
|
+
data.tar.gz: 2322e1e5922ddf716815de7cb850efa1295947430938de80e167def203b006aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e823aee747d165029b115ff61455a0354d4cd33e9c73084e65b62a0199bd14ca75520ff026884e9ac59ac5c9dd5370d09b90311fb7ddb049cfa4f0750a95783b
|
7
|
+
data.tar.gz: fd1b85928ef49acd74b93408994bfb5513c51d06d8166d14d3e96ce38d102cbed9faeb090f94d81dbce6b63a66183a6c62e34d72729ff31544808856bc087e29
|
data/README.md
CHANGED
@@ -9,10 +9,31 @@ This gem does just the packaging of the app and leaves deployment as an exercise
|
|
9
9
|
## Usage
|
10
10
|
|
11
11
|
1. Include Lamby and this gem in your Rails app's Gemfile.
|
12
|
-
|
12
|
+
|
13
|
+
gem 'lambda-layer-cake'
|
14
|
+
gem 'lamby'
|
15
|
+
|
16
|
+
1. Create an `app.rb` in the root of your project that contains (stock Lamby app.rb with an extra line for Bundler config):
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
# This line tells Bundler where the Layer Cake gems are located
|
20
|
+
ENV['BUNDLE_PATH'] = 'vendor/bundle/ruby/2.5.0'
|
21
|
+
|
22
|
+
require_relative 'config/boot'
|
23
|
+
require 'lamby'
|
24
|
+
require_relative 'config/application'
|
25
|
+
require_relative 'config/environment'
|
26
|
+
|
27
|
+
$app = Rack::Builder.new { run Rails.application }.to_app
|
28
|
+
|
29
|
+
def handler(event:, context:)
|
30
|
+
Lamby.handler $app, event, context, rack: :api
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
13
34
|
1. If you need extra system packages (such as `postgresql-libs` for the `pg` gem), create a file called `system-packages.txt` in the root of your Rails project and include package names, one per line.
|
14
35
|
1. When you are ready to deploy your app, use `rake .layer_cake/layer.zip` and `rake .layer_cake/app.zip` to build files suitable for deployment to Lambda.
|
15
|
-
1. Use the the demployment tools of your choice to
|
36
|
+
1. Use the the demployment tools of your choice to define a Lambda function. The `layer.zip`
|
16
37
|
|
17
38
|
|
18
39
|
## How It Works
|
@@ -30,7 +51,9 @@ This is my first attempt at publishing a complete gem and Rails plugin. I took i
|
|
30
51
|
Contributions on the following items is particularly appreciated:
|
31
52
|
|
32
53
|
* Modifying the Docker build environment to probe the host system's cache of gemfiles to more gracefully handle private and unreleased gems
|
33
|
-
*
|
54
|
+
* Give proper names to the rake tasks
|
55
|
+
* A better `clean` Rake task
|
56
|
+
* Under some circumstances, `app.zip` does rebuild due to not detecting dependencies
|
34
57
|
|
35
58
|
## License
|
36
59
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/build_env/build_ruby.rb
CHANGED
@@ -25,8 +25,8 @@ def main
|
|
25
25
|
end
|
26
26
|
|
27
27
|
STDERR.puts("Building Gems")
|
28
|
-
FileUtils.mkdir_p("/tmp/build")
|
29
|
-
unless(system("bundle","install","--deployment","--path=/tmp/build"))
|
28
|
+
FileUtils.mkdir_p("/tmp/build/bundle")
|
29
|
+
unless(system("bundle","install","--deployment","--path=/tmp/build/bundle"))
|
30
30
|
STDERR.puts("Couldn't build gems")
|
31
31
|
exit 3
|
32
32
|
end
|
@@ -66,10 +66,11 @@ def main
|
|
66
66
|
FileUtils.mkdir_p('/tmp/outputs/lib')
|
67
67
|
FileUtils.cp(libs.to_a, '/tmp/outputs/lib')
|
68
68
|
|
69
|
-
STDERR.puts("Moving
|
70
|
-
FileUtils.
|
71
|
-
#
|
72
|
-
|
69
|
+
STDERR.puts("Moving Bundle into place")
|
70
|
+
FileUtils.cp_r("/tmp/build/bundle", "/tmp/outputs")
|
71
|
+
#This directory is not needed at runtime
|
72
|
+
#STDERR.puts("Removing extra files")
|
73
|
+
#FileUtils.rm_rf("/tmp/outputs/bundle/ruby/2.5.0/cache")
|
73
74
|
|
74
75
|
STDERR.puts("All Done!")
|
75
76
|
end
|
data/lib/lambda-layer-cake.rb
CHANGED
@@ -1,70 +1,3 @@
|
|
1
1
|
module LambdaLayerCake
|
2
2
|
require 'lambda_layer_cake/railtie' if defined?(Rails)
|
3
|
-
|
4
|
-
class Builder
|
5
|
-
attr :version
|
6
|
-
|
7
|
-
def initialize(version)
|
8
|
-
@version = version
|
9
|
-
end
|
10
|
-
|
11
|
-
def clean!
|
12
|
-
FileUtils.rm_r(outputs_dir!)
|
13
|
-
end
|
14
|
-
|
15
|
-
def build_layer!
|
16
|
-
FileUtils.cp(%w{Gemfile Gemfile.lock system-packages.txt}.select {|f| File.exist?(f) }, inputs_dir!)
|
17
|
-
|
18
|
-
cmd = %W{docker run --rm
|
19
|
-
-v #{inputs_dir!}:/tmp/inputs
|
20
|
-
-v #{outputs_dir!}:/tmp/outputs
|
21
|
-
-v #{tools_dir}:/var/task
|
22
|
-
lambci/lambda:build-ruby2.5 ruby build_ruby.rb}
|
23
|
-
STDERR.puts("Excuting cmd: #{cmd.join(' ')}")
|
24
|
-
system(*cmd) or raise
|
25
|
-
end
|
26
|
-
|
27
|
-
def zip_layer!
|
28
|
-
pwd = Dir.pwd
|
29
|
-
begin
|
30
|
-
Dir.chdir(outputs_dir!)
|
31
|
-
cmd = *%W{zip -r #{File.join(build_dir!, "layer-#{version}.zip")} lib ruby}
|
32
|
-
system(*cmd) or raise
|
33
|
-
ensure
|
34
|
-
Dir.chdir(pwd)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def build_rails!
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
def inputs_dir!
|
45
|
-
working_dir!("layer_inputs")
|
46
|
-
end
|
47
|
-
|
48
|
-
def outputs_dir!
|
49
|
-
working_dir!("layer-#{version}")
|
50
|
-
end
|
51
|
-
|
52
|
-
def working_dir!(path_suffix = "")
|
53
|
-
File.expand_path(
|
54
|
-
File.join(
|
55
|
-
Rails.root,
|
56
|
-
'.layer_cake/',
|
57
|
-
path_suffix
|
58
|
-
)
|
59
|
-
).tap { |d| FileUtils.mkdir_p(d) }
|
60
|
-
end
|
61
|
-
|
62
|
-
def tools_dir
|
63
|
-
File.expand_path(__dir__ + "/../build_env") # Never needs to be created
|
64
|
-
end
|
65
|
-
|
66
|
-
def build_dir!
|
67
|
-
working_dir!
|
68
|
-
end
|
69
|
-
end
|
70
3
|
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module LambdaLayerCake
|
2
|
+
class RakeHelper
|
3
|
+
include Rake::DSL
|
4
|
+
|
5
|
+
#Files used to generate the gem layer
|
6
|
+
INPUT_FILES = Rake::FileList.new(
|
7
|
+
['Gemfile', 'Gemfile.lock', 'system-packages.txt'].collect { |f| File.join(Rails.root, f) }
|
8
|
+
)
|
9
|
+
|
10
|
+
|
11
|
+
#Files used to generate the
|
12
|
+
APP_FILES = Rake::FileList.new(Dir['*'] - ['spec','test','log', 'tmp'])
|
13
|
+
OUTPUT_DIR = File.expand_path(File.join(Rails.root,'.layer_cake/'))
|
14
|
+
|
15
|
+
def layer_directory
|
16
|
+
".layer_cake/layer-#{input_hash}"
|
17
|
+
end
|
18
|
+
def layer_dependencies
|
19
|
+
INPUT_FILES.existing
|
20
|
+
end
|
21
|
+
def layer_build
|
22
|
+
FileUtils.mkdir_p(inputs_dir)
|
23
|
+
FileUtils.cp(INPUT_FILES.existing, inputs_dir)
|
24
|
+
|
25
|
+
cmd = %W{docker run --rm
|
26
|
+
-v #{inputs_dir}:/tmp/inputs
|
27
|
+
-v #{outputs_dir}:/tmp/outputs
|
28
|
+
-v #{File.expand_path(__dir__ + "/../../build_env")}:/var/task
|
29
|
+
lambci/lambda:build-ruby2.5 ruby build_ruby.rb}
|
30
|
+
STDERR.puts("Excuting cmd: #{cmd.join(' ')}")
|
31
|
+
system(*cmd) or raise
|
32
|
+
end
|
33
|
+
|
34
|
+
def layer_task_definitions!
|
35
|
+
desc "Builds a layer.zip with gems and libraries"
|
36
|
+
task build_layer: ".layer_cake/layer.zip"
|
37
|
+
|
38
|
+
desc "Build a layer for version #{input_hash}"
|
39
|
+
directory ".layer_cake/layer-#{input_hash}"
|
40
|
+
file ".layer_cake/layer-#{input_hash}" => layer_dependencies do
|
41
|
+
layer_build
|
42
|
+
end
|
43
|
+
|
44
|
+
file ".layer_cake/layer-#{input_hash}.zip" => ".layer_cake/layer-#{input_hash}" do
|
45
|
+
pwd = Dir.pwd
|
46
|
+
begin
|
47
|
+
Dir.chdir(outputs_dir)
|
48
|
+
cmd = %W{zip -r #{File.join(working_dir, "layer-#{input_hash}.zip")} lib bundle}
|
49
|
+
system(*cmd) or raise
|
50
|
+
ensure
|
51
|
+
Dir.chdir(pwd)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
desc "Build the current layer version and symlink it to the versioned zip"
|
56
|
+
file ".layer_cake/layer.zip": ".layer_cake/layer-#{input_hash}.zip" do
|
57
|
+
FileUtils.ln_s("layer-#{input_hash}.zip",".layer_cake/layer.zip", force: true)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def app_task_definitions!
|
62
|
+
desc "Zip up Rails directory into an app"
|
63
|
+
file ".layer_cake/app.zip": APP_FILES do
|
64
|
+
FileUtils.mkdir_p(working_dir)
|
65
|
+
|
66
|
+
cmd = %W{zip -r .layer_cake/app.zip} + APP_FILES
|
67
|
+
system(*cmd) or raise
|
68
|
+
|
69
|
+
#Insert a symlink into the app.zip
|
70
|
+
FileUtils.mkdir_p(working_dir("app/vendor"))
|
71
|
+
FileUtils.ln_s("/tmp", working_dir("app/tmp"))
|
72
|
+
FileUtils.ln_s("/opt/bundle", working_dir("app/vendor/bundle"), force: true)
|
73
|
+
Dir.chdir(working_dir("app")) do
|
74
|
+
system(*((%W{zip --symlinks -r} << working_dir("app.zip")) + Dir["*"])) or raise
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def version_task_definitions!
|
80
|
+
desc "Output the version hash for the current input files (Gemfile, Gemfile.lock, system-packages.txt)"
|
81
|
+
task :version do
|
82
|
+
$stdout.write input_hash
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def clean_task_definitions!
|
87
|
+
desc "Clean out intermediate files"
|
88
|
+
task :clean do
|
89
|
+
FileUtils.rm_r(working_dir) if File.exists?(working_dir)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
# Some helper methods
|
95
|
+
def input_hash
|
96
|
+
@input_hash ||= begin
|
97
|
+
hash = Digest::SHA1.hexdigest(INPUT_FILES.existing.collect do |f|
|
98
|
+
File.read(f)
|
99
|
+
end.join("\x1C"))
|
100
|
+
hash[0..7]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def inputs_dir
|
105
|
+
working_dir("layer_inputs")
|
106
|
+
end
|
107
|
+
|
108
|
+
def outputs_dir
|
109
|
+
working_dir("layer-#{input_hash}")
|
110
|
+
end
|
111
|
+
|
112
|
+
def working_dir(path_suffix = "")
|
113
|
+
File.expand_path(
|
114
|
+
File.join(
|
115
|
+
OUTPUT_DIR,
|
116
|
+
path_suffix
|
117
|
+
)
|
118
|
+
)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/lib/tasks/layer_cake.rake
CHANGED
@@ -3,90 +3,13 @@ require 'fileutils'
|
|
3
3
|
|
4
4
|
require 'rake/packagetask'
|
5
5
|
|
6
|
-
|
7
|
-
desc 'Clean the build directories'
|
8
|
-
task :clean do
|
9
|
-
LambdaLayerCake.clean!
|
10
|
-
end
|
11
|
-
|
12
|
-
#Files used to generate the gem layer
|
13
|
-
INPUT_FILES = Rake::FileList.new(
|
14
|
-
['Gemfile', 'Gemfile.lock', 'system-packages.txt'].collect { |f| File.join(Rails.root, f) }
|
15
|
-
)
|
16
|
-
#Files used to generate the
|
17
|
-
APP_FILES = Rake::FileList.new(Dir['*'] - ['spec','test','log', 'tmp'])
|
18
|
-
OUTPUT_DIR = '.layer_cake'
|
19
|
-
|
20
|
-
# Some helper methods
|
21
|
-
def self.input_hash
|
22
|
-
@input_hash ||= begin
|
23
|
-
hash = Digest::SHA1.hexdigest(INPUT_FILES.existing.collect do |f|
|
24
|
-
File.read(f)
|
25
|
-
end.join("\x1C"))
|
26
|
-
hash[0..7]
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def inputs_dir
|
31
|
-
working_dir("layer_inputs")
|
32
|
-
end
|
33
|
-
|
34
|
-
def outputs_dir
|
35
|
-
working_dir("layer-#{input_hash}")
|
36
|
-
end
|
37
|
-
|
38
|
-
def working_dir(path_suffix = "")
|
39
|
-
File.expand_path(
|
40
|
-
File.join(
|
41
|
-
Rails.root,
|
42
|
-
'.layer_cake/',
|
43
|
-
path_suffix
|
44
|
-
)
|
45
|
-
)
|
46
|
-
end
|
47
|
-
|
48
|
-
desc "Output the version hash for the current input files (Gemfile, Gemfile.lock, system-packages.txt)"
|
49
|
-
task :version do
|
50
|
-
$stdout.write input_hash
|
51
|
-
end
|
52
|
-
|
53
|
-
desc "Builds a layer.zip with gems and libraries"
|
54
|
-
task build_layer: '.layer_cake/layer.zip'
|
55
|
-
|
56
|
-
desc "Build a layer for version #{input_hash}"
|
57
|
-
directory ".layer_cake/layer-#{input_hash}"
|
58
|
-
file ".layer_cake/layer-#{input_hash}": INPUT_FILES.existing do
|
59
|
-
FileUtils.mkdir_p(inputs_dir)
|
60
|
-
FileUtils.cp(INPUT_FILES.existing, inputs_dir)
|
6
|
+
require 'lambda_layer_cake/rake_helper'
|
61
7
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
system(*cmd) or raise
|
69
|
-
end
|
70
|
-
|
71
|
-
file ".layer_cake/layer-#{input_hash}.zip": ".layer_cake/layer-#{input_hash}" do
|
72
|
-
pwd = Dir.pwd
|
73
|
-
begin
|
74
|
-
Dir.chdir(outputs_dir)
|
75
|
-
cmd = %W{zip -r #{File.join(working_dir, "layer-#{input_hash}.zip")} lib ruby}
|
76
|
-
system(*cmd) or raise
|
77
|
-
ensure
|
78
|
-
Dir.chdir(pwd)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
desc "Build the current layer version and symlink it to the versioned zip"
|
83
|
-
file ".layer_cake/layer.zip": ".layer_cake/layer-#{input_hash}.zip" do
|
84
|
-
FileUtils.ln_s("layer-#{input_hash}.zip",".layer_cake/layer.zip", force: true)
|
85
|
-
end
|
86
|
-
|
87
|
-
desc "Zip up Rails directory into an app"
|
88
|
-
file ".layer_cake/app.zip": APP_FILES do
|
89
|
-
cmd = %W{zip -r .layer_cake/app.zip} + APP_FILES
|
90
|
-
system(*cmd) or raise
|
8
|
+
namespace :layer_cake do
|
9
|
+
LambdaLayerCake::RakeHelper.new.tap do |rh|
|
10
|
+
rh.clean_task_definitions!
|
11
|
+
rh.version_task_definitions!
|
12
|
+
rh.layer_task_definitions!
|
13
|
+
rh.app_task_definitions!
|
91
14
|
end
|
92
15
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lambda-layer-cake
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Logan Bowers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-09-
|
11
|
+
date: 2019-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubyzip
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: A set of Rake tasks to bundle Rails apps for use on AWS Lambda
|
42
56
|
email:
|
43
57
|
- logan@datacurrent.com
|
@@ -52,6 +66,7 @@ files:
|
|
52
66
|
- build_env/installed-packages.txt
|
53
67
|
- lib/lambda-layer-cake.rb
|
54
68
|
- lib/lambda_layer_cake/railtie.rb
|
69
|
+
- lib/lambda_layer_cake/rake_helper.rb
|
55
70
|
- lib/lambda_layer_cake/version.rb
|
56
71
|
- lib/tasks/layer_cake.rake
|
57
72
|
homepage: http://github.com/loganb/lambda-layer-cake
|