-
Notifications
You must be signed in to change notification settings - Fork 1
/
clean_outdated.rb
executable file
·84 lines (69 loc) · 2.48 KB
/
clean_outdated.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
#!/usr/bin/env ruby
require 'repomd_parser'
class OutdatedPackagesCleaner
def initialize(mirror_dir)
raise "#{mirror_dir} doesn't exist or isn't a directory" unless File.exist?(mirror_dir) and File.directory?(mirror_dir)
@mirror_dir = mirror_dir
@packages = {}
@inodes = {}
end
def start
repo_dirs = []
referenced_size = 0
outdated_size = 0
outdated_files = 0
Dir[@mirror_dir + '/**/*'].each do |f|
next unless File.directory?(f)
if File.basename(f) == 'repodata'
repo_dirs << File.expand_path(File.join(f, '..'))
end
end
repo_dirs.each do |repo_dir|
Dir[repo_dir + '/**/*'].each do |f|
@packages[f] = true if f =~ /rpm$/
end
primary_xml = nil
delta_xml = nil
metadata_files = RepomdParser::RepomdXmlParser.new(File.join(repo_dir, 'repodata/repomd.xml')).parse
metadata_files.each do |metadata_file|
primary_xml = File.join(repo_dir, metadata_file.location) if metadata_file.type == :primary
delta_xml = File.join(repo_dir, metadata_file.location) if metadata_file.type == :deltainfo
end
if primary_xml
rpm_packages = RepomdParser::PrimaryXmlParser.new(primary_xml).parse
rpm_packages.each do |rpm|
full_path = File.join(repo_dir, rpm.location)
referenced_size += get_size(full_path)
@packages.delete(full_path)
end
end
next unless delta_xml
drpm_packages = RepomdParser::DeltainfoXmlParser.new(delta_xml).parse
drpm_packages.each do |rpm|
full_path = File.join(repo_dir, rpm.location)
referenced_size += get_size(full_path)
@packages.delete(full_path)
end
end
@packages.keys.each do |file|
next if File.directory?(file)
# skipping files that are younger than 2 days, zypper service has a TTL of 1 day during which it uses cached metadata
file_age = Time.now() - File.mtime(file)
next if file_age < 48*60*60
outdated_size += get_size(file)
outdated_files += 1
File.delete(file)
end
puts format('Number of outdated files removed: % 6d, disk space freed (MB): % 6d', outdated_files, outdated_size / 1024 / 1024)
end
protected
def get_size(full_path)
return 0 unless File.exist?(full_path)
ino = File.stat(full_path).ino
return 0 if @inodes[ino]
@inodes[ino] = true
File.size(full_path)
end
end
mirror_dir = ARGV[0] || '/srv/www/htdocs/'
OutdatedPackagesCleaner.new(mirror_dir).start