Class: Dr::Repo

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/dr/repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

log, #log, #tag

Constructor Details

#initialize(loc) ⇒ Repo

Returns a new instance of Repo.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dr/repo.rb', line 23

def initialize(loc)
  @location = File.expand_path loc

  @packages_dir = "#{@location}/packages"

  meta = "#{@location}/metadata"
  begin
    @metadata = YAML.load_file(meta)
  rescue
    @metadata = {}
  end
end

Instance Attribute Details

#location ⇒ Object (readonly)

Returns the value of attribute location.



21
22
23
# File 'lib/dr/repo.rb', line 21

def location
  @location
end

Instance Method Details

#buildroot(arch) ⇒ Object



112
113
114
115
# File 'lib/dr/repo.rb', line 112

def buildroot(arch)
  cache_dir = "#{@location}/buildroots/"
  BuildRoot.new @metadata["distro"], arch, cache_dir
end

#codename_to_suite(codename_or_suite) ⇒ Object



366
367
368
369
370
371
372
# File 'lib/dr/repo.rb', line 366

def codename_to_suite(codename_or_suite)
  get_suites.each do |suite, codename|
    return suite if codename_or_suite == suite || codename_or_suite == codename
  end

  nil
end

#get_architectures ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/dr/repo.rb', line 149

def get_architectures
  arches = []
  File.open "#{@location}/archive/conf/distributions", "r" do |f|
    f.each_line do |l|
      m = l.match /^Architectures: (.+)/
      arches += m.captures[0].chomp.split(" ") if m
    end
  end

  arches.uniq
end

#get_build(pkg_name, version = nil) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/dr/repo.rb', line 331

def get_build(pkg_name, version=nil)
  pkg = get_package pkg_name

  hist = pkg.history
  raise "The package hasn't been built yet." unless hist.length > 0
  version = hist[0] unless version

  unless pkg.build_exists? version
    raise "Build #{version.style "version"} doesn't exist"
  end

  Dir["#{@location}/packages/#{pkg.name}/builds/#{version}/*"]
end

#get_build_metadata(pkg_name, version) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
# File 'lib/dr/repo.rb', line 345

def (pkg_name, version)
  pkg = get_package pkg_name
  raise "Build #{version} doesn't exist" unless pkg.build_exists? version

  md_file = "#{@location}/packages/#{pkg.name}/builds/#{version}/.metadata"
  if File.exists? md_file
    YAML.load_file md_file
  else
    {}
  end
end

#get_package(name) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/dr/repo.rb', line 117

def get_package(name)
  unless File.exists? "#{@packages_dir}/#{name}"
    raise "Package #{name.style "pkg-name"} doesn't exist in the repo"
  end

  if File.exists? "#{@packages_dir}/#{name}/source"
    GitPackage.new name, self
  else
    DebPackage.new name, self
  end
end

#get_subpackage_versions(pkg_name) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/dr/repo.rb', line 189

def get_subpackage_versions(pkg_name)
  pkg = get_package pkg_name
  suites = get_suites

  versions = {}
  suites.each do |suite, codename|
    versions[suite] = {}
    reprepro_cmd = "reprepro --basedir #{location}/archive " +
                 "--list-format '${package} ${version}\n' " +
                 "listfilter #{suite} 'Source (== #{pkg_name}) | " +
                 "Package (== #{pkg_name})' " +
                 "2>/dev/null"
    reprepro = ShellCmd.new reprepro_cmd, :tag => "reprepro"
    reprepro.out.chomp.each_line do |line|
      subpkg, version = line.split(" ").map(&:chomp)
      versions[suite][subpkg] = version
    end
  end
  versions
end

#get_suites ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dr/repo.rb', line 129

def get_suites
  suites = nil
  File.open "#{@location}/archive/conf/distributions", "r" do |f|
    suites = f.read.split "\n\n"
  end

  suites.map do |s|
    suite = nil
    codename = nil
    s.each_line do |l|
      m = l.match /^Suite: (.+)/
      suite = m.captures[0].chomp if m

      m = l.match /^Codename: (.+)/
      codename = m.captures[0].chomp if m
    end
    [suite, codename]
  end
end

#list_packages(suite = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/dr/repo.rb', line 91

