Class: Jets::Builders::LambdaLayer

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/jets/builders/lambda_layer.rb

Instance Method Summary collapse

Instance Method Details

#buildObject

At this point gems are in the stage/code and stage/rack folders still. We consolidate all gems to stage/opt. Then replace the binary gems.



8
9
10
11
# File 'lib/jets/builders/lambda_layer.rb', line 8

def build
  consolidate_gems_to_opt
  replace_compiled_gems unless Jets.config.gems.disable
end

#consolidate_gems_to_optObject

Also restructure the folder from:

vendor/gems/ruby/2.5.0

To:

ruby/gems/2.5.0

For Lambda Layer structure



19
20
21
22
23
24
25
26
27
28
# File 'lib/jets/builders/lambda_layer.rb', line 19

def consolidate_gems_to_opt
  src = "#{stage_area}/code/vendor/gems/ruby/#{Jets.ruby_folder}"
  dest = "#{stage_area}/opt/ruby/gems/#{Jets.ruby_folder}"
  rsync_and_link(src, dest)

  return unless Jets.rack?

  src = "#{stage_area}/rack/vendor/gems/ruby/#{Jets.ruby_folder}"
  rsync_and_link(src, dest)
end

#replace_compiled_gemsObject

replace_compiled_gems:

remove binary gems in vendor/gems/ruby/2.5.0
extract binary gems in opt/ruby/gems/2.5.0
move binary gems from opt/ruby/gems/2.5.0 to vendor/gems/ruby/2.5.0

After this point, gems have been replace in stage/code/vendor/gems with their binary extensions: a good state. This method moves these gems to the Lambda Layers structure and creates a symlinks to it. First:

from stage/code/vendor/gems/ruby/2.5.0
to stage/opt/ruby/gems/2.5.0

Then:

stage/code/vendor/gems/ruby/2.5.0 -> /opt/ruby/gems/2.5.0


58
59
60
61
62
63
64
65
66
# File 'lib/jets/builders/lambda_layer.rb', line 58

def replace_compiled_gems
  project_root = "#{stage_area}/opt"
  headline "Replacing compiled gems with AWS Lambda Linux compiled versions: #{project_root}"
  options = {
    build_root: cache_area, # used in jets-gems
    project_root: project_root, # used in gem_replacer and jets-gems
  }
  GemReplacer.new(options).run
end


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jets/builders/lambda_layer.rb', line 30

def rsync_and_link(src, dest)
  FileUtils.mkdir_p(dest)
  # Trailing slashes are required
  sh "rsync -a --links #{src}/ #{dest}/"

  FileUtils.rm_rf(src) # blow away original 2.5.0 folder

  # Create symlink that will point to the gems in the Lambda Layer:
  #   stage/opt/ruby/gems/2.5.0 -> /opt/ruby/gems/2.5.0
  FileUtils.ln_sf("/opt/ruby/gems/#{Jets::Gems.ruby_folder}", src)
end