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) ⇒ 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
# 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)
  @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
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

#apply_archive_command(stage) ⇒ Object



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

def apply_archive_command(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



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/dapp/dimg/git_artifact.rb', line 207

def apply_patch_command(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)


329
330
331
# File 'lib/dapp/dimg/git_artifact.rb', line 329

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

#archive_type(stage) ⇒ Object



203
204
205
# File 'lib/dapp/dimg/git_artifact.rb', line 203

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



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

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



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

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



291
292
293
294
295
296
297
298
299
# File 'lib/dapp/dimg/git_artifact.rb', line 291

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

#embedded_artifact(embedded_params) ⇒ Object



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

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



88
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
# File 'lib/dapp/dimg/git_artifact.rb', line 88

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
  end
end

#embedded_artifactsObject

rubocop:enable Metrics/ParameterLists



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

def embedded_artifacts
  [submodules_artifacts, nested_git_directory_artifacts].flatten
end

#embedded_inherit_path(path, embedded_rel_path) ⇒ Object



123
124
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
# File 'lib/dapp/dimg/git_artifact.rb', line 123

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



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

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)


337
338
339
# File 'lib/dapp/dimg/git_artifact.rb', line 337

def empty?
  repo_entries(latest_commit).empty?
end

#full_nameObject



325
326
327
# File 'lib/dapp/dimg/git_artifact.rb', line 325

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

#latest_commitObject



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/dapp/dimg/git_artifact.rb', line 301

def latest_commit
  @latest_commit ||= begin
    commit || begin
      if !tag.nil?
        repo.latest_tag_commit(tag)
      elsif !branch.nil?
        repo.latest_branch_commit(branch)
      else
        if local?
          repo.head_commit
        else
          repo.latest_branch_commit(repo.branch)
        end
      end
    end
  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



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

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



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

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



321
322
323
# File 'lib/dapp/dimg/git_artifact.rb', line 321

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

#patch_any_changes?(stage) ⇒ Boolean

Returns:

  • (Boolean)


333
334
335
# File 'lib/dapp/dimg/git_artifact.rb', line 333

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

#patch_size(from_commit) ⇒ Object



283
284
285
286
287
288
289
# File 'lib/dapp/dimg/git_artifact.rb', line 283

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

#stage_dependencies_checksum(stage) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/dapp/dimg/git_artifact.rb', line 249

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



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

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

#submodules_artifactsObject



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

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