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.dimg.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 :"dapp-git-#{paramshash}-type" => 'directory'

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

        commands << "#{repo.dimg.dapp.install_bin} #{credentials.join(' ')} -d \"#{File.dirname(to)}\""
        commands << "#{sudo}#{repo.dimg.dapp.tar_bin} -xf #{archive_file(stage, *archive_stage_commits(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
        when :directory
          changed_files = diff_patches(*dev_patch_stage_commits(stage)).map {|p| "\"#{File.join(to, cwd, p.delta.new_file[:path])}\""}
          commands << "#{repo.dimg.dapp.rm_bin} -rf #{changed_files.join(' ')}"
          commands << "#{repo.dimg.dapp.install_bin} #{credentials.join(' ')} -d \"#{to}\""
          commands << "#{sudo}#{repo.dimg.dapp.tar_bin} -xf #{archive_file(stage, *dev_patch_stage_commits(stage))} -C \"#{to}\""
        when :file
          commands << "#{repo.dimg.dapp.rm_bin} -rf \"#{to}\""
          commands << "#{repo.dimg.dapp.install_bin} #{credentials.join(' ')} -d \"#{File.dirname(to)}\""
          commands << "#{sudo}#{repo.dimg.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
        when :directory
          commands << "#{repo.dimg.dapp.install_bin} #{credentials.join(' ')} -d \"#{to}\""
          commands << "#{sudo}#{repo.dimg.dapp.git_bin} apply --whitespace=nowarn --directory=\"#{to}\" --unsafe-paths #{patch_file(stage, *patch_stage_commits(stage))}"
        when :file
          commands << "#{repo.dimg.dapp.install_bin} #{credentials.join(' ')} -d \"#{File.dirname(to)}\""
          commands << "#{sudo}#{repo.dimg.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)


189
190
191
# File 'lib/dapp/dimg/git_artifact.rb', line 189

def archive_any_changes?(stage)
  any_changes?(*archive_stage_commits(stage))
end

#archive_typeObject



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

def archive_type
  repo.dimg.stage_by_name(:g_a_archive).image.labels["dapp-git-#{paramshash}-type"].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



169
170
171
172
173
174
175
# File 'lib/dapp/dimg/git_artifact.rb', line 169

def dev_patch_hash(stage)
  return unless dev_mode?

  Digest::SHA256.hexdigest(diff_patches(latest_commit, nil).map do |patch|
    change_patch_new_file_path(stage, patch)
  end.join(':::'))
end

#full_nameObject



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

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

#latest_commitObject



177
178
179
# File 'lib/dapp/dimg/git_artifact.rb', line 177

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

#paramshashObject



181
182
183
# File 'lib/dapp/dimg/git_artifact.rb', line 181

def paramshash
  Digest::SHA256.hexdigest [full_name, to, cwd, *include_paths, *exclude_paths, owner, group].map(&:to_s).join(':::')
end

#patch_any_changes?(stage) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/dapp/dimg/git_artifact.rb', line 193

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

#patch_size(from_commit, to_commit) ⇒ 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 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
147
148
149
150
# File 'lib/dapp/dimg/git_artifact.rb', line 121

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

  paths = (include_paths(true) + base_paths(stage_dependencies, true)).uniq
  to_commit = dev_mode? ? nil : latest_commit

  stage_dependencies_key = [stage.name, to_commit]
  @stage_dependencies_checksums ||= {}
  @stage_dependencies_checksums[stage_dependencies_key] = begin
    if @stage_dependencies_checksums.key?(stage_dependencies_key)
      @stage_dependencies_checksums[stage_dependencies_key]
    else
      if (patches = diff_patches(nil, to_commit, paths: paths)).empty?
        repo.dimg.dapp.log_warning(desc: { code: :stage_dependencies_not_found,
                                           data: { repo: repo.respond_to?(:url) ? repo.url : 'local',
                                                   dependencies: stage_dependencies.join(', ') } })
      end

      patches.sort_by {|patch| patch.delta.new_file[:path]}
        .reduce(nil) {|prev_hash, patch|
        Digest::SHA256.hexdigest [
          prev_hash,
          patch.delta.new_file[:path],
          patch.delta.new_file[:mode].to_s,
          patch.to_s
        ].compact.join(':::')
      }
    end
  end
end