Class: Lambda::MicroVMs::Deployer

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda/microvms/deployer.rb

Overview

Project-aware deployment helper: package, upload to S3, create MicroVM image.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project:, client: nil, s3: nil) ⇒ Deployer

rubocop:disable Naming/MethodParameterName



17
18
19
20
21
# File 'lib/lambda/microvms/deployer.rb', line 17

def initialize(project:, client: nil, s3: nil) # rubocop:disable Naming/MethodParameterName
  @project = project.validate!
  @client = client || Client.new(region: project.region, profile: project.profile)
  @s3 = s3 || build_s3
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



15
16
17
# File 'lib/lambda/microvms/deployer.rb', line 15

def client
  @client
end

#projectObject (readonly)

Returns the value of attribute project.



15
16
17
# File 'lib/lambda/microvms/deployer.rb', line 15

def project
  @project
end

#s3Object (readonly)

Returns the value of attribute s3.



15
16
17
# File 'lib/lambda/microvms/deployer.rb', line 15

def s3
  @s3
end

Instance Method Details

#deployImage

Package, upload, and create a MicroVM image.

Returns:

  • (Image)

    created image resource



44
45
46
47
48
49
# File 'lib/lambda/microvms/deployer.rb', line 44

def deploy
  ensure_microvm_contract!
  artifact = package
  artifact_uri = upload(artifact)
  client.create_image(**project.create_image_params(artifact_uri: artifact_uri))
end

#packageString

Package the project into a deployable zip artifact.

Returns:

  • (String)

    artifact path



26
27
28
# File 'lib/lambda/microvms/deployer.rb', line 26

def package
  Packager.new(project).package
end

#runMicroVM

Run the configured image with configured runtime parameters.

Returns:

  • (MicroVM)

    started MicroVM resource



54
55
56
57
58
59
# File 'lib/lambda/microvms/deployer.rb', line 54

def run
  ensure_microvm_contract!
  image_arn = project.require!('image.arn', project.image_arn)
  role_arn = project.require!('role_arn', project.role_arn)
  client.image(image_arn).run(**project.run_params, role_arn: role_arn)
end

#upload(path) ⇒ String

Upload an artifact to the configured S3 bucket and prefix.

Parameters:

  • path (String)

    local artifact path

Returns:

  • (String)

    S3 URI



34
35
36
37
38
39
# File 'lib/lambda/microvms/deployer.rb', line 34

def upload(path)
  bucket = project.require!('deployment.bucket', project.s3_bucket)
  key = [project.s3_prefix.sub(%r{/\z}, ''), File.basename(path)].join('/')
  File.open(path, 'rb') { |body| s3.put_object(bucket: bucket, key: key, body: body) }
  "s3://#{bucket}/#{key}"
end