Class: Deb::S3::Release

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/deb/s3/release.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

access_policy, access_policy=, bucket, bucket=, debianize_op, gpg_options, gpg_options=, s3_escape, s3_exists?, s3_read, s3_remove, s3_store, safesystem, signing_key, signing_key=, template

Constructor Details

#initializeRelease

Returns a new instance of Release.



13
14
15
16
17
18
19
# File 'lib/deb/s3/release.rb', line 13

def initialize
  @codename = nil
  @architectures = []
  @components = []
  @files = {}
  @policy = :public_read
end

Instance Attribute Details

#architecturesObject

Returns the value of attribute architectures.



7
8
9
# File 'lib/deb/s3/release.rb', line 7

def architectures
  @architectures
end

#codenameObject

Returns the value of attribute codename.



6
7
8
# File 'lib/deb/s3/release.rb', line 6

def codename
  @codename
end

#componentsObject

Returns the value of attribute components.



8
9
10
# File 'lib/deb/s3/release.rb', line 8

def components
  @components
end

#filesObject

Returns the value of attribute files.



10
11
12
# File 'lib/deb/s3/release.rb', line 10

def files
  @files
end

#policyObject

Returns the value of attribute policy.



11
12
13
# File 'lib/deb/s3/release.rb', line 11

def policy
  @policy
end

Class Method Details

.parse_release(str) ⇒ Object



32
33
34
35
36
# File 'lib/deb/s3/release.rb', line 32

def parse_release(str)
  rel = self.new
  rel.parse(str)
  rel
end

.retrieve(codename) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/deb/s3/release.rb', line 22

def retrieve(codename)
  if s = Deb::S3::Utils.s3_read("dists/#{codename}/Release")
    self.parse_release(s)
  else
    rel = self.new
    rel.codename = codename
    rel
  end
end

Instance Method Details

#filenameObject



39
40
41
# File 'lib/deb/s3/release.rb', line 39

def filename
  "dists/#{@codename}/Release"
end

#generateObject



72
73
74
# File 'lib/deb/s3/release.rb', line 72

def generate
  template("release.erb").result(binding)
end

#parse(str) ⇒ Object



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/deb/s3/release.rb', line 43

def parse(str)
  parse = lambda do |field|
    value = str[/^#{field}: .*/]
    if value.nil?
      return nil
    else
      return value.split(": ",2).last
    end
  end

  # grab basic fields
  self.codename = parse.call("Codename")
  self.architectures = (parse.call("Architectures") || "").split(/\s+/)
  self.components = (parse.call("Components") || "").split(/\s+/)

  # find all the hashes
  str.scan(/^\s+([^\s]+)\s+(\d+)\s+(.+)$/).each do |(hash,size,name)|
    self.files[name] ||= { :size => size.to_i }
    case hash.length
    when 32
      self.files[name][:md5] = hash
    when 40
      self.files[name][:sha1] = hash
    when 64
      self.files[name][:sha256] = hash
    end
  end
end

#update_manifest(manifest) ⇒ Object



112
113
114
115
116
# File 'lib/deb/s3/release.rb', line 112

def update_manifest(manifest)
  self.components << manifest.component unless self.components.include?(manifest.component)
  self.architectures << manifest.architecture unless self.architectures.include?(manifest.architecture)
  self.files.merge!(manifest.files)
end

#validate_othersObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/deb/s3/release.rb', line 118

def validate_others
  to_apply = []
  self.components.each do |comp|
    %w(amd64 i386).each do |arch|
      next if self.files.has_key?("#{comp}/binary-#{arch}/Packages")

      m = Deb::S3::Manifest.new
      m.codename = self.codename
      m.component = comp
      m.architecture = arch
      if block_given?
        m.write_to_s3 { |f| yield f }
      else
        m.write_to_s3
      end
      to_apply << m
    end
  end

  to_apply.each { |m| self.update_manifest(m) }
end

#write_to_s3 {|self.filename| ... } ⇒ Object

Yields:



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
# File 'lib/deb/s3/release.rb', line 76

def write_to_s3
  # validate some other files are present
  if block_given?
    self.validate_others { |f| yield f }
  else
    self.validate_others
  end

  # generate the Release file
  release_tmp = Tempfile.new("Release")
  release_tmp.puts self.generate
  release_tmp.close
  yield self.filename if block_given?
  s3_store(release_tmp.path, self.filename)

  # sign the file, if necessary
  if Deb::S3::Utils.signing_key
    key_param = Deb::S3::Utils.signing_key != "" ? "--default-key=#{Deb::S3::Utils.signing_key}" : ""
    if system("gpg -a #{key_param} #{Deb::S3::Utils.gpg_options} -b #{release_tmp.path}")
      local_file = release_tmp.path+".asc"
      remote_file = self.filename+".gpg"
      yield remote_file if block_given?
      raise "Unable to locate Release signature file" unless File.exists?(local_file)
      s3_store(local_file, remote_file)
      File.unlink(local_file)
    else
      raise "Signing the Release file failed."
    end
  else
    # remove an existing Release.gpg, if it was there
    s3_remove(self.filename+".gpg")
  end

  release_tmp.unlink
end