Class: Dapp::Dimg::GitArtifact

Inherits:
Object
  • Object
show all
Includes:
Helper::Tar, Helper::Trivia
Defined in:
lib/dapp/dimg/git_artifact.rb

Overview

Git repo artifact

Instance Attribute Summary collapse

Instance Method Summary collapse

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::Tar

#tar_gz_read, #tar_read, #tar_write

Constructor Details

#initialize(repo, dimg, to:, name: nil, branch: nil, tag: nil, commit: nil, cwd: nil, include_paths: nil, exclude_paths: nil, owner: nil, group: nil, as: nil, stages_dependencies: {}, ignore_signature_auto_calculation: false, disable_go_git: nil) ⇒ GitArtifact

rubocop:disable Metrics/ParameterLists



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dapp/dimg/git_artifact.rb', line 15

def initialize(repo, dimg, to:, name: nil, branch: nil, tag: nil, commit: nil,
               cwd: nil, include_paths: nil, exclude_paths: nil, owner: nil, group: nil, as: nil,
               stages_dependencies: {}, ignore_signature_auto_calculation: false, disable_go_git: nil)
  @repo = repo
  @dimg = dimg
  @name = name

  @ignore_signature_auto_calculation = ignore_signature_auto_calculation

  @branch = branch
  @tag = tag
  @commit = commit

  @to = to
  @cwd = (cwd.nil? || cwd.empty? || cwd == '/') ? '' : File.expand_path(File.join('/', cwd, '/'))[1..-1]
  @include_paths = include_paths
  @exclude_paths = exclude_paths
  @owner = owner
  @group = group
  @as = as

  @stages_dependencies = stages_dependencies
  @disable_go_git = disable_go_git unless disable_go_git.nil?
end

Instance Attribute Details

#asObject (readonly)

Returns the value of attribute as.



10
11
12
# File 'lib/dapp/dimg/git_artifact.rb', line 10

def as
  @as
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/dapp/dimg/git_artifact.rb', line 9

def name
  @name
end

#repoObject (readonly)

Returns the value of attribute repo.



8
9
10
# File 'lib/dapp/dimg/git_artifact.rb', line 8

def repo
  @repo
end

Instance Method Details

#_stages_mapObject



393
394
395
396
397
398
399
400
401
# File 'lib/dapp/dimg/git_artifact.rb', line 393

def _stages_map
  {
    before_install: "beforeInstall",
    install: "install",
    before_setup: "beforeSetup",
    setup: "setup",
    build_artifact: "buildArtifact",
  }
end

#_stages_map_reversedObject



403
404
405
# File 'lib/dapp/dimg/git_artifact.rb', line 403

def _stages_map_reversed
  _stages_map.map {|k, v| [v, k]}.to_h
end

#apply_archive_command(stage) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/dapp/dimg/git_artifact.rb', line 199

def apply_archive_command(stage)
  return apply_archive_command_old(stage) if disable_go_git?

  res = repo.dapp.ruby2go_git_artifact(
    "GitArtifact" => JSON.dump(get_ruby2go_state_hash),
    "method" => "ApplyArchiveCommand",
    "Stage" => JSON.dump(get_stub_stage_state(stage)),
  )

  raise res["error"] if res["error"]

  self.set_ruby2go_state_hash(JSON.load(res["data"]["GitArtifact"]))
  stage.set_ruby2go_state_hash(JSON.load(res["data"]["Stage"]))

  Array(res["data"]["result"])
end

#apply_archive_command_old(stage) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/dapp/dimg/git_artifact.rb', line 216

def apply_archive_command_old(stage)
  [].tap do |commands|
    if archive_any_changes?(stage)
      case cwd_type(stage)
      when :directory
        stage.image.add_service_change_label(repo.dapp.dimgstage_g_a_type_label(paramshash).to_sym => 'directory')

        commands << "#{repo.dapp.install_bin} #{credentials.join(' ')} -d \"#{to}\""
        commands << "#{sudo} #{repo.dapp.tar_bin} -xf #{archive_file(stage)} -C \"#{to}\""
      when :file
        stage.image.add_service_change_label(repo.dapp.dimgstage_g_a_type_label(paramshash).to_sym => 'file')

        commands << "#{repo.dapp.install_bin} #{credentials.join(' ')} -d \"#{File.dirname(to)}\""
        commands << "#{sudo} #{repo.dapp.tar_bin} -xf #{archive_file(stage)} -C \"#{File.dirname(to)}\""
      end
    end
  end
