Class: Dapp::Dimg::Build::Stage::Base

Inherits:
Object
  • Object
show all
Includes:
Mod::Logging, Helper::Sha256, Helper::Trivia
Defined in:
lib/dapp/dimg/build/stage/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mod::Logging

#ignore_log_commands?, #image_should_be_introspected?, #image_should_be_introspected_after_build?, #image_should_be_introspected_before_build?, #log_build, #log_image_build, #log_image_commands, #log_image_created_at, #log_image_details, #log_image_instructions, #log_image_size, #log_name, #log_name_context, #log_state, #should_not_be_detailed?

Methods included from Helper::Trivia

#check_path?, #check_subpath?, #class_to_lowercase, class_to_lowercase, #delete_file, #ignore_path?, #ignore_path_base, #kwargs, #make_path, #path_checker, #search_file_upward

Methods included from Helper::Sha256

#hashsum, #paths_content_hashsum, #sha256

Constructor Details

#initialize(dimg, next_stage) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
18
# File 'lib/dapp/dimg/build/stage/base.rb', line 13

def initialize(dimg, next_stage)
  @dimg = dimg

  @next_stage = next_stage
  @next_stage.prev_stage = self
end

Instance Attribute Details

#dimgObject (readonly)

Returns the value of attribute dimg.



11
12
13
# File 'lib/dapp/dimg/build/stage/base.rb', line 11

def dimg
  @dimg
end

#next_stageObject

Returns the value of attribute next_stage.



10
11
12
# File 'lib/dapp/dimg/build/stage/base.rb', line 10

def next_stage
  @next_stage
end

#prev_stageObject

Returns the value of attribute prev_stage.



10
11
12
# File 'lib/dapp/dimg/build/stage/base.rb', line 10

def prev_stage
  @prev_stage
end

Instance Method Details

#adding_custom_dir_mountsObject



169
170
171
# File 'lib/dapp/dimg/build/stage/base.rb', line 169

def adding_custom_dir_mounts
  config_custom_dir_mounts.in_depth_merge(labels_custom_dir_mounts)
end

#adding_mounts_by_type(type) ⇒ Object



165
166
167
# File 'lib/dapp/dimg/build/stage/base.rb', line 165

def adding_mounts_by_type(type)
  (config_mounts_by_type(type) + labels_mounts_by_type(type)).uniq
end

#artifact?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/dapp/dimg/build/stage/base.rb', line 250

def artifact?
  false
end

#build!Object

rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity



86
87
88
89
90
# File 'lib/dapp/dimg/build/stage/base.rb', line 86

def build!
  prev_stage.build! if prev_stage
  renew             if should_be_renewed?
  image_build
end

#build_lock!Object

rubocop:disable Metrics/PerceivedComplexity



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/dapp/dimg/build/stage/base.rb', line 54

def build_lock!
  return yield if dimg.dapp.dry_run?

  try_lock = proc do
    next yield unless image_should_be_locked?

    no_lock = false

    dimg.dapp.lock("#{dimg.dapp.name}.image.#{image.name}") do
      image.reset_image_inspect

      if image_should_be_locked?
        yield
      else
        stage = self
        stage.renew until (stage = stage.next_stage).nil?

        no_lock = true
      end
    end

    yield if no_lock
  end

  if prev_stage
    prev_stage.build_lock! { try_lock.call }
  else
    try_lock.call
  end
end

#builder_checksumObject



223
224
# File 'lib/dapp/dimg/build/stage/base.rb', line 223

def builder_checksum
end

#config_custom_dir_mountsObject



173
174
175
176
177
178
179
180
# File 'lib/dapp/dimg/build/stage/base.rb', line 173

def config_custom_dir_mounts
  dimg.config._custom_dir_mount.reduce({}) do |mounts, mount|
    from_path = File.expand_path(mount._from)
    mounts[from_path] ||= []
    mounts[from_path] << mount._to
    mounts
  end
