View | Details | Raw Unified | Return to bug 36766
Collapse All | Expand All

(-)a/installer/data/mysql/atomicupdate/bug_36766-add_SFTP_notices.pl (-2 / +4 lines)
Lines 7-19 return { Link Here
7
        my ($args) = @_;
7
        my ($args) = @_;
8
        my ( $dbh, $out ) = @$args{qw(dbh out)};
8
        my ( $dbh, $out ) = @$args{qw(dbh out)};
9
9
10
        $dbh->do(q{
10
        $dbh->do(
11
            q{
11
            INSERT IGNORE INTO letter
12
            INSERT IGNORE INTO letter
12
            (module,code,branchcode,name,is_html,title,content,message_transport_type,lang)
13
            (module,code,branchcode,name,is_html,title,content,message_transport_type,lang)
13
            VALUES
14
            VALUES
14
            ('commandline', 'SFTP_FAILURE', '', 'File SFTP failed', 0, 'The SFTP by sftp_file.pl failed', 'SFTP upload failed:\n\n<<sftp_status>>', 'email', 'default'),
15
            ('commandline', 'SFTP_FAILURE', '', 'File SFTP failed', 0, 'The SFTP by sftp_file.pl failed', 'SFTP upload failed:\n\n<<sftp_status>>', 'email', 'default'),
15
            ('commandline', 'SFTP_SUCCESS', '', 'File SFTP success', 0, 'The SFTP by sftp_file.pl was successful', 'SFTP upload succeeded', 'email', 'default')
16
            ('commandline', 'SFTP_SUCCESS', '', 'File SFTP success', 0, 'The SFTP by sftp_file.pl was successful', 'SFTP upload succeeded', 'email', 'default')
16
        });
17
        }
18
        );
17
19
18
        say $out "Added new sample notices 'SFTP_FAILURE' and 'SFTP_SUCCESS'";
20
        say $out "Added new sample notices 'SFTP_FAILURE' and 'SFTP_SUCCESS'";
19
    },
21
    },
