Skip to content

Commit c335396

Browse files
Add/sif variable for singularity (#553)
* singularity modulefile templates: add SINGULARITY_SIF shell variable * singularity templates: SINGULARITY_CONTAINER variable, |module|-container function * docs: mention for singularity_container and container alias * adding test commands for container alias * try putting quotes around SINGULARITY_CONTAINER * remove extra quotes * sif variable: fixed bug for Csh in lua template * trying to comment echo in test tcsh * testing fix for tcsh sif test * try adding PODMAN_CONTAINER * condition test for container tech Signed-off-by: vsoch <[email protected]> Co-authored-by: Marco De La Pierre <[email protected]>
1 parent d1d1f74 commit c335396

File tree

8 files changed

+66
-12
lines changed

8 files changed

+66
-12
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ jobs:
6161
shell: bash
6262
env:
6363
module_sys: ${{ matrix.module[0] }}
64+
container_tech: ${{ matrix.container_tech }}
6465
run: |
6566
case "${module_sys}" in
6667
lmod)
@@ -101,6 +102,9 @@ jobs:
101102
cat test_output
102103
grep --quiet donuts test_output
103104
105+
python-container > container_output
106+
cat container_output
107+
104108
python-run python --version >test_output
105109
cat test_output
106110
grep --quiet 'Python 3.9.5' test_output
@@ -117,6 +121,7 @@ jobs:
117121
shell: tcsh -e {0}
118122
env:
119123
module_sys: ${{ matrix.module[0] }}
124+
container_tech: ${{ matrix.container_tech }}
120125
run: |
121126
switch ("${module_sys}")
122127
case lmod:
@@ -148,6 +153,9 @@ jobs:
148153
printf "\n\nmodule help ============================================\n"
149154
module help python/3.9.5-alpine
150155
156+
python-container > container_output
157+
cat container_output
158+
151159
python-exec echo donuts >test_output
152160
cat test_output
153161
grep --quiet donuts test_output

docs/getting_started/use-cases.rst

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ loaded, just do:
167167
This module is a singularity container wrapper for python v3.9.2-slim
168168
Commands include:
169169
- python-shell:
170-
singularity shell -s /bin/bash /home/shpc-user/singularity-hpc/modules/python/3.9.2-slim/python-3.9.2-slim- sha256:85ed629e6ff79d0bf796339ea188c863048e9aedbf7f946171266671ee5c04ef.sif
170+
singularity shell -s /bin/bash /home/shpc-user/singularity-hpc/modules/python/3.9.2-slim/python-3.9.2-slim-sha256:85ed629e6ff79d0bf796339ea188c863048e9aedbf7f946171266671ee5c04ef.sif
171171
- python:
172172
singularity exec /home/shpc-user/singularity-hpc/modules/python/3.9.2-slim/python-3.9.2-slim-sha256:85ed629e6ff79d0bf796339ea188c863048e9aedbf7f946171266671ee5c04ef.sif /usr/local/bin/python”)
173173
@@ -192,6 +192,17 @@ singularity recipe) then you can do:
192192
python/3.9.2-slim/module : org.label-schema.usage.singularity.version : 3.7.1-1.el7
193193
194194
195+
If your workflow requires knowledge of the local path to the sif image, this information
196+
can be output by using the "container" suffixed alias:
197+
198+
.. code-block:: console
199+
200+
$ python-container
201+
/home/shpc-user/singularity-hpc/modules/python/3.9.2-slim/python-3.9.2-slim-sha256:85ed629e6ff79d0bf796339ea188c863048e9aedbf7f946171266671ee5c04ef.sif
202+
203+
or equivalently by accessing the value of the **SINGULARITY_CONTAINER** environment variable (or **PODMAN_CONTAINER** for each of Podman and Docker).
204+
205+
195206
Adding Options
196207
--------------
197208

@@ -200,8 +211,8 @@ a container intended for gpu will have a feature: gpu set to true, and this will
200211
it could be the case that you want to define custom options at the time of use.
201212
In this case, you can export the following custom environment variables to add them:
202213

203-
**SINGULARITY_OPTS**: will provide additional options to the base Singularity command, such as ``--debug``
204-
**SINGULARITY_COMMAND_OPTS**: will provide additional options to the command (e.g., exec), such as ``--cleanenv`` or ``--nv``.
214+
**SINGULARITY_OPTS**: will provide additional options to the base Singularity command, such as ``--debug``
215+
**SINGULARITY_COMMAND_OPTS**: will provide additional options to the command (e.g., exec), such as ``--cleanenv`` or ``--nv``.
205216