def list_packages(suite=nil)
  pkgs = []

  if suite
    Dir.foreach @packages_dir do |pkg_name|
      unless pkg_name =~ /^\./
        versions = get_subpackage_versions pkg_name
        unless versions[codename_to_suite suite].empty?
          pkgs.push get_package pkg_name
        end
      end
    end
  else
    Dir.foreach @packages_dir do |pkg_name|
      pkgs.push get_package pkg_name unless pkg_name =~ /^\./
    end
  end

  pkgs.sort
end

#push(pkg_name, version, suite, force = false) ⇒ Object



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
242
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
# File 'lib/dr/repo.rb', line 210

def push(pkg_name, version, suite, force=false)
  pkg = get_package pkg_name

  if version
    unless pkg.build_exists? version
      raise "Build version '#{version}' not found"
    end
  else
    if pkg.history.length == 0
      log :err, "No built packages available for #{pkg_name}"
      log :err, "Please, run a build first and the push."
      raise "Push failed"
    end
    version = pkg.history[0]
  end

  if suite
    cmp = get_suites.map { |n, cn| suite == n || suite == cn }
    suite_exists = cmp.inject(false) { |r, o| r || o }
    raise "Suite '#{suite}' doesn't exist." unless suite_exists
  else
    # FIXME: This should be configurable
    suite = "testing"
  end

  debs = Dir["#{@location}/packages/#{pkg.name}/builds/#{version}/*"]
  names = debs.map { |deb| File.basename(deb).split("_")[0] }

  used_versions = get_subpackage_versions(pkg.name)[codename_to_suite(suite)]

  is_of_higher_version = true
  names.each do |name|
    if used_versions.has_key?(name) && version <= used_versions[name]
      is_of_higher_version = false
    end
  end

  unless is_of_higher_version
    log :warn, "The #{suite} suite already contains " +
               "#{pkg.name.style "pkg-name"} version " +
               "#{version.to_s.style "version"}"
    if force
      reprepro = "reprepro -b #{@location}/archive " +
                 "--gnupghome #{location}/gnupg-keyring/ removesrc " +
                 "#{suite} #{pkg.name}"
      ShellCmd.new reprepro, :tag => "reprepro", :show_out => false
    else
      log :warn, "The same package of a higher version is already in the " +
                 "#{suite} suite."

      raise AlreadyExists.new "Push failed"
    end
  end

  log :info, "Pushing #{pkg_name.style "pkg-name"} version " +
             "#{version.to_s.style "version"} to #{suite}"
  reprepro = "reprepro -b #{@location}/archive " +
             "--gnupghome #{location}/gnupg-keyring/ includedeb " +
             "#{suite} #{debs.join " "}"
  ShellCmd.new reprepro, :tag => "reprepro", :show_out => true
end

#query_for_deb_version(suite, pkg_name) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/dr/repo.rb', line 161

def query_for_deb_version(suite, pkg_name)
  reprepro_cmd = "reprepro --basedir #{location}/archive " +
                 "--list-format '${version}' list #{suite} " +
                 "#{pkg_name} 2>/dev/null"
  reprepro = ShellCmd.new reprepro_cmd, :tag => "reprepro"
  v = reprepro.out.chomp
  v = nil unless v.length > 0
  v
end

#remove(pkg_name, force = false) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/dr/repo.rb', line 289

def remove(pkg_name, force=false)
  pkg = get_package pkg_name

  if is_used? pkg_name
    log :warn, "The #{pkg_name.style "pkg-name"} package is still used"
    raise "Operation canceled, add -f to remove anyway" unless force

    log :info, "Will be force-removed anyway"
    versions = get_subpackage_versions(pkg_name)
    get_suites.each do |suite, codename|
      unpush pkg_name, suite unless versions[suite].empty?
    end
  end

  log :info, "Removing #{pkg_name.style "pkg-name"} from the repository"
  FileUtils.rm_rf "#{location}/packages/#{pkg_name}"
end

#remove_build(pkg_name, version, force = false) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/dr/repo.rb', line 307

