| 1 |
.. _modulepaths-persist-over-sudo: |
| 2 |
|
| 3 |
Make defined modulepaths persist over sudo |
| 4 |
========================================== |
| 5 |
|
| 6 |
When running a command as another user with ``sudo``, current user environment |
| 7 |
is most of the time flushed for security concerns. As a result, if one would |
| 8 |
like to use the ``modulecmd.tcl`` script in such context, an error is returned |
| 9 |
as ``modulecmd.tcl`` does not find modulepath defined (:envvar:`MODULEPATH` |
| 10 |
variable is not set). Following recipe describes how to ensure the default |
| 11 |
modulepaths are set every time the ``modulecmd.tcl`` script is run. |
| 12 |
|
| 13 |
Implementation |
| 14 |
-------------- |
| 15 |
|
| 16 |
Every time the ``modulecmd.tcl`` script is run, it evaluates during its |
| 17 |
start-up phase a global RC file following the same kind of evaluation than for |
| 18 |
modulefiles. |
| 19 |
|
| 20 |
To ensure modulepaths are always defined, a check could be added in this |
| 21 |
global RC file to verify at least one modulepath is set (thanks to the |
| 22 |
:mfcmd:`is-used` Tcl modulefile command). If no modulepath is found set, the |
| 23 |
``.modulespath`` configuration file, which contains the default modulepaths, |
| 24 |
can be parsed to enable on the fly the default modulepaths (with ``module |
| 25 |
use`` Tcl modulefile command). |
| 26 |
|
| 27 |
.. literalinclude:: ../../example/modulepaths-persist-over-sudo/rc |
| 28 |
:language: tcl |
| 29 |
:caption: Global RC file |
| 30 |
|
| 31 |
**Compatible with Modules v4.1+** |
| 32 |
|
| 33 |
Installation |
| 34 |
------------ |
| 35 |
|
| 36 |
Copy the global RC file of this recipe in the configuration directory: |
| 37 |
|
| 38 |
.. parsed-literal:: |
| 39 |
|
| 40 |
$ cp example/modulepaths-persist-over-sudo/rc \ |etcdir|\ / |
| 41 |
|
| 42 |
The location of the ``.modulespath`` file defined in the proposed RC script |
| 43 |
should be adapted to reflect the location where this file is installed on your |
| 44 |
setup. |
| 45 |
|
| 46 |
Usage example |
| 47 |
------------- |
| 48 |
|
| 49 |
Without the proposed RC file installed, :envvar:`MODULEPATH` environment |
| 50 |
variable is lost through the ``sudo`` call:: |
| 51 |
|
| 52 |
$ sudo $MODULES_CMD bash avail >/dev/null |
| 53 |
ERROR: No module path defined |
| 54 |
|
| 55 |
Once RC file is installed, flushed ``MODULEPATH`` is restored on the fly based |
| 56 |
on ``.modulespath`` configuration file:: |
| 57 |
|
| 58 |
$ sudo $MODULES_CMD bash use >/dev/null |
| 59 |
Search path for module files (in search order): |
| 60 |
/usr/share/Modules/modulefiles |
| 61 |
/etc/modulefiles |
| 62 |
/usr/share/modulefiles |
| 63 |
|
| 64 |
Thus available modulefiles are found again:: |
| 65 |
|
| 66 |
$ sudo $MODULES_CMD bash avail >/dev/null |
| 67 |
--------------- /usr/share/Modules/modulefiles --------------- |
| 68 |
dot module-git module-info modules null use.own |
| 69 |
|
| 70 |
.. vim:set tabstop=2 shiftwidth=2 expandtab autoindent: |