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
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
750
    #       
751
    my $digest = md5_hex($zipFile);
752
    $reason = "";
753
    if ( !exists $activeReleases->{$rtag_id}{s3} ) {
754
        $reason = 'NoS3Data';
755
 
756
    } elsif (! exists $activeReleases->{$rtag_id}{md5}) {
757
        $reason = 'NoSavedMd5';
758
 
759
    } elsif ($activeReleases->{$rtag_id}{md5} ne $digest ) {
760
        $reason = "Mismatch: $activeReleases->{$rtag_id}{md5} ne $digest";
761
 
762
    } else {
763
        $logger->verbose("Zip file has same md5 hash - upload skipped");
764
        #
765
        #   Update the known signature
766
        $activeReleases->{$rtag_id}{depsig} = $depsig;
767
        $activeReleases->{$rtag_id}{seqnum} = $seqnum;
768
        $activeReleases->{$rtag_id}{md5} = $digest;
769
        return;
770
    }
771
    $logger->verbose("ZipMd5 Test: $reason");
772
 
773
    #   Create a command to transfer the file to AWS use the cli tools
774
    #   Note: Ive seen problem with this when used from Perth to AWS (Sydney)
775
    #         If this is an issue use curl - see the savePkgToS3.sh for an implementation
776
    #
777
    $startTime = time;
778
    my $targetPath = generateBucketZipName($rtag_id);
7406 dpurdie 779
 
780
    my $s3_cmd = "aws --profile $conf->{'S3Profile'} --output json";
781
    $s3_cmd .= " --region $conf->{'S3Region'}" if (defined $conf->{'S3Region'});
782
    $s3_cmd .= " s3 cp $zipFile s3://$targetPath --metadata releaseseq=$seqnum,md5=$digest,depsig=$depsig";
783
 
7396 dpurdie 784
    $logger->logmsg("transferPackage:$targetPath");
785
    $logger->verbose2("transferPackage:s3_cmd:$s3_cmd");
786
 
787
    my $cmdRv;
788
    unless ($conf->{'noTransfers'}) {
789
        my $ph;
790
        open ($ph, "$s3_cmd |");
791
        while ( <$ph> )
792
        {
793
            chomp;
794
            $logger->verbose2("transferPackage:Data: $_");
795
        }
796
        close ($ph);
797
        $cmdRv = $?;
798
        $logger->verbose("transferPackage:End: $cmdRv");
799
    }
800
    #
801
    #   Display the size of the package (zipped)
802
    #       Diagnostic use
803
    #
804
    if ($conf->{txdetail}) {
805
        my $tzfsize = -s $zipFile;
806
        my $size = sprintf "%.3f", $tzfsize / 1024 / 1024 / 1024 ;
807
        my $duration = time - $startTime;
808
        $logger->logmsg("S3 Copy: Stats: $rtag_id, $size Gb, $duration Secs");
809
    }
810
 
811
    if ($cmdRv == 0) {
812
        $statistics{txCount}++;
7397 dpurdie 813
        $statistics{txBytes} += -s $zipFile; 
7396 dpurdie 814
 
815
        #
816
        #   Mark the current entry as having been processed
817
        #
818
        $activeReleases->{$rtag_id}{depsig} = $depsig;
819
        $activeReleases->{$rtag_id}{md5} = $digest;
820
        $activeReleases->{$rtag_id}{seqnum} = $seqnum;
821
        $activeReleases->{$rtag_id}{s3}{sent} = 1;
822
    }
823
}
824
 
