-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #337 from bdunne/nightly_rpm_cleaner
Add a purger for the nightly builds and run it before updating the RPM repo
- Loading branch information
Showing
6 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env ruby | ||
|
||
$LOAD_PATH << File.expand_path("../lib", __dir__) | ||
|
||
require 'manageiq-rpm_build' | ||
|
||
ManageIQ::RPMBuild::NightlyBuildPurger.new.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module ManageIQ | ||
module RPMBuild | ||
class NightlyBuildPurger | ||
include S3Common | ||
|
||
def run | ||
candidates = {} | ||
keepers = {} | ||
|
||
client.list_objects(:bucket => OPTIONS.rpm_repository.s3_api.bucket, :prefix => File.join("builds", "manageiq-nightly")).flat_map(&:contents).each do |object| | ||
package, timestamp = package_timestamp_from_key(object.key) | ||
candidates[package] ||= [] | ||
keepers[package] ||= [] | ||
|
||
if recent?(timestamp) | ||
keepers[package] << object.key | ||
next | ||
end | ||
|
||
position = timestamp[2, 8].to_i # YYYYMMDDHHMMSS -> YYMMDDHH | ||
candidates[package][position] = object.key | ||
end | ||
|
||
candidates.each_key do |package| | ||
# Keep the most recent 7 nightly builds of any package | ||
(candidates[package].compact[0..-7] - keepers[package]).each { |i| delete(i) } | ||
end | ||
end | ||
|
||
private | ||
|
||
def delete(key) | ||
print "Deleting #{key}... " | ||
client.delete_object(:bucket => OPTIONS.rpm_repository.s3_api.bucket, :key => key) | ||
puts "done." | ||
end | ||
|
||
def now | ||
@now ||= Time.now.utc | ||
end | ||
|
||
def package_timestamp_from_key(key) | ||
key.match(/.*\/(.*)-.*(\d{14}).*/).captures | ||
end | ||
|
||
def recent?(timestamp) | ||
require 'active_support' | ||
require 'active_support/time' | ||
require 'date' | ||
# Keep anything 1.week or newer | ||
DateTime.parse(timestamp) > 1.week.before(now) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
RSpec.describe(ManageIQ::RPMBuild::NightlyBuildPurger) do | ||
it("#package_timestamp_from_key (private)") do | ||
expect(described_class.new.send(:package_timestamp_from_key, "builds/manageiq-nightly/manageiq-pods-13.0.0-20211004000013.el8.x86_64.rpm")).to eq(["manageiq-pods-13.0.0", "20211004000013"]) | ||
expect(described_class.new.send(:package_timestamp_from_key, "builds/manageiq-nightly/manageiq-ui-13.0.0-20211004000013.el8.x86_64.rpm")).to eq(["manageiq-ui-13.0.0", "20211004000013"]) | ||
expect(described_class.new.send(:package_timestamp_from_key, "builds/manageiq-nightly/manageiq-ui-13.0.0-0.1.20210705000025.el8.x86_64.rpm")).to eq(["manageiq-ui-13.0.0", "20210705000025"]) | ||
end | ||
|
||
it("#recent? (private)") do | ||
allow(subject).to receive(:now).and_return(Time.at(1668100000).utc) # 2022-11-10 17:06:40 | ||
|
||
expect(subject.send(:recent?, "20221110170640")).to eq(true) # Now | ||
expect(subject.send(:recent?, "20221230170640")).to eq(true) # Future | ||
expect(subject.send(:recent?, "20221104170640")).to eq(true) # 6 days ago | ||
expect(subject.send(:recent?, "20221103170640")).to eq(false) # 1 week ago | ||
expect(subject.send(:recent?, "20221027170640")).to eq(false) # 2 weeks ago | ||
expect(subject.send(:recent?, "19700101000000")).to eq(false) | ||
expect { subject.send(:recent?, "xyz") }.to raise_error(Date::Error) | ||
end | ||
end |