Skip to content

Commit

Permalink
Merge pull request #9 from fac/abstract-s3-pushing
Browse files Browse the repository at this point in the history
Prepare for extending to use with other runtimes and asset repositories.
  • Loading branch information
HistoireDeBabar authored Mar 21, 2022
2 parents 009b3a6 + 689a690 commit 79a9bf8
Show file tree
Hide file tree
Showing 22 changed files with 449 additions and 372 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
serverless-tools (0.1.0)
serverless-tools (0.2.0)
aws-sdk-lambda
aws-sdk-s3
thor
Expand Down
6 changes: 6 additions & 0 deletions lib/serverless-tools/cli.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "thor"
require_relative "./comment"
require_relative "./deployer"
require_relative "./version"

module ServerlessTools
class CLI < Thor
Expand All @@ -16,5 +17,10 @@ def comment
def deploy(action, function=nil)
Deployer.deploy(action: action, function: function, filename: options[:filename])
end

desc "version", "prints the version of the library"
def version
puts VERSION
end
end
end
6 changes: 2 additions & 4 deletions lib/serverless-tools/deployer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "yaml"

require_relative "./deployer/deployer"
require_relative "./deployer/function_deployer"
require_relative "./deployer/yaml_config_loader"

module ServerlessTools
Expand All @@ -15,9 +15,7 @@ def self.deploy(action:, function: nil, filename: nil)
lambdas_to_deploy = function ? [function] : config_loader.functions

deployers = lambdas_to_deploy.map do |function_name|
Deployer.new(
config_loader.lambda_config(function_name: function_name)
)
FunctionDeployer.create_for_function(config: config_loader.lambda_config(function_name: function_name))
end

run_action(action: action, deployers: deployers)
Expand Down
33 changes: 0 additions & 33 deletions lib/serverless-tools/deployer/aws_lambda_function.rb

This file was deleted.

48 changes: 0 additions & 48 deletions lib/serverless-tools/deployer/deployer.rb

This file was deleted.

6 changes: 6 additions & 0 deletions lib/serverless-tools/deployer/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

module ServerlessTools
module Deployer
class RuntimeNotSupported < RuntimeError
def initialize(config:)
super("Could not infer lambda runtime from #{config}")
end
end

class ConfigFileNotFound < RuntimeError
def initialize(filename:)
super("Could not find config file '#{filename}'")
Expand Down
10 changes: 4 additions & 6 deletions lib/serverless-tools/deployer/function_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
module ServerlessTools
module Deployer
FunctionConfig = Struct.new(:repo, :s3_archive_name, :handler_file, :bucket, :name, keyword_init: true) do
def local_filename
"#{name}.zip"
end

def s3_key(git_sha:)
"#{repo}/deployments/#{git_sha}/#{name}/#{s3_archive_name}"
def runtime
unless handler_file.nil?
return "ruby" if handler_file.split(".").last == "rb"
end
end
end
end
Expand Down
56 changes: 56 additions & 0 deletions lib/serverless-tools/deployer/function_deployer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# fronzen_string_literal: true

require "aws-sdk-s3"
require "aws-sdk-lambda"

require_relative "../git"
require_relative "./s3_pusher"
require_relative "./lambda_updater"
require_relative "./ruby_builder"
require_relative "./errors"

module ServerlessTools
module Deployer
class FunctionDeployer
attr_reader :builder, :pusher, :updater

def initialize(builder:, pusher:, updater:)
@builder = builder
@pusher = pusher
@updater = updater
end

def build
builder.build
end

def push
pusher.push(**builder.output)
end

def update
updater.update(pusher.output)
end

def deploy
build
push
update
end

def self.create_for_function(config:)
send("#{config.runtime}_deployer", config)
rescue NoMethodError
raise RuntimeNotSupported.new(config: config)
end

def self.ruby_deployer(config)
self.new(
builder: RubyBuilder.new(config: config),
pusher: S3Pusher.new(client: Aws::S3::Client.new, git: Git.new, config: config),
updater: LambdaUpdater.new(client: Aws::Lambda::Client.new, config: config)
)
end
end
end
end
26 changes: 26 additions & 0 deletions lib/serverless-tools/deployer/lambda_updater.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module ServerlessTools
module Deployer
class LambdaUpdater
def initialize(client:, config:)
@client = client
@config = config
end

def update(options)
options[:function_name] = config.name

response = client.update_function_code(options)

puts "::set-output name=#{response[:function_name]}_status::#{response[:last_update_status]}"
rescue Aws::Lambda::Errors::ServiceError => e
puts "::error:: An error occured when updating #{config.name} #{e.message}"
end

private

attr_reader :client, :config
end
end
end
30 changes: 30 additions & 0 deletions lib/serverless-tools/deployer/ruby_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module ServerlessTools
module Deployer
class RubyBuilder
def initialize(config:)
@config = config
end

def build
`bundle`
`zip -r "#{local_filename}" #{config.handler_file} lib vendor/`
end

def output
{
local_filename: local_filename,
}
end

def local_filename
"#{config.name}.zip"
end

private

attr_reader :config
end
end
end
48 changes: 48 additions & 0 deletions lib/serverless-tools/deployer/s3_pusher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require "aws-sdk-s3"

module ServerlessTools
module Deployer
class S3Pusher
def initialize(client:, git:, config:)
@client = client
@git = git
@config = config
end

def push(local_filename:)
if object.exists?
puts "Did not upload #{object.key} as it already exists!"
else
object.upload_file(local_filename)
end
output
end

def output
return {} unless object.exists?
{
s3_bucket: object.bucket.name,
s3_key: object.key,
}
end

private

def object
@object ||= Aws::S3::Object.new(
bucket_name: config.bucket,
key: s3_key,
client: client
)
end

def s3_key
"#{config.repo}/deployments/#{git.sha}/#{config.name}/#{config.s3_archive_name}"
end

attr_reader :client, :git, :config
end
end
end
21 changes: 0 additions & 21 deletions lib/serverless-tools/deployer/s3_uploader.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/serverless-tools/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ServerlessTools
VERSION = "0.1.0"
VERSION = "0.2.0"
end
Loading

0 comments on commit 79a9bf8

Please sign in to comment.