end

#config_mounts_by_type(type) ⇒ Object



182
183
184
# File 'lib/dapp/dimg/build/stage/base.rb', line 182

def config_mounts_by_type(type)
  dimg.config.public_send("_#{type}_mount").map(&:_to)
end

#dependenciesObject



242
243
244
# File 'lib/dapp/dimg/build/stage/base.rb', line 242

def dependencies
  []
end

#dependencies_discardObject



246
247
248
# File 'lib/dapp/dimg/build/stage/base.rb', line 246

def dependencies_discard
  @dependencies = nil
end

#dependencies_empty?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/dapp/dimg/build/stage/base.rb', line 201

def dependencies_empty?
  dependencies.flatten.compact.delete_if { |val| val.respond_to?(:empty?) && val.empty? }.empty?
end

#empty?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/dapp/dimg/build/stage/base.rb', line 197

def empty?
  dependencies_empty?
end

#g_a_stage?Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/dapp/dimg/build/stage/base.rb', line 254

def g_a_stage?
  false
end

#get_ruby2go_state_hashObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dapp/dimg/build/stage/base.rb', line 20

def get_ruby2go_state_hash
  {}.tap {|hash|
    # NOTICE: Delete this when stage has been moved to go.
    # NOTICE: This is data for build.StubStage and build.StubImage.

    hash["Image"] = {
      "Labels" => image.labels
        .map{|k,v| [k.to_s, v.to_s]}
        .to_h,
      "ServiceChangeLabels" => image.send(:service_change_options)
        .fetch(:label, {})
        .map{|k,v| [k.to_s, v.to_s]}
        .to_h,
    }

    if prev_stage
      hash["PrevStage"] = prev_stage.get_ruby2go_state_hash
    end
  }
end

#git_artifacts_dependenciesObject



226
227
228
# File 'lib/dapp/dimg/build/stage/base.rb', line 226

def git_artifacts_dependencies
  dimg.git_artifacts.map { |git_artifact| git_artifact.stage_dependencies_checksum(self) }
end

#imageObject



98
99
100
101
102
103
104
105
106
# File 'lib/dapp/dimg/build/stage/base.rb', line 98

def image
  @image ||= begin
    if empty?
      prev_stage.image
    else
      Image::Stage.image_by_name(name: image_name, from: from_image, dapp: dimg.dapp)
    end
  end
end

#image_add_custom_mountsObject



146
147
148
149
150
151
152
# File 'lib/dapp/dimg/build/stage/base.rb', line 146

def image_add_custom_mounts
  adding_custom_dir_mounts.each do |from, to_pathes|
    FileUtils.mkdir_p(from) unless File.exist?(from)
    to_pathes.tap(&:uniq!).map { |to_path| image.add_volume "#{from}:#{to_path}" }
    image.add_service_change_label :"dapp-mount-custom-dir-#{from.gsub('/', '--')}" => to_pathes.join(';')
  end
end

#image_add_mountsObject



127
128
129
130
131
132
# File 'lib/dapp/dimg/build/stage/base.rb', line 127

def image_add_mounts
  image_add_service_mounts
  image_add_custom_mounts

  image_add_mounts_labels
end

#image_add_mounts_labelsObject



154
155
156
157
158
159
160
161
162
163
# File 'lib/dapp/dimg/build/stage/base.rb', line 154

def image_add_mounts_labels
  [:tmp_dir, :build_dir].each do |type|
    next if (mounts = adding_mounts_by_type(type)).empty?
    image.add_service_change_label :"dapp-mount-#{type.to_s.tr('_', '-')}" => mounts.join(';')
  end

  adding_custom_dir_mounts.each do |from, to_pathes|
    image.add_service_change_label :"dapp-mount-custom-dir-#{from.gsub('/', '--')}" => to_pathes.join(';')
  end
end

#image_add_service_mountsObject



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dapp/dimg/build/stage/base.rb', line 134

