diff options
author | Michael Scherer <misc@mageia.org> | 2011-07-19 01:14:54 +0000 |
---|---|---|
committer | Michael Scherer <misc@mageia.org> | 2011-07-19 01:14:54 +0000 |
commit | be91201208f86bdb6b042613a498fd16585acfba (patch) | |
tree | 3a55c7b96fb59bffcd1c45e2d1b665e09f2ef582 /modules/mirror_cleaner | |
parent | 2a69c0738d2a174ffbb326a9e1464e54f99a0758 (diff) | |
download | puppet-be91201208f86bdb6b042613a498fd16585acfba.tar puppet-be91201208f86bdb6b042613a498fd16585acfba.tar.gz puppet-be91201208f86bdb6b042613a498fd16585acfba.tar.bz2 puppet-be91201208f86bdb6b042613a498fd16585acfba.tar.xz puppet-be91201208f86bdb6b042613a498fd16585acfba.zip |
add a module to clean orphan, as discussed on the ml :
- move rpm without a corresponding src.rpm to a directory after 2 weeks
- remove files after 1 month
Diffstat (limited to 'modules/mirror_cleaner')
-rwxr-xr-x | modules/mirror_cleaner/files/orphans_cleaner.pl | 73 | ||||
-rw-r--r-- | modules/mirror_cleaner/manifests/init.pp | 34 |
2 files changed, 107 insertions, 0 deletions
diff --git a/modules/mirror_cleaner/files/orphans_cleaner.pl b/modules/mirror_cleaner/files/orphans_cleaner.pl new file mode 100755 index 00000000..04a73fe2 --- /dev/null +++ b/modules/mirror_cleaner/files/orphans_cleaner.pl @@ -0,0 +1,73 @@ +#!/usr/bin/perl + +# this script will look at the list of rpm, and move orphan to a directory, if they are too old +# another script should take care of cleaning this directory ( or puppet ) + +use strict; +use RPM4; +use File::stat; +use File::Basename; +use File::Copy; +use File::Path qw(make_path); + +my @arches = ('i586','x86_64'); +my @sections = ('core','nonfree','tainted'); +my @medias = ('backports', 'backports_testing', 'release', 'updates', 'updates_testing'); +my $move_delay = 60*60*24*14; + +my ($path, $dest_path) = @ARGV; + +my $qf = "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}.rpm %{SOURCERPM}"; + +my %hash ; +my ($filename, $srpm, $dest_rpm); + + +my ($source_hdlist, $binary_hdlist, $rpm_path); + +foreach my $a ( @arches ) { + foreach my $s ( @sections ) { + foreach my $m ( @medias ) { + + $rpm_path = "$path/$a/media/$s/$m"; + $binary_hdlist = "$rpm_path/media_info/hdlist.cz"; + $source_hdlist = "$path/SRPMS/$s/$m/media_info/hdlist.cz"; + + next if not -f $source_hdlist; + next if not -f $binary_hdlist; + + next if stat($source_hdlist)->size() <= 64; + next if stat($binary_hdlist)->size() <= 64; + + open(my $hdfh, "zcat '$binary_hdlist' 2>/dev/null |") or die "Can't open $_"; + while (my $hdr = stream2header($hdfh)) { + ($filename, $srpm) = split(/ /,$hdr->queryformat($qf)); + push(@{$hash{$srpm}}, $filename); + } + close($hdfh); + + + open($hdfh, "zcat '$source_hdlist' 2>/dev/null |") or die "Can't open $_"; + while (my $hdr = stream2header($hdfh)) { + $srpm = $hdr->queryformat("%{NAME}-%{VERSION}-%{RELEASE}.src.rpm"); + delete $hash{$srpm}; + } + close($hdfh); + + foreach my $v ( values %hash ) + { + foreach my $rpm ( @{$v} ) { + $rpm = "$rpm_path/$rpm"; + # sometimes, packages are removed without hdlist to be updated + next if not -f "$rpm"; + if (time() > $move_delay + stat("$rpm")->ctime()) { + ( $dest_rpm = $rpm ) =~ s/$path/$dest_path/; + my $dir = dirname $dest_rpm; + make_path $dir if not -d $dir; + copy($rpm, $dest_rpm) + } + } + } + } + } +} diff --git a/modules/mirror_cleaner/manifests/init.pp b/modules/mirror_cleaner/manifests/init.pp new file mode 100644 index 00000000..1db1e922 --- /dev/null +++ b/modules/mirror_cleaner/manifests/init.pp @@ -0,0 +1,34 @@ +class mirror_cleaner { + class base { + file { "/usr/local/bin/orphans_cleaner.pl": + ensure => present, + owner => root, + group => root, + mode => 755, + source => 'puppet:///modules/mirror_cleaner/orphans_cleaner.pl', + } + } + + define orphans($base) { + include mirror_cleaner::base + + $orphan_dir = "$base/$name" + + file { $orphan_dir: + ensure => directory + } + + cron { "clean orphans $name": + command => "/usr/local/bin/orphans_cleaner.pl $base/$name $orphan_dir", + hour => 5, + minute => 30, + user => root, + } + + tidy { $orphan_dir: + age => "4w", + recurse => true, + matches => ["*.rpm"], + } + } +} |