Class: Dapp::StageImage

Inherits:
DockerImage show all
Defined in:
lib/dapp/stage_image.rb

Overview

StageImage

Instance Attribute Summary

Attributes inherited from DockerImage

#from, #name

Instance Method Summary collapse

Methods inherited from DockerImage

#id, #info, #pull!, #pulled?, #push!, #tagged?, #untag!

Methods included from Helper::Shellout

included, #shellout, #shellout!

Constructor Details

#initialize(name:, built_id: nil, from: nil) ⇒ StageImage

Returns a new instance of StageImage.



4
5
6
7
8
9
10
11
# File 'lib/dapp/stage_image.rb', line 4

def initialize(name:, built_id: nil, from: nil)
  @bash_commands = []
  @options = {}
  @change_options = {}
  @container_name = SecureRandom.hex
  @built_id = built_id
  super(name: name, from: from)
end

Instance Method Details

#add_change_cmd(value) ⇒ Object



29
30
31
# File 'lib/dapp/stage_image.rb', line 29

def add_change_cmd(value)
  add_change_option(:cmd, value)
end

#add_change_entrypoint(value) ⇒ Object



41
42
43
# File 'lib/dapp/stage_image.rb', line 41

def add_change_entrypoint(value)
  add_change_option(:entrypoint, value)
end

#add_change_env(value) ⇒ Object



21
22
23
# File 'lib/dapp/stage_image.rb', line 21

def add_change_env(value)
  add_change_option(:env, value)
end

#add_change_expose(value) ⇒ Object



17
18
19
# File 'lib/dapp/stage_image.rb', line 17

def add_change_expose(value)
  add_change_option(:expose, value)
end

#add_change_label(value) ⇒ Object



25
26
27
# File 'lib/dapp/stage_image.rb', line 25

def add_change_label(value)
  add_change_option(:label, value)
end

#add_change_onbuild(value) ⇒ Object



33
34
35
# File 'lib/dapp/stage_image.rb', line 33

def add_change_onbuild(value)
  add_change_option(:onbuild, value)
end

#add_change_user(value) ⇒ Object



45
46
47
# File 'lib/dapp/stage_image.rb', line 45

def add_change_user(value)
  add_change_option(:user, value)
end

#add_change_volume(value) ⇒ Object



13
14
15
# File 'lib/dapp/stage_image.rb', line 13

def add_change_volume(value)
  add_change_option(:volume, value)
end

#add_change_workdir(value) ⇒ Object



37
38
39
# File 'lib/dapp/stage_image.rb', line 37

def add_change_workdir(value)
  add_change_option(:workdir, value)
end

#add_commands(*commands) ⇒ Object



57
58
59
# File 'lib/dapp/stage_image.rb', line 57

def add_commands(*commands)
  @bash_commands.concat(commands.flatten)
end

#add_volume(value) ⇒ Object



49
50
51
# File 'lib/dapp/stage_image.rb', line 49

def add_volume(value)
  add_option(:volume, value)
end

#add_volumes_from(value) ⇒ Object



53
54
55
# File 'lib/dapp/stage_image.rb', line 53

def add_volumes_from(value)
  add_option(:'volumes-from', value)
end

#build!(**kvargs) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dapp/stage_image.rb', line 65

def build!(**kvargs)
  @built_id = if should_be_built?
                begin
                  run!(**kvargs)
                  commit!
                ensure
                  shellout("docker rm #{container_name}")
                end
              else
                from.built_id
              end
end

#built_idObject



61
62
63
# File 'lib/dapp/stage_image.rb', line 61

def built_id
  @built_id ||= id
end

#export!(name, log_verbose: false, log_time: false, force: false) ⇒ Object



78
79
80
81
82
83
# File 'lib/dapp/stage_image.rb', line 78

def export!(name, log_verbose: false, log_time: false, force: false)
  image = self.class.new(built_id: built_id, name: name)
  image.tag!(log_verbose: log_verbose, log_time: log_time, force: force)
  image.push!(log_verbose: log_verbose, log_time: log_time)
  image.untag!
end

#tag!(log_verbose: false, log_time: false, force: false) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/dapp/stage_image.rb', line 85

def tag!(log_verbose: false, log_time: false, force: false)
  if !(existed_id = id).nil? && !force
    raise Error::Build, code: :another_image_already_tagged if built_id != existed_id
    return
  end
  shellout!("docker tag #{built_id} #{name}", log_verbose: log_verbose, log_time: log_time)
end