-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrkn-fetch
executable file
·141 lines (104 loc) · 3.76 KB
/
rkn-fetch
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
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use Data::Dumper;
use MIME::Base64;
use SOAP::Lite;
my ($SOAP, $key, $sig);
sub init_soap() {
eval {
$SOAP = SOAP::Lite->new(
proxy => "http://vigruzki.rkn.gov.ru/services/OperatorRequest/",
ns => "http://vigruzki.rkn.gov.ru/OperatorRequest/",
);
};
die "SOAP init exception: $@\n" if $@;
}
sub read_file($) {
my $fname = shift;
local $/ = undef;
open F, "<", $fname or die "Cannot open $fname: $!\n";
my $contents = <F>;
close F;
return $contents;
}
sub soap_call {
my $resp;
print "SOAP call: $_[0]\n";
eval { $resp = $SOAP->call(@_) };
die "SOAP exception: $@\n" if $@;
die "SOAP empty response!\n" if !defined($resp);
die "SOAP error ".$resp->faultcode().": ".$resp->faultstring()." (".$resp->faultdetail().")\n"
if $resp->fault();
return $resp;
}
sub send_request() {
my $resp = soap_call("sendRequest",
SOAP::Data->name("requestFile" => $key )->type("base64Binary"),
SOAP::Data->name("signatureFile" => $sig )->type("base64Binary"),
SOAP::Data->name("dumpFormatVersion" => "2.2")->type("string"),
);
$resp = $resp->body()->{sendRequestResponse};
die "SOAP error: $resp->{resultComment}\n" if $resp->{result} ne 'true';
print "SOAP request is sent, code = ".$resp->{code}."\n";
return $resp->{code};
}
sub get_result($) {
my $code = shift;
my $delay = 90;
for my $iter (1..5) {
print "Sleep $delay seconds and try attempt #$iter ...\n";
sleep($delay);
my $resp = soap_call("getResult", SOAP::Data->name("code" => $code));
$resp = $resp->body()->{getResultResponse};
printf "ResultCode = %s, ResultComment = %s\n", $resp->{resultCode}, $resp->{resultComment} || '';
return decode_base64($resp->{registerZipArchive})
if $resp->{result} eq "true" and $resp->{resultCode} > 0;
warn "ResultComment = $resp->{resultComment}\n";
}
die "ERROR: getResult failed.\n";
}
sub need_update($;$) {
my ($tstamp_file, $debug) = @_;
my $resp = soap_call("getLastDumpDateEx");
my $info = $resp->body()->{getLastDumpDateExResponse};
print "RKN Info = ", Dumper($info), "\n" if $debug;
return "NEED_CREATE" if not $tstamp_file or not -e $tstamp_file;
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($tstamp_file);
#printf "DEBUG: mtime = %s, dtu = %s, dt = %s\n", $mtime, $info->{lastDumpDateUrgently}, $info->{lastDumpDate};
return "NEED_URGENTLY_UPDATE\n" if $mtime < $info->{lastDumpDateUrgently} / 1000;
return "NEED_UPDATE\n" if $mtime < $info->{lastDumpDate} / 1000;
return 0;
}
sub getinfo {
my $tstamp_file = shift;
printf "%s\n", need_update($tstamp_file, 1) || "FRESH_OK";
}
sub getdata {
my $destfile = shift or die "ERROR: missing destination file\n";
open F, '>', $destfile or die "ERROR: cannot create $destfile: $!\n";
my $code = send_request();
my $data = get_result($code);
print F $data;
close F;
}
sub update {
my $destfile = shift or die "ERROR: missing destination file\n";
my $tstamp_file = shift || $destfile;
return getdata($destfile) if need_update($tstamp_file);
print "FRESH_OK\n";
}
########################################################################
die "Usage: $0 keyfile sigfile [command [args...]]\n" if @ARGV < 2;
binmode(STDOUT,':utf8');
binmode(STDERR,':utf8');
$key = read_file(shift @ARGV);
$sig = read_file(shift @ARGV);
init_soap();
my $command = @ARGV ? shift @ARGV : 'info';
if ($command eq 'update') { update(@ARGV) } # optional args = destination_filepath, timestamp_file
elsif ($command eq 'force' ) { getdata(@ARGV) } # optional args = destination_filepath
elsif ($command eq 'info' ) { getinfo(@ARGV) } # optional args = timestamp_file
else { die "Wrong command $command, must be info, update or force\n" }
## END ##