Module: Debian

Included in:
MPRM::Repo
Defined in:
lib/mprm/debian.rb

Instance Method Summary collapse

Instance Method Details

#build_apt_repo(path, component, arch, release, label, origin, gpg, nocache) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mprm/debian.rb', line 12

def build_apt_repo(path, component, arch, release, label, origin, gpg, nocache)
  release.each { |r|
    component.each { |c|
      arch.each { |a|
        fpath = path + "/dists/" + r + "/" + c + "/" + "binary-" + a + "/"
        pfpath = fpath + "Packages"
        rfpath = fpath + "Release"

        MPRM.logger.debug "Building Path: #{fpath}"

        FileUtils.mkpath(fpath)
        FileUtils.touch(pfpath)
        FileUtils.touch(rfpath)
        generate_packages_gz(fpath,pfpath,path,rfpath,r,c,a)
      }
    }
    generate_release(path,r,component,arch,label,origin)

    unless gpg == false
      generate_release_gpg(path,r, gpg)
    end
  }
end

#generate_packages_gz(fpath, pfpath, path, rfpath, r, c, a) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
120
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
# File 'lib/mprm/debian.rb', line 72

def generate_packages_gz(fpath,pfpath,path,rfpath,r,c,a)
  MPRM.logger.debug "Generating Packages: #{r} : #{c} : binary-#{a}"

  npath = "dists/" + r + "/" + c + "/" + "binary-" + a + "/"

  d = File.open(pfpath, "w+")
  write_mutex = Mutex.new

  Parallel.each(Dir.glob("#{fpath}*.deb"), in_threads: 5) do |deb|
    algs = {
      'md5' => Digest::MD5.new,
      'sha1' => Digest::SHA1.new,
      'sha256' => Digest::SHA256.new
    }
    sums = {
      'md5' => '',
      'sha1' => '',
      'sha256' => ''
    }
    tdeb = File.basename(deb)
    init_size = File.size(deb)
    deb_contents = nil

    FileUtils.mkdir_p "tmp/#{tdeb}/"
    if not nocache
      sums.keys.each do |s|
        sum_path = "#{path}/dists/#{r}/#{c}/binary-#{a}/#{s}-results/#{tdeb}"
        FileUtils.mkdir_p File.dirname(sum_path)

        if File.exist?(sum_path)
          stored_sum = File.read(sum_path)
          sum = stored_sum unless nocache.nil?
        end

        unless sum
          deb_contents ||= File.read(deb)
          sum = algs[s].hexdigest(deb_contents)
        end

        sums[s] = sum
        if nocache.nil?
          File.open(sum_path, 'w') { |f| f.write(sum) }
        elsif sum != stored_sum
          MPRM.logger.debug "WARN: #{s}sum mismatch on #{deb}\n"
        end
      end
    end
    MPRM.logger.debug `ar p #{deb} control.tar.gz | tar zx -C tmp/#{tdeb}/`

    package_info = [
      "Filename: #{npath}#{s3_compatible_encode(tdeb)}",
      "MD5: #{sums['md5']}",
      "SHA1: #{sums['sha1']}",
      "SHA256: #{sums['sha256']}",
      "Size: #{init_size}"
    ]

    write_mutex.synchronize do
      # Copy the control file data into the Packages list
      d.write(File.read("tmp/#{tdeb}/control").gsub!(/\n+/, "\n"))
      d.write(package_info.join("\n"))
      d.write("\n\n") # blank line between package info in the Packages file
    end
  end

  FileUtils.rmtree 'tmp/'

  d.close

  Zlib::GzipWriter.open(pfpath + ".gz") do |gz|
    f = File.new(pfpath, "r")
    f.each do |line|
      gz.write(line)
    end
  end
end

#generate_release(path, release, component, arch, label, origin) ⇒ Object



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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/mprm/debian.rb', line 149

