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

def initialize(repo, dimg, to:, name: nil, branch: 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 || repo.branch
  @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



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/dapp/dimg/git_artifact.rb', line 178

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



201
202
203
204
205
206
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
# File 'lib/dapp/dimg/git_artifact.rb', line 201

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)


308
309
310
# File 'lib/dapp/dimg/git_artifact.rb', line 308

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

#archive_type(stage) ⇒ Object



197
198
199
# File 'lib/dapp/dimg/git_artifact.rb', line 197

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/dapp/dimg/git_artifact.rb', line 148

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
    commit = repo.lookup_commit(stage.layer_commit(self))

    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
end

#dev_patch_hash(**options) ⇒ Object



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

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



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

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], ignore_git_fetch: dimg.ignore_git_fetch)
    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



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

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



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

def embedded_artifacts
  [submodules_artifacts, nested_git_directory_artifacts].flatten
end

#embedded_inherit_path(path, embedded_rel_path) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/dapp/dimg/git_artifact.rb', line 122

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
      inherited_paths << (path_parts.empty? ? '**' : path_parts.join('/'))
      break
    end
  end

  inherited_paths
end

#embedded_inherit_paths(paths, embedded_rel_path) ⇒ Object



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

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)


316
317
318
# File 'lib/dapp/dimg/git_artifact.rb', line 316

def empty?
  repo_entries(latest_commit).empty?
end

#full_nameObject



304
305
306
# File 'lib/dapp/dimg/git_artifact.rb', line 304

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

#latest_commitObject



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

def latest_commit
  @latest_commit ||= begin
    (commit || repo.latest_commit(branch)).tap do |c|
      repo.dapp.log_info("Repository `#{repo.name}`: latest commit `#{c}` to `#{to}`") unless ignore_signature_auto_calculation
    end
  end
end

#nested_git_directory_artifact(patch) ⇒ Object



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

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



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

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



300
301
302
# File 'lib/dapp/dimg/git_artifact.rb', line 300

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

#patch_any_changes?(stage) ⇒ Boolean

Returns:

  • (Boolean)


312
313
314
# File 'lib/dapp/dimg/git_artifact.rb', line 312

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

#patch_size(from_commit) ⇒ Object



274
275
276
277
278
279
280
# File 'lib/dapp/dimg/git_artifact.rb', line 274

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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/dapp/dimg/git_artifact.rb', line 243

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_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?
        repo.dapp.log_warning(desc: { code: :stage_dependencies_not_found,
                                      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



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

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



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

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