Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

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