-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchzfs-plus-kernel-update-repo
executable file
·389 lines (307 loc) · 10.6 KB
/
archzfs-plus-kernel-update-repo
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#! /usr/bin/env ruby
# First off, force the Gemfile's path and setup bundler:
ENV[ "BUNDLE_GEMFILE" ] = "#{__dir__}/Gemfile"
require "bundler/setup"
require "fileutils"
require "open-uri"
require "optparse"
require "pathname"
require "ruby-progressbar"
require "timeout"
require "yaml"
require_relative "lib/pacman_db_parser"
require_relative "lib/pacman_package"
###############################################################################
### Constants ###
###############################################################################
# Main repo url:
ARCHZFS = "https://github.com/archzfs/archzfs/releases/download/experimental"
.freeze
###############################################################################
### Utility functions ###
###############################################################################
# Exits removing the lock file.
def clean_exit( outcome )
@lock.delete if [email protected]? and @lock.exist?
exit outcome
end
# Downloader with progress bar.
def download_with_progressbar( url )
file = url.split( "/" ).last
puts "Downloading '#{file}'..."
bar = ProgressBar.create starting_at: 0,
total: nil,
format: " > %a%e %B %p%% %r KB/sec",
rate_scale: lambda { |rate| rate / 1024 }
return URI.open url,
content_length_proc: Proc.new { |content_length|
bar.total = content_length
},
progress_proc: Proc.new { |bytes_transferred|
if bar.total and bar.total < bytes_transferred then
bar.total = nil
end
bar.progress = bytes_transferred
}
end
# Retrieves the configuration, handling eventual initial config creation and
# migration from older versions.
# Returns a hash.
def get_config
# Config file name:
config = Pathname.new "#{__dir__}/config.yml"
# If the file exists, simply return its data as a hash:
return YAML.load_file config if config.file?
# If we get here we'll need the config sample:
sample_file = "#{config}.sample"
sample_config = YAML.load_file sample_file
# If the older config file is present, migrate it:
if File.exist? "kernels.conf" then
old_config = File.open( "kernels.conf" ).read.split.grep /^linux/
sample_config[ :packages ] = old_config
FileUtils.rm "kernels.conf"
end
# Write the new config file:
config.open "w" do |f|
f.puts \
"###",
"# For a complete reference see: #{sample_file}",
"###",
"",
sample_config.to_yaml
end
# Finally, return the config data:
return sample_config
end
###############################################################################
### Main logic ###
###############################################################################
### Parse eventual parameters
###############################################################################
options = {
force: false
}
# Parameters parser:
parser = OptionParser.new do |opts|
opts.banner = "Usage: #{$0.split( /[\\\/]/ ).last} <switches>"
opts.on "-f", "--force-refresh", "Forces a database refresh." do
options[ :force ] = true
end
opts.on "-h", "--help", "Prints the help reference." do
puts opts
clean_exit 0
end
end
parser.parse!
### Current versions retrieval
###############################################################################
# Repository's path:
repo = "#{__dir__}/repo/archzfs-plus-kernel"
# Ensure it does exist:
FileUtils.mkpath repo
# Lock file:
@lock = Pathname.new "#{repo}/lock"
# If the lock file already exists:
if @lock.exist? then
# If the file has been created less than 5 minutes ago, it's valid and we
# should quit:
if Time.now - @lock.ctime <= (60 * 5) then
puts "Lock file #{@lock} present! Is someone else running this script?"
exit -1
# Otherwise it's a stale lock file which should be deleted:
else
@lock.delete
end
end
# If we got here, create the lock file:
@lock.write "lock"
# Ensure signal interrupt won't break stuff:
[ "INT", "TERM" ].each do |s|
Signal.trap s do
clean_exit 0
end
end
# Let's ensure any kind of error won't leave a lock file behind:
begin
# Database name:
FileUtils.mkpath repo
database = Pathname.new "#{repo}/archzfs-plus-kernel.db.tar.xz"
# Last chcek timestamp:
last_check = Pathname.new "#{repo}/.last_check"
# Fetch the configuration:
config = get_config
puts "Config is set to: #{config}"
# We start off with no current packages and no extra kernel deps:
current_pkgs = Hash.new
extra_kernel_deps = Array.new
# If the local database exists, read the current packages from it:
if database.exist? then
database_data = PacmanDbParser.new database.read
# This is needed to avoid removing kernels needed by certain configurations
# (eg. linux-rc, linux-git):
extra_kernel_deps = database_data
.kernel_deps(
config[ :packages ]
.map { |p| [ p, "zfs-linux-#{p.split( "-" ).last}" ] }
.flatten
).map { |d| d.name }
current_pkgs = database_data
.pkgs
.map { |p|
[ p.name, p ]
}.to_h
puts "Current packages:"
current_pkgs.each do |k,v|
puts " #{k} => #{v.version}"
end
end
# Regex which matches all packages which should be kept:
pkgs_to_keep = (config[ :packages ] + extra_kernel_deps).uniq.sort
if pkgs_to_keep.empty? then
pkgs_to_keep = /WILLNOTMATCH/
else
pkgs_to_keep =
/^(((zfs-)?(#{pkgs_to_keep.join "|"})(-headers)?)$|zfs-utils.*)/
end
# Delete any current package which has been removed from the config and
# remove it from the database:
current_pkgs
.keys
.filter { |c| !c.match pkgs_to_keep }
.each \
do |pkg|
puts "Removing package '#{pkg}'..."
`repo-remove #{database} #{pkg}`
Dir[ "#{repo}/#{pkg}-[0-9]*" ].map { |f| FileUtils.rm_rf f }
end
# This tells us if the configuration changed enough we are missing some
# required packages:
config_updated = (
current_pkgs
.keys
.filter { |c| c.match pkgs_to_keep }.grep_v( /zfs/ ).empty? and
!config[ :packages ].empty?
)
### Available versions retrieval
#############################################################################
# If the file is new enough, just quit:
if !config_updated and
last_check.exist? and
(age = Time.now - last_check.mtime) <
config[ :seconds_between_updates ] and
!options[ :force ] \
then
puts "Last check happened #{age.to_i} seconds ago, nothing to do."
clean_exit 0
end
# Fetch archzfs' repo db:
archzfs_db = PacmanDbParser.new(
download_with_progressbar( "#{ARCHZFS}/archzfs.db" ).read
)
# Process the found packages:
archzfs_pkgs = Array.new
puts "Packages on archzfs (and kernel dependencies) which match config:"
archzfs_db.data.each do |k,v|
if k.match? pkgs_to_keep then
puts " #{v[ :pkg ].name} => #{v[ :pkg ].version}"
archzfs_pkgs << v[ :pkg ]
unless v[ :k_depends ].nil? then
puts " #{v[ :k_depends ].name} => #{v[ :k_depends ].version}"
archzfs_pkgs << v[ :k_depends ]
end
end
end
archzfs_pkgs = archzfs_pkgs.sort.uniq
### Find packages to download and to delete
#############################################################################
to_delete = Array.new
to_download = Array.new
to_be_kept = Array.new
archzfs_pkgs.each do |p|
# If this package is not present in the current ones, it has to be
# downloaded:
if !current_pkgs.key? p.name then
to_download << p
# If the available version is different current one, it has to be
# downloaded and the old one has to be deleted:
elsif p.version != current_pkgs[ p.name ].version then
to_download << p
to_delete << current_pkgs[ p.name ]
# Everything else should be kept as it is:
else
to_be_kept << current_pkgs[ p.name ]
end
end
### Update the repository
#############################################################################
# If there's nothing to download or delete:
if to_delete.empty? and to_download.empty? then
# Update the file's mtime, just to avoid issues:
FileUtils.touch last_check
# Show a message and quit:
puts "Already up to date, nothing to do."
# Else, it's update time:
else
# Delete the outdated packages:
to_delete.map { |d| d.package_files.values.flatten }.flatten
.map { |d| FileUtils.rm_rf "#{repo}/#{d}" }
# Container of files to add to the repository:
to_add = Array.new
# Update the repository:
to_download.each do |d|
d.package_files.each do |name, file_names|
file_names.each do |file_name|
# Gotta reset this:
current_attempt = 0
# Let's be sure no accidental errors won't stop us:
begin
# Linux kernels download url:
base_url = "https://archive.archlinux.org/packages/l/#{name}"
# archzfs download url:
unless d.name.match /^linux/ then
base_url = "#{ARCHZFS}"
end
# Download the package:
File.open "#{repo}/#{file_name}", "wb" do |f|
f.write(
download_with_progressbar( "#{base_url}/#{file_name}" ).read
)
# Mark it as to be added to the repo if needed:
to_add << file_name unless file_name =~ /.sig$/
end
# Ensure we'll try again a few times:
rescue => e
# If we're within the threshold, sleep a while and retry:
if current_attempt <= 15 then
sleep 2 * current_attempt += 1
retry
# Otherwise we gotta stop:
else
# Clean up all downloaded files:
file_names.map { |f| FileUtils.rm_rf "#{repo}/#{f}" }
# Clean these up, just to be 100% sure:
to_add = Array.new
# Return this error:
raise "#{e.class} => Failed to retrieve 'l/#{name}/#{file_name}'"
end
end
end
end
end
# Once we get here, we can safely add all packages to the repository:
to_add.each do |package|
puts "Adding '#{package}' to 'archzfs-plus-kernel'..."
`repo-add #{database} #{repo}/#{package}`
end
# Once everything's ready, update the last_check file:
FileUtils.touch last_check
end
# Fix repo permissions:
`find "#{repo}" -type d -exec chmod 755 '{}' \\;`
`find "#{repo}" -type f -exec chmod 644 '{}' \\;`
# If something bad shuld happen, let's log it:
rescue => e
puts "Error during update: #{e.message}", e.backtrace
end
# Finally, remove the lock file:
clean_exit 0