206217

207218
Custom Images that are Added

docs/getting_started/user-guide.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ Module Names
397397

398398
The setting ``module_name`` is a format string in `Jinja2 <https://jinja.palletsprojects.com/en/3.0.x/>`_
399399
that is used to generate your module command names. For each module, in addition
400-
to aliases that are custom to the module, a set of commands for run, inspect, exec,
401-
and shell are generated. These commands will use the ``module_name`` format string
400+
to aliases that are custom to the module, a set of commands for run, exec,
401+
shell, inspect, and container are generated. These commands will use the ``module_name`` format string
402402
to determine their names. For example, for a python container with the default ``module_name``
403403
of "{{ tool }}" we will derive the following aliases for a Singularity module:
404404

@@ -409,6 +409,7 @@ of "{{ tool }}" we will derive the following aliases for a Singularity module:
409409
python-exec
410410
python-inspect-deffile
411411
python-inspect-runscript
412+
python-container
412413
413414
A container identifier is parsed as follows:
414415

@@ -431,6 +432,7 @@ If you change the format string to ``{{ tool }}-{{ version }}`` you would see:
431432
python-3.9.5-alpine-exec
432433
python-3.9.5-alpine-deffile
433434
python-3.9.5-alpine-runscript
435+
python-3.9.5-alpine-container
434436
435437
436438
And of course you are free to add any string that you wish, e.g., ``plab-{{ tool }}``

shpc/main/modules/templates/docker.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Commands include:
2323
{{ command }} run -i{% if settings.enable_tty %}t{% endif %} -u `id -u`:`id -g` --rm --entrypoint "" {% if settings.environment_file %}--env-file <moduleDir>/{{ settings.environment_file }} {% endif %} {% if settings.bindpaths %}-v {{ settings.bindpaths }} {% endif %}{% if features.home %}-v {{ features.home }} {% endif %} -v ${PWD} -w ${PWD} <container> "$@"
2424
- {|module_name|}-inspect:
2525
{{ command }} inspect <container>
26+
- {|module_name|}-container:
27+
echo "$PODMAN_CONTAINER"
2628
2729
{% if aliases %}{% for alias in aliases %} - {{ alias.name }}:
2830
{{ command }} run -i{% if settings.enable_tty %}t{% endif %} -u `id -u`:`id -g` --rm --entrypoint {{ alias.entrypoint }} {% if settings.environment_file %}--env-file <moduleDir>/{{ settings.environment_file }}{% endif %} {% if settings.bindpaths %}-v {{ settings.bindpaths }} {% endif %}{% if features.home %}-v {{ features.home }} {% endif %}{% if alias.docker_options %}{{ alias.docker_options }} {% endif %} -v ${PWD} -w ${PWD} <container> "{{ alias.args }}" "$@"
@@ -32,6 +34,7 @@ For each of the above, you can export:
3234
3335
- PODMAN_OPTS: to define custom options for {{ command }}
3436
- PODMAN_COMMAND_OPTS: to define custom options for the command
37+
- PODMAN_CONTAINER: the container unique resource identifier
3538
]])
3639

3740
{% include "includes/default_version.lua" %}
@@ -48,6 +51,10 @@ local moduleDir = subprocess("realpath " .. myFileName()):match("(.*[/])") or ".
4851
-- interactive shell to any container, plus exec for aliases
4952
local containerPath = '{{ image }}'
5053

54+
-- service environment variable to access docker URI
55+
setenv("PODMAN_CONTAINER", containerPath)
56+
set_shell_function("{|module_name|}-container", "echo " .. containerPath, "echo " .. containerPath)
57+
5158
local shellCmd = "{{ command }} ${PODMAN_OPTS} run -i{% if settings.enable_tty %}t{% endif %} ${PODMAN_COMMAND_OPTS} -u `id -u`:`id -g` --rm --entrypoint {{ shell }} {% if settings.environment_file %}--env-file " .. moduleDir .. "/{{ settings.environment_file }}{% endif %} {% if settings.bindpaths %}-v {{ settings.bindpaths }} {% endif %}{% if features.home %}-v {{ features.home }} {% endif %} -v ${PWD} -w ${PWD} " .. containerPath
5259
-- execCmd needs entrypoint to be the executor
5360
local execCmd = "{{ command }} ${PODMAN_OPTS} run ${PODMAN_COMMAND_OPTS} -i{% if settings.enable_tty %}t{% endif %} -u `id -u`:`id -g` --rm {% if settings.environment_file %}--env-file " .. moduleDir .. "/{{ settings.environment_file }}{% endif %} {% if settings.bindpaths %}-v {{ settings.bindpaths }} {% endif %}{% if features.home %}-v {{ features.home }} {% endif %} -v ${PWD} -w ${PWD} "

