class LambdaLayerCake::RakeHelper

Constants

APP_FILES

Files used to generate the

INPUT_FILES

Files used to generate the gem layer

OUTPUT_DIR

Public Instance Methods

app_task_definitions!() click to toggle source
# File lib/lambda_layer_cake/rake_helper.rb, line 61
def app_task_definitions!
  desc "Zip up Rails directory into an app"
  file ".layer_cake/app.zip": APP_FILES do
    FileUtils.mkdir_p(working_dir)

    cmd = %W{zip -r .layer_cake/app.zip}  + APP_FILES
    system(*cmd) or raise
    
    #Insert a symlink into the app.zip
    FileUtils.rm_r(working_dir("app/")) if(File.exists?(working_dir("app")))
    FileUtils.mkdir_p(working_dir("app/vendor"))
    FileUtils.ln_s("/tmp", working_dir("app/tmp"))
    FileUtils.ln_s("/opt/bundle", working_dir("app/vendor/bundle"), force: true)
    Dir.chdir(working_dir("app")) do
      system(*((%W{zip --symlinks -r} << working_dir("app.zip")) + Dir["*"])) or raise
    end
  end    
end
clean_task_definitions!() click to toggle source
# File lib/lambda_layer_cake/rake_helper.rb, line 93
def clean_task_definitions!
  desc "Clean out intermediate files"
  task :clean do
    FileUtils.rm_r(working_dir) if File.exists?(working_dir)
  end
end
layer_build() click to toggle source
# File lib/lambda_layer_cake/rake_helper.rb, line 21
def layer_build
  FileUtils.mkdir_p(inputs_dir)
  FileUtils.cp(INPUT_FILES.existing, inputs_dir)
  
  cmd = %W{docker run --rm
    -v #{inputs_dir}:/tmp/inputs 
    -v #{outputs_dir}:/tmp/outputs 
    -v #{File.expand_path(__dir__ + "/../../build_env")}:/var/task 
    lambci/lambda:#{docker_tag} ruby build_ruby.rb}
  STDERR.puts("Excuting cmd: #{cmd.join(' ')}")
  system(*cmd) or raise    
end
layer_dependencies() click to toggle source
# File lib/lambda_layer_cake/rake_helper.rb, line 18
def layer_dependencies
  INPUT_FILES.existing
end
layer_directory() click to toggle source
# File lib/lambda_layer_cake/rake_helper.rb, line 15
def layer_directory
  ".layer_cake/layer-#{input_hash}"
end
layer_task_definitions!() click to toggle source
# File lib/lambda_layer_cake/rake_helper.rb, line 34
def layer_task_definitions!
  desc "Builds a layer.zip with gems and libraries"
  task build_layer: ".layer_cake/layer.zip"

  desc "Build a layer for version #{input_hash}"
  directory ".layer_cake/layer-#{input_hash}"
  file ".layer_cake/layer-#{input_hash}" => layer_dependencies do
    layer_build
  end

  file ".layer_cake/layer-#{input_hash}.zip" => ".layer_cake/layer-#{input_hash}" do
    pwd = Dir.pwd
    begin
      Dir.chdir(outputs_dir)
      cmd = %W{zip -r #{File.join(working_dir, "layer-#{input_hash}.zip")} lib bundle}
      system(*cmd) or raise
    ensure
      Dir.chdir(pwd)
    end
  end

  desc "Build the current layer version and symlink it to the versioned zip"
  file ".layer_cake/layer.zip": ".layer_cake/layer-#{input_hash}.zip" do
    FileUtils.ln_s("layer-#{input_hash}.zip",".layer_cake/layer.zip", force: true)
  end
end
version_task_definitions!() click to toggle source
# File lib/lambda_layer_cake/rake_helper.rb, line 80
def version_task_definitions!
  desc "Output the version hash for the current input files (Gemfile, Gemfile.lock, system-packages.txt)"
  task :version do
    $stdout.write input_hash
  end

  desc "Reports the ruby version of the lambda environment"
  task :ruby_version do
    $stdout.write ruby_version
  end

end

Private Instance Methods

docker_tag() click to toggle source
# File lib/lambda_layer_cake/rake_helper.rb, line 128
def docker_tag
  "build-ruby#{/^\d+\.\d+/.match(RUBY_VERSION)[0]}"
end
input_hash() click to toggle source

Some helper methods

# File lib/lambda_layer_cake/rake_helper.rb, line 102
def input_hash
  @input_hash ||= begin
    hash = Digest::SHA1.hexdigest(INPUT_FILES.existing.collect do |f|
      File.read(f)
    end.join("\x1C"))
    hash[0..7]
  end
end
inputs_dir() click to toggle source
# File lib/lambda_layer_cake/rake_helper.rb, line 111
def inputs_dir
  working_dir("layer_inputs")
end
outputs_dir() click to toggle source
# File lib/lambda_layer_cake/rake_helper.rb, line 115
def outputs_dir
  working_dir("layer-#{input_hash}")
end
ruby_version() click to toggle source
# File lib/lambda_layer_cake/rake_helper.rb, line 132
def ruby_version
  cmd = %W{docker run --rm
    -v #{inputs_dir}:/tmp/inputs 
    -v #{outputs_dir}:/tmp/outputs 
    -v #{File.expand_path(__dir__ + "/../../build_env")}:/var/task 
    lambci/lambda:#{docker_tag} ruby -e }.push '$stdout.write RUBY_VERSION'
  STDERR.puts("Excuting cmd: #{cmd.join(' ')}")
  system(*cmd) or raise
end
working_dir(path_suffix = "") click to toggle source
# File lib/lambda_layer_cake/rake_helper.rb, line 119
def working_dir(path_suffix = "")
  File.expand_path(
    File.join(
      OUTPUT_DIR,
      path_suffix
    )
  )
end