end

#apply_patch_command(stage) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/dapp/dimg/git_artifact.rb', line 251

def apply_patch_command(stage)
  return apply_patch_command_old(stage) if disable_go_git?

  res = repo.dapp.ruby2go_git_artifact(
    "GitArtifact" => JSON.dump(get_ruby2go_state_hash),
    "method" => "ApplyPatchCommand",
    "Stage" => JSON.dump(get_stub_stage_state(stage)),
  )

  raise res["error"] if res["error"]

  self.set_ruby2go_state_hash(JSON.load(res["data"]["GitArtifact"]))
  stage.set_ruby2go_state_hash(JSON.load(res["data"]["Stage"]))

  Array(res["data"]["result"])
end

#apply_patch_command_old(stage) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/dapp/dimg/git_artifact.rb', line 268

def apply_patch_command_old(stage)
  [].tap do |commands|
    if dev_mode?
      if any_changes?(*dev_patch_stage_commits(stage))
        case archive_type(stage)
        when :directory
          files_to_remove_file_name = file_name('dev_files_to_remove')
          File.open(dimg.tmp_path('archives', files_to_remove_file_name), File::RDWR | File::CREAT) do |f|
            diff_patches(*dev_patch_stage_commits(stage))
              .map {|p| File.join(to, cwd, p.delta.new_file[:path])}
              .each(&f.method(:puts))
          end

          commands << "#{repo.dapp.rm_bin} -rf $(#{repo.dapp.cat_bin} #{dimg.container_tmp_path('archives', files_to_remove_file_name)})"
          commands << "#{repo.dapp.install_bin} #{credentials.join(' ')} -d \"#{to}\""
          commands << "#{sudo} #{repo.dapp.tar_bin} -xf #{archive_file(stage)} -C \"#{to}\""
          commands << "#{repo.dapp.find_bin} \"#{to}\" -empty -type d -delete"
        when :file
          commands << "#{repo.dapp.rm_bin} -rf \"#{to}\""
          commands << "#{repo.dapp.install_bin} #{credentials.join(' ')} -d \"#{File.dirname(to)}\""
          commands << "#{sudo} #{repo.dapp.tar_bin} -xf #{archive_file(stage)} -C \"#{File.dirname(to)}\""
        else
          raise
        end
      end
    else
      if patch_any_changes?(stage)
        case archive_type(stage)
        when :directory
          commands << "#{repo.dapp.install_bin} #{credentials.join(' ')} -d \"#{to}\""
          commands << "#{sudo} #{repo.dapp.git_bin} apply --whitespace=nowarn --directory=\"#{to}\" --unsafe-paths #{patch_file(stage, *patch_stage_commits(stage))}"
        when :file
          commands << "#{repo.dapp.install_bin} #{credentials.join(' ')} -d \"#{File.dirname(to)}\""
          commands << "#{sudo} #{repo.dapp.git_bin} apply --whitespace=nowarn --directory=\"#{File.dirname(to)}\" --unsafe-paths #{patch_file(stage, *patch_stage_commits(stage))}"
        else
          raise
        end
      end
    end
  end
end

#archive_any_changes?(stage) ⇒ Boolean

Returns:

  • (Boolean)


450
451
452
# File 'lib/dapp/dimg/git_artifact.rb', line 450

def archive_any_changes?(stage)
  repo_entries(stage_commit(stage)).any?
end

#archive_type(stage) ⇒ Object



235
236
237
# File 'lib/dapp/dimg/git_artifact.rb', line 235

def archive_type(stage)
  stage.prev_stage.image.labels[repo.dapp.dimgstage_g_a_type_label(paramshash)].to_s.to_sym
end

#cwd_type(stage) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/dapp/dimg/git_artifact.rb', line 154

def cwd_type(stage)
  if dev_mode?
    p = repo.workdir_path.join(cwd)
    if p.exist?
      if p.directory?
        :directory
      else
        :file
      end
    end
  elsif cwd == ''
    :directory
  else
    cwd_type_by_commit(repo.lookup_commit(stage.layer_commit(self)))
  end
end