def image_add_service_mounts
  [:tmp_dir, :build_dir].each do |type|
    next if (mounts = adding_mounts_by_type(type)).empty?

    mounts.each do |path|
      absolute_path = File.expand_path(File.join('/', path))
      tmp_path = dimg.send(type, 'mount', dimg.dapp.consistent_uniq_slugify(absolute_path)).tap(&:mkpath)
      image.add_volume "#{tmp_path}:#{absolute_path}"
    end
  end
end

#labels_custom_dir_mountsObject



190
191
192
193
194
195
# File 'lib/dapp/dimg/build/stage/base.rb', line 190

def labels_custom_dir_mounts
  from_image.labels.map do |label, value|
    next unless label =~ /dapp-mount-custom-dir-(?<from>.+)/
    [File.expand_path(Regexp.last_match(:from).gsub('--', '/')), value.split(';')]
  end.compact.to_h
end

#labels_mounts_by_type(type) ⇒ Object



186
187
188
# File 'lib/dapp/dimg/build/stage/base.rb', line 186

def labels_mounts_by_type(type)
  from_image.labels.select { |l, _| l == "dapp-mount-#{type.to_s.tr('_', '-')}" }.map { |_, value| value.split(';') }.flatten
end

#layer_commit(git_artifact) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/dapp/dimg/build/stage/base.rb', line 230

def layer_commit(git_artifact)
  commits[git_artifact] ||= begin
    if image.built?
      image.labels[dimg.dapp.dimgstage_g_a_commit_label(git_artifact.paramshash)]
    elsif g_a_stage? && !empty?
      git_artifact.latest_commit
    elsif prev_stage
      prev_stage.layer_commit(git_artifact)
    end
  end
end

#nameObject



219
220
221
# File 'lib/dapp/dimg/build/stage/base.rb', line 219

def name
  class_to_lowercase.to_sym
end

#prepare_imageObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dapp/dimg/build/stage/base.rb', line 108

def prepare_image
  return if dimg.dapp.dry_run?

  image_add_mounts

  image.add_service_change_label dapp: dimg.stage_dapp_label
  image.add_service_change_label 'dapp-version'.to_sym => ::Dapp::VERSION
  image.add_service_change_label 'dapp-cache-version'.to_sym => ::Dapp::BUILD_CACHE_VERSION
  image.add_service_change_label 'dapp-dimg'.to_sym => false
  image.add_service_change_label 'dapp-dev-mode'.to_sym => true if dimg.dev_mode?

  if dimg.dapp.ssh_auth_sock
    image.add_volume "#{dimg.dapp.ssh_auth_sock}:/tmp/dapp-ssh-agent"
    image.add_env SSH_AUTH_SOCK: '/tmp/dapp-ssh-agent'
  end

  yield if block_given?
end

#save_in_cache!Object



92
93
94
95
96
# File 'lib/dapp/dimg/build/stage/base.rb', line 92

def save_in_cache!
  prev_stage.save_in_cache! if prev_stage
  return unless should_be_tagged?
  image.save_in_cache! unless dimg.dapp.dry_run?
end

#set_ruby2go_state_hash(hash) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dapp/dimg/build/stage/base.rb', line 41

def set_ruby2go_state_hash(hash)
  if prev_stage
    prev_stage.set_ruby2go_state_hash(hash["PrevStage"])
  end

  # NOTICE: This is data from build.StubImage.

  hash["Image"]["ServiceChangeLabels"].each do |k, v|
    image.add_service_change_label(k.to_sym => v)
  end
end

#signatureObject



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/dapp/dimg/build/stage/base.rb', line 205

def signature
  if empty?
    prev_stage.signature
  else
    args = []
    args << prev_stage.signature unless prev_stage.nil?
    args << dimg.build_cache_version
    args << builder_checksum
    args.concat(dependencies.flatten)

    hashsum args
  end
end