summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-cdr-sftp_and_import
blob: aa4bf6471cb3aef939f14420c09bf08fff5e9207 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/perl

use strict;
use Getopt::Std;
use Date::Format;
use Net::SFTP::Foreign::Compat;
use Net::FTP;
use FS::UID qw(adminsuidsetup datasrc);
use FS::cdr;

###
# parse command line
###

use vars qw( $opt_m $opt_p $opt_r $opt_e $opt_d $opt_v $opt_P $opt_a $opt_c $opt_g $opt_s $opt_b );
getopts('c:m:p:r:e:d:v:P:agsb');

$opt_e ||= 'csv';
#$opt_e = ".$opt_e" unless $opt_e =~ /^\./;
$opt_e =~ s/^\.//;

$opt_p ||= '';

die "invalid cdrtypenum" if $opt_c && $opt_c !~ /^\d+$/;

my %options = ();

my $user = shift or die &usage;
adminsuidsetup $user;

# %%%FREESIDE_CACHE%%%
my $cachedir = '%%%FREESIDE_CACHE%%%/cache.'. datasrc. '/cdrs';
mkdir $cachedir unless -d $cachedir;

my $format = shift or die &usage;

use vars qw( $servername );
$servername = shift or die &usage;

###
# get the file list
###

warn "Retrieving directory listing\n" if $opt_v;

$opt_m = 'sftp' if !defined($opt_m);
$opt_m = lc($opt_m);

my $ls;

if($opt_m eq 'ftp') {
  $options{'Port'}    = $opt_P if $opt_P;
  $options{'Debug'}   = $opt_v if $opt_v;
  $options{'Passive'} = $opt_a if $opt_a;

  my $ls_ftp = ftp();

  $ls = [ grep { /^$opt_p.*\.$opt_e$/i } $ls_ftp->ls ];
}
elsif($opt_m eq 'sftp') {
  $options{'port'}    = $opt_P if $opt_P;
  $options{'debug'}   = $opt_v if $opt_v;

  my $ls_sftp = sftp();

  $ls_sftp->setcwd($opt_r) or die "can't chdir to $opt_r\n"
    if $opt_r;

  $ls = $ls_sftp->ls('.', wanted => qr/^$opt_p.*\.$opt_e$/i,
                          names_only => 1 );
}
else {
  die "Method '$opt_m' not supported; must be ftp or sftp\n";
}

###
# import each file
###

foreach my $filename ( @$ls ) {

  warn "Downloading $filename\n" if $opt_v;

  #get the file
  if($opt_m eq 'ftp') {
    my $ftp = ftp();
    $ftp->get($filename, "$cachedir/$filename")
      or die "Can't get $filename: ". $ftp->message . "\n";
  }
  else {
    my $sftp = sftp();
    $sftp->get($filename, "$cachedir/$filename")
      or die "Can't get $filename: ". $sftp->error . "\n";
  }

  warn "Processing $filename\n" if $opt_v;
 
  my $ungziped = $filename;
  $ungziped =~ s/\.gz$//;
  if ( $opt_g ) {
      if(system('gunzip', "$cachedir/$filename") != 0) {
        warn "gunzip of '$cachedir/$filename' failed" if $opt_v;
        unlink "$cachedir/$filename";
        next;
      }
  }

  my $file_timestamp = $filename.'-'.time2str('%Y-%m-%d', time);

  my $import_options = {
    'file'            => "$cachedir/$ungziped",
    'format'          => $format,
    'batch_namevalue' => $file_timestamp,
    'empty_ok'        => 1,
  };
  $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
  
  my $error = FS::cdr::batch_import($import_options);

  if ( $error ) {

    if ( $opt_s ) {
      warn "$ungziped: $error\n";
    } else {
      unlink "$cachedir/$filename";
      unlink "$cachedir/$ungziped" if $opt_g;
      die $error;
    }

  } else {

    if ( $opt_d ) {
      if ( $opt_m eq 'ftp') {
        my $ftp = ftp();
        $ftp->rename($filename, "$opt_d/$file_timestamp")
          or do {
            unlink "$cachedir/$filename";
            unlink "$cachedir/$ungziped" if $opt_g;
            die "Can't move $filename to $opt_d: ".$ftp->message . "\n";
          };
      } else {
        my $sftp = sftp();
        $sftp->rename($filename, "$opt_d/$file_timestamp")
          or do {
            unlink "$cachedir/$filename";
            unlink "$cachedir/$ungziped" if $opt_g;
            die "can't move $filename to $opt_d: ". $sftp->error . "\n";
          };
      }
    }

  }

  unlink "$cachedir/$filename";
  unlink "$cachedir/$ungziped" if $opt_g;

}

###
# subs
###

sub usage {
  "Usage:
  cdr.sftp_and_import [ -m method ] [ -p prefix ] [ -e extension ] 
    [ -r remotefolder ] [ -d donefolder ] [ -v level ] [ -P port ]
    [ -a ] [ -g ] [ -s ] [ -c cdrtypenum ] user format [sftpuser@]servername
  ";
}

use vars qw( $sftp $ftp );

sub ftp {
  return $ftp if $ftp && $ftp->pwd;
  
  my ($hostname, $userpass) = reverse split('@', $servername);
  my ($ftp_user, $ftp_pass) = split(':', $userpass);

  my $ftp = Net::FTP->new($hostname, %options) 
    or die "FTP connection to '$hostname' failed.";
  $ftp->login($ftp_user, $ftp_pass) or die "FTP login failed: ".$ftp->message;
  $ftp->cwd($opt_r) or die "can't chdir to $opt_r\n" if $opt_r;
  $ftp->binary or die "can't set BINARY mode: ". $ftp->message if $opt_b;
  return $ftp;
}

sub sftp {

  #reuse connections
  return $sftp if $sftp && $sftp->cwd;

  my %sftp = ( host => $servername );

  $sftp = Net::SFTP::Foreign->new(%sftp);
  $sftp->error and die "SFTP connection failed: ". $sftp->error;

  $sftp;
}

=head1 NAME

freeside-cdr-sftp_and_import - Download CDR files from a remote server via SFTP

=head1 SYNOPSIS

  freeside-cdr-sftp_and_import [ -m method ] [ -p prefix ] [ -e extension ] 
    [ -r remotefolder ] [ -d donefolder ] [ -v level ] [ -P port ]
    [ -a ] [ -g ] [ -s ] [ -c cdrtypenum ] user format [sftpuser@]servername

=head1 DESCRIPTION

Command line tool to download CDR files from a remote server via SFTP 
or FTP and then import them into the database.

-m: transfer method (sftp or ftp), defaults to sftp

-p: file prefix, if specified

-e: file extension, defaults to .csv

-r: if specified, changes into this remote folder before starting

-d: if specified, moves files to the specified folder when done

-P: if specified, sets the port to use

-a: use ftp passive mode

-b: use ftp binary mode

-v: set verbosity level; this script only has one level, but it will 
    be passed as the 'debug' argument to the transport method

-c: cdrtypenum to set, defaults to none

-g: File is gzipped

-s: Warn and skip files which could not be imported rather than abort

user: freeside username

format: CDR format name

[sftpuser@]servername: remote server
(or ftpuser:ftppass@servername)

=head1 BUGS

=head1 SEE ALSO

L<FS::cdr>

=cut

1;