825
#-------------------------------------------------------------------------------
826
# Function        : getPackageBase 
827
#
828
# Description     : Calculate the base of a package in dpkg_archive
829
#                   With errors and wanings
830
#
831
# Inputs          : $pname      - Package name
832
#                   $pver       - Package version
833
#
834
#
835
# Returns         : undef - bad
836
#                   Path to the S3TRANSFER section within the archive
837
sub getPackageBase {
838
    my ($pname, $pver) = @_;
839
 
840
    #
841
    #   Locate package
842
    #
843
    unless ( -d $conf->{'dpkg_archive'}) {
844
        $logger->warn("addPartsToImage: dpkg_archive not found");
845
        return undef;
846
    }
847
 
848
    my $src = catdir($conf->{'dpkg_archive'}, $pname, $pver);
849
    unless ( -d $src ) {
850
        $logger->warn("addPartsToImage: Package not found: $pname, $pver");
851
        return undef;
852
    }
853
 
7397 dpurdie 854
    $src = catdir( $src, $conf->{'transferDir'});
7396 dpurdie 855
    unless ( -d $src ) {
7397 dpurdie 856
        $logger->verbose("addPartsToImage: Package has no $conf->{'transferDir'}: $pname, $pver");
7396 dpurdie 857
        return undef;
858
    }
859
    return $src;
860
}
861
 
862
#-------------------------------------------------------------------------------
863
# Function        : generateBucketZipName 
864
#
865
# Description     : Generate the name of the zipfile created within the bucket  
866
#
867
# Inputs          : $rtag_id 
868
#
869
# Returns         : Full name - including bucket name
870
#
871
sub generateBucketZipName
872
{
873
    my ($rtag_id) = @_;
874
    my $targetName = 'Release-' . $rtag_id . '.zip';
875
    my $targetPath = catdir ($conf->{'S3Bucket'}, $targetName );
876
    return $targetPath;
877
}
878
 
879
 
880
#-------------------------------------------------------------------------------
881
# Function        : getDataFromRm 
882
#
883
# Description     : Get an array of data from RM
884
#                   Normally an array of arrays 
885
#
886
# Inputs          : $name           - Query Name
887
#                   $m_sqlstr       - Query
888
#                   $options        - Ref to a hash of options
889
#                                       sql     - show sql
890
#                                       data    - show data
891
#                                       dump    - show results
892
#                                       oneRow  - Only fetch one row
893
#                                       error   - Must find data
894
#                                       
895
# Returns         : ref to array of data
896
#
897
sub getDataFromRm
898
{
899
    my ($name,$m_sqlstr, $options ) = @_;
900
    my @row;
901
    my $data;
902
 
903
    if (ref $options ne 'HASH') {
904
        $options = {}; 
905
    }
906
 
907
    if ($options->{sql}) {
908
        $logger->logmsg("$name: $m_sqlstr")
909
    }
910
    my $sth = $RM_DB->prepare($m_sqlstr);
911
    if ( defined($sth) )
912
    {
913
        if ( $sth->execute( ) ) {
914
            if ( $sth->rows ) {
915
                while ( @row = $sth->fetchrow_array ) {
916
                    if ($options->{data}) {
917
                        $logger->warn ("$name: @row");
918
                    }
919
                    #Debug0("$name: @row");
920
                    push @{$data}, [@row];
921
 
922
                    last if $options->{oneRow};
923
                }
924
            }
925
            $sth->finish();
926
        } else {
927
            $logger->warn("Execute failure:$name: $m_sqlstr", $sth->errstr() );
928
        }
929
    } else {
930
        $logger->warn("Prepare failure:$name" );
931
    }
932
 
933
    if (!$data && $options->{error}) {
934
        $logger->warn( $options->{error} );
935
    }
936
 
937
    if ($data && $options->{oneRow}) {
938
        $data = $data->[0];
939
    }
940
 
941
    if ($options->{dump}) {
942
        Utils::DebugDumpData("$name", $data);
943
    }
944
    return $data;
945
}
946
 
