Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hook for custom apptainer environment #28845

Open
wants to merge 3 commits into
base: next
Choose a base branch
from

Conversation

dschwen
Copy link
Member

@dschwen dschwen commented Oct 14, 2024

Reason

I'm missing my aliases in the apptainer shells.

Design

Add an apptainer env script that sources $HOME/.bashrc_apptainer if it exists.

Impact

Permit users to set up familiar environments in apptainer containers.

@dschwen dschwen marked this pull request as draft October 14, 2024 16:12
@dschwen dschwen marked this pull request as ready for review October 14, 2024 16:12
@milljm
Copy link
Member

milljm commented Oct 14, 2024

I can address this in #28813

Copy link
Member

@loganharbour loganharbour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to bump the whole conda stack due to changing mpi.def (which is versioned alongside moose-mpi)

@@ -0,0 +1,3 @@
if [ -e $HOME/.bashrc_apptainer ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do a chmod +x on this file if you haven't. and while you're at it, do one on 01-fix-exec.sh too because I think we forgot it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x is not really necessary for source, but I can make that change

@milljm
Copy link
Member

milljm commented Oct 14, 2024

Can you try the following, see if alias work:

apptainer shell -B /path/to/your/.bashrc_apptainer:/init_env oras://theimage

I am finding many things don't work by simply sourcing the file, as you are suggesting to do.

If the above works, then this feature is already present. We just need to be more clear as to its availability.

Edit:
Seems to work for me.

Edit2:
Seems to only work in the lowest level container (rocky:8.9-0). So, the test I performed here should be ignored. It does not work for the rest of our containers.

@dschwen
Copy link
Member Author

dschwen commented Oct 14, 2024

Can you try the following, see if alias work:

apptainer shell -B /path/to/your/.bashrc_apptainer:/init_env oras://theimage

I'm using vscode_container, so unless that script passes through cli args, this script would need to be patched.

@dschwen
Copy link
Member Author

dschwen commented Oct 14, 2024

I am finding many things don't work by simply sourcing the file, as you are suggesting to do.

Why wouldn't that work?!

@moosebuild
Copy link
Contributor

Job Precheck, step Conda build config check on 3b38a76 wanted to post the following:

A change of the following file(s) triggered this check:

conda/peacock

The following file(s) are unchanged:

conda/mpi/conda_build_config.yaml

The conda build config was not changed but one or more of its dependencies have changed

@milljm
Copy link
Member

milljm commented Oct 14, 2024

I am finding many things don't work by simply sourcing the file, as you are suggesting to do.

Why wouldn't that work?!

Here has been my experience:

$ apptainer shell -B /home/milljm/.testing:/init_env oras://mooseharbor.hpc.inl.gov/moose-dev/moose-dev-mpich-x86_64:28a1964
INFO:    Using cached SIF image
moose-dev-mpich-x86_64:28a1964 [milljm@rod: ~ ]
$ cat /init_env 
function my_test(){
  echo 'hello from my_test function'
}
alias jas='ls -latrh'

So the file exists inside the container well enough. But...

moose-dev-mpich-x86_64:28a1964 [milljm@rod: ~ ]
$ my_test 
bash: my_test: command not found
moose-dev-mpich-x86_64:28a1964 [milljm@rod: ~ ]
$ jas
bash: jas: command not found

no dice. Not until I source it again

moose-dev-mpich-x86_64:28a1964 [milljm@rod: ~ ]
$ . /init_env 
moose-dev-mpich-x86_64:28a1964 [milljm@rod: ~ ]
$ my_test
hello from my_test function
moose-dev-mpich-x86_64:28a1964 [milljm@rod: ~ ]
$ jas
total 195M
-rw-------  1 milljm milljm  256 Sep 18  2013 .pulse-cookie
<trimmed>

All the while we do seem to be doing it:

moose/apptainer/mpi.def

Lines 52 to 55 in a35543b

# Load a custom environment if it exists
if [ -e /init_env ]; then
source /init_env
fi

Perhaps what you are doing may work. As I have been experiencing different results as to when this file gets sourced.

@milljm
Copy link
Member

milljm commented Oct 14, 2024

I am trying what you are suggesting, and it won't work. My PR I am working on should fix this. But simply "sourcing" files doesn't seem to be enough. Don't ask me why, lol.

editing the atrocious PS prompt...

$ apptainer shell moose-dev-mpich-x86_64_28a1964.sifw 
$ jas
bash: jas: command not found

$ my_test
bash: my_test: command not found

$ echo $MYVAR  # exports work!?
foobar

$ cat /.singularity.d/env/02-bashrc-apptainer.sh 
if [ -e $HOME/.bashrc_apptainer ]; then
    source $HOME/.bashrc_apptainer
fi

$ cat $HOME/.bashrc_apptainer
function my_test(){
  echo 'hello from my_test function'
}
alias jas='ls -latrh'
export MYVAR=foobar

$ . /.singularity.d/env/02-bashrc-apptainer.sh  # sourcing it again works. 🤷 
$ my_test
hello from my_test function

$ jas
total 2.2G
<trimmed>
drwxr-xr-x  3 milljm milljm   60 Oct 14 12:56 ..
$ 

@dschwen
Copy link
Member Author

dschwen commented Oct 14, 2024

Hm... I suppose that env stuff is not sourced in my current shell, and aliases are not inherited. But why is the bash not sourcing my .bashrc then?

@dschwen dschwen closed this Oct 14, 2024
@dschwen dschwen reopened this Oct 14, 2024
@moosebuild
Copy link
Contributor

Job Precheck, step Conda build config check on e44885a wanted to post the following:

A change of the following file(s) triggered this check:

conda/peacock

The following file(s) are unchanged:

conda/mpi/conda_build_config.yaml

The conda build config was not changed but one or more of its dependencies have changed

@loganharbour
Copy link
Member

Isn't this due to the --norc option or whatever we pass bash?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants