GC3: Grid Computing Competence Center

Blog index

GC3 graudates into S3IT
Posted early Tuesday morning, July 1st, 2014
How to create a module that also load a virtualenvironment
Posted late Friday morning, March 7th, 2014
Openstack workshop at GC3
Posted at noon on Saturday, February 22nd, 2014
Moving LVM volumes used by a Cinder storage
Posted late Friday evening, February 21st, 2014
How to configure swift glusterfs
Posted Monday night, February 10th, 2014
Yet another timeout problem when starting many instances at once
Posted late Friday night, February 8th, 2014
Fixing LDAP Authentication over TLS/SSL
Posted Monday night, January 6th, 2014
Linker command-line options for Intel MKL
Posted Saturday night, January 4th, 2014
A virtue of lazyness
Posted Saturday afternoon, December 21st, 2013
(Almost) readable CFEngine logs
Posted Thursday afternoon, December 19th, 2013
CFEngine error: ExpandAndMapIteratorsFromScalar called with invalid strlen
Posted Wednesday afternoon, December 11th, 2013
'Martian source' log messages and the default IP route
Posted Monday afternoon, November 25th, 2013
GC3 takes over maintenance of the Schroedinger cluster
Posted at noon on Monday, November 4th, 2013
Grid Engine: how to find the set of nodes that ran a job (after it's finished)
Posted early Wednesday morning, October 30th, 2013
Python2 vs Python3
Posted at teatime on Friday, September 13th, 2013
GC3Pie 2.1.1 released
Posted Friday evening, September 6th, 2013
Happy SysAdmin day!
Posted mid-morning Friday, July 26th, 2013
Object-oriented Python training
Posted Thursday afternoon, July 25th, 2013
Elasticluster 1.0.0 released
Posted Thursday night, July 18th, 2013
Short Autotools tutorial
Posted at lunch time on Friday, July 5th, 2013
Patch Emacs' PostScript printing
Posted Tuesday evening, June 11th, 2013
Slides of the Object-oriented Python course now available!
Posted Tuesday evening, June 11th, 2013
Automated deployment of CFEngine keys
Posted at midnight, May 31st, 2013
blog/Resize_an_image
Posted Tuesday evening, May 14th, 2013
Join us at the Compute Cloud Experience Workshop!
Posted early Monday morning, April 29th, 2013
GC3 Beamer theme released
Posted at lunch time on Friday, April 5th, 2013
VM-MAD at the International Supercompting Conference 2013
Posted at lunch time on Tuesday, March 26th, 2013
The GC3 is on GitHub
Posted at lunch time on Monday, March 18th, 2013
How to enable search in IkiWiki
Posted Friday afternoon, March 15th, 2013
GC3Pie Training
Posted Thursday night, March 7th, 2013
Object-oriented Python training
Posted Thursday afternoon, March 7th, 2013
Advance Reservations in GridEngine
Posted late Thursday morning, March 7th, 2013
GridEngine accounting queries with PostgreSQL
Posted Wednesday night, March 6th, 2013
Floating IPs not available on Hobbes
Posted at teatime on Tuesday, February 26th, 2013
Notes on SWIFT
Posted mid-morning Tuesday, February 12th, 2013
An online Python code quality analyzer
Posted at lunch time on Saturday, February 9th, 2013
Seminar on cloud infrastructure
Posted Sunday night, February 3rd, 2013
GC3 announce its cloud infrastructure Hobbes
Posted Wednesday afternoon, January 30th, 2013
GC3Pie 2.0.2 released
Posted Monday afternoon, January 28th, 2013
Continuous Integration with Jenkins
Posted at noon on Saturday, January 26th, 2013
On the importance of testing in a clean environment
Posted mid-morning Monday, January 21st, 2013
Weirdness with ImageMagick's `convert`
Posted at teatime on Tuesday, January 15th, 2013
boto vs libcloud
Posted Tuesday afternoon, January 15th, 2013
Resolve timeout problem when starting many instances at once
Posted at lunch time on Monday, January 7th, 2013
Proceedings of the EGI Community Forum 2012 published
Posted at teatime on Monday, December 17th, 2012
SGE Workaround Installation
Posted at lunch time on Tuesday, December 4th, 2012
How to pass an argument of list type to a CFEngine3 bundle
Posted mid-morning Thursday, November 22nd, 2012
GC3 at the 'Clouds for Future Internet' workshop
Posted mid-morning Wednesday, November 21st, 2012
GC3 attends European Commission Cloud Expert Group
Posted mid-morning Monday, October 29th, 2012
SwiNG - SDCD2012 event
Posted at lunch time on Monday, October 22nd, 2012
Large Scale Computing Infrastructures class starts tomorrow!
Posted late Tuesday afternoon, September 25th, 2012
From bare metal to cloud at GC3
Posted mid-morning Monday, September 24th, 2012
GC3 at the EGI Technical Forum 2012
Posted Thursday night, September 20th, 2012
Training on GC3Pie and Python
Posted late Friday evening, September 7th, 2012
GC3Pie used for research in Computational Quantum Chemistry
Posted late Thursday afternoon, September 6th, 2012
``What's so great about MPI or Boost.MPI?''
Posted mid-morning Thursday, September 6th, 2012
blog/How to generate UML diagram with `pyreverse`
Posted late Thursday morning, August 23rd, 2012
Git's `rebase` command
Posted mid-morning Friday, June 15th, 2012
AppPot 0.27 released!
Posted at noon on Thursday, June 14th, 2012
Urban computing - connecting to your server using `mosh`
Posted mid-morning Wednesday, June 6th, 2012
Whitespace cleanup with Emacs
Posted Tuesday afternoon, June 5th, 2012
Translate pages on this site
Posted Thursday evening, May 31st, 2012
Scientific paper citing GC3Pie
Posted Wednesday evening, May 30th, 2012
GC3 attends Nordugrid 2012 conference
Posted at lunch time on Wednesday, May 30th, 2012
How the front page image was made
Posted late Wednesday evening, May 16th, 2012
GC3 blog launched!
Posted late Tuesday evening, May 15th, 2012
New GC3 Wiki now online!
Posted Tuesday evening, May 15th, 2012
AppPot paper on arXiv
Posted Tuesday evening, May 15th, 2012
GC3 at the EGI Technical Forum 2011
Posted Tuesday evening, May 15th, 2012

More on topic...

How to create a module that also load a virtualenvironment

Modules is a software to dynamically modify the environment of the current shell. It is widely deployed in computational clusters (including the Schroedinger and Calvin cluster we run), to allow the cohabitation of multiple versions of the same software, or multiple softwares that cannot be used both at the same time (for instance, two incompatible MPI implementations).

Virtual environment is a tool to create isolated Python environment, and we use it to install GC3Pie without root privileges and avoiding any possible conflict between libraries needed by GC3Pie and the operating system.

Yesterday we wanted to create a module to load a specific GC3Pie environment we provided for all the users, but this happen to be a bit more complex than it seems.

How virtualenv work

Virtualenv creates a directory with a set of symlinks and files, including a script in `bin/activate. To enable the environment, you have to source the script from within the shell:

. <path/to/virtualenv>/bin/activate

This will change the PATH, LD_LIBRARY_PATH PYTHONPATH and other relevant variable in order to use all the packages contained in the virtualenv directory. It also define a shell function deactivate which is used to unload the environment, and revert back the value of the variables to before activating the environment.

How modules work

Modules is a combination of a shell function, a binary program and a Tcl script which is used to define the environment to load and unload.

The definition of the module function is very simple:

module () 
{ 
    eval `/usr/bin/modulecmd bash $*`
}

it basically call the modulecmd command and evaluate its output.

When running module load <modulename>, modulecmd will interpret the Tcl script (called modulefile) corresponding to the <modulename> file and will print in the standard output the corresponding declaration of variables. For instance, if you want to add a directory /foo/bar/bin to the PATH environment variable, you only have to add the following in the modulefile:

prepend-path PATH /foo/bar/bin

Modules will also take care of removing the directory when module unload <modulename> is called.

The problem is that modulefiles do not allow you to explicitly load the content of a shell file when load is called.

There is a system keyword used to call an external program, but since the command is executed in a subshell it cannot be used to load the environment.

Solution

Keeping in mind the module function definition and the fact that modulefiles are basically Tcl script, the solution is quite easy. Just put:

puts stdout ". <virtualenv>/bin/activate;"

into the modulefile script (please note the ; at the end, it's needed because the output of modulecmd is in one single line).

This will write to standard output the string . <virtualenv>/bin/activate; and then this will be valuated by the shell, which is what we wanted right?

Almost, because the modulefile is always interpreted, although some of the functions are only called when load subcommand is issued. Also, we would like to call deactivate when the module is loaded.

Luckly, modulefile syntax also allow for standard Tcl conditionals and also defines a few functions, including module-info to check the subcommand issued (load, unload, switch etc). The final solution is therefore:

if { [ module-info mode load ] } {
  puts stdout ". /site/apps/gc3pie-dev/bin/activate;"
} elseif { [ module-info mode remove ] } {
  puts stdout "deactivate;"
}

--AM

top