Skip to content

Commit

Permalink
Add support for fusion container custom options
Browse files Browse the repository at this point in the history
This commit add the ability to provide custom container options
where using fusion file system, using the following setting:

docker.fusionOptions = "<CUSTOM OPTIONS>">

When omitted the following default is used

docker.fusionOptions = "--rm --privileged"

Signed-off-by: Paolo Di Tommaso <paolo.ditommaso@gmail.com>
  • Loading branch information
pditommaso committed Dec 26, 2022
1 parent 393a489 commit 56e29d3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ abstract class ContainerBuilder<V extends ContainerBuilder> {
String getImage() { image }

V addRunOptions(String str) {
runOptions.add(str)
if( str )
runOptions.add(str)
return (V)this
}

V addEngineOptions(String str) {
engineOptions.add(str)
if( str )
engineOptions.add(str)
return (V)this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
package nextflow.container

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j

/**
* Models container engine configuration
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
@Slf4j
@CompileStatic
class ContainerConfig extends LinkedHashMap {

Expand Down Expand Up @@ -76,4 +78,20 @@ class ContainerConfig extends LinkedHashMap {
return Boolean.parseBoolean(result.toString())
return false
}

String fusionOptions() {
final result = get('fusionOptions')
return result!=null ? result : defaultFusionOptions()
}

protected String defaultFusionOptions() {
final eng = getEngine()
if( !eng )
return null
if( eng=='docker' || eng=='podman' )
return '--rm --privileged'
// default to null
log.warn "Fusion file system is not supported by '$eng' container engine"
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ class SingularityBuilder extends ContainerBuilder<SingularityBuilder> {

@Override
SingularityBuilder addRunOptions(String str) {
runOptions.add(str)
return this
super.addRunOptions(str)
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ class FusionHelper {
final engine = containerConfig.getEngine()
final containerBuilder = ContainerBuilder.create(engine, containerName)
.addMountWorkDir(false)
.addRunOptions('--rm')
.addRunOptions(containerConfig.fusionOptions())
.params(containerConfig)
.params(privileged: true)

// add fusion env vars
for(Map.Entry<String,String> it : launcher.fusionEnv()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,23 @@ class ContainerConfigTest extends Specification {

}

def 'should get fusion options' () {
when:
def cfg = new ContainerConfig(OPTS)

then:
cfg.fusionOptions() == EXPECTED

where:
OPTS | EXPECTED
[:] | null
[engine:'docker'] | '--rm --privileged'
[engine:'podman'] | '--rm --privileged'
and:
[engine:'docker', fusionOptions:'--cap-add foo']| '--cap-add foo'
[engine:'podman', fusionOptions:'--cap-add bar']| '--cap-add bar'
and:
[engine:'sarus', fusionOptions:'--other'] | '--other'
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class FusionHelperTest extends Specification {
[engine:'docker'] | [:] | 'image:1' | ['echo', 'hello'] | ["sh", "-c", "docker run -i --rm --privileged image:1 echo 'hello'"]
[engine:'docker'] | [FOO:'one'] | 'image:2' | ['echo', 'hello'] | ["sh", "-c", "docker run -i -e \"FOO=one\" --rm --privileged image:2 echo 'hello'"]
and:
[engine:'singularity'] | [:] | 'image:1' | ['echo', 'hello'] | ["sh", "-c", "set +u; env - PATH=\"\$PATH\" \${TMP:+SINGULARITYENV_TMP=\"\$TMP\"} \${TMPDIR:+SINGULARITYENV_TMPDIR=\"\$TMPDIR\"} singularity exec --rm image:1 echo 'hello'"]
[engine:'singularity'] | [FOO:'one'] | 'image:1' | ['echo', 'hello'] | ["sh", "-c", "set +u; env - PATH=\"\$PATH\" \${TMP:+SINGULARITYENV_TMP=\"\$TMP\"} \${TMPDIR:+SINGULARITYENV_TMPDIR=\"\$TMPDIR\"} SINGULARITYENV_FOO=one singularity exec --rm image:1 echo 'hello'"]
[engine:'singularity'] | [:] | 'image:1' | ['echo', 'hello'] | ["sh", "-c", "set +u; env - PATH=\"\$PATH\" \${TMP:+SINGULARITYENV_TMP=\"\$TMP\"} \${TMPDIR:+SINGULARITYENV_TMPDIR=\"\$TMPDIR\"} singularity exec image:1 echo 'hello'"]
[engine:'singularity'] | [FOO:'one'] | 'image:1' | ['echo', 'hello'] | ["sh", "-c", "set +u; env - PATH=\"\$PATH\" \${TMP:+SINGULARITYENV_TMP=\"\$TMP\"} \${TMPDIR:+SINGULARITYENV_TMPDIR=\"\$TMPDIR\"} SINGULARITYENV_FOO=one singularity exec image:1 echo 'hello'"]

}

Expand Down

0 comments on commit 56e29d3

Please sign in to comment.