Skip to content

Commit dee5f53

Browse files
authored
Merge pull request prometheus#716 from prometheus/grobie/checksums
Render tarball sha256 checksum if available for release
2 parents 5f38896 + e8611ae commit dee5f53

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

content/css/docs.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,9 @@ footer {
233233
}
234234

235235
.downloads .checksum {
236-
color: #aaa;
237-
font-style: italic;
236+
font-family: monospace;
237+
font-size: .6em;
238+
vertical-align: bottom;
238239
}
239240

240241
/* Docs-related styles. */

content/download.html

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ <h1>Download</h1>
2929
the <a href="/docs/introduction/getting_started/">installation instructions</a>.
3030
</p>
3131

32-
<div class="alert alert-info" role="alert">
33-
<strong>Work in progress!</strong>
34-
We will provide more precompiled binary versions as well as checksums soon.
35-
</div>
36-
3732
<div class="panel panel-default download-selection">
3833
<div class="panel-body">
3934
Operating system <%= dropdown(:os, Downloads.operating_systems, :popular, popular: %w(darwin linux windows)) %>
@@ -64,13 +59,13 @@ <h2 id="<%= repository.name %>"><%= repository.name %></h2>
6459
</tr>
6560
</thead>
6661
<tbody>
67-
<% release.assets.each do |asset| %>
68-
<tr data-os="<%= asset.os %>" data-arch="<%= asset.arch %>">
69-
<td class="filename"><a class="download" href="<%= asset.url %>"><%= asset.name %></a></td>
70-
<td><%= asset.os %></td>
71-
<td><%= asset.arch %></td>
72-
<td><%= format_bytes asset.size %></td>
73-
<td class="checksum">not available yet</td>
62+
<% release.binaries.each do |binary| %>
63+
<tr data-os="<%= binary.os %>" data-arch="<%= binary.arch %>">
64+
<td class="filename"><a class="download" href="<%= binary.url %>"><%= binary.name %></a></td>
65+
<td><%= binary.os %></td>
66+
<td><%= binary.arch %></td>
67+
<td><%= format_bytes binary.size %></td>
68+
<td class="checksum"><%= Downloads.checksum(release, binary.name) %></td>
7469
</tr>
7570
<% end %>
7671
</tbody>

lib/helpers/download.rb

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
require 'fileutils'
12
require 'json'
3+
require 'open-uri'
24

35
module Downloads
46
# repositories returns a list of all repositories with releases.
57
def self.repositories
6-
@repositories ||= begin
8+
@_repositories ||= begin
79
repos = Dir.glob('downloads/*').map { |dir| Repository.new(dir) }
810
repos.sort_by { |r| r.name == 'prometheus' ? '0' : r.name }
911
end
@@ -13,18 +15,46 @@ def self.repositories
1315
# provided for.
1416
def self.operating_systems
1517
repositories.inject([]) do |list, repo|
16-
list += repo.releases.map { |r| r.assets.map(&:os) }.flatten
18+
list += repo.releases.map { |r| r.binaries.map(&:os) }.flatten
1719
end.uniq.sort
1820
end
1921

2022
# architectures returns a list of all architectures downloads can be
2123
# provided for.
2224
def self.architectures
2325
repositories.inject([]) do |list, repo|
24-
list += repo.releases.map { |r| r.assets.map(&:arch) }.flatten
26+
list += repo.releases.map { |r| r.binaries.map(&:arch) }.flatten
2527
end.uniq.sort
2628
end
2729

30+
# checksum returns the checksum for a given filename of a given release. It
31+
# might try to download the sha256sums.txt from the given release if available
32+
# and not already cached.
33+
def self.checksum(release, name)
34+
@_checksums ||= {}
35+
@_checksums[release.id] ||= begin
36+
asset = release.assets.find { |a| a['name'] == 'sha256sums.txt' }
37+
38+
if asset
39+
cache = ['downloads', '.cache', release.id, 'sha256sums.txt'].join('/')
40+
unless File.exists?(cache)
41+
FileUtils.mkdir_p(File.dirname(cache))
42+
File.open(cache, 'wb') do |file|
43+
file.write(URI.parse(asset['browser_download_url']).read)
44+
end
45+
end
46+
47+
File.readlines(cache).each_with_object({}) do |line, memo|
48+
checksum, filename = line.split(/\s+/)
49+
memo[filename] = checksum
50+
end
51+
else
52+
{}
53+
end
54+
end
55+
@_checksums[release.id][name]
56+
end
57+
2858
class Repository
2959
def initialize(dir)
3060
@repo = JSON.parse(File.read(File.join(dir, 'repo.json')))
@@ -66,6 +96,10 @@ def initialize(data)
6696
@data = data
6797
end
6898

99+
def id
100+
@data['id']
101+
end
102+
69103
def name
70104
@data['name']
71105
end
@@ -79,11 +113,17 @@ def prerelease
79113
end
80114

81115
def assets
82-
@data['assets'].map { |d| Asset.new(d) }
116+
@data['assets']
117+
end
118+
119+
def binaries
120+
assets.
121+
select { |d| d['name'] && %w[.tar.gz .zip].any? { |ext| d['name'].end_with?(ext) } }.
122+
map { |d| Binary.new(d) }
83123
end
84124
end
85125

86-
class Asset
126+
class Binary
87127
def initialize(data)
88128
@data = data
89129
end

0 commit comments

Comments
 (0)