-
Notifications
You must be signed in to change notification settings - Fork 1
/
verify_repo.rb
executable file
·115 lines (87 loc) · 2.78 KB
/
verify_repo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
72
73
74
75
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
111
112
113
114
115
#!/usr/bin/env ruby
require 'bundler/setup'
require 'typhoeus'
require 'uri'
require 'tmpdir'
require 'fileutils'
require 'progressbar'
require 'repomd_parser'
class RepoVerifier
def initialize(repo_url, token)
repo_url += '/' unless repo_url =~ /\/$/
@repo_url = repo_url
@token = token
@broken_files = []
end
def verify
@tempdir = Dir.mktmpdir('verify-repo')
repomd_file = get('repodata/repomd.xml')
metadata_files = Hash.new { |hash, key| hash[key] = [] }
RepomdParser::RepomdXmlParser.new(repomd_file).parse.each do |xml_file|
metadata_files[xml_file.type] << xml_file if %i[primary deltainfo].include?(xml_file.type)
end
puts "Repo: #{@repo_url}\n"
puts "Verifying RPMs\n\n"
verify_files(metadata_files[:primary], RepomdParser::PrimaryXmlParser)
puts "Verifying delta-RPMs\n\n"
verify_files(metadata_files[:deltainfo], RepomdParser::DeltainfoXmlParser)
if @broken_files.empty?
puts 'All files have correct size. Hooray!'
else
puts "Packages with wrong file size:\n"
puts @broken_files.join("\n")
end
ensure
FileUtils.rm_r(@tempdir)
end
protected
def verify_files(metadata_files, klass)
hydra = Typhoeus::Hydra.new
pb = ProgressBar.create
total = 0
metadata_files.each do |xml_file|
filename = File.basename(xml_file.location)
primary_xml_file = get(xml_file.location)
rpms = klass.new(primary_xml_file).parse
rpms.each do |package|
uri = URI.join(@repo_url, package.location)
uri.query = @token
request = Typhoeus::Request.new(uri.to_s, followlocation: true, method: :head)
request.on_complete do |response|
if response.success?
actual_size = response.headers["Content-Length"].to_i
if (actual_size != package.size.to_i)
@broken_files << "#{package.location}, actual size: #{actual_size}, metadata size: #{package.size}"
end
pb.increment
else
raise "Something went wrong: #{uri.to_s}"
end
end
hydra.queue(request)
total += 1
end
end
pb.total = total
hydra.run
puts ''
end
def get(filename)
uri = URI.join(@repo_url, filename)
uri.query = @token
response = Typhoeus.get(uri, followlocation: true)
file = File.open(File.join(@tempdir, File.basename(filename)), 'w')
file.write(response.body)
file.close
file.path
end
end
if (!ARGV[0] || ARGV[0] == '-h' || ARGV[0] == '--help')
puts "Usage: #{File.basename($PROGRAM_NAME)} REPO_URL"
puts 'Authentication token can be supplied as query string, e.g.: https://repo.url/?auth_token'
exit 1
end
uri = URI.parse(ARGV[0])
token = uri.query
uri.query = nil
RepoVerifier.new(uri.to_s, token).verify