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, set_logfile, set_verbosity, #tag

Constructor Details

#initialize(loc) ⇒ Repo

Returns a new instance of Repo.



29
30
31
32
33
# File 'lib/dr/repo.rb', line 29

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

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



23
24
25
# File 'lib/dr/repo.rb', line 23

def location
  @location
end

Instance Method Details

#buildroot(arch, build_env = :default) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/dr/repo.rb', line 125

def buildroot(arch, build_env=:default)
  if build_env == :default
    build_env = get_configuration[:default_build_environment].to_sym
  end

  cache_dir = "#{@location}/buildroots/"
  BuildRoot.new build_env, arch, cache_dir
end

#codename_to_suite(codename_or_suite) ⇒ Object



384
385
386
387
388
389
390
# File 'lib/dr/repo.rb', line 384

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_architecturesObject



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/dr/repo.rb', line 166

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_archive_pathObject



25
26
27
# File 'lib/dr/repo.rb', line 25

def get_archive_path
  "#{@location}/archive"
end

#get_build(pkg_name, version = nil) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/dr/repo.rb', line 349

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



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

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_configurationObject



88
89
90
91
92
93
94
95
# File 'lib/dr/repo.rb', line 88

def get_configuration
    meta_file = "#{@location}/metadata"
    if File.exists? meta_file
      Utils::symbolise_keys YAML.load_file(meta_file)
    else
      {}
    end
end

#get_package(name) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dr/repo.rb', line 134

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



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/dr/repo.rb', line 206

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_suitesObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/dr/repo.rb', line 146

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

#is_used?(pkg_name, version = nil) ⇒ Boolean

Returns:

  • (Boolean)


392
393
394
395
396
397
398
399
400
401
402
# File 'lib/dr/repo.rb', line 392

def is_used?(pkg_name, version=nil)
  versions_by_suite = get_subpackage_versions pkg_name
  versions_by_suite.inject(false) do |rslt, hash_pair|
    suite, versions = hash_pair
    if version == nil
      rslt || !versions.empty?
    else
      rslt || versions.has_value?(version)
    end
  end
end

#list_packages(suite = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dr/repo.rb', line 104

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



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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/dr/repo.rb', line 227

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) && 
       PkgVersion.new(version) <= PkgVersion.new(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



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

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



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

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



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/dr/repo.rb', line 325

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

#set_configuration(new_metadata) ⇒ Object



97
98
99
100
101
102
# File 'lib/dr/repo.rb', line 97

def set_configuration()
  # TODO: Some validation needed
  File.open("#{@location}/metadata", "w" ) do |out|
    out.write Utils::stringify_symbols().to_yaml
  end
end

#setup(conf) ⇒ Object



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

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"

   = {
    "default_build_environment" => conf[:build_environment].to_s
  }
  File.open("#{@location}/metadata", "w" ) do |out|
    out.write .to_yaml
  end
end

#sign_deb(deb) ⇒ Object



375
376
377
378
379
380
381
382
# File 'lib/dr/repo.rb', line 375

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)


194
195
196
197
198
199
200
201
202
203
204
# File 'lib/dr/repo.rb', line 194

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)


188
189
190
191
192
# File 'lib/dr/repo.rb', line 188

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



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

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