Class: Dapp::Dimg::GitArtifact

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

Overview

Git repo artifact

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::Tar

#tar_gz_read, #tar_read, #tar_write

Constructor Details

#initialize(repo, to:, name: nil, branch: nil, commit: nil, cwd: nil, include_paths: nil, exclude_paths: nil, owner: nil, group: nil, stages_dependencies: {}) ⇒ GitArtifact

rubocop:disable Metrics/ParameterLists



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dapp/dimg/git_artifact.rb', line 13

def initialize(repo, to:, name: nil, branch: nil, commit: nil,
               cwd: nil, include_paths: nil, exclude_paths: nil, owner: nil, group: nil,
               stages_dependencies: {})
  @repo = repo
  @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

  @stages_dependencies = stages_dependencies
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#repoObject (readonly)

Returns the value of attribute repo.



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

def repo
  @repo
end

Instance Method Details

#apply_archive_command(stage) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dapp/dimg/git_artifact.rb', line 63

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, *archive_stage_commit(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, *archive_stage_commit(stage))} -C \"#{File.dirname(to)}\""
      end
    end
  end
end

#apply_patch_command(stage) ⇒ Object



86
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
113
114
115
116
117
118
119
# File 'lib/dapp/dimg/git_artifact.rb', line 86

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, *dev_patch_stage_commits(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, *dev_patch_stage_commits(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)


183
184
185
# File 'lib/dapp/dimg/git_artifact.rb', line 183

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

#archive_type(stage) ⇒ Object



82
83
84
# File 'lib/dapp/dimg/git_artifact.rb', line 82

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

rubocop:enable Metrics/ParameterLists



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dapp/dimg/git_artifact.rb', line 33

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(stage) ⇒ Object



165
166
167
168
169
# File 'lib/dapp/dimg/git_artifact.rb', line 165

def dev_patch_hash(stage)
  return unless dev_mode?

  hexdigest *diff_patches(latest_commit, nil).map {|patch| change_patch_new_file_path(stage, patch)}
end

#full_nameObject



179
180
181
# File 'lib/dapp/dimg/git_artifact.rb', line 179

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

#latest_commitObject



171
172
173
# File 'lib/dapp/dimg/git_artifact.rb', line 171

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

#paramshashObject



175
176
177
# File 'lib/dapp/dimg/git_artifact.rb', line 175

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

#patch_any_changes?(stage) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/dapp/dimg/git_artifact.rb', line 187

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

#patch_size(from_commit, to_commit) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/dapp/dimg/git_artifact.rb', line 148

def patch_size(from_commit, to_commit)
  diff_patches(from_commit, to_commit).reduce(0) do |bytes, patch|
    patch.hunks.each do |hunk|
      hunk.lines.each do |l|
        bytes +=
          case l.line_origin
          when :eof_newline_added, :eof_newline_removed then 1
          when :addition, :deletion, :binary            then l.content.size
          else # :context, :file_header, :hunk_header, :eof_no_newline
            0
          end
      end
    end
    bytes
  end
end

#stage_dependencies_checksum(stage) ⇒ Object



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

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