forked from molgenis/ngs-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy2grid.pl
207 lines (186 loc) · 5.49 KB
/
copy2grid.pl
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/perl
#
# Copy data from the (Groningen GCC) cluster to the (bbmri.nl VO on) Grid.
#
#
# Initialize evironment
#
use strict;
use Getopt::Std;
use Log::Log4perl qw(:easy);
my %log_levels = (
'ALL' => $ALL,
'TRACE' => $TRACE,
'DEBUG' => $DEBUG,
'INFO' => $INFO,
'WARN' => $WARN,
'ERROR' => $ERROR,
'FATAL' => $FATAL,
'OFF' => $OFF,
);
#
# Get options.
#
my %opts;
Getopt::Std::getopts('t:f:d:p:c:l:', \%opts);
my $type = $opts{'t'};
my $list_file = $opts{'f'};
my $destination_dir = $opts{'d'}; # srm://srm.grid.sara.nl/pnfs/grid.sara.nl/data/bbmri.nl/....
my $dcache_proxy = $opts{'p'};
my $certificate_dir = $opts{'c'};
my $log_level = $opts{'l'};
#
# Configure logging.
#
# Provides default if user did not specify log level:
$log_level = (defined($log_level) ? $log_level : 'INFO');
# Reset log level to default if user specified illegal log level.
$log_level = (defined($log_levels{$log_level}) ? $log_levels{$log_level} : $log_levels{'INFO'});
#Log::Log4perl->init('log4perl.properties');
Log::Log4perl->easy_init(
#{ level => $log_level,
# file => ">>copy2grid.log",
# layout => '%F{1}-%L-%M: %m%n' },
{ level => $log_level,
file => "STDOUT",
layout => '%d L:%L %p> %m%n' },
);
my $logger = Log::Log4perl::get_logger();
#
# Check options.
#
if ($list_file =~ /^$/) {
_Usage();
exit;
}
unless (-f $list_file && -r $list_file) {
_Usage();
exit;
}
unless ($destination_dir =~ /\/$/) {
$destination_dir .= '/';
}
unless (defined($type)) {
if ($destination_dir =~ m/srm:\/\//) {
$type = 'c2g';
} elsif ($destination_dir =~ m/\//) {
$type = 'g2c';
} else {
$logger->fatal('Destination dir in unrecognised format...');
exit;
}
}
#
# Start job.
#
$logger->info('Starting...');
#
# Create filehandles.
#
my $list_fh;
eval {
open($list_fh, "<$list_file");
};
if ($@) {
$logger->fatal('Cannot create filehandle: ' . $@);
exit;
}
my $found_dcache_srmcp = 0;
my $dcache_srmcp = `which srmcp`;
if ($dcache_srmcp =~ m/\/srmcp$/) {
chomp($dcache_srmcp);
$logger->trace('Found srmcp command in: ' . $dcache_srmcp);
$found_dcache_srmcp = 1;
} else {
$logger->trace('Could not find srmcp command in $PATH.');
$logger->trace('Will try to find srmcp command in the "usual suspect" places of the GCC cluster.');
my $gcc_home;
if (defined($ENV{'GCC_HOME'})) {
$gcc_home = $ENV{'GCC_HOME'};
} else {
$logger->fatal('${GCC_HOME} environment variable is not defined.');
exit 1;
}
$dcache_srmcp = $gcc_home. '/tools/dcache/opt/d-cache/srm/bin/srmcp';
if (-f $dcache_srmcp && -r $dcache_srmcp && -X $dcache_srmcp) {
$logger->trace('Found srmcp command in: ' . $dcache_srmcp);
$found_dcache_srmcp = 1;
} else {
$logger->trace('Could not find srmcp command in the "usual suspect" places on the GCC cluster either.');
}
}
unless ($found_dcache_srmcp) {
$logger->fatal('Could not find srmcp command!');
exit;
}
my $cmd_base = $dcache_srmcp;
$cmd_base .= ' -x509_user_proxy=' . $dcache_proxy;
$cmd_base .= ' -x509_user_trusted_certificates=' . $certificate_dir;
$cmd_base .= ' -server_mode=passive';
# Disabled SRM V2: SE srm.grid.sara.nl supports SRM protocol version 2, but many of the other SEs do not.
#$cmd_base .= ' -2';
my $total_file_count = 0;
#
# Parse file that lists the files that need to be copied.
#
while (my $line = <$list_fh>) {
my $file_path = $line;
$file_path =~ s/[\n\r\f]+$//;
my $file_name;
my $cmd = $cmd_base;
if ($file_path =~ /\/([^\/]+)$/) {
$file_name = $1;
} else {
$file_name = $file_path;
#$logger->fatal('Cannot parse file name from file path');
#exit;
}
if ($type eq 'g2c') {
$cmd .= ' ' . $file_path;
$cmd .= ' file:///' . $destination_dir . $file_name;
} elsif ($type eq'c2g') {
$cmd .= ' file:///' . $file_path;
$cmd .= ' ' . $destination_dir . $file_name;
}
$logger->debug('Executing command: ' . $cmd);
my $result = `$cmd 2>&1`;
$logger->info($result);
$total_file_count++;
}
#
# Close filehandles.
#
close($list_fh);
$logger->info('Processed ' . $total_file_count . ' files.');
$logger->info('Finished!');
#
##
### Subs.
##
#
sub _Usage {
print "\n";
print 'copy2grid.pl - Copies data from the (Groningen GCC) cluster to the (bbmri.nl VO on) Grid'."\n";
print ' Assumes DCache is either in your $PATH or in the $GCC_HOME/tools/dcache dir on the Groningen GCC cluster'."\n";
print "\n";
print "Usage:\n";
print "\n";
print " copy.grid.pl options\n";
print "\n";
print "Available options are:\n";
print "\n";
print " -t [g2c|c2g] Type or tranfser mode:\n";
print " * c2g - To transfer from a Cluster to the Grid.\n";
print " * g2c - To transfer from the Grid to a Cluster.\n";
print " -f [file] Input file listing files that need to be transferred.\n";
print " -d [path] Directory the files need to be tansfered to.\n";
print " If the mode is cluster2grid a grid dir on an SRM.\n";
print " For example srm://srm.grid.sara.nl/pnfs/grid.sara.nl/data/bbmri.nl/batch_x\n";
print " * If the transfer mode is c2g, a grid dir on an SRM.\n";
print " * If the transfer mode is g2c, an absolute dir on a local mount.\n";
print " -p [file] Proxy certificate (your cert).\n";
print " -c [dir] Certificate directory containing all the grid-security certs (certs from other machines in the grid).\n";
print " -l [LEVEL] Log4perl log level. One of: ALL, TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL or OFF.\n";
print "\n";
exit;
}