Skip to content

Instantly share code, notes, and snippets.

@foursixnine
Created April 3, 2017 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foursixnine/623397e6f3231c3dc8a0c435d874e5df to your computer and use it in GitHub Desktop.
Save foursixnine/623397e6f3231c3dc8a0c435d874e5df to your computer and use it in GitHub Desktop.
Enforce caching cleanup
use strict;
use warnings;
use File::Basename;
use Fcntl qw(:flock);
use List::MoreUtils;
use Data::Dumper;
use JSON;
use File::Glob;
my $cache = {};
my $host = "http://openqa.suse.de";
#my $limit = 5;
my $delete = 0;
my $db_file = "/var/lib/openqa/cache/cache.db";
sub read_db {
local $/; # use slurp mode to read the whole file at once.
open(my $fh, "<", $db_file) or die "File not found";
flock($fh, LOCK_EX);
eval { $cache = JSON->new->relaxed->decode(<$fh>); };
die "parse error in $db_file:\n$@" if $@;
print("Got: \n");
print(Dumper($cache));
close($fh);
print("Read db file\n");
}
read_db;
my @assets = glob("/var/lib/openqa/cache/*.img /var/lib/openqa/cache/*.qcow2 /var/lib/openqa/cache/*.iso");
foreach my $asset (@assets){
if( -B $asset){
if ( grep /$asset/, @{$cache->{$host}}){
print "Keeping $asset\n";
} else {
print "Deleting $asset\n";
unlink($asset) or die "Could not delete $asset\n";
}
} else {
print "Found $asset but is not the files we're looking for\n";
}
}
@foursixnine
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment