-
Notifications
You must be signed in to change notification settings - Fork 9
/
Yum.pm
282 lines (225 loc) · 10.4 KB
/
Yum.pm
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
package SMT::Mirror::Yum;
use strict;
use URI;
use File::Path;
use File::Find;
use Time::HiRes qw(gettimeofday tv_interval);
use SMT::Utils;
use SMT::Mirror::Job;
use base 'SMT::Mirror::RpmMd'; # sets @SMT::Mirror::Yum::ISA = ('SMT::Mirror::RpmMd')
=head1 NAME
SMT::Mirror::Yum - mirroring of a yum metadata repository
=head1 SYNOPSIS
use SMT::Mirror::Yum;
$mirror = SMT::Mirror::Yum->new();
$mirror->uri( "http://repo.com/yum" );
$mirror->localBaseDir("/srv/www/htdocs/repo/");
$mirror->localRepoDir("RPMMD/yum/");
$mirror->mirror();
$mirror->clean();
=head1 DESCRIPTION
Mirroring of a yum metadata repository.
All functions and options are the same as in SMT::Mirror::RpmMd.
=cut
# constructor
sub new
{
my $pkgname = shift;
my $self = {};
bless($self);
$self = $self->SUPER::new(@_);
bless($self);
return $self;
}
# mirrors the repository to destination
sub mirror()
{
my $self = shift;
my %options = @_;
my $dryrun = 0;
my $t0 = [gettimeofday] ;
$dryrun = 1 if(exists $options{dryrun} && defined $options{dryrun} && $options{dryrun});
# extract the url components to create
# the destination directory
# so we save the repo to:
# $destdir/hostname.com/path
my $saveuri = SMT::Utils::getSaveUri($self->{URI});
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("Mirroring: %s"), $saveuri ));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("Target: %s"), $self->fullLocalRepoPath() ));
my $errors = $self->SUPER::mirror(%options);
return $errors if( $errors );
# find out if we have old style yum repo with headers directoy
my $job = SMT::Mirror::Job->new(
vblevel => ($errors ? $self->vblevel() : undef),
log => ($errors ? $self->{LOG} : undef), # suppress logging if there were no errors before
UserAgent => $self->{USERAGENT},
dbh => $self->{DBH},
dryrun => $dryrun );
$job->uri( $self->uri() );
$job->localBasePath( $self->localBasePath() );
$job->localRepoPath( $self->localRepoPath() );
$job->localFileLocation( "headers/header.info" );
my $mres = $job->mirror();
if( $mres == 2 && $self->statistic()->{DOWNLOAD} == 0 &&
$self->statistic()->{LINK} == 0 && $self->statistic()->{COPY} == 0 )
{
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Finished mirroring '%s'. All files are up-to-date."), $saveuri));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, "", 1, 0);
$self->job2statistic($job);
return 0;
}
elsif( $mres == 0 && -e $job->fullLocalPath() )
{
open(HDR, "< ".$job->fullLocalPath()) and do
{
while(<HDR>)
{
if($_ =~ /^(\d+):([^=]+)/)
{
my $epoch = $1;
my $file = $2;
if($file =~ /^(.+)-([^-]+)-([^-]+)\.([a-zA-Z0-9_]+)$/)
{
my $name = $1;
my $version = $2;
my $release = $3;
my $arch = $4;
my $hdrLocation = "headers/".$name."-".$epoch."-".$version."-".$release.".".$arch.".hdr";
my $hjob = SMT::Mirror::Job->new(vblevel => $self->vblevel(), UserAgent => $self->{USERAGENT}, log => $self->{LOG}, dbh => $self->{DBH}, dryrun => $dryrun );
$hjob->uri( $self->uri() );
$hjob->localBasePath( $self->localBasePath() );
$hjob->localRepoPath( $self->localRepoPath() );
$hjob->localFileLocation( $hdrLocation );
$hjob->noChecksumCheck(1);
if( $dryrun )
{
if( $hjob->outdated() )
{
printLog($self->{LOG}, $self->vblevel(), LOG_INFO2, sprintf("N %s", $hjob->fullLocalPath() ));
$self->{STATISTIC}->{DOWNLOAD} += 1;
}
else
{
printLog($self->{LOG}, $self->vblevel(), LOG_DEBUG, sprintf("U '%s'", $hjob->fullLocalPath() ));
$self->{STATISTIC}->{UPTODATE} += 1;
}
next;
}
$mres = $hjob->mirror();
$self->job2statistic($hjob);
}
}
}
close HDR;
};
$self->job2statistic($job);
}
# getting the headers/header.info file failed, but
# all the previous mirroing via Mirror::RpmMd succedded.
# This means the header.info is not there (NCC return 403 for not found
# files), so no need to mirror headers dir
elsif ($job->wasNotFound() || ($errors == 0 && $job->wasForbidden()))
{
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Finished mirroring '%s'. All files are up-to-date."), $saveuri));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, "", 1, 0);
return 0;
}
# if there were errors before, we can't tell what's up with headers dir
else
{
printLog($self->{LOG}, $self->vblevel(), LOG_WARN,
__("Cannot determine if 'header' directory needs mirroring. If this is a RES5 repository, this warning can be ignored."));
$self->job2statistic($job);
}
if( $dryrun )
{
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Finished dryrun '%s'"), $saveuri));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Files to download : %s"), $self->{STATISTIC}->{DOWNLOAD}));
}
else
{
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Finished mirroring '%s'"), $saveuri));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Total transferred files : %s"), $self->{STATISTIC}->{DOWNLOAD}));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Total transferred file size : %s bytes (%s)"),
$self->{STATISTIC}->{DOWNLOAD_SIZE}, SMT::Utils::byteFormat($self->{STATISTIC}->{DOWNLOAD_SIZE})));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Total linked files : %s"), $self->{STATISTIC}->{LINK}));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Total copied files : %s"), $self->{STATISTIC}->{COPY}));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Files up to date : %s"), $self->{STATISTIC}->{UPTODATE}));
}
#
# I think YUM repositories do not have patches
#
#printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> New Security Updates : %s"), $self->{STATISTIC}->{NEWSECPATCHES}));
#printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> New Recommended Updates : %s"), $self->{STATISTIC}->{NEWRECPATCHES}));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Errors : %s"), $self->{STATISTIC}->{ERROR}));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Mirror Time : %s"), SMT::Utils::timeFormat(tv_interval($t0))));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, "", 1, 0);
return $self->{STATISTIC}->{ERROR};
}
sub clean
{
my $self = shift;
my $t0 = [gettimeofday] ;
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("Cleaning: %s"), $self->fullLocalRepoPath() ) );
$self->SUPER::clean();
my $headerFile = SMT::Utils::cleanPath($self->localBasePath(), $self->localRepoPath(), "headers/header.info");
if( -e $headerFile )
{
$self->{CLEANLIST} = {};
find ( { wanted =>
sub
{
if ( $File::Find::dir =~ /\/headers/ && -f $File::Find::name )
{
my $name = SMT::Utils::cleanPath($File::Find::name);
$self->{CLEANLIST}->{$name} = 1;
}
}
, no_chdir => 1 }, $self->fullLocalRepoPath() );
# remove headers/header.info first
my $hdrLocation = SMT::Utils::cleanPath($self->fullLocalRepoPath(), "headers/header.info");
delete $self->{CLEANLIST}->{$hdrLocation} if (exists $self->{CLEANLIST}->{$hdrLocation});
open(HDR, "< $headerFile") and do
{
while(<HDR>)
{
if($_ =~ /^(\d+):([^=]+)/)
{
my $epoch = $1;
my $file = $2;
if($file =~ /^(.+)-([^-]+)-([^-]+)\.([a-zA-Z0-9_]+)$/)
{
my $name = $1;
my $version = $2;
my $release = $3;
my $arch = $4;
$hdrLocation = SMT::Utils::cleanPath($self->fullLocalRepoPath(), "headers/".$name."-".$epoch."-".$version."-".$release.".".$arch.".hdr");
# if this path is in the CLEANLIST, delete it
delete $self->{CLEANLIST}->{$hdrLocation} if (exists $self->{CLEANLIST}->{$hdrLocation});
}
}
}
};
close HDR;
my $cnt = 0;
foreach my $file ( keys %{$self->{CLEANLIST}} )
{
printLog($self->{LOG}, $self->vblevel(), LOG_DEBUG, "Delete: $file");
$cnt += unlink $file;
# header do not have a checksum, so they are not in the DB
#$self->{DBH}->do(sprintf("DELETE from RepositoryContentData where localpath = %s", $self->{DBH}->quote($file) ) );
}
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("Finished cleaning: '%s'"), $self->fullLocalRepoPath()."/headers/" ));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Removed files : %s"), $cnt));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, sprintf(__("=> Clean Time : %s"), SMT::Utils::timeFormat(tv_interval($t0))));
printLog($self->{LOG}, $self->vblevel(), LOG_INFO1, "", 1, 0);
}
}
=head1 SEE ALSO
SMT::Mirror::RpmMd
=head1 AUTHOR
mc@suse.de
=head1 COPYRIGHT
Copyright 2007-2012 SUSE LINUX Products GmbH, Nuernberg, Germany.
=cut
1;