Subversion Repositories DevTools

Rev

Rev 1044 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1042 dpurdie 1
#! /usr/bin/perl
2
########################################################################
3
# Copyright (C) Vix Technology, All rights reserved
4
#
5
# Module name   : pkg_purge
6
# Module type   :
7
# Compiler(s)   : Perl
8
# Environment(s):
9
#
10
# Description   : Dpkg_archive package purge tool
11
#                 Designed to be run periodically from cron
12
#
13
#                 Operations
14
#                 Purges packages marked for deletion
15
#                   Uses .deletepkg tag file
16
#                   Uses current ArchiveList
17
#
18
#......................................................................#
19
 
20
require 5.008_002;
21
use strict;
22
use warnings;
23
 
24
use Pod::Usage;
25
use Getopt::Long;
26
use File::Find;
27
use File::Path;
28
use POSIX qw(strftime);
29
use Data::Dumper;
30
use FindBin;                                    # Determine the current directory
31
 
32
 
33
#
34
#   Definitions
35
#
36
our $dpkg       = "$ENV{HOME}/dpkg_archive";    # Root of the archive
37
our $metaDir    = '.dpkg_archive';              # Name of meta directory
38
our $metadir    = "$dpkg/$metaDir";
39
our $list       = "$metadir/ArchiveList";       # Current Archive List
40
our $deleteTag  = '.deletepkg';                 # Tag file for deletion
41
our $rootDir    = $FindBin::Bin;                # This script - and its helpers
42
 
43
#
44
#   Options
45
#
46
our $opt_age        = 100;                          # Keep for xxx days
47
our $opt_verbose    = 0;
1044 dpurdie 48
our $opt_info       = 0;
5793 dpurdie 49
our $opt_help       = 0;
1042 dpurdie 50
 
51
#
52
#   Globals
53
#
54
our $needPkgs;                                  # Need these packages
55
 
56
#-------------------------------------------------------------------------------
57
# Function        : Mainline Entry
58
#
59
# Description     : Designed to be used with CRON and CRON to email
60
#                   admin on errors
61
#
62
# Inputs          : None
63
#
64
# Returns         : None Zero on error
65
#
66
 
67
my $result = GetOptions (
5793 dpurdie 68
                "help:+"        => \$opt_help,
1042 dpurdie 69
                "verbose:+"     => \$opt_verbose,
1044 dpurdie 70
                "info:+"        => \$opt_info,
1042 dpurdie 71
                "age=i"         => \$opt_age,
72
 
73
                );
74
die ("Bad options") if ! $result;
5793 dpurdie 75
pod2usage(-verbose => $opt_help - 1 ) if $opt_help;
1042 dpurdie 76
 
77
#
78
#   Must have an archive
79
#
80
die ("Home directory MUST have link to dpkg_archive") if ( ! -d $dpkg  );
81
die ("No dpkg_archive metadata") if ( ! -d $metadir );
82
die ("No ArchiveList") if ( ! -f $list );
83
Log ("Start of package purge. Age: $opt_age" );
84
 
85
#
86
#   Read in current archive list
87
#
88
open (my $fh, '<', $list ) || die ("Cant open $list. $!");
89
while ( <$fh> )
90
{
91
    chomp;
92
    $needPkgs->{$_} = 1;
93
}
94
close $fh;
95
 
96
#
97
#   Scan the package tree
98
#
99
opendir(DIR, $dpkg) || die "Can't opendir $dpkg: $!";
100
my @dlist = readdir(DIR);
101
closedir DIR;
102
 
