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.



17
18
19
20
21
# File 'lib/dr/repo.rb', line 17

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

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

Instance Attribute Details

#location ⇒ Object (readonly)

Returns the value of attribute location.



15
16
17
# File 'lib/dr/repo.rb', line 15

def location
  @location
end

Instance Method Details

#buildroot(arch) ⇒ Object



86
87
88
# File 'lib/dr/repo.rb', line 86

def buildroot(arch)
  BuildRoot.new arch, "#{@location}/build-root-#{arch}.tar.gz"
end

#get_architectures ⇒ Object



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

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



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

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

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

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

#get_package(name) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dr/repo.rb', line 90

def get_package(name)
  unless File.exists? "#{@packages_dir}/#{name}"
    raise "Package '#{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



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

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 (== raspberrypi-firmware)' " +
                 "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



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

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



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dr/repo.rb', line 73

def list_packages(suite=nil)
  pkgs = []
  if suite
    a = 1
  else
    Dir.foreach @packages_dir do |pkg_name|
      pkgs.push get_package pkg_name unless pkg_name =~ /^\./
    end
  end

  pkgs
end

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



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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/dr/repo.rb', line 164

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, "Package #{pkg_name} has not been built yet"
      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

  # FIXME: This will not work with packages that don't build a deb
  #        with the same name as the source package
  current_version = query_for_deb_version suite, pkg.name

  if current_version != nil && current_version >= version
    log :warn, "Version #{version.fg("blue")} already available in #{suite}"
    if force
      reprepro = "reprepro -b #{@location}/archive " +
                 "--gnupghome #{location}/gnupg-keyring/ removesrc " +
                 "#{suite} #{pkg.name}"
      ShellCmd.new reprepro, :tag => "reprepro", :show_out => true
    else
      log :err, "The same package of a higher version is already in the repo."
      raise "Push failed"
    end
  end

  log :info, "Pushing #{pkg_name} version #{version} to #{suite}"
  debs = Dir["#{@location}/packages/#{pkg.name}/builds/#{version}/*"]
  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



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

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



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/dr/repo.rb', line 231

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

  if is_used? pkg_name
    log :warn, "Package #{pkg_name} is still used"
    raise "The '#{pkg_name}' package is still used." unless force

    log :info, "Will be force-removed anyway"
    get_suites.zip(versions).each do |suite, version|
      log :info, "Removing #{pkg_name} v#{version} from #{suite}"
      unpush pkg_name, suite[0] if version != nil
    end
  end

  if !is_used?(pkg_name) || force
    log :info, "Removing #{pkg_name} from the repository"
    FileUtils.rm_rf "#{location}/packages/#{pkg_name}"
  end
end

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



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/dr/repo.rb', line 251

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.fg("blue")} version of " +
                 "#{pkg_name.fg("blue")}"
      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} is " +
                 "still being used, add -f to force-remove"
      return
    end
  else
    log :info, "Removing the #{version.fg("blue")} version of " +
                "#{pkg_name.fg("blue")}"
  end

  pkg.remove_build version
end

#setup(conf) ⇒ Object



23
24
25
26
27
28
29
30
31
32
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
62
63
64
65
66
67
68
69
70
71
# File 'lib/dr/repo.rb', line 23

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][i].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

  conf[:arches].each do |arch|
    BuildRoot.new arch, "#{@location}/build-root-#{arch}.tar.gz"
  end
end

#unpush(pkg_name, suite) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/dr/repo.rb', line 214

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} from #{suite}"
  reprepro = "reprepro -b #{@location}/archive " +
             "--gnupghome #{location}/gnupg-keyring/ removesrc " +
             "#{suite} #{pkg.name}"
  ShellCmd.new reprepro, :tag => "reprepro", :show_out => true
end