shpc/main/modules/templates/docker.tcl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ proc ModulesHelp { } {
2222
puts stderr " {{ command }} run -i{% if settings.enable_tty %}t{% endif %} -u `id -u`:`id -g` --rm --entrypoint \"\" {% if settings.environment_file %} --env-file <moduleDir>/{{ settings.environment_file }} {% endif %} {% if settings.bindpaths %}-v {{ settings.bindpaths }} {% endif %}{% if features.home %}-v {{ features.home }} {% endif %} -v . -w . <container> \"\$@\""
2323
puts stderr " - {|module_name|}-inspect:"
2424
puts stderr " {{ command }} inspect <container>"
25+
puts stderr " - {|module_name|}-container:"
26+
puts stderr " echo \"\$PODMAN_CONTAINER\""
2527
puts stderr ""
2628
{% if aliases %}{% for alias in aliases %} puts stderr " - {{ alias.name }}:"
2729
puts stderr " {{ command }} run -i{% if settings.enable_tty %}t{% endif %} --rm -u `id -u`:`id -g` --entrypoint {{ alias.entrypoint | replace("$", "\$") }} {% if settings.environment_file %}--settings.environment_file <moduleDir>/{{ settings.environment_file }} {% endif %}{% if settings.bindpaths %}-v {{ settings.bindpaths }} {% endif %}{% if features.home %}-v {{ features.home }} {% endif %}{% if alias.docker_options %}{{ alias.docker_options | replace("$", "\$") }} {% endif %} -v . -w . <container> {{ alias.args | replace("$", "\$") }} \"\$@\""
@@ -31,7 +33,7 @@ proc ModulesHelp { } {
3133
puts stderr ""
3234
puts stderr " - PODMAN_OPTS: to define custom options for {{ command }}"
3335
puts stderr " - PODMAN_COMMAND_OPTS: to define custom options for the command"
34-
36+
puts stderr " - PODMAN_CONTAINER: to define the container unique resource identifier"
3537
}
3638

3739
# Environment - only set if not already defined
@@ -66,6 +68,10 @@ conflict {{ parsed_name.tool }}
6668
{% if aliases %}{% for alias in aliases %}{% if alias.name != parsed_name.tool %}conflict {{ alias.name }}{% endif %}
6769
{% endfor %}{% endif %}
6870

71+
# service environment variable to access full SIF image path
72+
setenv PODMAN_CONTAINER "${containerPath}"
73+
set-alias {|module_name|}-container "echo ${containerPath}"
74+
6975
# interactive shell to any container, plus exec for aliases
7076
set shellCmd "{{ command }} \${PODMAN_OPTS} run \${PODMAN_COMMAND_OPTS} -u `id -u`:`id -g` --rm -i{% if settings.enable_tty %}t{% endif %} --entrypoint {{ shell }} {% if settings.environment_file %}--env-file ${moduleDir}/{{ settings.environment_file }}{% endif %} {% if settings.bindpaths %}-v {{ settings.bindpaths }} {% endif %}{% if features.home %}-v {{ features.home }} {% endif %} -v $workdir -w $workdir ${containerPath}"
7177

shpc/main/modules/templates/singularity.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ help(
99
This module is a singularity container wrapper for {{ name }} v{{ version }}
1010
{% if description %}{{ description }}{% endif %}
1111
12-
Container:
12+
Container (available through variable SINGULARITY_CONTAINER):
1313
1414
- {{ container_sif }}
1515
@@ -25,6 +25,8 @@ Commands include:
2525
singularity inspect -r <container>
2626
- {|module_name|}-inspect-deffile:
2727
singularity inspect -d <container>
28+
- {|module_name|}-container:
29+
echo "$SINGULARITY_CONTAINER"
2830
2931
{% if aliases %}{% for alias in aliases %} - {{ alias.name }}:
3032
singularity exec {% if features.gpu %}{{ features.gpu }} {% endif %}{% if features.home %}-B {{ features.home }} --home {{ features.home }} {% endif %}{% if features.x11 %}-B {{ features.x11 }} {% endif %}{% if settings.environment_file %}-B <moduleDir>/{{ settings.environment_file }}:/.singularity.d/env/{{ settings.environment_file }}{% endif %} {% if settings.bindpaths %}-B {{ settings.bindpaths }} {% endif %}{% if alias.singularity_options %}{{ alias.singularity_options }} {% endif %}<container> {{ alias.command }} "$@"
@@ -34,6 +36,7 @@ For each of the above, you can export:
3436
3537
- SINGULARITY_OPTS: to define custom options for singularity (e.g., --debug)
3638
- SINGULARITY_COMMAND_OPTS: to define custom options for the command (e.g., -b)
39+
- SINGULARITY_CONTAINER: full path to the Singularity Container
3740
]])
3841

