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