(-)a/misc/cronjobs/sftp_file.pl (-44 / +35 lines)
Lines 20-29 use C4::Context; Link Here
20
use Net::SFTP::Foreign;
20
use Net::SFTP::Foreign;
21
use C4::Log qw( cronlogaction );
21
use C4::Log qw( cronlogaction );
22
use Koha::Email;
22
use Koha::Email;
23
use Getopt::Long qw( GetOptions );
23
use Getopt::Long   qw( GetOptions );
24
use Pod::Usage qw( pod2usage );
24
use Pod::Usage     qw( pod2usage );
25
use Carp qw( carp );
25
use Carp           qw( carp );
26
use C4::Letters qw( GetPreparedLetter EnqueueLetter );
26
use C4::Letters    qw( GetPreparedLetter EnqueueLetter );
27
use File::Basename qw( basename );
27
use File::Basename qw( basename );
28
28
29
=head1 NAME
29
=head1 NAME
Lines 106-115 B<sftp_file.pl --host test.com --user test --pass 123cne --upload_dir uploads -- Link Here
106
106
107
In this example the script will upload /tmp/test.mrc on the Koha server to the test.com remote host using the username:password of test:123cne. The file will be SFTP to the uploads directory.
107
In this example the script will upload /tmp/test.mrc on the Koha server to the test.com remote host using the username:password of test:123cne. The file will be SFTP to the uploads directory.
108
108
109
=head1 TO DO
110
111
=back
112
113
=cut
109
=cut
114
110
115
# These variables can be set by command line options,
111
# These variables can be set by command line options,
Lines 130-154 my $status_email = undef; Link Here
130
my $status_email_message = undef;
126
my $status_email_message = undef;
131
my $sftp_status          = undef;
127
my $sftp_status          = undef;
132
128
133
my $command_line_options = join(" ",@ARGV);
129
my $command_line_options = join( " ", @ARGV );
134
130
135
GetOptions(
131
GetOptions(
136
    'help|?'         => \$help,
132
    'help|?'       => \$help,
137
    'man'            => \$man,
133
    'man'          => \$man,
138
    'verbose'        => \$verbose,
134
    'verbose'      => \$verbose,
139
    'host=s'         => \$host,
135
    'host=s'       => \$host,
140
    'user=s'         => \$user,
136
    'user=s'       => \$user,
141
    'pass=s'         => \$pass,
137
    'pass=s'       => \$pass,
142
    'upload_dir=s'   => \$upload_dir,
138
    'upload_dir=s' => \$upload_dir,
143
    'port=s'         => \$port,
139
    'port=s'       => \$port,
144
    'file=s'         => \$file,
140
    'file=s'       => \$file,
145
    'email=s'        => \$email,
141
    'email=s'      => \$email,
146
) or pod2usage(2);
142
) or pod2usage(2);
147
pod2usage( -verbose => 2 ) if ($man);
143
pod2usage( -verbose => 2 ) if ($man);
148
pod2usage( -verbose => 2 ) if ($help and $verbose);
144
pod2usage( -verbose => 2 ) if ( $help and $verbose );
149
pod2usage(1) if $help;
145
pod2usage(1) if $help;
150
146
151
cronlogaction({ info => $command_line_options });
147
cronlogaction( { info => $command_line_options } );
152
148
153
# Check we have all the SFTP details we need
149
# Check we have all the SFTP details we need
154
if ( !$user || !$pass || !$host || !$upload_dir ) {
150
if ( !$user || !$pass || !$host || !$upload_dir ) {
Lines 164-221 my $sftp = Net::SFTP::Foreign->new( Link Here
164
    timeout        => 10,
160
    timeout        => 10,
165
    stderr_discard => 1,
161
    stderr_discard => 1,
166
);
162
);
167
$sftp->die_on_error( "Cannot ssh to $host" . $sftp->error );
163
$sftp->die_on_error( "Cannot ssh to $host " . $sftp->error );
168
164
169
# Change to remote directory
165
# Change to remote directory
170
$sftp->setcwd( $upload_dir )
166
$sftp->setcwd($upload_dir)
171
    or return warn "Cannot change remote dir : $sftp->error\n";
167
    or $sftp->die_on_error( "Cannot change remote dir : " . $sftp->error );
172
168
173
# If the --email parameter is defined then prepare sending an email confirming the success
169
# If the --email parameter is defined then prepare sending an email confirming the success
174
# or failure of the SFTP
170
# or failure of the SFTP
175
if ( $email ) {
171
if ($email) {
176
    if ( C4::Context->preference('KohaAdminEmailAddress') ) {
172
    if ( C4::Context->preference('KohaAdminEmailAddress') ) {
177
        $admin_address = C4::Context->preference('KohaAdminEmailAddress');
173
        $admin_address = C4::Context->preference('KohaAdminEmailAddress');
178
    }
174
    }
179
175
180
    if ( !Koha::Email->is_valid($email) ) {
176
    if ( !Koha::Email->is_valid($email) ) {
181
        return warn "The email address you defined in the --email parameter is invalid\n";
177
        die "The email address you defined in the --email parameter is invalid\n";
182
    }
178
    }
183
}
179
}
184
180
185
# Do the SFTP upload
181
# Do the SFTP upload
186
open my $fh, '<', $file;
182
open my $fh, '<', $file;
187
if (
183
if ( $sftp->put( $fh, basename($file) ) ) {
188
    $sftp->put(
184
189
        $fh, basename($file)
190
    )
191
) {
192
    # Send success email
185
    # Send success email
193
    $sftp_status = 'SUCCESS';
186
    $sftp_status = 'SUCCESS';
194
    close $fh;
187
    close $fh;
195
} else {
188
} else {
189
196
    # Send failure email
190
    # Send failure email
197
    $sftp_status = 'FAILURE';
191
    $sftp_status = 'FAILURE';
198
}
192
}
199
193
200
# Send email confirming the success or failure of the SFTP
194
# Send email confirming the success or failure of the SFTP
201
if ( $email ) {
195
if ($email) {
202
    $status_email =  C4::Letters::GetPreparedLetter (
196
    $status_email = C4::Letters::GetPreparedLetter(
203
        module => 'commandline',
197
        module                 => 'commandline',
204
        letter_code => "SFTP_$sftp_status", #SFTP_SUCCESS, SFTP_FAILURE
198
        letter_code            => "SFTP_$sftp_status",             #SFTP_SUCCESS, SFTP_FAILURE
205
        message_transport_type => 'email',
199
        message_transport_type => 'email',
206
        substitute => {
200
        substitute             => { sftp_error => $sftp->error }
207
            sftp_error => $sftp->error
208
        }
209
    );
201
    );
210
202
211
    C4::Letters::EnqueueLetter(
203
    C4::Letters::EnqueueLetter(
212
        {
204
        {
213
            letter                  => $status_email,
205
            letter                 => $status_email,
214
            to_address              => $email,
206
            to_address             => $email,
215
            from_address            => $admin_address,
207
            from_address           => $admin_address,
216
            message_transport_type  => 'email'
208
            message_transport_type => 'email'
217
        }
209
        }
218
    ) or warn "can't enqueue letter " . $status_email->{code};
210
    ) or warn "can't enqueue letter " . $status_email->{code};
219
}
211
}
220
212
221
cronlogaction({ action => 'End', info => "COMPLETED" });
213
cronlogaction( { action => 'End', info => "COMPLETED" } );
222
- 

Return to bug 36766