#cwd_type_by_commit(commit) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/dapp/dimg/git_artifact.rb', line 171

def cwd_type_by_commit(commit)
  cwd_entry = begin
    commit.tree.path(cwd)
  rescue Rugged::TreeError
  end

  if cwd_entry
    if cwd_entry[:type] == :tree
      :directory
    else
      :file
    end
  end
end

#dev_patch_hash(**options) ⇒ Object



352
353
354
355
356
357
358
359
360
# File 'lib/dapp/dimg/git_artifact.rb', line 352

def dev_patch_hash(**options)
  return unless dev_mode?
  hexdigest begin
    diff_patches(nil, nil, **options).map do |patch|
      file = patch.delta.new_file
      [file[:path], File.read(File.join(repo.workdir_path, file[:path])), file[:mode]]
    end
  end
end

#disable_go_git?Boolean

Returns:

  • (Boolean)


186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/dapp/dimg/git_artifact.rb', line 186

def disable_go_git?
  return @disable_go_git unless @disable_go_git.nil?

  @disable_go_git = (dev_mode? || !!ENV["DAPP_DISABLE_GO_GIT"] || begin
    commit = dev_mode? ? nil : latest_commit
    repo.submodules(
      commit,
      paths: include_paths_or_cwd,
      exclude_paths: exclude_paths(true)
    ).any?
  end)
end

#embedded_artifact(embedded_params) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dapp/dimg/git_artifact.rb', line 73

def embedded_artifact(embedded_params)
  embedded_rel_path = embedded_params[:path]
  embedded_repo     = begin
    if embedded_params[:type] == :remote
      GitRepo::Remote.get_or_create(repo.dapp, embedded_rel_path, url: embedded_params[:url])
    elsif embedded_params[:type] == :local
      embedded_path = File.join(repo.workdir_path, embedded_params[:path])
      GitRepo::Local.new(repo.dapp, embedded_rel_path, embedded_path)
    else
      raise
    end
  end

  self.class.new(embedded_repo, dimg, embedded_artifact_options(embedded_params))
end

#embedded_artifact_options(embedded_params) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dapp/dimg/git_artifact.rb', line 89

def embedded_artifact_options(embedded_params)
  embedded_rel_path = embedded_params[:path]

  {}.tap do |options|
    options[:name]                = repo.dapp.consistent_uniq_slugify("embedded-#{embedded_rel_path}")
    options[:cwd]                 = begin
      subpath = Pathname(cwd).subpath_of(embedded_rel_path)
      subpath = '' if subpath == '.'
      subpath
    end
    options[:to]                  = Pathname(cwd).subpath_of?(embedded_rel_path) ? to : File.join(to, embedded_rel_path)
    options[:include_paths]       = embedded_inherit_paths(include_paths, embedded_rel_path)
    options[:exclude_paths]       = embedded_inherit_paths(exclude_paths, embedded_rel_path)
    options[:stages_dependencies] = begin
      stages_dependencies
        .map { |stage, paths| [stage, embedded_inherit_paths(paths, embedded_rel_path)] }
        .to_h
    end
    options[:branch]              = embedded_params[:branch]
    options[:commit]              = embedded_params[:commit]
    options[:owner]               = owner
    options[:group]               = group

    options[:ignore_signature_auto_calculation] = ignore_signature_auto_calculation
    options[:disable_go_git] = embedded_params[:disable_go_git]
  end
end

#embedded_artifactsObject

rubocop:enable Metrics/ParameterLists



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

def embedded_artifacts
  [submodules_artifacts, nested_git_directory_artifacts].flatten
end

#embedded_inherit_path(path, embedded_rel_path) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/dapp/dimg/git_artifact.rb', line 125

def embedded_inherit_path(path, embedded_rel_path)
  path_parts      = path.split('/')
  test_path       = nil
  inherited_paths = []

  until path_parts.empty?
    current_path_part = path_parts.shift
    test_path         = [test_path, current_path_part].compact.join('/')

    match = File.fnmatch(test_path, embedded_rel_path, File::FNM_PATHNAME|File::FNM_DOTMATCH)
    break unless match || File.fnmatch(File.join(test_path, '**', '*'), embedded_rel_path, File::FNM_PATHNAME|File::FNM_DOTMATCH)

    any = (current_path_part == '**')

    if any
      inherited_paths << [current_path_part, path_parts].flatten.join('/')
      inherited_paths << path_parts.join('/') unless path_parts.empty?
    elsif match && !path_parts.empty?
      inherited_paths << path_parts.join('/')
      break
    elsif path_parts.empty?
      inherited_paths << '**'
      break
    end
  end

  inherited_paths
