+");
+
+sub sftp_connect {
+ my ($host, $sftpuser, $port) = @_;
+ my $sftp;
+ my $connection_tries = 1;
+
+ while (1) {
+ mylog('info', "Connecting to $sftpuser\@$host try number $connection_tries...");
+ $sftp = Net::SFTP::Foreign->new(
+ host => $host,
+ user => $sftpuser,
+ port => $port,
+ # for now we don't support passwords. use authorized_keys.
+ timeout => 30,
+ #more => ($opt{v} ? '-v' : ''),
+ );
+
+ if ($sftp->error && $connection_tries < 1200) {
+ $connection_tries++;
+ mylog('error', "Connection failed to $sftpuser\@$host: ". $sftp->error.
+ ", trying again in 60 sec...");
+ sleep 60;
+ }
+ else { last; }
+ }
+
+ return $sftp;
+}
+
+our $log;
+sub mylog {
+ my( $level, $message ) = @_;
+ #warn "$message\n" if $opt{v};
+ print STDERR "$message\n" if $opt{v};
+ $log ||= FS::Log->new('freeside-ipifony-download');
+ $log->log(level=>$level, message=>$message);