Repository: easybuilders/easybuild-framework

Issue:
singularity def issue with switching to easybuild user to build eb package
Getting weird issue where singularity is creating the user ``easybuild`` but it is not switching to ``easybuild`` user when reaching ``eb <easyconfig>.eb --robot`` line. 

As you can see i ran ``id`` and ``tail /etc/passwd`` and seems also issue with way log output is streamed but easybuild is uid 1000

```
id
+ id
tail /etc/passwd | grep easybuild
+ grep easybuild
+ tail /etc/passwd
# use EasyBuild to install specified software
eb binutils-2.28.eb --robot
+ eb binutils-2.28.eb --robot
easybuild:x:1000:1000::/home/easybuild:/bin/bash
ERROR: You seem to be running EasyBuild with root privileges which is not wise, so let's end this here.
== temporary log file in case of crash /scratch/tmp/eb-CxVXMk/easybuild-lUdmUu.log
FATAL:   failed to execute %post proc: exit status 1
FATAL:   While performing build: while running engine: while running /usr/local/libexec/singularity/bin/starter: exit status 255
```

This seemed to happen all of a sudden. I tried testing this in version 3.4.x and 3.5.x of singularity and got same issue. Right now i am using eb version 4.0.x 


[Singularity.binutils-2.28.txt](https://github.com/easybuilders/easybuild-framework/files/4100862/Singularity.binutils-2.28.txt)

[build (1).log](https://github.com/easybuilders/easybuild-framework/files/4100849/build.1.log)



Discussion:

switching to easybuild user is not working

```
# use EasyBuild to install specified software
eb Anaconda3-5.3.0.eb --robot
+ eb Anaconda3-5.3.0.eb --robot
/bin/eb: line 77: which: command not found
ERROR: You seem to be running EasyBuild with root privileges which is not wise, so let's end this here.
== temporary log file in case of crash /scratch/tmp/eb-6A5vDL/easybuild-4_ubs8.log
FATAL:   failed to execute %post proc: exit status 1
FATAL:   While performing build: while running engine: while running /usr/local/libexec/singularity/bin/starter: exit status 255
FATAL:   While performing build: build image size <= 0
```

The workaround is to change the following code line https://github.com/easybuilders/easybuild-framework/blob/bf663f70e44256644f4adc5bcbfc7d1a3fbc1232/easybuild/tools/containers/singularity.py#L98

to 

```
su - easybuild << 'EOF'
```

And replace the exit statement https://github.com/easybuilders/easybuild-framework/blob/bf663f70e44256644f4adc5bcbfc7d1a3fbc1232/easybuild/tools/containers/singularity.py#L126

to
```
EOF
```

I wont bother making the change now @boegel  as other things will need to be replaced too.
I stumbled into the same issue.
The solution @shahzebsiddiqui provided works and it is a viable option IMHO.

Resolving patch:
diff --git a/easybuild/tools/containers/singularity.py b/easybuild/tools/containers/singularity.py
--- a/easybuild/tools/containers/singularity.py
+++ b/easybuild/tools/containers/singularity.py
@@ -94,8 +94,9 @@
 }
 EOF
 
-# change to 'easybuild' user
-su - easybuild
+# switch to 'easybuild' user for following commands
+# quotes around EOF delimiter are important to ensure environment variables are not expanded prematurely!
+su - easybuild << 'EOF'
 
 # verbose commands, exit on first error
 set -ve
@@ -122,8 +123,8 @@
 mkdir -p /app/lmodcache
 $LMOD_DIR/update_lmod_system_cache_files -d /app/lmodcache -t /app/lmodcache/timestamp /app/modules/all
 
-# exit from 'easybuild' user
-exit
+# end of set of commands to run as 'easybuild' user
+EOF
 
 # cleanup, everything in /scratch is assumed to be temporary
 rm -rf /scratch/*


Test patch: