| 1 |
# |
| 2 |
# siteconfig.tcl - Site specific configuration script that copies, the first |
| 3 |
# time a module tagged 'remote' is loaded, its application directory from |
| 4 |
# a remote network share to local disk with rsync, so this and every later |
| 5 |
# load of the same module read from local disk instead of the network |
| 6 |
# share. |
| 7 |
# |
| 8 |
# Author: Xavier Delaruelle <[email protected]> |
| 9 |
# Compatibility: Modules v5.7+ |
| 10 |
# |
| 11 |
# Installation: put this file in the 'etc' directory of your Modules |
| 12 |
# installation. Refer to the "Modulecmd startup" section in the |
| 13 |
# module(1) man page to get this location. |
| 14 |
|
| 15 |
# root of the mounted network share and of its local counterpart |
| 16 |
set g_remoteAppDir /remote_apps |
| 17 |
set g_localAppDir /local_apps |
| 18 |
|
| 19 |
# return the application directory basename mapped to given bare module name |
| 20 |
# and version, or an empty string if this module has no mapped directory |
| 21 |
proc getAppDirBasename {modname} { |
| 22 |
set mapfile [file join $::g_remoteAppDir .module_appdir_map] |
| 23 |
if {![file readable $mapfile]} { |
| 24 |
return {} |
| 25 |
} |
| 26 |
set fid [open $mapfile r] |
| 27 |
set fdata [split [read $fid] "\n"] |
| 28 |
close $fid |
| 29 |
foreach fline $fdata { |
| 30 |
if {[llength $fline] == 2 && [lindex $fline 0] eq $modname} { |
| 31 |
return [lindex $fline 1] |
| 32 |
} |
| 33 |
} |
| 34 |
return {} |
| 35 |
} |
| 36 |
|
| 37 |
# copy application directory from the remote network share to local disk, on |
| 38 |
# the first load of a module tagged 'remote' (see the modulepath root |
| 39 |
# .modulerc file for how this tag gets applied) |
| 40 |
proc syncRemoteAppDir {modfile modname modnamevr modspec mode requested} { |
| 41 |
if {$mode ne {load}} { |
| 42 |
return |
| 43 |
} |
| 44 |
set itrp [getCurrentModfileInterpName] |
| 45 |
if {![interp eval $itrp {module-info tags remote}]} { |
| 46 |
return |
| 47 |
} |
| 48 |
|
| 49 |
set appdir [getAppDirBasename $modname] |
| 50 |
if {$appdir eq {}} { |
| 51 |
return |
| 52 |
} |
| 53 |
|
| 54 |
set syncedfile [file join $::g_localAppDir ".$appdir.synced"] |
| 55 |
if {[file exists $syncedfile]} { |
| 56 |
return |
| 57 |
} |
| 58 |
|
| 59 |
report "Syncing '$appdir' application directory from remote share..." |
| 60 |
file mkdir $::g_localAppDir |
| 61 |
set srcdir [file join $::g_remoteAppDir $appdir] |
| 62 |
set destdir [file join $::g_localAppDir $appdir] |
| 63 |
if {[catch {exec rsync -a --delete $srcdir/ $destdir/} errMsg]} { |
| 64 |
reportError "Failed to sync '$appdir' from remote share\n$errMsg" |
| 65 |
return |
| 66 |
} |
| 67 |
|
| 68 |
# mark this application directory as synced so it does not get copied |
| 69 |
# again on a later load |
| 70 |
close [open $syncedfile w] |
| 71 |
} |
| 72 |
add-hook before-modulefile-eval syncRemoteAppDir |
| 73 |
|
| 74 |
# vim:set tabstop=3 shiftwidth=3 expandtab autoindent: |