remote account send for csv pusher
authorBill Erickson <berick@esilibrary.com>
Fri, 7 Dec 2012 16:44:15 +0000 (11:44 -0500)
committerBill Erickson <berick@esilibrary.com>
Fri, 7 Dec 2012 16:44:15 +0000 (11:44 -0500)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/support-scripts/csv_notify_pusher.pl

index 9cfef0e..067002a 100755 (executable)
@@ -5,14 +5,19 @@ use DateTime;
 use OpenSRF::AppSession;
 use OpenILS::Utils::CStoreEditor;
 use OpenILS::Utils::Cronscript;
+use OpenILS::Utils::RemoteAccount;
 binmode(STDOUT, ":utf8");
 
 my %defaults = (
-    'osrf-config=s' => '/openils/conf/opensrf_core.xml',
-    'start-date=s'  => DateTime->now->strftime('%F'),
-    'end-date=s'    => '',
-    'event-defs=s'  => '',
-    'verbose'       => 0
+    'osrf-config=s'     => '/openils/conf/opensrf_core.xml',
+    'start-date=s'      => DateTime->now->strftime('%F'),
+    'end-date=s'        => '',
+    'event-defs=s'      => '',
+    'out-file=s'        => '',
+    'remote-account=s'  => 0,
+    'nolockfile'        => 1,
+    'verbose'           => 0,
+    'cleanup-file'      => 0 # remove output file when done
 );
 
 sub help {
@@ -20,11 +25,14 @@ sub help {
 
 Collect CSV notification output and deliver to 3rd party
 
-# collection notifications for today for the selected defs
-$0 --event-def 104,105,106
+# Collect a week of notifications
 
-# collect a week of notifications
-$0 --event-def 104,105,106 --start-date 2012-01-01 --end-date 2012-01-07
+$0 \
+    --event-def 104,105,106 \
+    --start-date 2012-01-01 \
+    --end-date 2012-01-07 \
+    --out-file /var/run/evergreen/csv/2012-01-07.notify.csv
+    --remote-account 6
 
 HELP
     exit;
@@ -39,6 +47,8 @@ help() if $opts->{help};
 my $verbose = $opts->{'verbose'};
 my $start_date = $opts->{'start-date'} || '';
 my $end_date = $opts->{'end-date'} || '';
+my $out_file = $opts->{'out-file'} || '&STDOUT';
+my $remote_account = $opts->{'remote-account'};
 
 my @event_defs = split(/,/, $opts->{'event-defs'});
 
@@ -48,8 +58,12 @@ if ($verbose) {
     print "processing event_defs = @event_defs\n";
     print "start_date = $start_date\n";
     print "end_date = $end_date\n";
+    print "out_file = $out_file\n";
 }
 
+open(OUTFILE, ">$out_file") or 
+    die "unable to open out-file '$out_file' for writing: $!\n";
+
 my %date_filter;
 $date_filter{run_time} = {'>=' => $start_date} if $start_date;
 $date_filter{run_time} = {'<' => $end_date} if $end_date;
@@ -79,11 +93,11 @@ my $req = $ses->request(
 );
 
 # header
-print '"Media Type","Language","Notice Type","Notification Level",';
-print '"Patron Number","Patron Title","Patron First Names","Patron Surname",';
-print '"Telephone Number","Email Address","Library Code","Site Code",';
-print '"Site Name","Item Barcode","Due Date","Item Title","Transaction ID"';
-print "\n";
+print OUTFILE '"Media Type","Language","Notice Type","Notification Level",';
+print OUTFILE '"Patron Number","Patron Title","Patron First Names","Patron Surname",';
+print OUTFILE '"Telephone Number","Email Address","Library Code","Site Code",';
+print OUTFILE '"Site Name","Item Barcode","Due Date","Item Title","Transaction ID"';
+print OUTFILE "\n";
 
 # dump the CSV chunk from each event output
 while (my $resp = $req->recv(timeout => 12000)) {
@@ -92,5 +106,39 @@ while (my $resp = $req->recv(timeout => 12000)) {
         last;
     };
     my $content = $resp->content or next;
-    print $content->{data};
+    print OUTFILE $content->{data};
+}
+
+# no output file?  nothing left to do
+exit unless $opts->{'out-file'};
+
+if ($remote_account) {
+
+    my $racct = $o->editor->retrieve_config_remote_account($remote_account);
+    die "No such remote account $remote_account" unless $racct;
+
+    my $type;
+    my $host = $racct->host;
+    ($host =~ s/^(S?FTP)://i and $type = uc($1)) or                   
+    ($host =~ s/^(SSH|SCP)://i and $type = 'SCP');
+
+    my $acct = OpenILS::Utils::RemoteAccount->new(
+        type            => $type,
+        account_object  => $racct,
+        local_file      => $out_file,
+        remote_file     => $out_file
+    );
+
+    my $res = $acct->put;
+
+    die "Unable to push to remote server [$remote_account] : " . 
+        $acct->error . "\n" unless $res;
+
+    print "Pushed file to $res\n" if $verbose;
 }
+
+if ($opts->{'cleanup-file'}) {
+    unlink($out_file) or die "Unable to clean up file '$out_file' : $!\n";
+}
+
+