end

#embedded_inherit_paths(paths, embedded_rel_path) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/dapp/dimg/git_artifact.rb', line 117

def embedded_inherit_paths(paths, embedded_rel_path)
  paths
    .select { |path| check_path?(embedded_rel_path, path) || check_subpath?(embedded_rel_path, path) }
    .map { |path| embedded_inherit_path(path, embedded_rel_path) }
    .flatten
    .compact
end

#empty?Boolean

Returns:

  • (Boolean)


458
459
460
# File 'lib/dapp/dimg/git_artifact.rb', line 458

def empty?
  repo_entries(latest_commit).empty?
end

#full_nameObject



446
447
448
# File 'lib/dapp/dimg/git_artifact.rb', line 446

def full_name
  "#{repo.name}#{name ? "_#{name}" : nil}"
end

#get_ruby2go_state_hashObject



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/dapp/dimg/git_artifact.rb', line 362

def get_ruby2go_state_hash
  {
    "Name" => @name.to_s,
    "As" => @as.to_s,
    "Branch" => @branch.to_s,
    "Tag" => @tag.to_s,
    "Commit" => @commit.to_s,
    "To" => @to.to_s,
    "Cwd" => @cwd.to_s,
    "RepoPath" => File.join("/", @cwd.to_s),
    "Owner" => @owner.to_s,
    "Group" => @group.to_s,
    "IncludePaths" => include_paths(true),
    "ExcludePaths" => exclude_paths(true),
    "StagesDependencies" => @stages_dependencies.map {|k, v| [_stages_map[k], Array(v).map(&:to_s)]}.to_h,
    "Paramshash" => paramshash.to_s,
    "PatchesDir" => dimg.tmp_path('patches'),
    "ContainerPatchesDir" => dimg.container_tmp_path('patches'),
    "ArchivesDir" => dimg.tmp_path('archives'),
    "ContainerArchivesDir" => dimg.container_tmp_path('archives'),
  }.tap {|res|
    if repo.is_a? ::Dapp::Dimg::GitRepo::Local
      res["LocalGitRepo"] = repo.get_ruby2go_state_hash
    elsif repo.is_a? ::Dapp::Dimg::GitRepo::Remote
      res["RemoteGitRepo"] = repo.get_ruby2go_state_hash
    else
      raise
    end
  }
end

#get_stub_stage_state(stage) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
# File 'lib/dapp/dimg/git_artifact.rb', line 239

def get_stub_stage_state(stage)
  stage.get_ruby2go_state_hash.tap do |stage_state|
    # Data for StubStage specific for ApplyPatchCommand
    stage_state["LayerCommitMap"] = {
      paramshash => stage.layer_commit(self),
    }
    stage_state["PrevStage"]["LayerCommitMap" ] = {
      paramshash => stage.prev_stage.layer_commit(self),
    }
  end
end

#latest_commitObject



428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/dapp/dimg/git_artifact.rb', line 428

def latest_commit
  @latest_commit ||= begin
    res = repo.dapp.ruby2go_git_artifact("GitArtifact" => JSON.dump(get_ruby2go_state_hash), "method" => "LatestCommit")

    raise res["error"] if res["error"]

    self.set_ruby2go_state_hash(JSON.load(res["data"]["GitArtifact"]))

    res["data"]["result"]
  end.tap do |c|
    repo.dapp.log_info("Repository `#{repo.name}`: latest commit `#{c}` to `#{to}`") unless ignore_signature_auto_calculation
  end
end

#nested_git_directory_artifact(patch) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/dapp/dimg/git_artifact.rb', line 65

def nested_git_directory_artifact(patch)
  params = {}.tap do |p|
    p[:path] = patch.delta.new_file[:path]
    p[:type] = :local
  end
  embedded_artifact(params)
end

#nested_git_directory_artifactsObject



58
59
60
61
62
63
# File 'lib/dapp/dimg/git_artifact.rb', line 58

