| 7396 |
dpurdie |
1 |
#! /usr/bin/perl
|
|
|
2 |
########################################################################
|
|
|
3 |
# COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.
|
|
|
4 |
#
|
|
|
5 |
# Module name : blatS3Sync.pl
|
|
|
6 |
# Module type :
|
|
|
7 |
# Compiler(s) : Perl
|
|
|
8 |
# Environment(s):
|
|
|
9 |
#
|
|
|
10 |
# Description : This is a blat related task that will perform S3 SYNC
|
|
|
11 |
# transfers for configured releases
|
|
|
12 |
#
|
|
|
13 |
# Usage : ARGV[0] - Path to config file for this instance
|
|
|
14 |
#
|
|
|
15 |
#......................................................................#
|
|
|
16 |
|
|
|
17 |
require 5.008_002;
|
|
|
18 |
use strict;
|
|
|
19 |
use warnings;
|
|
|
20 |
use Getopt::Long;
|
|
|
21 |
use File::Basename;
|
|
|
22 |
use Data::Dumper;
|
|
|
23 |
use File::Spec::Functions;
|
|
|
24 |
use POSIX ":sys_wait_h";
|
|
|
25 |
use File::Temp qw/tempfile/;
|
|
|
26 |
use Digest::MD5 qw(md5_base64 md5_hex);
|
|
|
27 |
use File::Path qw( rmtree );
|
|
|
28 |
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
|
|
|
29 |
use JSON;
|
|
|
30 |
|
|
|
31 |
use FindBin; # Determine the current directory
|
|
|
32 |
use lib "$FindBin::Bin/lib"; # Allow local libraries
|
|
|
33 |
|
|
|
34 |
use Utils;
|
|
|
35 |
use StdLogger; # Log to sdtout
|
|
|
36 |
use Logger; # Log to file
|
|
|
37 |
|
|
|
38 |
#
|
|
|
39 |
# Database interface
|
|
|
40 |
# Pinched from jats and modified so that this software is not dependent on JATS
|
|
|
41 |
#
|
|
|
42 |
use IO::Handle;
|
|
|
43 |
use JatsRmApi;
|
|
|
44 |
use DBI;
|
|
|
45 |
|
|
|
46 |
#
|
|
|
47 |
# Globals
|
|
|
48 |
#
|
|
|
49 |
my $logger = StdLogger->new(); # Stdout logger. Only during config
|
|
|
50 |
$logger->err("No config file specified") unless (defined $ARGV[0]);
|
|
|
51 |
$logger->err("Config File does not exist: $ARGV[0]") unless (-f $ARGV[0]);
|
|
|
52 |
my $name = basename( $ARGV[0]);
|
|
|
53 |
$name =~ s~.conf$~~;
|
|
|
54 |
my $now = 0;
|
|
|
55 |
my $startTime = 0;
|
|
|
56 |
my $tagDirTime = 0;
|
|
|
57 |
my $lastDirScan = 0;
|
|
|
58 |
my $lastS3Refresh = 0;
|
|
|
59 |
my $lastTagListUpdate = 0;
|
|
|
60 |
my $mtimeConfig = 0;
|
|
|
61 |
my $conf;
|
|
|
62 |
my $yday = -1;
|
|
|
63 |
my $linkUp = 1;
|
|
|
64 |
my $RM_DB;
|
|
|
65 |
my $activeReleases;
|
| 7397 |
dpurdie |
66 |
my $wedgedCount = 0;
|
| 7396 |
dpurdie |
67 |
|
|
|
68 |
#
|
|
|
69 |
# Contain statisics maintained while operating
|
|
|
70 |
# Can be dumped with a kill -USR2
|
|
|
71 |
# List here for documentation
|
|
|
72 |
#
|
|
|
73 |
|
|
|
74 |
my %statistics = (
|
|
|
75 |
SeqNum => 0, # Bumped when $statistics are dumped
|
|
|
76 |
timeStamp => 0, # DateTime when statistics are dumped
|
|
|
77 |
upTime => 0, # Seconds since program start
|
|
|
78 |
Cycle => 0, # Major process loop counter
|
|
|
79 |
phase => 'Init', # Current phase of operation
|
|
|
80 |
state => 'OK', # Nagios state
|
| 7397 |
dpurdie |
81 |
wedged => 0, # Wedge indication - main loop not cycling
|
| 7396 |
dpurdie |
82 |
#
|
|
|
83 |
# The following are reset each day
|
|
|
84 |
dayStart => 0, # DateTime when daily data was reset
|
|
|
85 |
txCount => 0, # Packages Transferred
|
| 7397 |
dpurdie |
86 |
txBytes => 0, # Bytes Transferred
|
| 7396 |
dpurdie |
87 |
delCount => 0, # Packages marked for deletion
|
|
|
88 |
linkErrors => 0, # Transfer (S3) errors
|
|
|
89 |
#
|
|
|
90 |
# Per Cycle Data - Calculated each processing Cycle
|
| 7406 |
dpurdie |
91 |
total => 0, # Number targets
|
| 7396 |
dpurdie |
92 |
);
|
|
|
93 |
|
|
|
94 |
#
|
|
|
95 |
# Describe configuration parameters
|
|
|
96 |
#
|
|
|
97 |
my %cdata = (
|
|
|
98 |
'piddir' => {'mandatory' => 1 , 'fmt' => 'dir'},
|
|
|
99 |
'sleep' => {'default' => 5 , 'fmt' => 'period'},
|
|
|
100 |
'sleepLinkDown' => {'default' => '1m' , 'fmt' => 'period'},
|
|
|
101 |
'dpkg_archive' => {'mandatory' => 1 , 'fmt' => 'dir'},
|
|
|
102 |
'logfile' => {'mandatory' => 1 , 'fmt' => 'vfile'},
|
|
|
103 |
'logfile.size' => {'default' => '1M' , 'fmt' => 'size'},
|
|
|
104 |
'logfile.count' => {'default' => 9 , 'fmt' => 'int'},
|
|
|
105 |
|
|
|
106 |
'verbose' => {'default' => 0 , 'fmt' => 'int'}, # Debug ...
|
|
|
107 |
'active' => {'default' => 1 , 'fmt' => 'bool'}, # Disable alltogether
|
|
|
108 |
'debug' => {'default' => 0 , 'fmt' => 'bool'}, # Log to screen
|
|
|
109 |
'txdetail' => {'default' => 0 , 'fmt' => 'bool'}, # Show transfer times
|
|
|
110 |
'noTransfers' => {'default' => 0 , 'fmt' => 'bool'}, # Debugging option to prevent transfers
|
| 7397 |
dpurdie |
111 |
'transferDir' => {'default' => 'pkg/S3TRANSFER' , 'fmt' => 'text'},
|
| 7396 |
dpurdie |
112 |
|
| 7397 |
dpurdie |
113 |
'tagdir' => {'mandatory' => 1 , 'fmt' => 'mkdir'},
|
|
|
114 |
'workdir' => {'mandatory' => 1 , 'fmt' => 'mkdir'},
|
| 7396 |
dpurdie |
115 |
'forcedirscan' => {'default' => 100 , 'fmt' => 'period'},
|
|
|
116 |
'forces3update' => {'default' => '30m' , 'fmt' => 'period'},
|
|
|
117 |
'tagListUpdate' => {'default' => '1h' , 'fmt' => 'period'},
|
|
|
118 |
'S3Bucket' => {'mandatory' => 1 , 'fmt' => 'text'},
|
|
|
119 |
'S3Profile' => {'mandatory' => 1 , 'fmt' => 'text'},
|
| 7406 |
dpurdie |
120 |
'S3Region' => {'default' => undef , 'fmt' => 'text'},
|
| 7396 |
dpurdie |
121 |
|
|
|
122 |
);
|
|
|
123 |
|
|
|
124 |
|
|
|
125 |
#
|
|
|
126 |
# Read in the configuration
|
|
|
127 |
# Set up a logger
|
|
|
128 |
# Write a pidfile - thats not used
|
|
|
129 |
$now = $startTime = time();
|
|
|
130 |
readConfig();
|
|
|
131 |
Utils::writepid($conf);
|
|
|
132 |
$logger->logmsg("Starting...");
|
|
|
133 |
readStatistics();
|
|
|
134 |
sighandlers($conf);
|
|
|
135 |
|
|
|
136 |
#
|
|
|
137 |
# Main processing loop
|
|
|
138 |
# Will exit when terminated by parent
|
|
|
139 |
#
|
|
|
140 |
while (1)
|
|
|
141 |
{
|
|
|
142 |
$logger->verbose3("Processing");
|
|
|
143 |
$statistics{Cycle}++;
|
| 7397 |
dpurdie |
144 |
$wedgedCount = 0;
|
| 7396 |
dpurdie |
145 |
$now = time();
|
|
|
146 |
|
|
|
147 |
$statistics{phase} = 'ReadConfig';
|
|
|
148 |
readConfig();
|
|
|
149 |
if ( $conf->{'active'} )
|
|
|
150 |
{
|
|
|
151 |
$statistics{phase} = 'Refresh S3 Info';
|
|
|
152 |
refreshS3Info();
|
|
|
153 |
if( $linkUp )
|
|
|
154 |
{
|
|
|
155 |
$statistics{phase} = 'Monitor Requests';
|
|
|
156 |
monitorRequests();
|
|
|
157 |
|
|
|
158 |
$statistics{phase} = 'maintainTagList';
|
|
|
159 |
maintainTagList();
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
$statistics{phase} = 'Sleep';
|
|
|
164 |
sleep( $linkUp ? $conf->{'sleep'} : $conf->{'sleepLinkDown'} );
|
|
|
165 |
reapChildren();
|
|
|
166 |
|
|
|
167 |
# If my PID file ceases to be, then exit the daemon
|
|
|
168 |
# Used to force daemon to restart
|
|
|
169 |
#
|
|
|
170 |
unless ( -f $conf->{'pidfile'} )
|
|
|
171 |
{
|
|
|
172 |
$logger->logmsg("Terminate. Pid file removed");
|
|
|
173 |
last;
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
$statistics{phase} = 'Terminated';
|
|
|
177 |
$logger->logmsg("Child End");
|
|
|
178 |
exit 0;
|
|
|
179 |
|
|
|
180 |
#-------------------------------------------------------------------------------
|
|
|
181 |
# Function : reapChildren
|
|
|
182 |
#
|
|
|
183 |
# Description : Reap any and all dead children
|
|
|
184 |
# Call in major loops to prevent zombies accumulating
|
|
|
185 |
#
|
|
|
186 |
# Inputs : None
|
|
|
187 |
#
|
|
|
188 |
# Returns :
|
|
|
189 |
#
|
|
|
190 |
sub reapChildren
|
|
|
191 |
{
|
|
|
192 |
my $currentPhase = $statistics{phase};
|
|
|
193 |
$statistics{phase} = 'Reaping';
|
|
|
194 |
|
|
|
195 |
my $kid;
|
|
|
196 |
do {
|
|
|
197 |
$kid = waitpid(-1, WNOHANG);
|
|
|
198 |
} while ( $kid > 0 );
|
|
|
199 |
|
|
|
200 |
$statistics{phase} = $currentPhase;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
|
|
|
204 |
#-------------------------------------------------------------------------------
|
|
|
205 |
# Function : readConfig
|
|
|
206 |
#
|
|
|
207 |
# Description : Re read the config file if it modification time has changed
|
|
|
208 |
#
|
|
|
209 |
# Inputs : Nothing
|
|
|
210 |
#
|
|
|
211 |
# Returns : 0 - Config not read
|
|
|
212 |
# 1 - Config read
|
|
|
213 |
# Config file has changed
|
|
|
214 |
#
|
|
|
215 |
sub readConfig
|
|
|
216 |
{
|
|
|
217 |
my ($mtime) = Utils::mtime($ARGV[0]);
|
|
|
218 |
my $rv = 0;
|
|
|
219 |
|
|
|
220 |
if ( $mtimeConfig != $mtime )
|
|
|
221 |
{
|
|
|
222 |
$logger->logmsg("Reading config file: $ARGV[0]");
|
|
|
223 |
$mtimeConfig = $mtime;
|
|
|
224 |
my $errors;
|
|
|
225 |
($conf, $errors) = Utils::readconf ( $ARGV[0], \%cdata );
|
|
|
226 |
if ( scalar @{$errors} > 0 )
|
|
|
227 |
{
|
|
|
228 |
warn "$_\n" foreach (@{$errors});
|
|
|
229 |
die ("Config contained errors\n");
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
#
|
|
|
233 |
# Reset some information
|
|
|
234 |
# Create a new logger
|
|
|
235 |
#
|
|
|
236 |
$logger = Logger->new($conf) unless $conf->{debug};
|
|
|
237 |
$conf->{logger} = $logger;
|
|
|
238 |
$conf->{'pidfile'} = $conf->{'piddir'} . '/' . $name . '.pid';
|
|
|
239 |
$logger->setVerbose($conf->{verbose});
|
|
|
240 |
$logger->verbose("Log Levl: $conf->{verbose}");
|
|
|
241 |
|
|
|
242 |
#
|
|
|
243 |
# Setup statistics filename
|
|
|
244 |
$conf->{'statsfile'} = $conf->{'piddir'} . '/' . $name . '.stats';
|
|
|
245 |
$conf->{'statsfiletmp'} = $conf->{'piddir'} . '/' . $name . '.stats.tmp';
|
|
|
246 |
|
|
|
247 |
#
|
|
|
248 |
# When config is read force some actions
|
|
|
249 |
# - Force tagList to be created
|
|
|
250 |
# - Force refresh from S3
|
|
|
251 |
$lastTagListUpdate = 0;
|
|
|
252 |
$lastS3Refresh = 0;
|
| 7406 |
dpurdie |
253 |
$rv = 1;
|
| 7396 |
dpurdie |
254 |
|
| 7406 |
dpurdie |
255 |
#
|
|
|
256 |
# When config is read force some actions
|
| 7396 |
dpurdie |
257 |
#Utils::DebugDumpData ("Config", $conf);
|
|
|
258 |
|
| 7406 |
dpurdie |
259 |
$logger->warn("All Transfers disabled") if ( $conf->{'noTransfers'} );
|
|
|
260 |
$logger->warn("S3Sync is inactive") unless ( $conf->{'active'} );
|
|
|
261 |
}
|
|
|
262 |
|
| 7396 |
dpurdie |
263 |
return $rv;
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
#-------------------------------------------------------------------------------
|
|
|
267 |
# Function : refreshS3Info
|
|
|
268 |
#
|
|
|
269 |
# Description : At startup, and at time after startup examine the S3 bucket
|
|
|
270 |
# and recover information from it
|
|
|
271 |
#
|
|
|
272 |
# Inputs :
|
|
|
273 |
#
|
|
|
274 |
# Returns : 0 - Gross error ( Bucket access)
|
|
|
275 |
#
|
|
|
276 |
sub refreshS3Info
|
|
|
277 |
{
|
|
|
278 |
my $rv = 1;
|
|
|
279 |
if ( !$linkUp || ($now > ($lastS3Refresh + $conf->{'forces3update'})) )
|
|
|
280 |
{
|
|
|
281 |
$logger->verbose2("refreshS3Info");
|
|
|
282 |
$lastS3Refresh = $now;
|
|
|
283 |
|
|
|
284 |
#
|
|
|
285 |
# Examine the s3 bucket and extract useful information
|
|
|
286 |
#
|
|
|
287 |
my $startTime = time;
|
|
|
288 |
$rv = examineS3Bucket();
|
|
|
289 |
unless ($rv) {
|
|
|
290 |
$statistics{linkErrors}++;
|
|
|
291 |
$linkUp = 0;
|
| 7397 |
dpurdie |
292 |
} else {
|
|
|
293 |
$linkUp = 1;
|
| 7396 |
dpurdie |
294 |
}
|
|
|
295 |
|
|
|
296 |
#
|
|
|
297 |
# Display the duration of the refresh
|
|
|
298 |
# Diagnostic use
|
|
|
299 |
#
|
|
|
300 |
if ($conf->{txdetail}) {
|
|
|
301 |
my $duration = time - $startTime;
|
|
|
302 |
$logger->logmsg("refreshS3Info: Stats: $duration Secs");
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
}
|
|
|
306 |
return $rv;
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
|
|
|
310 |
|
|
|
311 |
#-------------------------------------------------------------------------------
|
|
|
312 |
# Function : monitorRequests
|
|
|
313 |
#
|
|
|
314 |
# Description : Monitor S3Sync requests
|
|
|
315 |
# This is simply done my polling Release Manager - at the moment
|
|
|
316 |
#
|
|
|
317 |
# Inputs : None
|
|
|
318 |
#
|
|
|
319 |
# Returns : Nothing
|
|
|
320 |
#
|
|
|
321 |
sub monitorRequests
|
|
|
322 |
{
|
|
|
323 |
#
|
|
|
324 |
# Determine if new tags are present by examining the time
|
|
|
325 |
# that the directory was last modified.
|
|
|
326 |
#
|
|
|
327 |
# Allow for a forced scan to catch packages that did not transfer
|
|
|
328 |
# on the first attempt
|
|
|
329 |
#
|
|
|
330 |
my $tagCount = 0;
|
|
|
331 |
my ($mtime) = Utils::mtime($conf->{'tagdir'} );
|
|
|
332 |
if ( ($mtime > $tagDirTime) || ($now > ($lastDirScan + $conf->{'forcedirscan'})) )
|
|
|
333 |
{
|
|
|
334 |
$logger->verbose2("monitorRequests: $conf->{'tagdir'}");
|
|
|
335 |
#$logger->verbose2("monitorRequests: mtime:" . ($mtime > $tagDirTime));
|
|
|
336 |
#$logger->verbose2("monitorRequests: last:" . ($now > ($lastDirScan + $conf->{'forcedirscan'})));
|
|
|
337 |
|
|
|
338 |
#
|
|
|
339 |
# Package tags information is not really used
|
|
|
340 |
# Just delete all the tags
|
|
|
341 |
# Used to trigger the scan - rather than rely on the slow data
|
|
|
342 |
# base poll. Still need a change in release sequence number
|
|
|
343 |
#
|
|
|
344 |
my $dh;
|
|
|
345 |
unless (opendir($dh, $conf->{'tagdir'}))
|
|
|
346 |
{
|
|
|
347 |
$logger->warn ("can't opendir $conf->{'tagdir'}: $!");
|
|
|
348 |
return;
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
#
|
|
|
352 |
# Process each entry
|
|
|
353 |
# Ignore those that start with a .
|
|
|
354 |
# Remove all files
|
|
|
355 |
#
|
|
|
356 |
while (my $tag = readdir($dh) )
|
|
|
357 |
{
|
|
|
358 |
next if ( $tag =~ m~^\.~ );
|
|
|
359 |
my $file = "$conf->{'tagdir'}/$tag";
|
|
|
360 |
$logger->verbose3("processTags: $file");
|
|
|
361 |
|
|
|
362 |
next unless ( -f $file );
|
|
|
363 |
unlink $file;
|
|
|
364 |
}
|
|
|
365 |
|
|
|
366 |
#
|
|
|
367 |
# Reset the scan time triggers
|
|
|
368 |
#
|
|
|
369 |
$tagDirTime = $mtime;
|
|
|
370 |
$lastDirScan = $now;
|
|
|
371 |
|
|
|
372 |
#
|
|
|
373 |
# Examine Release Manager looking for active releases that have S3Sync support
|
|
|
374 |
# Purpose is to:
|
|
|
375 |
# Detect new Releases
|
|
|
376 |
# Detect dead Releases
|
|
|
377 |
# Detect changed Releases
|
|
|
378 |
#
|
|
|
379 |
connectRM(\$RM_DB, $conf->{verbose} > 3);
|
|
|
380 |
|
|
|
381 |
foreach my $rtag_id (keys %{$activeReleases}) {
|
|
|
382 |
$activeReleases->{$rtag_id}{exists} = 0;
|
|
|
383 |
}
|
|
|
384 |
my $m_sqlstr = "SELECT rt.rtag_id,rm.seqnum, rt.s3sync, rt.official, rm.timestamp " .
|
|
|
385 |
"FROM RELEASE_MANAGER.release_tags rt, RELEASE_MANAGER.release_modified rm " .
|
|
|
386 |
"WHERE rt.s3sync = 'Y' AND rm.rtag_id = rt.rtag_id AND rt.official in ('N', 'R', 'C')";
|
|
|
387 |
|
|
|
388 |
my $curData = getDataFromRm ('monitorRequests', $m_sqlstr, {data => 0} );
|
| 7406 |
dpurdie |
389 |
$statistics{total} = scalar @{$curData};
|
|
|
390 |
|
| 7396 |
dpurdie |
391 |
foreach my $entry (@{$curData}) {
|
|
|
392 |
my ($rtag_id, $seqnum) = @{$entry};
|
| 7406 |
dpurdie |
393 |
$logger->verbose3("rtagid: $rtag_id, seqnumm: $seqnum");
|
| 7396 |
dpurdie |
394 |
|
|
|
395 |
if (! exists $activeReleases->{$rtag_id} || ! exists $activeReleases->{$rtag_id}{s3} ) {
|
|
|
396 |
$logger->logmsg("New Release Detected. rtag_id: $rtag_id, seq:$seqnum");
|
|
|
397 |
processChangedRelease($rtag_id, $seqnum);
|
|
|
398 |
$lastTagListUpdate = 0;
|
|
|
399 |
|
|
|
400 |
} elsif (($activeReleases->{$rtag_id}{seqnum} || 0) ne ($seqnum || 0) ) {
|
|
|
401 |
$logger->logmsg("Change Release Detected. rtag_id: $rtag_id, seq:$seqnum");
|
|
|
402 |
processChangedRelease($rtag_id, $seqnum);
|
|
|
403 |
}
|
|
|
404 |
|
|
|
405 |
# Update activeReleases so that changes will be detected
|
|
|
406 |
$activeReleases->{$rtag_id}{exists} = 1;
|
|
|
407 |
}
|
|
|
408 |
|
|
|
409 |
# Detect Releases that are no longer active
|
|
|
410 |
foreach my $rtag_id (keys %{$activeReleases}) {
|
|
|
411 |
unless ($activeReleases->{$rtag_id}{exists}) {
|
|
|
412 |
$logger->logmsg("Dead Release Detected. rtag_id: $rtag_id");
|
|
|
413 |
removeDeadRelease($rtag_id);
|
|
|
414 |
delete $activeReleases->{$rtag_id};
|
|
|
415 |
$lastTagListUpdate = 0;
|
|
|
416 |
}
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
disconnectRM(\$RM_DB);
|
|
|
420 |
}
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
#-------------------------------------------------------------------------------
|
|
|
424 |
# Function : examineS3Bucket
|
|
|
425 |
#
|
|
|
426 |
# Description : Scan the S3 bucket looking for Releases
|
|
|
427 |
# Used to pre-populate the process so that we:
|
|
|
428 |
# - Delete dead releases
|
|
|
429 |
# - Don't do excessive work on startup
|
|
|
430 |
#
|
|
|
431 |
# Inputs : Nothing
|
|
|
432 |
#
|
|
|
433 |
# Returns : Updates global structure ($activeReleases)
|
|
|
434 |
# Returns : 0 - Gross error ( Bucket access)
|
|
|
435 |
#
|
|
|
436 |
sub examineS3Bucket
|
|
|
437 |
{
|
| 7406 |
dpurdie |
438 |
my $bucket;
|
|
|
439 |
my $prefix;
|
|
|
440 |
|
| 7396 |
dpurdie |
441 |
#
|
|
|
442 |
# Remove data collected from s3
|
|
|
443 |
#
|
|
|
444 |
foreach my $rtag_id (keys %{$activeReleases}) {
|
|
|
445 |
delete $activeReleases->{$rtag_id}{s3} ;
|
|
|
446 |
}
|
|
|
447 |
|
| 7406 |
dpurdie |
448 |
if ($conf->{'S3Bucket'} =~ m~(.*?)/(.*)~) {
|
|
|
449 |
$bucket = $1;
|
|
|
450 |
$prefix = $2;
|
|
|
451 |
} else {
|
|
|
452 |
$bucket = $conf->{'S3Bucket'};
|
|
|
453 |
}
|
| 7396 |
dpurdie |
454 |
|
| 7406 |
dpurdie |
455 |
my $s3_cmd = "aws --profile $conf->{'S3Profile'} --output json";
|
|
|
456 |
$s3_cmd .= " --region $conf->{'S3Region'}" if (defined $conf->{'S3Region'});
|
|
|
457 |
$s3_cmd .= " s3api list-objects --bucket $bucket";
|
|
|
458 |
$s3_cmd .= " --prefix '$prefix'" if (defined $prefix);
|
|
|
459 |
|
| 7396 |
dpurdie |
460 |
$logger->verbose2("examineS3Bucket:s3_cmd:$s3_cmd");
|
|
|
461 |
|
|
|
462 |
my $ph;
|
|
|
463 |
my $jsontxt = "";
|
|
|
464 |
open ($ph, "$s3_cmd |");
|
|
|
465 |
while ( <$ph> ) {
|
|
|
466 |
chomp;
|
|
|
467 |
$logger->verbose3("examineS3Bucket:Data: $_");
|
|
|
468 |
$jsontxt .= $_;
|
|
|
469 |
}
|
|
|
470 |
close ($ph);
|
|
|
471 |
my $cmdRv = $?;
|
|
|
472 |
if ($cmdRv != 0) {
|
|
|
473 |
$logger->warn("Cannot read S3 Bucket Data");
|
|
|
474 |
return 0;
|
|
|
475 |
}
|
|
|
476 |
|
|
|
477 |
if ($jsontxt) {
|
|
|
478 |
my $json = from_json ($jsontxt);
|
|
|
479 |
#Utils::DebugDumpData("JSON",$json->{'Contents'});
|
|
|
480 |
foreach my $item ( @{$json->{'Contents'}})
|
|
|
481 |
{
|
| 7406 |
dpurdie |
482 |
if ($item->{Key} =~ m~(?:^|/)Release-(.*)\.zip$~ ) {
|
| 7396 |
dpurdie |
483 |
|
|
|
484 |
my $rtag_id = $1;
|
|
|
485 |
my $metaData = gets3ObjectMetaData($item->{Key});
|
|
|
486 |
|
|
|
487 |
#
|
|
|
488 |
# Update info in the global structure ($activeReleases)
|
|
|
489 |
# This data could be discarded - only needed for diagnostics
|
|
|
490 |
#
|
|
|
491 |
$activeReleases->{$rtag_id}{s3}{seqnum} = $metaData->{'releaseseq'};
|
|
|
492 |
$activeReleases->{$rtag_id}{s3}{md5} = $metaData->{'md5'};
|
|
|
493 |
$activeReleases->{$rtag_id}{s3}{depsig} = $metaData->{'depsig'};
|
|
|
494 |
|
|
|
495 |
#
|
|
|
496 |
# Recover information from S3
|
|
|
497 |
# Should only be done on the first call after restart
|
|
|
498 |
#
|
|
|
499 |
if (! exists $activeReleases->{$rtag_id}{md5} ) {
|
|
|
500 |
$activeReleases->{$rtag_id}{md5} = $activeReleases->{$rtag_id}{s3}{md5};
|
|
|
501 |
$activeReleases->{$rtag_id}{depsig} = $activeReleases->{$rtag_id}{s3}{depsig};
|
|
|
502 |
}
|
|
|
503 |
|
|
|
504 |
|
|
|
505 |
} else {
|
|
|
506 |
$logger->warn("Unknown item in bucket: $item->{Key}");
|
|
|
507 |
}
|
|
|
508 |
}
|
|
|
509 |
}
|
|
|
510 |
#Utils::DebugDumpData("activeReleases",$activeReleases);
|
|
|
511 |
return 1;
|
|
|
512 |
}
|
|
|
513 |
|
|
|
514 |
#-------------------------------------------------------------------------------
|
|
|
515 |
# Function : gets3ObjectMetaData
|
|
|
516 |
#
|
|
|
517 |
# Description : Get Metadata about one object
|
|
|
518 |
# Must do object by object :(
|
|
|
519 |
#
|
|
|
520 |
# Inputs : $key - Key
|
|
|
521 |
#
|
|
|
522 |
# Returns :
|
|
|
523 |
#
|
|
|
524 |
|
|
|
525 |
sub gets3ObjectMetaData
|
|
|
526 |
{
|
|
|
527 |
my ($key) = @_;
|
| 7406 |
dpurdie |
528 |
my $bucket;
|
|
|
529 |
my $prefix;
|
| 7396 |
dpurdie |
530 |
|
| 7406 |
dpurdie |
531 |
if ($conf->{'S3Bucket'} =~ m~(.*?)/(.*)~) {
|
|
|
532 |
$bucket = $1;
|
|
|
533 |
$prefix = $2;
|
|
|
534 |
} else {
|
|
|
535 |
$bucket = $conf->{'S3Bucket'};
|
|
|
536 |
$prefix = '';
|
|
|
537 |
}
|
| 7396 |
dpurdie |
538 |
|
| 7406 |
dpurdie |
539 |
my $s3_cmd = "aws --profile $conf->{'S3Profile'} --output json";
|
|
|
540 |
$s3_cmd .= " --region $conf->{'S3Region'}" if (defined $conf->{'S3Region'});
|
|
|
541 |
$s3_cmd .= " s3api head-object --bucket $bucket --key $key";
|
|
|
542 |
|
| 7396 |
dpurdie |
543 |
$logger->verbose2("gets3ObjectMetaData:s3_cmd:$s3_cmd");
|
|
|
544 |
|
|
|
545 |
my $ph;
|
|
|
546 |
my $jsontxt = "";
|
|
|
547 |
open ($ph, "$s3_cmd |");
|
|
|
548 |
while ( <$ph> ) {
|
|
|
549 |
chomp;
|
|
|
550 |
$logger->verbose3("gets3ObjectMetaData:Data: $_");
|
|
|
551 |
$jsontxt .= $_;
|
|
|
552 |
}
|
|
|
553 |
close ($ph);
|
|
|
554 |
|
|
|
555 |
my $json;
|
|
|
556 |
$json->{Metadata} = {};
|
|
|
557 |
|
|
|
558 |
if ($jsontxt) {
|
|
|
559 |
$json = from_json ($jsontxt);
|
|
|
560 |
#Utils::DebugDumpData("JSON",$json);
|
|
|
561 |
}
|
|
|
562 |
return $json->{Metadata};
|
|
|
563 |
}
|
|
|
564 |
|
|
|
565 |
|
|
|
566 |
#-------------------------------------------------------------------------------
|
|
|
567 |
# Function : removeDeadRelease
|
|
|
568 |
#
|
|
|
569 |
# Description : Remove a Dead Release from the S3 bucket
|
|
|
570 |
#
|
|
|
571 |
# Inputs : $rtag_id - Release identifier
|
|
|
572 |
#
|
|
|
573 |
# Returns : 0 - Nothing deleted
|
|
|
574 |
# 1 - Something deleted
|
|
|
575 |
#
|
|
|
576 |
sub removeDeadRelease {
|
|
|
577 |
my ($rtag_id) = @_;
|
|
|
578 |
my $cmdRv;
|
|
|
579 |
my $rv = 0;
|
|
|
580 |
|
|
|
581 |
# Create the process pipe to delete the package
|
|
|
582 |
|
|
|
583 |
my $targetPath = generateBucketZipName($rtag_id);
|
| 7406 |
dpurdie |
584 |
|
|
|
585 |
my $s3_cmd = "aws --profile $conf->{'S3Profile'} --output json";
|
|
|
586 |
$s3_cmd .= " --region $conf->{'S3Region'}" if (defined $conf->{'S3Region'});
|
|
|
587 |
$s3_cmd .= " s3 rm s3://$targetPath";
|
|
|
588 |
|
| 7396 |
dpurdie |
589 |
$logger->logmsg("removeDeadRelease:$targetPath");
|
|
|
590 |
$logger->verbose2("removeDeadRelease:s3_cmd:$s3_cmd");
|
|
|
591 |
|
|
|
592 |
my $ph;
|
|
|
593 |
open ($ph, "$s3_cmd |");
|
|
|
594 |
while ( <$ph> ) {
|
|
|
595 |
chomp;
|
|
|
596 |
$logger->verbose2("removeDeadRelease:Data: $_");
|
|
|
597 |
}
|
|
|
598 |
close ($ph);
|
|
|
599 |
$cmdRv = $?;
|
|
|
600 |
|
|
|
601 |
#
|
|
|
602 |
# Common code
|
|
|
603 |
#
|
|
|
604 |
$logger->verbose("removeDeadRelease:End: $cmdRv");
|
|
|
605 |
if ( $cmdRv == 0 ) {
|
|
|
606 |
$rv = 1;
|
|
|
607 |
$statistics{delCount}++;
|
|
|
608 |
|
|
|
609 |
} else {
|
|
|
610 |
$logger->warn("removeDeadRelease:Error: $rtag_id, $?");
|
|
|
611 |
}
|
|
|
612 |
return $rv;
|
|
|
613 |
}
|
|
|
614 |
|
|
|
615 |
#-------------------------------------------------------------------------------
|
|
|
616 |
# Function : processChangedRelease
|
|
|
617 |
#
|
|
|
618 |
# Description : Create/Update a release to the S3 bucket
|
|
|
619 |
#
|
|
|
620 |
# Various attempts are made to reduce the work that needs to be done
|
|
|
621 |
# There are three checks to skip a transfer
|
|
|
622 |
# 1) The Release sequence number - must be diff for processing to occur
|
|
|
623 |
# 2) Packages inserted into the image
|
|
|
624 |
# Dependent package versions are used to generate a MD5
|
|
|
625 |
# If this does not change then there is no need to do work
|
|
|
626 |
# 3) An MD5 over the zip Image
|
|
|
627 |
# If this is the same as the one in S3, then don't upload
|
|
|
628 |
#
|
|
|
629 |
# These three pices of information are held as metadata along with the
|
|
|
630 |
# package. These are read at start up.
|
|
|
631 |
#
|
|
|
632 |
#
|
|
|
633 |
# Inputs : $rtag_id - Release identifier
|
|
|
634 |
# $seqnum - Release sequence number
|
|
|
635 |
# Added as metadata to objects
|
|
|
636 |
#
|
|
|
637 |
# Returns : Nothing
|
|
|
638 |
#
|
|
|
639 |
sub processChangedRelease {
|
|
|
640 |
my ($rtag_id, $seqnum) = @_;
|
|
|
641 |
|
|
|
642 |
#
|
|
|
643 |
# Cleanout previous zip files
|
|
|
644 |
#
|
|
|
645 |
my @files = glob($conf->{'workdir'} . '/*.zip');
|
|
|
646 |
$logger->verbose("Delete old zips: @files");
|
|
|
647 |
unlink @files;
|
|
|
648 |
|
|
|
649 |
#
|
|
|
650 |
# Create an image of the data to be transferred
|
|
|
651 |
# Based on packages that support S3Sync
|
|
|
652 |
#
|
|
|
653 |
my $m_sqlstr = "SELECT p.pkg_name, pv.pkg_version, pv.pv_id " .
|
|
|
654 |
" FROM RELEASE_MANAGER.release_content rc, RELEASE_MANAGER.packages p, RELEASE_MANAGER.package_versions pv " .
|
|
|
655 |
" WHERE rc.rtag_id = " . $rtag_id .
|
|
|
656 |
" AND rc.s3sync = 'Y' " .
|
|
|
657 |
" AND rc.pv_id = pv.pv_id " .
|
|
|
658 |
" AND pv.pkg_id = p.pkg_id " .
|
|
|
659 |
" ORDER by pv.pv_id";
|
|
|
660 |
|
|
|
661 |
my $curData = getDataFromRm ('s3Pkgs', $m_sqlstr, {data => 0, dump => 0} );
|
|
|
662 |
#Utils::DebugDumpData("activeReleases",$activeReleases);
|
|
|
663 |
|
|
|
664 |
#
|
|
|
665 |
# Generate a md5 of the PVIDs of the packages that will go into the image
|
|
|
666 |
# Used to detect true changes - only of the packages we are interested in
|
|
|
667 |
#
|
|
|
668 |
my $signature = Digest::MD5->new;
|
|
|
669 |
foreach my $entry (@{$curData}) {
|
|
|
670 |
$signature->add( $entry->[2] )
|
|
|
671 |
}
|
|
|
672 |
my $depsig = $signature->hexdigest();
|
|
|
673 |
my $reason = "";
|
|
|
674 |
if ( !exists $activeReleases->{$rtag_id}{s3} ) {
|
|
|
675 |
$reason = 'NoS3Data';
|
|
|
676 |
|
|
|
677 |
} elsif (! exists $activeReleases->{$rtag_id}{depsig}) {
|
|
|
678 |
$reason = 'NoSavedData';
|
|
|
679 |
|
|
|
680 |
} elsif ($activeReleases->{$rtag_id}{depsig} ne $depsig ) {
|
|
|
681 |
$reason = "Mismatch: $activeReleases->{$rtag_id}{depsig} ne $depsig";
|
|
|
682 |
|
|
|
683 |
} else {
|
|
|
684 |
$logger->verbose("Dependencies unchanged - upload skipped");
|
|
|
685 |
$activeReleases->{$rtag_id}{seqnum} = $seqnum;
|
|
|
686 |
return;
|
|
|
687 |
}
|
|
|
688 |
$logger->verbose("Dependency Test: $reason");
|
|
|
689 |
|
|
|
690 |
#
|
| 7397 |
dpurdie |
691 |
# Create a monifest to go into the zip
|
| 7396 |
dpurdie |
692 |
#
|
| 7397 |
dpurdie |
693 |
my $manifest;
|
|
|
694 |
$manifest->{Packages} = [];
|
|
|
695 |
$manifest->{rtag_id} = $rtag_id;
|
|
|
696 |
|
|
|
697 |
#
|
|
|
698 |
# Generate the zip of the objects to be pushed to S3
|
|
|
699 |
# Add directories
|
|
|
700 |
# Update the manifest entries
|
|
|
701 |
#
|
| 7396 |
dpurdie |
702 |
my $startTime = time;
|
|
|
703 |
my $zip = Archive::Zip->new();
|
|
|
704 |
foreach my $entry (@{$curData}) {
|
|
|
705 |
my $src = getPackageBase($entry->[0], $entry->[1]);
|
|
|
706 |
if (defined $src) {
|
|
|
707 |
$logger->verbose("Zip addTree Src: $src");
|
|
|
708 |
|
| 7397 |
dpurdie |
709 |
my %data;
|
|
|
710 |
$data{name} = $entry->[0];
|
|
|
711 |
$data{version} = $entry->[1];
|
|
|
712 |
$data{pvid} = $entry->[2];
|
|
|
713 |
push @{$manifest->{Packages}}, \%data;
|
|
|
714 |
|
| 7396 |
dpurdie |
715 |
if ( $zip->addTree( $src, '' ) != AZ_OK ) {
|
|
|
716 |
$logger->warn("Zip addTree Error: $rtag_id");
|
|
|
717 |
return;
|
|
|
718 |
}
|
|
|
719 |
}
|
|
|
720 |
}
|
|
|
721 |
|
| 7397 |
dpurdie |
722 |
# Add the manifest into the zip
|
|
|
723 |
my $jsonText = to_json( $manifest, { ascii => 1, pretty => 1 });
|
|
|
724 |
$zip->addString( $jsonText, 'ReleaseManifest.json' );
|
|
|
725 |
$logger->verbose("ManfestJson: $jsonText");
|
|
|
726 |
|
|
|
727 |
# Generate the zip file
|
| 7396 |
dpurdie |
728 |
my $zipFile = catdir( $conf->{'workdir'} , 'Images-' . $rtag_id . '.zip');
|
|
|
729 |
if ( $zip->writeToFileNamed($zipFile) != AZ_OK ) {
|
|
|
730 |
$logger->warn("Zip write Error: $rtag_id");
|
|
|
731 |
return;
|
|
|
732 |
}
|
|
|
733 |
$logger->verbose("Zip created: $zipFile");
|
|
|
734 |
|
|
|
735 |
#
|
|
|
736 |
# Display the size of the package (zipped)
|
|
|
737 |
# Diagnostic use
|
|
|
738 |
#
|
|
|
739 |
if ($conf->{txdetail}) {
|
|
|
740 |
my $tzfsize = -s $zipFile;
|
|
|
741 |
my $size = sprintf "%.3f", $tzfsize / 1024 / 1024 / 1024 ;
|
|
|
742 |
my $duration = time - $startTime;
|
|
|
743 |
$logger->logmsg("zipImage: Stats: $rtag_id, $size Gb, $duration Secs");
|
|
|
744 |
}
|
|
|
745 |
|
|
|
746 |
#
|
|
|
747 |
# Have a ZIP file of the desired contents
|
|
|
748 |
# Could try to detect if it differs from the one already in the bucket
|
|
|
749 |
# Don't want to trigger CI/CD pipeline operations unless we need to
|
| 7410 |
dpurdie |
750 |
#
|
|
|
751 |
my $digest;
|
|
|
752 |
my $md5Digestor = Digest::MD5->new;
|
|
|
753 |
if (open( my $zipFileHandle, '<', $zipFile )) {
|
|
|
754 |
binmode ($zipFileHandle);
|
|
|
755 |
$md5Digestor->addfile($zipFileHandle);
|
|
|
756 |
$digest = $md5Digestor->hexdigest();
|
|
|
757 |
} else {
|
|
|
758 |
$logger->warn("Cannot open ZIP: $rtag_id, $!");
|
|
|
759 |
return;
|
|
|
760 |
}
|
|
|
761 |
$logger->verbose("Zip MD5: $digest");
|
| 7396 |
dpurdie |
762 |
$reason = "";
|
|
|
763 |
if ( !exists $activeReleases->{$rtag_id}{s3} ) {
|
|
|
764 |
$reason = 'NoS3Data';
|
|
|
765 |
|
|
|
766 |
} elsif (! exists $activeReleases->{$rtag_id}{md5}) {
|
|
|
767 |
$reason = 'NoSavedMd5';
|
|
|
768 |
|
|
|
769 |
} elsif ($activeReleases->{$rtag_id}{md5} ne $digest ) {
|
|
|
770 |
$reason = "Mismatch: $activeReleases->{$rtag_id}{md5} ne $digest";
|
|
|
771 |
|
|
|
772 |
} else {
|
|
|
773 |
$logger->verbose("Zip file has same md5 hash - upload skipped");
|
|
|
774 |
#
|
|
|
775 |
# Update the known signature
|
|
|
776 |
$activeReleases->{$rtag_id}{depsig} = $depsig;
|
|
|
777 |
$activeReleases->{$rtag_id}{seqnum} = $seqnum;
|
|
|
778 |
return;
|
|
|
779 |
}
|
|
|
780 |
$logger->verbose("ZipMd5 Test: $reason");
|
|
|
781 |
|
|
|
782 |
# Create a command to transfer the file to AWS use the cli tools
|
|
|
783 |
# Note: Ive seen problem with this when used from Perth to AWS (Sydney)
|
|
|
784 |
# If this is an issue use curl - see the savePkgToS3.sh for an implementation
|
|
|
785 |
#
|
|
|
786 |
$startTime = time;
|
|
|
787 |
my $targetPath = generateBucketZipName($rtag_id);
|
| 7406 |
dpurdie |
788 |
|
|
|
789 |
my $s3_cmd = "aws --profile $conf->{'S3Profile'} --output json";
|
|
|
790 |
$s3_cmd .= " --region $conf->{'S3Region'}" if (defined $conf->{'S3Region'});
|
|
|
791 |
$s3_cmd .= " s3 cp $zipFile s3://$targetPath --metadata releaseseq=$seqnum,md5=$digest,depsig=$depsig";
|
|
|
792 |
|
| 7396 |
dpurdie |
793 |
$logger->logmsg("transferPackage:$targetPath");
|
|
|
794 |
$logger->verbose2("transferPackage:s3_cmd:$s3_cmd");
|
|
|
795 |
|
|
|
796 |
my $cmdRv;
|
|
|
797 |
unless ($conf->{'noTransfers'}) {
|
|
|
798 |
my $ph;
|
|
|
799 |
open ($ph, "$s3_cmd |");
|
|
|
800 |
while ( <$ph> )
|
|
|
801 |
{
|
|
|
802 |
chomp;
|
|
|
803 |
$logger->verbose2("transferPackage:Data: $_");
|
|
|
804 |
}
|
|
|
805 |
close ($ph);
|
|
|
806 |
$cmdRv = $?;
|
|
|
807 |
$logger->verbose("transferPackage:End: $cmdRv");
|
|
|
808 |
}
|
|
|
809 |
#
|
|
|
810 |
# Display the size of the package (zipped)
|
|
|
811 |
# Diagnostic use
|
|
|
812 |
#
|
|
|
813 |
if ($conf->{txdetail}) {
|
|
|
814 |
my $tzfsize = -s $zipFile;
|
|
|
815 |
my $size = sprintf "%.3f", $tzfsize / 1024 / 1024 / 1024 ;
|
|
|
816 |
my $duration = time - $startTime;
|
|
|
817 |
$logger->logmsg("S3 Copy: Stats: $rtag_id, $size Gb, $duration Secs");
|
|
|
818 |
}
|
|
|
819 |
|
|
|
820 |
if ($cmdRv == 0) {
|
|
|
821 |
$statistics{txCount}++;
|
| 7397 |
dpurdie |
822 |
$statistics{txBytes} += -s $zipFile;
|
| 7396 |
dpurdie |
823 |
|
|
|
824 |
#
|
|
|
825 |
# Mark the current entry as having been processed
|
|
|
826 |
#
|
|
|
827 |
$activeReleases->{$rtag_id}{depsig} = $depsig;
|
|
|
828 |
$activeReleases->{$rtag_id}{md5} = $digest;
|
|
|
829 |
$activeReleases->{$rtag_id}{seqnum} = $seqnum;
|
|
|
830 |
$activeReleases->{$rtag_id}{s3}{sent} = 1;
|
|
|
831 |
}
|
|
|
832 |
}
|
|
|
833 |
|
|
|
834 |
#-------------------------------------------------------------------------------
|
|
|
835 |
# Function : getPackageBase
|
|
|
836 |
#
|
|
|
837 |
# Description : Calculate the base of a package in dpkg_archive
|
|
|
838 |
# With errors and wanings
|
|
|
839 |
#
|
|
|
840 |
# Inputs : $pname - Package name
|
|
|
841 |
# $pver - Package version
|
|
|
842 |
#
|
|
|
843 |
#
|
|
|
844 |
# Returns : undef - bad
|
|
|
845 |
# Path to the S3TRANSFER section within the archive
|
|
|
846 |
sub getPackageBase {
|
|
|
847 |
my ($pname, $pver) = @_;
|
|
|
848 |
|
|
|
849 |
#
|
|
|
850 |
# Locate package
|
|
|
851 |
#
|
|
|
852 |
unless ( -d $conf->{'dpkg_archive'}) {
|
|
|
853 |
$logger->warn("addPartsToImage: dpkg_archive not found");
|
|
|
854 |
return undef;
|
|
|
855 |
}
|
|
|
856 |
|
|
|
857 |
my $src = catdir($conf->{'dpkg_archive'}, $pname, $pver);
|
|
|
858 |
unless ( -d $src ) {
|
|
|
859 |
$logger->warn("addPartsToImage: Package not found: $pname, $pver");
|
|
|
860 |
return undef;
|
|
|
861 |
}
|
|
|
862 |
|
| 7397 |
dpurdie |
863 |
$src = catdir( $src, $conf->{'transferDir'});
|
| 7396 |
dpurdie |
864 |
unless ( -d $src ) {
|
| 7397 |
dpurdie |
865 |
$logger->verbose("addPartsToImage: Package has no $conf->{'transferDir'}: $pname, $pver");
|
| 7396 |
dpurdie |
866 |
return undef;
|
|
|
867 |
}
|
|
|
868 |
return $src;
|
|
|
869 |
}
|
|
|
870 |
|
|
|
871 |
#-------------------------------------------------------------------------------
|
|
|
872 |
# Function : generateBucketZipName
|
|
|
873 |
#
|
|
|
874 |
# Description : Generate the name of the zipfile created within the bucket
|
|
|
875 |
#
|
|
|
876 |
# Inputs : $rtag_id
|
|
|
877 |
#
|
|
|
878 |
# Returns : Full name - including bucket name
|
|
|
879 |
#
|
|
|
880 |
sub generateBucketZipName
|
|
|
881 |
{
|
|
|
882 |
my ($rtag_id) = @_;
|
|
|
883 |
my $targetName = 'Release-' . $rtag_id . '.zip';
|
|
|
884 |
my $targetPath = catdir ($conf->{'S3Bucket'}, $targetName );
|
|
|
885 |
return $targetPath;
|
|
|
886 |
}
|
|
|
887 |
|
|
|
888 |
|
|
|
889 |
#-------------------------------------------------------------------------------
|
|
|
890 |
# Function : getDataFromRm
|
|
|
891 |
#
|
|
|
892 |
# Description : Get an array of data from RM
|
|
|
893 |
# Normally an array of arrays
|
|
|
894 |
#
|
|
|
895 |
# Inputs : $name - Query Name
|
|
|
896 |
# $m_sqlstr - Query
|
|
|
897 |
# $options - Ref to a hash of options
|
|
|
898 |
# sql - show sql
|
|
|
899 |
# data - show data
|
|
|
900 |
# dump - show results
|
|
|
901 |
# oneRow - Only fetch one row
|
|
|
902 |
# error - Must find data
|
|
|
903 |
#
|
|
|
904 |
# Returns : ref to array of data
|
|
|
905 |
#
|
|
|
906 |
sub getDataFromRm
|
|
|
907 |
{
|
|
|
908 |
my ($name,$m_sqlstr, $options ) = @_;
|
|
|
909 |
my @row;
|
|
|
910 |
my $data;
|
|
|
911 |
|
|
|
912 |
if (ref $options ne 'HASH') {
|
|
|
913 |
$options = {};
|
|
|
914 |
}
|
|
|
915 |
|
|
|
916 |
if ($options->{sql}) {
|
|
|
917 |
$logger->logmsg("$name: $m_sqlstr")
|
|
|
918 |
}
|
|
|
919 |
my $sth = $RM_DB->prepare($m_sqlstr);
|
|
|
920 |
if ( defined($sth) )
|
|
|
921 |
{
|
|
|
922 |
if ( $sth->execute( ) ) {
|
|
|
923 |
if ( $sth->rows ) {
|
|
|
924 |
while ( @row = $sth->fetchrow_array ) {
|
|
|
925 |
if ($options->{data}) {
|
|
|
926 |
$logger->warn ("$name: @row");
|
|
|
927 |
}
|
|
|
928 |
#Debug0("$name: @row");
|
|
|
929 |
push @{$data}, [@row];
|
|
|
930 |
|
|
|
931 |
last if $options->{oneRow};
|
|
|
932 |
}
|
|
|
933 |
}
|
|
|
934 |
$sth->finish();
|
|
|
935 |
} else {
|
|
|
936 |
$logger->warn("Execute failure:$name: $m_sqlstr", $sth->errstr() );
|
|
|
937 |
}
|
|
|
938 |
} else {
|
|
|
939 |
$logger->warn("Prepare failure:$name" );
|
|
|
940 |
}
|
|
|
941 |
|
|
|
942 |
if (!$data && $options->{error}) {
|
|
|
943 |
$logger->warn( $options->{error} );
|
|
|
944 |
}
|
|
|
945 |
|
|
|
946 |
if ($data && $options->{oneRow}) {
|
|
|
947 |
$data = $data->[0];
|
|
|
948 |
}
|
|
|
949 |
|
|
|
950 |
if ($options->{dump}) {
|
|
|
951 |
Utils::DebugDumpData("$name", $data);
|
|
|
952 |
}
|
|
|
953 |
return $data;
|
|
|
954 |
}
|
|
|
955 |
|
|
|
956 |
#-------------------------------------------------------------------------------
|
|
|
957 |
# Function : maintainTagList
|
|
|
958 |
#
|
|
|
959 |
# Description : Maintain a data structure for the maintenance of the
|
|
|
960 |
# tags directory
|
|
|
961 |
#
|
|
|
962 |
# Inputs : None
|
|
|
963 |
#
|
|
|
964 |
# Returns : Nothing
|
|
|
965 |
#
|
|
|
966 |
sub maintainTagList
|
|
|
967 |
{
|
|
|
968 |
#
|
|
|
969 |
# Time to perform the scan
|
|
|
970 |
# Will do at startup and every time period there after
|
|
|
971 |
#
|
|
|
972 |
return unless ( $now > ($lastTagListUpdate + $conf->{tagListUpdate} ));
|
|
|
973 |
$logger->verbose("maintainTagList");
|
|
|
974 |
$lastTagListUpdate = $now;
|
|
|
975 |
|
|
|
976 |
#
|
|
|
977 |
# Generate new configuration
|
|
|
978 |
#
|
|
|
979 |
my %config;
|
|
|
980 |
$config{s3Sync} = 1; # Indicate that it may be special
|
|
|
981 |
|
|
|
982 |
%{$config{releases}} = map { $_ => 1 } keys %{$activeReleases};
|
|
|
983 |
|
|
|
984 |
#
|
|
|
985 |
# Save data
|
|
|
986 |
#
|
|
|
987 |
my $dump = Data::Dumper->new([\%config], [qw(*config)]);
|
|
|
988 |
#print $dump->Dump;
|
|
|
989 |
#$dump->Reset;
|
|
|
990 |
|
|
|
991 |
#
|
|
|
992 |
# Save config data
|
|
|
993 |
#
|
|
|
994 |
my $conf_file = catfile( $conf->{'tagdir'},'.config' );
|
|
|
995 |
$logger->verbose3("maintainTagList: Writting $conf_file");
|
|
|
996 |
|
|
|
997 |
my $fh;
|
|
|
998 |
open ( $fh, '>', $conf_file ) or $logger->err("Can't create $conf_file: $!");
|
|
|
999 |
print $fh $dump->Dump;
|
|
|
1000 |
close $fh;
|
|
|
1001 |
}
|
|
|
1002 |
|
|
|
1003 |
#-------------------------------------------------------------------------------
|
|
|
1004 |
# Function : resetDailyStatistics
|
|
|
1005 |
#
|
|
|
1006 |
# Description : Called periodically to reset the daily statistics
|
|
|
1007 |
#
|
|
|
1008 |
# Inputs : $time - Current time
|
|
|
1009 |
#
|
|
|
1010 |
# Returns :
|
|
|
1011 |
#
|
|
|
1012 |
sub resetDailyStatistics
|
|
|
1013 |
{
|
|
|
1014 |
my ($time) = @_;
|
|
|
1015 |
|
|
|
1016 |
#
|
|
|
1017 |
# Detect a new day
|
|
|
1018 |
#
|
|
|
1019 |
my $today = (localtime($time))[7];
|
|
|
1020 |
if ($yday != $today)
|
|
|
1021 |
{
|
|
|
1022 |
$yday = $today;
|
|
|
1023 |
$logger->logmsg('Resetting daily statistics' );
|
|
|
1024 |
|
|
|
1025 |
# Note: Must match @recoverTags in readStatistics
|
|
|
1026 |
$statistics{dayStart} = $time;
|
|
|
1027 |
$statistics{txCount} = 0;
|
| 7397 |
dpurdie |
1028 |
$statistics{txBytes} = 0;
|
| 7396 |
dpurdie |
1029 |
$statistics{delCount} = 0;
|
|
|
1030 |
$statistics{linkErrors} = 0;
|
|
|
1031 |
}
|
|
|
1032 |
}
|
|
|
1033 |
|
|
|
1034 |
#-------------------------------------------------------------------------------
|
|
|
1035 |
# Function : readStatistics
|
|
|
1036 |
#
|
|
|
1037 |
# Description : Read in the last set of stats
|
|
|
1038 |
# Used after a restart to recover daily statistics
|
|
|
1039 |
#
|
|
|
1040 |
# Inputs :
|
|
|
1041 |
#
|
|
|
1042 |
# Returns :
|
|
|
1043 |
#
|
|
|
1044 |
sub readStatistics
|
|
|
1045 |
{
|
| 7397 |
dpurdie |
1046 |
my @recoverTags = qw(dayStart txCount txBytes delCount linkErrors);
|
| 7396 |
dpurdie |
1047 |
|
|
|
1048 |
if ($conf->{'statsfile'} and -f $conf->{'statsfile'})
|
|
|
1049 |
{
|
|
|
1050 |
if (open my $fh, $conf->{'statsfile'})
|
|
|
1051 |
{
|
|
|
1052 |
while (<$fh>)
|
|
|
1053 |
{
|
|
|
1054 |
m~(.*):(.*)~;
|
|
|
1055 |
if ( grep( /^$1$/, @recoverTags ) )
|
|
|
1056 |
{
|
|
|
1057 |
$statistics{$1} = $2;
|
|
|
1058 |
$logger->verbose("readStatistics $1, $2");
|
|
|
1059 |
}
|
|
|
1060 |
}
|
|
|
1061 |
close $fh;
|
|
|
1062 |
$yday = (localtime($statistics{dayStart}))[7];
|
|
|
1063 |
}
|
|
|
1064 |
}
|
|
|
1065 |
}
|
|
|
1066 |
|
|
|
1067 |
|
|
|
1068 |
#-------------------------------------------------------------------------------
|
|
|
1069 |
# Function : periodicStatistics
|
|
|
1070 |
#
|
|
|
1071 |
# Description : Called on a regular basis to write out statistics
|
|
|
1072 |
# Used to feed information into Nagios
|
|
|
1073 |
#
|
|
|
1074 |
# This function is called via an alarm and may be outside the normal
|
|
|
1075 |
# processing loop. Don't make assumptions on the value of $now
|
|
|
1076 |
#
|
|
|
1077 |
# Inputs :
|
|
|
1078 |
#
|
|
|
1079 |
# Returns :
|
|
|
1080 |
#
|
|
|
1081 |
sub periodicStatistics
|
|
|
1082 |
{
|
|
|
1083 |
#
|
|
|
1084 |
# A few local stats
|
|
|
1085 |
#
|
|
|
1086 |
$statistics{SeqNum}++;
|
|
|
1087 |
$statistics{timeStamp} = time();
|
|
|
1088 |
$statistics{upTime} = $statistics{timeStamp} - $startTime;
|
| 7397 |
dpurdie |
1089 |
$statistics{wedged} = $wedgedCount++ > 30 ? 1 : 0;
|
| 7396 |
dpurdie |
1090 |
|
| 7406 |
dpurdie |
1091 |
if ( $statistics{wedged}) {
|
|
|
1092 |
$statistics{state} = 'Wedged';
|
|
|
1093 |
} elsif(!$linkUp){
|
|
|
1094 |
$statistics{state} = 'S3 Bucket Read Error';
|
|
|
1095 |
} else {
|
|
|
1096 |
$statistics{state} = 'OK';
|
|
|
1097 |
}
|
|
|
1098 |
|
| 7396 |
dpurdie |
1099 |
# Reset daily accumulations - on first use each day
|
|
|
1100 |
resetDailyStatistics($statistics{timeStamp});
|
|
|
1101 |
|
|
|
1102 |
#
|
|
|
1103 |
# Write statistics to a file
|
|
|
1104 |
# Write to a tmp file, then rename.
|
|
|
1105 |
# Attempt to make the operation atomic - so that the file consumer
|
|
|
1106 |
# doesn't get a badly formed file.
|
|
|
1107 |
#
|
|
|
1108 |
if ($conf->{'statsfiletmp'})
|
|
|
1109 |
{
|
|
|
1110 |
my $fh;
|
|
|
1111 |
unless (open ($fh, '>', $conf->{'statsfiletmp'}))
|
|
|
1112 |
{
|
|
|
1113 |
$fh = undef;
|
|
|
1114 |
$logger->warn("Cannot create temp stats file: $!");
|
|
|
1115 |
}
|
|
|
1116 |
else
|
|
|
1117 |
{
|
|
|
1118 |
foreach my $key ( sort { lc($a) cmp lc($b) } keys %statistics)
|
|
|
1119 |
{
|
|
|
1120 |
print $fh $key . ':' . $statistics{$key} . "\n";
|
|
|
1121 |
$logger->verbose2('Statistics:'. $key . ':' . $statistics{$key});
|
|
|
1122 |
}
|
|
|
1123 |
close $fh;
|
|
|
1124 |
|
|
|
1125 |
# Rename temp to real file
|
|
|
1126 |
rename $conf->{'statsfiletmp'}, $conf->{'statsfile'} ;
|
|
|
1127 |
}
|
|
|
1128 |
}
|
|
|
1129 |
}
|
|
|
1130 |
|
|
|
1131 |
#-------------------------------------------------------------------------------
|
|
|
1132 |
# Function : sighandlers
|
|
|
1133 |
#
|
|
|
1134 |
# Description : Install signal handlers
|
|
|
1135 |
#
|
|
|
1136 |
# Inputs : $conf - System config
|
|
|
1137 |
#
|
|
|
1138 |
# Returns : Nothing
|
|
|
1139 |
#
|
|
|
1140 |
sub sighandlers
|
|
|
1141 |
{
|
|
|
1142 |
my $conf = shift;
|
|
|
1143 |
my $logger = $conf->{logger};
|
|
|
1144 |
|
|
|
1145 |
$SIG{TERM} = sub {
|
|
|
1146 |
# On shutdown
|
|
|
1147 |
$logger->logmsg('Received SIGTERM. Shutting down....' );
|
|
|
1148 |
unlink $conf->{'pidfile'} if (-f $conf->{'pidfile'});
|
|
|
1149 |
exit 0;
|
|
|
1150 |
};
|
|
|
1151 |
|
|
|
1152 |
$SIG{HUP} = sub {
|
|
|
1153 |
# On logrotate
|
|
|
1154 |
$logger->logmsg('Received SIGHUP.');
|
|
|
1155 |
$logger->rotatelog();
|
|
|
1156 |
};
|
|
|
1157 |
|
|
|
1158 |
$SIG{USR1} = sub {
|
|
|
1159 |
# On Force Rescans
|
|
|
1160 |
$logger->logmsg('Received SIGUSR1.');
|
|
|
1161 |
$lastTagListUpdate = 0;
|
|
|
1162 |
$lastS3Refresh = 0;
|
|
|
1163 |
};
|
|
|
1164 |
|
|
|
1165 |
alarm 60;
|
|
|
1166 |
$SIG{ALRM} = sub {
|
|
|
1167 |
# On Dump Statistics
|
|
|
1168 |
$logger->verbose2('Received SIGUSR2.');
|
|
|
1169 |
periodicStatistics();
|
|
|
1170 |
alarm 60;
|
|
|
1171 |
};
|
|
|
1172 |
|
|
|
1173 |
$SIG{__WARN__} = sub { $logger->warn("@_") };
|
|
|
1174 |
$SIG{__DIE__} = sub { $logger->err("@_") };
|
|
|
1175 |
}
|
|
|
1176 |
|
|
|
1177 |
|
|
|
1178 |
#-------------------------------------------------------------------------------
|
|
|
1179 |
# Function : Error, Verbose, Warning
|
|
|
1180 |
#
|
|
|
1181 |
# Description : Support for JatsRmApi
|
|
|
1182 |
#
|
|
|
1183 |
# Inputs : Message
|
|
|
1184 |
#
|
|
|
1185 |
# Returns : Nothing
|
|
|
1186 |
#
|
|
|
1187 |
sub Error
|
|
|
1188 |
{
|
|
|
1189 |
$logger->err("@_");
|
|
|
1190 |
}
|
|
|
1191 |
|
|
|
1192 |
sub Verbose
|
|
|
1193 |
{
|
|
|
1194 |
$logger->verbose2("@_");
|
|
|
1195 |
}
|
|
|
1196 |
|
|
|
1197 |
sub Warning
|
|
|
1198 |
{
|
|
|
1199 |
$logger->warn("@_");
|
|
|
1200 |
}
|
|
|
1201 |
|
|
|
1202 |
|