103
foreach my $pname ( sort @dlist )
104
{
105
    next if ( $pname eq '.' );
106
    next if ( $pname eq '..' );
107
    next if ( $pname eq $metaDir );
108
    my $dir = $dpkg . '/'. $pname;
109
    next unless ( -d $dir );
110
 
111
    opendir(DIR, $dir )|| die "Can't opendir $dir: $!";
112
    my @vlist = readdir(DIR);
113
    closedir DIR;
114
 
115
    foreach my $vname ( sort @vlist )
116
    {
117
        next if ( $vname eq '.' );
118
        next if ( $vname eq '..' );
119
        next if ( $vname =~ m~\.TMPDIR$~ );
120
        my $vdir = $dir . '/' . $vname;
121
        next if ( -l $vdir );
122
        next unless ( -d $vdir );
123
 
124
        my $pv = "$pname/$vname";
125
        if ( exists $needPkgs->{$pv} )
126
        {
127
            #
128
            #   Need this package
129
            #   If it has a delete tag, then remove it
130
            #
131
            if ( -f "$vdir/$deleteTag" )
132
            {
133
                RemoveTagFile ($pname, $vname)
134
            }
135
            else
136
            {
137
                Log ("Package needed: $pname $vname") if ( $opt_verbose > 1 );
138
            }
139
        }
140
        else
141
        {
142
            #
143
            #   Don't need this package
144
            #   Its not in the current ArchiveList
145
            #
146
            #   If it doesn't have a tag file then  it must be a
147
            #   stray - tag it for latter. It will get aged out one day
148
            #
149
            #   If it does, then test its age
150
            #
151
            unless ( -f "$vdir/$deleteTag" )
152
            {
153
                CreateTagFile ($pname, $vname);
154
            }
155
            elsif ( -M "$vdir/$deleteTag" > $opt_age )
156
            {
157
                DeletePackage ($pname, $vname);
158
            }
159
            else
160
            {
161
                if ( $opt_verbose > 0 )
162
                {
163
                    my $age = -M "$vdir/$deleteTag";
1044 dpurdie 164
                    Log ("Package not old enough: $age: $pname $vname") ;
1042 dpurdie 165
                }
166
            }
167
        }
168
    }
169
}
170
 
171
#-------------------------------------------------------------------------------
172
# Function        : RemoveTagFile
173
#                   DeletePackage
174
#                   CreateTagFile
175
#
176
# Description     : Manipulate the tag files
177
#                   Use existing program to do the hard work
178
#
179
# Inputs          : $pname
180
#                   $pver
181
#
182
# Returns         : Nothing
183
#
184
sub RemoveTagFile
185
{
1044 dpurdie 186
    system ("$rootDir/delete_package", '-K', @_ ) unless $opt_info;
1042 dpurdie 187
    Log ("Package marked for retention: @_");
188
}
189
 
190
sub DeletePackage
191
{
1044 dpurdie 192
    system ("$rootDir/delete_package", @_ ) unless $opt_info;
1042 dpurdie 193
    Log ("Package Deleted: @_");
194
}
195
 
196
sub CreateTagFile
197
{
1044 dpurdie 198
    system ("$rootDir/delete_package", '-T', @_ ) unless $opt_info;
1042 dpurdie 199
    Log ("Package Tagged: @_");
200
}
201
 
202
#-------------------------------------------------------------------------------
203
# Function        : Log
204
#
205
# Description     : Log action to log file
1044 dpurdie 206
#                   If in info mode then simply display it
1042 dpurdie 207
#
208
# Inputs          : $text to log
209
#
210
# Returns         : Nothing
211
#
212
sub Log
213
{
5793 dpurdie 214
    my ($text) = @_;
1044 dpurdie 215
    if ( $opt_info )
216
    {
217
        print "$text\n";
218
        return;
219
    }
1042 dpurdie 220
 
221
    my $logfile = "$metadir/pkg_purge/";
222
    mkpath ($logfile);
223
 
224
    $logfile .= strftime "pkg_purge.%Y-%m-%d.txt", localtime();
225
 
5793 dpurdie 226
    open my $fh, ">>$logfile" or die "Can't logfile $logfile: $!\n";
227
    print $fh localtime()." : $text\n";
1042 dpurdie 228
    close $fh;
229
}
230
 
5793 dpurdie 231
#-------------------------------------------------------------------------------
232
#   Documentation
233
#
234
 
235
=pod
236
 
237
=head1 NAME
238
 
239
pkg_purge - Purge packages from the archive
240
 
241
=head1 SYNOPSIS
242
 
243
  pkg_purge [options]
244
 
245
 Options:
246
    -help               - brief help message
247
    -help -help         - Detailed help message
248
    -verbose            - Verbose operation
249
    -info               - Display operations that would be done
250
    -age=nn             - Set purge age
251
 
252
=head1 OPTIONS
253
 
254
=over 8
255
 
256
=item B<-help>
257
 
258
Print a brief help message and exits.
259
 
260
=item B<-help -help>
261
 
262
Print a detailed help message with an explanation for each option.
263
 
264
=item B<-man>
265
 
266
Prints the manual page and exits.
267
 
268
=item B<-verbose>
269
 
270
Increases program output. This option may be specified multiple times
271
 
272
=item B<-info>
273
 
274
Display operations that would be done, but does not purge any files.
275
 
276
=item B<-age=nn>
277
 
278
Sets the purge age. Packages that have been flagged for deletion more than nn days ago will be purged.
279
 
280
=back
281
 
282
=cut
283
 
284