3942
{% include "includes/default_version.lua" %}
@@ -50,8 +53,12 @@ setenv("SINGULARITY_SHELL", "{{ settings.singularity_shell }}")
5053
if not os.getenv("SINGULARITY_OPTS") then setenv ("SINGULARITY_OPTS", "") end
5154
if not os.getenv("SINGULARITY_COMMAND_OPTS") then setenv ("SINGULARITY_COMMAND_OPTS", "") end
5255

53-
-- interactive shell to any container, plus exec for aliases
5456
local containerPath = '{{ container_sif }}'
57+
-- service environment variable to access full SIF image path
58+
setenv("SINGULARITY_CONTAINER", containerPath)
59+
set_shell_function("{|module_name|}-container", "echo " .. containerPath, "echo " .. containerPath)
60+
61+
-- interactive shell to any container, plus exec for aliases
5562
local shellCmd = "singularity ${SINGULARITY_OPTS} shell ${SINGULARITY_COMMAND_OPTS} -s {{ settings.singularity_shell }} {% if features.gpu %}{{ features.gpu }} {% endif %}{% if features.home %}-B {{ features.home }} --home {{ features.home }} {% endif %}{% if features.x11 %}-B {{ features.x11 }} {% endif %}{% if settings.environment_file %}-B " .. moduleDir .. "/{{ settings.environment_file }}:/.singularity.d/env/{{ settings.environment_file }}{% endif %} {% if settings.bindpaths %}-B {{ settings.bindpaths }}{% endif %} " .. containerPath
5663
local execCmd = "singularity ${SINGULARITY_OPTS} exec ${SINGULARITY_COMMAND_OPTS} {% if features.gpu %}{{ features.gpu }} {% endif %}{% if features.home %}-B {{ features.home }} --home {{ features.home }} {% endif %}{% if features.x11 %}-B {{ features.x11 }} {% endif %}{% if settings.environment_file %}-B " .. moduleDir .. "/{{ settings.environment_file }}:/.singularity.d/env/{{ settings.environment_file }}{% endif %} {% if settings.bindpaths %}-B {{ settings.bindpaths }}{% endif %} "
5764
local runCmd = "singularity ${SINGULARITY_OPTS} run ${SINGULARITY_COMMAND_OPTS} {% if features.gpu %}{{ features.gpu }} {% endif %}{% if features.home %}-B {{ features.home }} --home {{ features.home }} {% endif %}{% if features.x11 %}-B {{ features.x11 }} {% endif %}{% if settings.environment_file %}-B " .. moduleDir .. "/{{ settings.environment_file }}:/.singularity.d/env/{{ settings.environment_file }}{% endif %} {% if settings.bindpaths %}-B {{ settings.bindpaths }}{% endif %} " .. containerPath

