add customer fields option with agent, display_custnum, status and name, RT#73721
[freeside.git] / FS / bin / freeside-cdr-sftp_and_import
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Std;
5 use Date::Format;
6 use Net::SFTP::Foreign::Compat;
7 use Net::FTP;
8 use FS::UID qw(adminsuidsetup datasrc);
9 use FS::cdr;
10
11 ###
12 # parse command line
13 ###
14
15 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 );
16 getopts('c:m:p:r:e:d:v:P:agsb');
17
18 $opt_e ||= 'csv';
19 #$opt_e = ".$opt_e" unless $opt_e =~ /^\./;
20 $opt_e =~ s/^\.//;
21
22 $opt_p ||= '';
23
24 die "invalid cdrtypenum" if $opt_c && $opt_c !~ /^\d+$/;
25
26 my %options = ();
27
28 my $user = shift or die &usage;
29 adminsuidsetup $user;
30
31 # %%%FREESIDE_CACHE%%%
32 my $cachedir = '%%%FREESIDE_CACHE%%%/cache.'. datasrc. '/cdrs';
33 mkdir $cachedir unless -d $cachedir;
34
35 my $format = shift or die &usage;
36
37 use vars qw( $servername );
38 $servername = shift or die &usage;
39
40 ###
41 # get the file list
42 ###
43
44 warn "Retrieving directory listing\n" if $opt_v;
45
46 $opt_m = 'sftp' if !defined($opt_m);
47 $opt_m = lc($opt_m);
48
49 my $ls;
50
51 if($opt_m eq 'ftp') {
52   $options{'Port'}    = $opt_P if $opt_P;
53   $options{'Debug'}   = $opt_v if $opt_v;
54   $options{'Passive'} = $opt_a if $opt_a;
55
56   my $ls_ftp = ftp();
57
58   $ls = [ grep { /^$opt_p.*\.$opt_e$/i } $ls_ftp->ls ];
59 }
60 elsif($opt_m eq 'sftp') {
61   $options{'port'}    = $opt_P if $opt_P;
62   $options{'debug'}   = $opt_v if $opt_v;
63
64   my $ls_sftp = sftp();
65
66   $ls_sftp->setcwd($opt_r) or die "can't chdir to $opt_r\n"
67     if $opt_r;
68
69   $ls = $ls_sftp->ls('.', wanted => qr/^$opt_p.*\.$opt_e$/i,
70                           names_only => 1 );
71 }
72 else {
73   die "Method '$opt_m' not supported; must be ftp or sftp\n";
74 }
75
76 ###
77 # import each file
78 ###
79
80 foreach my $filename ( @$ls ) {
81
82   warn "Downloading $filename\n" if $opt_v;
83
84   #get the file
85   if($opt_m eq 'ftp') {
86     my $ftp = ftp();
87     $ftp->get($filename, "$cachedir/$filename")
88       or die "Can't get $filename: ". $ftp->message . "\n";
89   }
90   else {
91     my $sftp = sftp();
92     $sftp->get($filename, "$cachedir/$filename")
93       or die "Can't get $filename: ". $sftp->error . "\n";
94   }
95
96   warn "Processing $filename\n" if $opt_v;
97  
98   my $ungziped = $filename;
99   $ungziped =~ s/\.gz$//;
100   if ( $opt_g ) {
101       if(system('gunzip', "$cachedir/$filename") != 0) {
102         warn "gunzip of '$cachedir/$filename' failed" if $opt_v;
103         unlink "$cachedir/$filename";
104         next;
105       }
106   }
107
108   my $file_timestamp = $filename.'-'.time2str('%Y-%m-%d', time);
109
110   my $import_options = {
111     'file'            => "$cachedir/$ungziped",
112     'format'          => $format,
113     'batch_namevalue' => $file_timestamp,
114     'empty_ok'        => 1,
115   };
116   $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
117   
118   my $error = FS::cdr::batch_import($import_options);
119
120   if ( $error ) {
121
122     if ( $opt_s ) {
123       warn "$ungziped: $error\n";
124     } else {
125       unlink "$cachedir/$filename";
126       unlink "$cachedir/$ungziped" if $opt_g;
127       die $error;
128     }
129
130   } else {
131
132     if ( $opt_d ) {
133       if ( $opt_m eq 'ftp') {
134         my $ftp = ftp();
135         $ftp->rename($filename, "$opt_d/$file_timestamp")
136           or do {
137             unlink "$cachedir/$filename";
138             unlink "$cachedir/$ungziped" if $opt_g;
139             die "Can't move $filename to $opt_d: ".$ftp->message . "\n";
140           };
141       } else {
142         my $sftp = sftp();
143         $sftp->rename($filename, "$opt_d/$file_timestamp")
144           or do {
145             unlink "$cachedir/$filename";
146             unlink "$cachedir/$ungziped" if $opt_g;
147             die "can't move $filename to $opt_d: ". $sftp->error . "\n";
148           };
149       }
150     }
151
152   }
153
154   unlink "$cachedir/$filename";
155   unlink "$cachedir/$ungziped" if $opt_g;
156
157 }
158
159 ###
160 # subs
161 ###
162
163 sub usage {
164   "Usage:
165   cdr.sftp_and_import [ -m method ] [ -p prefix ] [ -e extension ] 
166     [ -r remotefolder ] [ -d donefolder ] [ -v level ] [ -P port ]
167     [ -a ] [ -g ] [ -s ] [ -c cdrtypenum ] user format [sftpuser@]servername
168   ";
169 }
170
171 use vars qw( $sftp $ftp );
172
173 sub ftp {
174   return $ftp if $ftp && $ftp->pwd;
175   
176   my ($hostname, $userpass) = reverse split('@', $servername);
177   my ($ftp_user, $ftp_pass) = split(':', $userpass);
178
179   my $ftp = Net::FTP->new($hostname, %options) 
180     or die "FTP connection to '$hostname' failed.";
181   $ftp->login($ftp_user, $ftp_pass) or die "FTP login failed: ".$ftp->message;
182   $ftp->cwd($opt_r) or die "can't chdir to $opt_r\n" if $opt_r;
183   $ftp->binary or die "can't set BINARY mode: ". $ftp->message if $opt_b;
184   return $ftp;
185 }
186
187 sub sftp {
188
189   #reuse connections
190   return $sftp if $sftp && $sftp->cwd;
191
192   my %sftp = ( host => $servername );
193
194   $sftp = Net::SFTP::Foreign->new(%sftp);
195   $sftp->error and die "SFTP connection failed: ". $sftp->error;
196
197   $sftp;
198 }
199
200 =head1 NAME
201
202 freeside-cdr-sftp_and_import - Download CDR files from a remote server via SFTP
203
204 =head1 SYNOPSIS
205
206   freeside-cdr-sftp_and_import [ -m method ] [ -p prefix ] [ -e extension ] 
207     [ -r remotefolder ] [ -d donefolder ] [ -v level ] [ -P port ]
208     [ -a ] [ -g ] [ -s ] [ -c cdrtypenum ] user format [sftpuser@]servername
209
210 =head1 DESCRIPTION
211
212 Command line tool to download CDR files from a remote server via SFTP 
213 or FTP and then import them into the database.
214
215 -m: transfer method (sftp or ftp), defaults to sftp
216
217 -p: file prefix, if specified
218
219 -e: file extension, defaults to .csv
220
221 -r: if specified, changes into this remote folder before starting
222
223 -d: if specified, moves files to the specified folder when done
224
225 -P: if specified, sets the port to use
226
227 -a: use ftp passive mode
228
229 -b: use ftp binary mode
230
231 -v: set verbosity level; this script only has one level, but it will 
232     be passed as the 'debug' argument to the transport method
233
234 -c: cdrtypenum to set, defaults to none
235
236 -g: File is gzipped
237
238 -s: Warn and skip files which could not be imported rather than abort
239
240 user: freeside username
241
242 format: CDR format name
243
244 [sftpuser@]servername: remote server
245 (or ftpuser:ftppass@servername)
246
247 =head1 BUGS
248
249 =head1 SEE ALSO
250
251 L<FS::cdr>
252
253 =cut
254
255 1;
256