def remove_build(pkg_name, version, force=false)
  pkg = get_package pkg_name

  if is_used?(pkg_name, version)
    if force
      log :info, "Force-removing #{version.style "version"} version of " +
                 "#{pkg_name.style "pkg-name"}"
      versions_by_suite = get_subpackage_versions pkg_name
      versions_by_suite.each do |suite, versions|
        unpush pkg_name, suite if versions.has_value? version
      end
    else
      log :warn, "This build of #{pkg_name.style "pkg-name"} is " +
                 "still being used, add -f to force-remove"
      return
    end
  else
    log :info, "Removing the #{version.style "version"} version of " +
               "#{pkg_name.style "pkg-name"}"
  end

  pkg.remove_build version
end

#setup(conf) ⇒ 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dr/repo.rb', line 36

def setup(conf)
  log :info, "Creating the archive directory"
  begin
    FileUtils.mkdir_p location
  rescue Exception => e
    log :err, "Unable to create a directory at '#{@location.fg("blue")}'"
    raise e
  end

  FileUtils.mkdir_p "#{@location}/archive"

  gpg = GnuPG.new "#{@location}/gnupg-keyring"
  key = gpg.generate_key conf[:gpg_name], conf[:gpg_mail], conf[:gpg_pass]
  gpg.export_pub key, "#{@location}/archive/repo.gpg.key"

  log :info, "Writing the configuration file"
  FileUtils.mkdir_p "#{@location}/archive/conf"
  File.open "#{@location}/archive/conf/distributions", "w" do |f|
    conf[:suites].each_with_index do |s, i|
      f.puts "Suite: #{s}"

      if conf[:codenames][i].length > 0
        f.puts "Codename: #{conf[:codenames][i]}"
      end

      if conf[:name].length > 0
        f.puts "Origin: #{conf[:name]} - #{s}"
        f.puts "Label: #{conf[:name]} - #{s}"
      end

      if conf[:desc].length > 0
        f.puts "Description: #{conf[:desc]}"
      end

      f.puts "Architectures: #{conf[:arches].join " "}"
      f.puts "Components: #{conf[:components].join " "}"

      f.puts "SignWith: #{key}"
      f.puts ""
    end
  end

  FileUtils.mkdir_p @packages_dir
  FileUtils.mkdir_p "#{@location}/buildroots"

  @metadata = {"distro" => conf[:distro]}
  File.open("#{@location}/metadata", "w" ) do |out|
    YAML.dump(@metadata)
  end

  conf[:arches].each do |arch|
    buildroot arch
  end
end

#sign_deb(deb) ⇒ Object



357
358
359
360
361
362
363
364
# File 'lib/dr/repo.rb', line 357

def sign_deb(deb)
  keyring = "#{@location}/gnupg-keyring"
  gpg = GnuPG.new keyring
  key_id = gpg.get_key_id get_key

  cmd = "dpkg-sig -k '#{key_id}' -s builder -g '--homedir #{keyring}' #{deb}"
  ShellCmd.new cmd, :tag => "dpkg-sig", :show_out => true
end

#suite_has_higher_pkg_version?(suite, pkg, version) ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
180
181
182
183
184
185
186
187
# File 'lib/dr/repo.rb', line 177

def suite_has_higher_pkg_version?(suite, pkg, version)
  used_versions = get_subpackage_versions(pkg.name)[codename_to_suite(suite)]

  has_higher_version = false
  used_versions.each do |subpkg_name, subpkg_version|
    if subpkg_version.to_s >= version.to_s
      has_higher_version = true
    end
  end
  has_higher_version
end

#suite_has_package?(suite, pkg_name) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
174
175
# File 'lib/dr/repo.rb', line 171

def suite_has_package?(suite, pkg_name)
  pkg_versions = get_subpackage_versions(pkg_name)[codename_to_suite(suite)]

  pkg_versions.length > 0
end

#unpush(pkg_name, suite) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/dr/repo.rb', line 272

def unpush(pkg_name, suite)
  pkg = get_package pkg_name

  cmp = get_suites.map { |n, cn| suite == n || suite == cn }
  suite_exists = cmp.inject(false) { |r, o| r || o }
  unless suite_exists
    log :err, "Suite '#{suite}' doesn't exist."
    raise "Unpush failed"
  end

  log :info, "Removing #{pkg_name.style "pkg-name"} from #{suite}"
  reprepro = "reprepro -b #{@location}/archive " +
             "--gnupghome #{location}/gnupg-keyring/ removesrc " +
             "#{suite} #{pkg.name}"
  ShellCmd.new reprepro, :tag => "reprepro", :show_out => true
end