shpc/main/modules/templates/singularity.tcl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ proc ModulesHelp { } {
1111
puts stderr "This module is a singularity container wrapper for {{ name }} v{{ version }}"
1212
{% if description %}puts stderr "{{ description }}"{% endif %}
1313
puts stderr ""
14-
puts stderr "Container:"
14+
puts stderr "Container (available through variable SINGULARITY_CONTAINER):"
1515
puts stderr ""
1616
puts stderr " - {{ container_sif }}"
1717
puts stderr ""
@@ -27,6 +27,8 @@ proc ModulesHelp { } {
2727
puts stderr " singularity inspect -r <container>"
2828
puts stderr " - {|module_name|}-inspect-deffile:"
2929
puts stderr " singularity inspect -d <container>"
30+
puts stderr " - {|module_name|}-container:"
31+
puts stderr " echo \"\$SINGULARITY_CONTAINER\""
3032
puts stderr ""
3133
{% if aliases %}{% for alias in aliases %} puts stderr " - {{ alias.name }}:"
3234
puts stderr " singularity exec {% if features.gpu %}{{ features.gpu }} {% endif %}{% if features.home %}-B {{ features.home | replace("$", "\$") }} --home {{ features.home | replace("$", "\$") }} {% endif %}{% if features.x11 %}-B {{ features.x11 | replace("$", "\$") }} {% endif %}{% if settings.environment_file %}-B <moduleDir>/{{ settings.environment_file }}:/.singularity.d/env/{{ settings.environment_file }}{% endif %} {% if settings.bindpaths %}-B {{ settings.bindpaths }} {% endif %}{% if alias.singularity_options %}{{ alias.singularity_options | replace("$", "\$") }} {% endif %}<container> {{ alias.command | replace("$", "\$") }} \"\$@\""
@@ -36,6 +38,7 @@ proc ModulesHelp { } {
3638
puts stderr ""
3739
puts stderr " - SINGULARITY_OPTS: to define custom options for singularity (e.g., --debug)"
3840
puts stderr " - SINGULARITY_COMMAND_OPTS: to define custom options for the command (e.g., -b)"
41+
puts stderr " - SINGULARITY_CONTAINER: The Singularity (sif) path"
3942

4043
}
4144

@@ -73,6 +76,10 @@ conflict {{ parsed_name.tool }}
7376
# singularity environment variable to set shell
7477
setenv SINGULARITY_SHELL {{ settings.singularity_shell }}
7578

79+
# service environment variable to access full SIF image path
80+
setenv SINGULARITY_CONTAINER "${containerPath}"
81+
set-alias {|module_name|}-container "echo ${containerPath}"
82+
7683
# interactive shell to any container, plus exec for aliases
7784
set shellCmd "singularity \${SINGULARITY_OPTS} shell \${SINGULARITY_COMMAND_OPTS} -s {{ settings.singularity_shell }} {% if features.gpu %}{{ features.gpu }} {% endif %}{% if features.home %}-B {{ features.home | replace("$", "\$") }} --home {{ features.home | replace("$", "\$") }} {% endif %}{% if features.x11 %}-B {{ features.x11 | replace("$", "\$") }} {% endif %}{% if settings.environment_file %}-B ${moduleDir}/{{ settings.environment_file }}:/.singularity.d/env/{{ settings.environment_file }}{% endif %} {% if settings.bindpaths %}-B {{ settings.bindpaths }}{% endif %} ${containerPath}"
7885
set execCmd "singularity \${SINGULARITY_OPTS} exec \${SINGULARITY_COMMAND_OPTS} {% if features.gpu %}{{ features.gpu }} {% endif %}{% if features.home %}-B {{ features.home | replace("$", "\$") }} --home {{ features.home | replace("$", "\$") }} {% endif %}{% if features.x11 %}-B {{ features.x11 | replace("$", "\$") }} {% endif %}{% if settings.environment_file %}-B ${moduleDir}/{{ settings.environment_file }}:/.singularity.d/env/{{ settings.environment_file }}{% endif %} {% if settings.bindpaths %}-B {{ settings.bindpaths }}{% endif %} "

shpc/tests/test_container.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616

1717
def test_pull_gh(tmp_path):
18-
"""Test a singularity container command"""
18+
"""
19+
Test a singularity container command
20+
"""
1921
cli = container.SingularityContainer()
2022

2123
# Test default Singularity pull
@@ -39,7 +41,9 @@ def test_pull_oras(tmp_path):
3941

4042

4143
def test_podman(tmp_path):
42-
"""Test a singularity container command"""
44+
"""
45+
Test a singularity container command
46+
"""
4347
cli = container.PodmanContainer()
4448

4549
# Test default Singularity pull
@@ -50,7 +54,9 @@ def test_podman(tmp_path):
5054

5155

5256
def test_docker(tmp_path):
53-
"""Test a singularity container command"""
57+
"""
58+
Test a singularity container command
59+
"""
5460
cli = container.DockerContainer()
5561

5662
# Test default Singularity pull

0 commit comments

Comments
 (0)