947
#-------------------------------------------------------------------------------
948
# Function        : maintainTagList
949
#
950
# Description     : Maintain a data structure for the maintenance of the
951
#                   tags directory
952
#
953
# Inputs          : None
954
#
955
# Returns         : Nothing
956
#
957
sub maintainTagList
958
{
959
    #
960
    #   Time to perform the scan
961
    #   Will do at startup and every time period there after
962
    #
963
    return unless ( $now > ($lastTagListUpdate + $conf->{tagListUpdate} ));
964
    $logger->verbose("maintainTagList");
965
    $lastTagListUpdate = $now;
966
 
967
    #
968
    #   Generate new configuration
969
    #
970
    my %config;
971
    $config{s3Sync} = 1;                # Indicate that it may be special
972
 
973
    %{$config{releases}} = map { $_ => 1 } keys %{$activeReleases};
974
 
975
    #
976
    #   Save data
977
    #
978
    my $dump =  Data::Dumper->new([\%config], [qw(*config)]);
979
#print $dump->Dump;
980
#$dump->Reset;
981
 
982
    #
983
    #   Save config data
984
    #
985
    my $conf_file = catfile( $conf->{'tagdir'},'.config' );
986
    $logger->verbose3("maintainTagList: Writting $conf_file");
987
 
988
    my $fh;
989
    open ( $fh, '>', $conf_file ) or $logger->err("Can't create $conf_file: $!");
990
    print $fh $dump->Dump;
991
    close $fh;
992
}
993
 
994
#-------------------------------------------------------------------------------
995
# Function        : resetDailyStatistics 
996
#
997
# Description     : Called periodically to reset the daily statistics
998
#
999
# Inputs          : $time       - Current time
1000
#
1001
# Returns         : 
1002
#
1003
sub resetDailyStatistics
1004
{
1005
    my ($time) = @_;
1006
 
1007
    #
1008
    #   Detect a new day
1009
    #
1010
    my $today = (localtime($time))[7];
1011
    if ($yday != $today)
1012
    {
1013
        $yday = $today;
1014
        $logger->logmsg('Resetting daily statistics' );
1015
 
1016
        # Note: Must match @recoverTags in readStatistics
1017
        $statistics{dayStart} = $time;
1018
        $statistics{txCount} = 0;
7397 dpurdie 1019
        $statistics{txBytes} = 0;
7396 dpurdie 1020
        $statistics{delCount} = 0;
1021
        $statistics{linkErrors} = 0;
1022
    }
1023
}
1024
 
1025
#-------------------------------------------------------------------------------
1026
# Function        : readStatistics 
1027
#
1028
# Description     : Read in the last set of stats
1029
#                   Used after a restart to recover daily statistics
1030
#
1031
# Inputs          : 
1032
#
1033
# Returns         : 
1034
#
1035
sub readStatistics
1036
{
7397 dpurdie 1037
    my @recoverTags = qw(dayStart txCount txBytes delCount linkErrors);
7396 dpurdie 1038
 
1039
    if ($conf->{'statsfile'} and -f $conf->{'statsfile'})
1040
    {
1041
        if (open my $fh, $conf->{'statsfile'})
1042
        {
1043
            while (<$fh>)
1044
            {
1045
                m~(.*):(.*)~;
1046
                if ( grep( /^$1$/, @recoverTags ) ) 
1047
                {
1048
                    $statistics{$1} = $2;
1049
                    $logger->verbose("readStatistics $1, $2");
1050
                }
1051
            }
1052
            close $fh;
1053
            $yday = (localtime($statistics{dayStart}))[7];
1054
        }
1055
    }
1056
}
1057
 
1058
 
