-
Notifications
You must be signed in to change notification settings - Fork 600
/
AwsBatchFileCopyStrategy.groovy
166 lines (145 loc) · 5.08 KB
/
AwsBatchFileCopyStrategy.groovy
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
/*
* Copyright 2020, Seqera Labs
* Copyright 2013-2019, Centre for Genomic Regulation (CRG)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nextflow.cloud.aws.batch
import java.nio.file.Path
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import nextflow.executor.SimpleFileCopyStrategy
import nextflow.processor.TaskBean
import nextflow.util.Escape
/**
* Defines the script operation to handle file when running in the Cirrus cluster
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
@Slf4j
@CompileStatic
class AwsBatchFileCopyStrategy extends SimpleFileCopyStrategy {
private AwsOptions opts
private Map<String,String> environment
AwsBatchFileCopyStrategy(TaskBean task, AwsOptions opts ) {
super(task)
this.opts = opts
this.environment = task.environment
}
/**
* @return A script snippet that download from S3 the task scripts:
* {@code .command.env}, {@code .command.sh}, {@code .command.in},
* etc.
*/
String getBeforeStartScript() {
S3Helper.getUploaderScript(opts)
}
/**
* {@inheritDoc}
*/
@Override
String getEnvScript(Map environment, boolean container) {
if( container )
throw new IllegalArgumentException("Parameter `container` not supported by ${this.class.simpleName}")
final result = new StringBuilder()
final copy = environment ? new HashMap<String,String>(environment) : Collections.<String,String>emptyMap()
final path = copy.containsKey('PATH')
// remove any external PATH
if( path )
copy.remove('PATH')
// when a remote bin directory is provide managed it properly
if( opts.remoteBinDir ) {
result << "${opts.getAwsCli()} s3 cp --recursive --only-show-errors s3:/${opts.remoteBinDir} \$PWD/nextflow-bin\n"
result << "chmod +x \$PWD/nextflow-bin/*\n"
result << "export PATH=\$PWD/nextflow-bin:\$PATH\n"
}
// finally render the environment
final envSnippet = super.getEnvScript(copy,false)
if( envSnippet )
result << envSnippet
return result.toString()
}
@Override
String getStageInputFilesScript(Map<String,Path> inputFiles) {
def result = 'downloads=()\n'
result += super.getStageInputFilesScript(inputFiles) + '\n'
result += 'nxf_parallel "${downloads[@]}"\n'
return result
}
/**
* {@inheritDoc}
*/
@Override
String stageInputFile( Path path, String targetName ) {
// third param should not be escaped, because it's used in the grep match rule
def stage_cmd = opts.maxTransferAttempts > 1
? "downloads+=(\"nxf_cp_retry nxf_s3_download s3:/${Escape.path(path)} ${Escape.path(targetName)}\")"
: "downloads+=(\"nxf_s3_download s3:/${Escape.path(path)} ${Escape.path(targetName)}\")"
return stage_cmd
}
/**
* {@inheritDoc}
*/
@Override
String getUnstageOutputFilesScript(List<String> outputFiles, Path targetDir) {
// collect all the expected names (pattern) for files to be un-staged
def result = []
def normalized = normalizeGlobStarPaths(outputFiles)
// create a bash script that will copy the out file to the working directory
log.trace "[AWS BATCH] Unstaging file path: $normalized"
if( normalized ) {
result << 'uploads=()'
normalized.each {
result << "uploads+=(\"nxf_s3_upload '${Escape.path(it)}' s3:/${Escape.path(targetDir)}\")" // <-- add true to avoid it stops on errors
}
result << 'nxf_parallel "${uploads[@]}"'
}
return result.join(separatorChar)
}
/**
* {@inheritDoc}
*/
@Override
String touchFile( Path file ) {
final aws = opts.getAwsCli()
"echo start | $aws s3 cp --only-show-errors - s3:/${Escape.path(file)}"
}
/**
* {@inheritDoc}
*/
@Override
String fileStr( Path path ) {
Escape.path(path.getFileName())
}
/**
* {@inheritDoc}
*/
@Override
String copyFile( String name, Path target ) {
"nxf_s3_upload ${Escape.path(name)} s3:/${Escape.path(target.getParent())}"
}
/**
* {@inheritDoc}
*/
String exitFile( Path path ) {
final aws = opts.getAwsCli()
"| $aws s3 cp --only-show-errors - s3:/${Escape.path(path)} || true"
}
/**
* {@inheritDoc}
*/
@Override
String pipeInputFile( Path path ) {
" < ${Escape.path(path.getFileName())}"
}
}