Class: Cutlass::PackBuild

Inherits:
Object
  • Object
show all
Defined in:
lib/cutlass/pack_build.rb

Overview

Build an image with ‘pack` and cloud native buildpacks

begin
  build = PackBuild.new(app_dir: dir, buildpacks: ["heroku/ruby"], builder: "heroku/buildpacks:18")
  build.call

  build.stdout # => "...Successfully built image"
  build.success? # => true
ensure
  build.teardown
end

Instance Method Summary collapse

Constructor Details

#initialize(app_dir:, config: {}, builder: nil, buildpacks: [], image_name: Cutlass.default_image_name, exception_on_failure: true) ⇒ PackBuild

Returns a new instance of PackBuild.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cutlass/pack_build.rb', line 23

def initialize(
  app_dir:,
  config: {},
  builder: nil,
  buildpacks: [],
  image_name: Cutlass.default_image_name,
  exception_on_failure: true
)
  @app_dir = app_dir
  @builder = builder
  @image_name = image_name
  @env_arguments = config.map { |key, value| "--env #{key}=#{value}" }.join(" ")
  @exception_on_failure = exception_on_failure
  @image = nil
  @result = nil

  @buildpacks = Array(buildpacks).map do |buildpack|
    if buildpack.respond_to?(:name)
      buildpack.name
    else
      buildpack
    end
  end
end

Instance Method Details

#builder_argObject



110
111
112
# File 'lib/cutlass/pack_build.rb', line 110

def builder_arg
  "-B #{builder}" if builder
end

#callObject



71
72
73
74
75
76
77
78
# File 'lib/cutlass/pack_build.rb', line 71

def call
  puts pack_command if Cutlass.debug?
  call_pack

  puts @result.stdout if Cutlass.debug?
  puts @result.stderr if Cutlass.debug?
  self
end

#failed?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/cutlass/pack_build.rb', line 80

def failed?
  !success?
end

#image_idObject



48
49
50
51
# File 'lib/cutlass/pack_build.rb', line 48

def image_id
  raise "No image ID, container was not successfully built, #{error_message}" if @image.nil?
  @image.id
end

#pack_commandObject



114
115
116
# File 'lib/cutlass/pack_build.rb', line 114

def pack_command
  "pack build #{image_name} --path #{app_dir} #{builder_arg} --buildpack #{buildpacks.join(",")} #{env_arguments} #{"-v" if Cutlass.debug?}"
end

#resultObject



53
54
55
56
57
# File 'lib/cutlass/pack_build.rb', line 53

def result
  raise "Must execute method `call` first" unless @result

  @result
end

#stderrObject



67
68
69
# File 'lib/cutlass/pack_build.rb', line 67

def stderr
  result.stderr
end

#stdoutObject



63
64
65
# File 'lib/cutlass/pack_build.rb', line 63

def stdout
  result.stdout
end

#success?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/cutlass/pack_build.rb', line 84

def success?
  result.success?
end

#teardownObject



59
60
61
# File 'lib/cutlass/pack_build.rb', line 59

def teardown
  @image&.remove(force: true)
end