1059
#-------------------------------------------------------------------------------
1060
# Function        : periodicStatistics 
1061
#
1062
# Description     : Called on a regular basis to write out statistics
1063
#                   Used to feed information into Nagios
1064
#                   
1065
#                   This function is called via an alarm and may be outside the normal
1066
#                   processing loop. Don't make assumptions on the value of $now
1067
#
1068
# Inputs          : 
1069
#
1070
# Returns         : 
1071
#
1072
sub periodicStatistics
1073
{
1074
    #
1075
    #   A few local stats
1076
    #
1077
    $statistics{SeqNum}++;
1078
    $statistics{timeStamp} = time();
1079
    $statistics{upTime} = $statistics{timeStamp} - $startTime;
7397 dpurdie 1080
    $statistics{wedged} = $wedgedCount++ > 30  ? 1 : 0;
7396 dpurdie 1081
 
7406 dpurdie 1082
    if ( $statistics{wedged}) {
1083
         $statistics{state} = 'Wedged';
1084
    } elsif(!$linkUp){
1085
        $statistics{state} = 'S3 Bucket Read Error';
1086
    } else {
1087
        $statistics{state} = 'OK';
1088
    }
1089
 
7396 dpurdie 1090
    #   Reset daily accumulations - on first use each day
1091
    resetDailyStatistics($statistics{timeStamp});
1092
 
1093
    #
1094
    #   Write statistics to a file
1095
    #       Write to a tmp file, then rename.
1096
    #       Attempt to make the operation atomic - so that the file consumer
1097
    #       doesn't get a badly formed file.
1098
    #   
1099
    if ($conf->{'statsfiletmp'})
1100
    {
1101
        my $fh;
1102
        unless (open ($fh, '>', $conf->{'statsfiletmp'}))
1103
        {
1104
            $fh = undef;
1105
            $logger->warn("Cannot create temp stats file: $!");
1106
        }
1107
        else
1108
        {
1109
            foreach my $key ( sort { lc($a) cmp lc($b) } keys %statistics)
1110
            {
1111
                print $fh $key . ':' . $statistics{$key} . "\n";
1112
                $logger->verbose2('Statistics:'. $key . ':' . $statistics{$key});
1113
            }
1114
            close $fh;
1115
 
1116
            # Rename temp to real file
1117
            rename  $conf->{'statsfiletmp'},  $conf->{'statsfile'} ;
1118
        }
1119
    }
1120
}
1121
 
1122
#-------------------------------------------------------------------------------
1123
# Function        : sighandlers
1124
#
1125
# Description     : Install signal handlers
1126
#
1127
# Inputs          : $conf           - System config
1128
#
1129
# Returns         : Nothing
1130
#
1131
sub sighandlers
1132
{
1133
    my $conf = shift;
1134
    my $logger = $conf->{logger};
1135
 
1136
    $SIG{TERM} = sub {
1137
        # On shutdown
1138
        $logger->logmsg('Received SIGTERM. Shutting down....' );
1139
        unlink $conf->{'pidfile'} if (-f $conf->{'pidfile'});
1140
        exit 0;
1141
    };
1142
 
1143
    $SIG{HUP} = sub {
1144
        # On logrotate
1145
        $logger->logmsg('Received SIGHUP.');
1146
        $logger->rotatelog();
1147
    };
1148
 
1149
    $SIG{USR1} = sub {
1150
        # On Force Rescans
1151
        $logger->logmsg('Received SIGUSR1.');
1152
        $lastTagListUpdate = 0;
1153
        $lastS3Refresh = 0;
1154
    };
1155
 
1156
    alarm 60;
1157
    $SIG{ALRM} = sub {
1158
        # On Dump Statistics
1159
        $logger->verbose2('Received SIGUSR2.');
1160
        periodicStatistics();
1161
        alarm 60;
1162
    };
1163
 
1164
    $SIG{__WARN__} = sub { $logger->warn("@_") };
1165
    $SIG{__DIE__} = sub { $logger->err("@_") };
1166
}
1167
 
1168
 
1169
#-------------------------------------------------------------------------------
1170
# Function        : Error, Verbose, Warning
1171
#
1172
# Description     : Support for JatsRmApi
1173
#
1174
# Inputs          : Message
1175
#
1176
# Returns         : Nothing
1177
#
1178
sub Error
1179
{
1180
    $logger->err("@_");
1181
}
1182
 
1183
sub Verbose
1184
{
1185
    $logger->verbose2("@_");
1186
}
1187
 
1188
sub Warning
1189
{
1190
    $logger->warn("@_");
1191
}
1192
 
1193