def generate_release(path,release,component,arch,label,origin)
  date = Time.now.utc

  release_info = {}
  unreasonable_array = ["Packages", "Packages.gz", "Release"]
  component_ar = []
  Dir.glob(path + "/dists/" + release + "/*").select { |f|
    f.slice!(path + "/dists/" + release + "/")
    unless f == "Release" or f == "Release.gpg"
      component_ar << f
    end
  }

  component_ar.each do |c|
    arch.each do |ar|
      unreasonable_array.each do |unr|
        tmp_path = "#{path}/dists/#{release}/#{c}/binary-#{ar}"
        tmp_hash = {}
        filename = "#{c}/binary-#{ar}/#{unr}".chomp

        byte_size = File.size("#{tmp_path}/#{unr}").to_s
        file_contents = File.read("#{tmp_path}/#{unr}")

        tmp_hash['size'] = byte_size
        tmp_hash['md5'] = Digest::MD5.hexdigest(file_contents)
        tmp_hash['sha1'] = Digest::SHA1.hexdigest(file_contents)
        tmp_hash['sha256'] = Digest::SHA256.hexdigest(file_contents)
        release_info[filename] = tmp_hash
      end
    end
  end


  template_dir = File.join(File.dirname(__FILE__), "..", "..", "templates")
  erb = ERB.new(File.read("#{template_dir}/deb_release.erb"), nil, "-").result(binding)

  File.open("#{path}/dists/#{release}/Release.tmp","wb") do |x|
    x.puts erb
  end

  FileUtils.move("#{path}/dists/#{release}/Release.tmp", "#{path}/dists/#{release}/Release")
end

#generate_release_gpg(path, release, gpg) ⇒ Object

We expect that GPG is installed and a key has already been made



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/mprm/debian.rb', line 193

def generate_release_gpg(path,release,gpg)
  Dir.chdir("#{path}/dists/#{release}") do
    if gpg_sign_algorithm.nil?
      sign_algorithm = "none"
    else
      sign_algorithm = gpg_sign_algorithm
    end

    if gpg.nil?
      sign_cmd = "gpg --digest-algo \"#{sign_algorithm}\" --yes --output Release.gpg -b Release"
    elsif gpg_passphrase
      sign_cmd = "echo \'#{gpg_passphrase}\' | gpg --digest-algo \"#{sign_algorithm}\" -u #{gpg} --passphrase-fd 0 --yes --output Release.gpg -b Release"
    else
      sign_cmd = "gpg -a --digest-algo \"#{sign_algorithm}\" -u #{gpg} --yes --output Release.gpg -b Release"
    end
    MPRM.logger.debug "Exec: #{sign_cmd}"
    MPRM.logger.debug `#{sign_cmd}`
  end
end

#move_apt_packages(path, component, arch, release, directory) ⇒ Object



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
62
63
64
65
66
67
68
69
70
# File 'lib/mprm/debian.rb', line 36

def move_apt_packages(path,component,arch,release,directory)
  unless File.exist?(directory)
    MPRM.logger.debug "ERROR: #{directory} doesn't exist... not doing anything\n"
    return false
  end

  files_moved = []
  release.each { |r|
    component.each { |c|
      arch.each { |a|
        Dir.glob(directory + "/*.deb") do |file|
          MPRM.logger.debug file
          if file =~ /^.*#{a}.*\.deb$/i || file =~ /^.*all.*\.deb$/i || file =~ /^.*any.*\.deb$/i
            if file =~ /^.*#{r}.*\.deb$/i
              # Lets do this here to help mitigate packages like "asdf-123+wheezy.deb"
              FileUtils.cp(file, "#{path}/dists/#{r}/#{c}/binary-#{a}/")
              FileUtils.rm(file)
            else
              FileUtils.cp(file, "#{path}/dists/#{r}/#{c}/binary-#{a}/")
              files_moved << file
            end
          end
        end
      }
    }
  }

  files_moved.each do |f|
    if File.exist?(f)
      FileUtils.rm(f)
    end
  end
  # Regex?
  #/^.*#{arch}.*\.deb$/i
end

#s3_compatible_encode(str) ⇒ Object



213
214
215
# File 'lib/mprm/debian.rb', line 213

def s3_compatible_encode(str)
  str.gsub(/[#\$&'\(\)\*\+,\/:;=\?@\[\]]/) { |x| x.each_byte.map { |b| '%' + b.to_s(16) }.join }
end