def nested_git_directory_artifacts
  return [] unless dev_mode?
  repo
    .nested_git_directories_patches(paths: include_paths_or_cwd, exclude_paths: exclude_paths(true), **diff_patches_options)
    .map(&method(:nested_git_directory_artifact))
end

#paramshashObject



442
443
444
# File 'lib/dapp/dimg/git_artifact.rb', line 442

def paramshash
  hexdigest full_name, to, cwd, *include_paths, *exclude_paths, owner, group
end

#patch_any_changes?(stage) ⇒ Boolean

Returns:

  • (Boolean)


454
455
456
# File 'lib/dapp/dimg/git_artifact.rb', line 454

def patch_any_changes?(stage)
  any_changes?(*patch_stage_commits(stage))
end

#patch_size(from_commit) ⇒ Object



344
345
346
347
348
349
350
# File 'lib/dapp/dimg/git_artifact.rb', line 344

def patch_size(from_commit)
  to_commit = dev_mode? ? nil : latest_commit
  diff_patches(from_commit, to_commit).reduce(0) do |bytes, patch|
    bytes += patch.delta.deleted? ? patch.delta.old_file[:size] : patch.delta.new_file[:size]
    bytes
  end
end

#set_ruby2go_state_hash(new_state) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/dapp/dimg/git_artifact.rb', line 407

def set_ruby2go_state_hash(new_state)
  [
    [:@name, new_state["Name"]],
    [:@as, new_state["As"]],
    [:@branch, new_state["Branch"]],
    [:@tag, new_state["Tag"]],
    [:@commit, new_state["Commit"]],
    [:@cwd, new_state["Cwd"]],
    [:@owner, new_state["Owner"]],
    [:@group, new_state["Group"]],
  ].each do |var, new_value|
    if new_value != ""
      instance_variable_set(var, new_value)
    end
  end

  @stages_dependencies = new_state["StagesDependencies"].map do |k, v|
    [_stages_map_reversed[k], v]
  end.to_h
end

#stage_dependencies_checksum(stage) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/dapp/dimg/git_artifact.rb', line 310

def stage_dependencies_checksum(stage)
  return [] if (stage_dependencies = stages_dependencies[stage.name]).empty?

  paths = base_paths(stage_dependencies, true)
  commit = dev_mode? ? nil : latest_commit

  stage_dependencies_key = [stage.name, commit]

  @stage_dependencies_warning ||= {}
  @stage_dependencies_checksums ||= {}
  @stage_dependencies_checksums[stage_dependencies_key] ||= begin
    if dev_mode?
      dev_patch_hash(paths: paths)
    else
      if (entries = repo_entries(commit, paths: paths)).empty? && @stage_dependencies_warning[stage.name].nil?
        @stage_dependencies_warning[stage.name] = true
        code = cwd_type_by_commit(repo.lookup_commit(commit)) == :file ? :stage_dependencies_file_not_found : :stage_dependencies_not_found
        repo.dapp.log_warning(desc: { code: code,
                                      data: { repo: repo.respond_to?(:url) ? repo.url : 'local',
                                              dependencies: stage_dependencies.join(', ') } })
      end

      entries
        .sort_by {|root, entry| File.join(root, entry[:name])}
        .reduce(nil) {|prev_hash, (root, entry)|
          content = nil
          content = repo.lookup_object(entry[:oid]).content if entry[:type] == :blob

          hexdigest prev_hash, File.join(root, entry[:name]), entry[:filemode].to_s, content
        }
    end
  end
end

#submodule_artifact(submodule_params) ⇒ Object



52
53
54
55
56
# File 'lib/dapp/dimg/git_artifact.rb', line 52

def submodule_artifact(submodule_params)
  embedded_artifact(**submodule_params, disable_go_git: true)
rescue Rugged::InvalidError => e
  raise Error::Rugged, code: :git_local_incorrect_gitmodules_params, data: { error: e.message }
end

#submodules_artifactsObject



45
46
47
48
49
50
# File 'lib/dapp/dimg/git_artifact.rb', line 45

def submodules_artifacts
  commit = dev_mode? ? nil : latest_commit
  repo.submodules_params(commit,
                         paths: include_paths_or_cwd,
                         exclude_paths: exclude_paths(true)).map(&method(:submodule_artifact))
end