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: {}) ⇒ 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
# 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: {})
  @repo = repo
  @dimg = dimg
  @name = name

  @branch = branch || repo.dapp.options[:git_artifact_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



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

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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/dapp/dimg/git_artifact.rb', line 189

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
          changed_files = diff_patches(*dev_patch_stage_commits(stage)).map {|p| "\"#{File.join(to, cwd, p.delta.new_file[:path])}\""}
          commands << "#{repo.dapp.rm_bin} -rf #{changed_files.join(' ')}"
          commands << "#{repo.dapp.install_bin} #{credentials.join(' ')} -d \"#{to}\""
          commands << "#{sudo}#{repo.dapp.tar_bin} -xf #{archive_file(stage)} -C \"#{to}\""
        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)


285
286
287
# File 'lib/dapp/dimg/git_artifact.rb', line 285

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

#archive_type(stage) ⇒ Object



185
186
187
# File 'lib/dapp/dimg/git_artifact.rb', line 185

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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/dapp/dimg/git_artifact.rb', line 136

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



263
264
265
266
267
268
269
270
271
# File 'lib/dapp/dimg/git_artifact.rb', line 263

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



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

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],
                                    branch: embedded_params[:branch],
                                    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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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]                 = embedded_inherit_path(cwd, embedded_rel_path).last
    options[: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[:owner]               = owner
    options[:group]               = group
  end
end

#embedded_artifactsObject

rubocop:enable Metrics/ParameterLists



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

def embedded_artifacts
  [submodules_artifacts, nested_git_directory_artifacts].flatten
end

#embedded_inherit_path(path, embedded_rel_path) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dapp/dimg/git_artifact.rb', line 116

def embedded_inherit_path(path, embedded_rel_path)
  path_parts      = path.split('/')
  test_path       = nil
  inherited_paths = []
  until path_parts.empty?
    last_part_path = path_parts.shift
    test_path      = [test_path, last_part_path].compact.join('/')

    non_match    = !File.fnmatch(test_path, embedded_rel_path, File::FNM_PATHNAME|File::FNM_DOTMATCH)
    part_for_all = (last_part_path == '**')

    if non_match || part_for_all
      inherited_paths << [last_part_path, path_parts].flatten.join('/')
      break unless part_for_all
    end
  end

  inherited_paths
end

#embedded_inherit_paths(paths, embedded_rel_path) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/dapp/dimg/git_artifact.rb', line 108

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)


293
294
295
# File 'lib/dapp/dimg/git_artifact.rb', line 293

def empty?
  repo_entries(latest_commit).empty?
end

#full_nameObject



281
282
283
# File 'lib/dapp/dimg/git_artifact.rb', line 281

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

#latest_commitObject



273
274
275
# File 'lib/dapp/dimg/git_artifact.rb', line 273

def latest_commit
  @latest_commit ||= (commit || repo.latest_commit(branch))
end

#nested_git_directory_artifact(patch) ⇒ Object



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

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



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

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



277
278
279
# File 'lib/dapp/dimg/git_artifact.rb', line 277

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

#patch_any_changes?(stage) ⇒ Boolean

Returns:

  • (Boolean)


289
290
291
# File 'lib/dapp/dimg/git_artifact.rb', line 289

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

#patch_size(from_commit) ⇒ Object



255
256
257
258
259
260
261
# File 'lib/dapp/dimg/git_artifact.rb', line 255

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



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/dapp/dimg/git_artifact.rb', line 224

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



48
49
50
51
52
# File 'lib/dapp/dimg/git_artifact.rb', line 48

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

#submodules_artifactsObject



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

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