modmanage.in 8.38 KB
Newer Older
1
#!@PMODULES_HOME@/bin/bash
2

3
shopt -s expand_aliases
4

5 6
declare -r bindir=$(cd $(dirname "$0") && pwd)
declare -r libdir=$(cd "${bindir}/../lib" && pwd)
7

8
source "${libdir}/libpmodules.bash"
9 10 11 12 13 14

print_version() {
	echo "
Pmodules ${version} using Tcl Environment Modules @MODULES_VERSION@
Copyright GNU GPL v2
" 1>&2
15 16
}

17 18 19 20 21 22 23 24 25 26 27
usage() {
	local -r prog=$(basename $0)
	print_version
	echo "
Usage: ${prog} [ switches ] [ subcommand ] [subcommand-args ]

Switches:
        --dry-run     do nothing
        --force       force overwrite

Available SubCommands and Args:
28
        init [--src=<src>] [--user=<user>] <dst>
29
                Initialize a new minimal Pmodule environment.
30 31 32

        install <module> [--with=<dep>...]
                Install matching modules
33 34 35
"
}
     
36 37

declare force='no'
38 39
declare dry_run='no'
declare DRY=''
40 41
declare subcommand=''
declare sargs=()
42

43 44 45 46 47 48 49 50
subcommand_help_init() {
	echo "
init [--src=<src>] [--user=<user>] <dst>
                Initialize a new minimal Pmodule environment in directory
                <dst>. The <user> parameter must only be present if
                 ${prog} is executed as root.
" 1>&2
}
51

52 53 54 55 56 57 58
subcommand_help_install() {
	echo "
install <module> [--with=<dep>...]
                Install matching modules
" 1>&2
}

59 60 61 62 63 64 65 66 67
subcommand_help() {
	if [[ $# == 0 ]]; then
	        usage
	elif typeset -F subcommand_help_$1 > /dev/null 2>&1 ; then
	        # help for sub-command
		subcommand_help_$1
	else
		usage
	fi
68 69
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#
# Derive the relative module installation path
# from the relative modulefile path
#
# $1: relative module file path
#
get_module_prefix() {
	local -a comp=( ${1//\// } )    # split rel.path into components
	local path="${comp[0]}"		# result path
	local -i i
	for ((i=1; i<${#comp[@]}; i+=2)); do
		path+="/${comp[$((-i-1))]}/${comp[$((-i))]}"
	done
	echo "${path}"
}

#
# Derive the relative module release file path
# from the relative module file path
#
# $1: relative module file path
#
get_releasefile_name() {
	echo "$(dirname "$1")/.release-$(basename "$1")"
}

#
# Sync a module from one Pmodules environment to another:
# - sync module installation
# - sync modulefile
# - sync release file
#
# $1: relative modulefile path
# $2: source prefix of Pmodule environment
# $3: target prefix of Pmodule environment
#
sync_module() {
	local -r rel_modulefile=$1
	local -r src_prefix=$2
	local -r target_prefix=$3

	local -r rel_module_prefix=$( get_module_prefix "${rel_modulefile}" )
	local -r rel_releasefile=$( get_releasefile_name "${rel_modulefile}" )

114
	$DRY mkdir -p "${target_prefix}/${rel_module_prefix}" || return $?
115 116 117
	$DRY rsync --links --perms --recursive --delete \
	      "${src_prefix}/${rel_module_prefix}/" \
	      "${target_prefix}/${rel_module_prefix}/" || return $?
118 119 120 121 122 123 124 125

	local -r src_modulefile="${src_prefix}/${PSI_MODULES_ROOT}/${rel_modulefile}"
	local -r src_releasefile="${src_prefix}/${PSI_MODULES_ROOT}/${rel_releasefile}"
	local -r target_modulefile="${target_prefix}/${PSI_MODULES_ROOT}/${rel_modulefile}"
	local -r target_releasefile="${target_prefix}/${PSI_MODULES_ROOT}/${rel_releasefile}"

	if [[ -e "${src_modulefile}" ]] || [[ -e "${src_releasefile}" ]]; then
	        local dir=$( dirname "${target_modulefile}" )
126
		$DRY mkdir -p "${dir}" || return $?
127 128 129 130 131 132 133 134 135
	fi
	if [[ -e "${src_modulefile}" ]]; then
	        $DRY rsync --links --perms --recursive \
		     "${src_modulefile}" "${target_modulefile}" || return $?
	fi
	if [[ -e "${src_releasefile}" ]]; then
	        $DRY rsync --links --perms --recursive \
		     "${src_releasefile}" "${target_releasefile}" || return $?
	fi
136 137
}

138 139
subcommand_init() {
	local src=''
140
	local target_prefixes=()
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	local user=''
	local opts=''
	opts=$(get_options -o h -l src: -l user: -l help -- "$@")
	if [[ $? != 0 ]]; then
		subcommand_help_init
		exit 1
	fi
	eval set --  "${opts}"
	while (($# > 0)); do
		case $1 in 
			--src )
				src=$2
				shift
				;;
			--user )
				user=$2
				shift
				;;
			-- )
				:
				;;
			-* | -h | --help )
				echo "$1: illegal option" 1>&2
				subcommand_help_init
				exit 1
				;;
			* )
168
				target_prefixes+=( "$1" )
169 170 171 172
				;;
		esac
		shift
	done
173
	(( ${#target_prefixes[@]} != 0 )) || die 1 "Error: no target directory specified!"
174

175 176
	if [[ -n "${src}" ]]; then
		[[ -d "${src}" ]] || die 1 "Error: ${src}: source directory does not exist!"
177 178
		[[ -r "${src}/config/profile.bash" ]] || \
		    die 1 "Error: ${src}: shell profile does not exist or is not readable!"
179 180 181 182
		source "${src}/config/profile.bash"
	fi
	local -i euid=$(id -u)
	if (( euid == 0 )); then
183
	        [[ -n "${user}" ]] || \
184
		    die 1 "Error: --user parameter is required!"
185 186
		id -u "${user}" > /dev/null 2>&1 || \
		    die 1 "Error: Unable to retrieve user id of user '${user}'"
187
	else
188
		[[ -z "${user}" ]] || \
189
		    die 1 "Error: --user option is only allowed if running as root!"
190
	fi
191 192 193
	
	check_pmodules_env || die 1 "Giving up ..."
	
194
	echo "
195 196 197 198
Attempting to create a minimal module environment from the
environment at '${PSI_PREFIX}'
"

199
	init_pmodules_environment() {
200
		local -r src_prefix="${PSI_PREFIX}"
201
		local -r target_prefix=$1
202 203
		local src=''
		local dst=''
204 205 206 207
		echo "Initializing target directory '${target_prefix}' ..."
		echo
		if [[ -d "${target_prefix}" ]]  && [[ ${force} == no ]]; then
			echo "Warning: ${target_prefix} already exists."
208
			get_YN_answer "Do you really want to re-run the initialization? (y/N) " || \
209
				      die 1 "Abort ..."
210
		fi
211

212 213
		echo "Creating target directory '${target_prefix}'..."
		$DRY mkdir -p "${target_prefix}" || die 1 "Error: make directory failed!"
214
		echo
215

216
		src="${src_prefix}/${PSI_CONFIG_DIR}/"
217
		dst="${target_prefix}/${PSI_CONFIG_DIR}/"
218 219 220 221 222
		echo "Synching configuration from '${src}' to '${dst}'..."
		$DRY rsync --recursive --links --perms --delete \
		     "${src}" "${dst}" || die 1 "Error: synch operation failed!"
		echo
		
223
		src="${src_prefix}/${PSI_TEMPLATES_DIR}/"
224
		dst="${target_prefix}/${PSI_TEMPLATES_DIR}/"
225 226 227 228
		echo "Synching template files from '${src}' to '${dst}'..."
		$DRY rsync --recursive --links --perms --delete \
		     "${src}" "${dst}" || die 1 "Error: synch operation failed!"
		echo
229

230 231 232 233 234
		dst="${target_prefix}/${PSI_MODULES_ROOT}/"
		echo "Creating root directory '${dst}' for module hierarchy ..."
		$DRY mkdir -p "${dst}"
		echo

235 236
		echo "Syncing Pmodules ..."
		sync_module "Tools/Pmodules/${PMODULES_VERSION}" \
237
			    "${src_prefix}" \
238
			    "${target_prefix}" || die 1 "Error: sync Pmodules failed!"
239
		echo
240 241 242 243 244 245 246

		dst="${target_prefix}/${PSI_CONFIG_DIR}/environment.bash"
		echo "Adding installation source '${src_prefix}' to '${dst}'..."
		sed -i .bak '/PMODULES_INSTALL_SOURCE/d' "${dst}"
		echo "declare -x PMODULES_INSTALL_SOURCE=\"${src_prefix}\"" >> "${dst}"
		echo

247 248 249 250
		if [[ -n "${user}" ]]; then
	                echo "Changing user of new module environment to '${user}'..."
                        $DRY chown -R "${user}" "${target_prefix}" || die 1 "Error: changing owner failed!"
                        echo
251
		fi
252
		echo "New minimal module environment created at '${target_prefix}'."
253
		echo "To use this environment, execute"
254 255
		echo "   sudo ln -fs ${target_prefix} /opt/psi"
		echo "   source /opt/psi/${PSI_CONFIG_DIR}/profile.bash"
256 257 258
	}

	umask 022
259 260
	for target_prefix in "${target_prefixes[@]}"; do
		init_pmodules_environment "${target_prefix}"
261 262
	done

263 264
}

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
subcommand_install() {
	local opts=''
	local -a with=()
	local -a releases=()
	local -a module_pattern=()

	opts=$(get_options -o h -l with: -l release: -l help -- "$@")
	if [[ $? != 0 ]]; then
		subcommand_help_install
		exit 1
	fi
	eval set --  "${opts}"
	while (($# > 0)); do
		case $1 in
			--release )
				releases+=( "$2" )
				echo $2
				shift
				;;
			--with )
				with+=( "$2" )
				shift
				;;
			-- )
				:
				;;
			-* | -h | --help )
				echo "$1: illegal option" 1>&2
				subcommand_help_init
				exit 1
				;;
			* )
				module_pattern+=( "$1" )
				;;
		esac
		shift
	done
302
	PSI_PREFIX="${PMODULES_INSTALL_SOURCE}" ${PMODULES_HOME}/bin/modulecmd bash search "${module_pattern[@]}" "${with[@]/#/--with}" "${releases[@]/#/--release=}" --no-header
303 304
}

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
while (($# > 0)); do
	case $1 in
		-h | -H | -\? | --help | -help )
			usage
			exit 1
			;;
		-V | --version )
			print_version
			exit 1
			;;
		-f | --force )
			force='yes'
			;;
		--dry-run )
			dry_run='yes'
			DRY='echo'
			;;
		-* )
			echo "$1: unknown switch.\n" 1>&2
			exit 1
			;;
326
		init|install|help )
327 328 329 330 331 332 333 334 335 336
			subcommand="subcommand_$1"
			shift
			sargs=( $* )
			shift $#
			;;
		* )
			echo "$1: unknown sub-command" 1>&2
			exit 1
	esac
	shift
337
done
338 339 340 341 342 343 344 345

$subcommand "${sargs[@]}"

# Local Variables:
# mode: sh
# sh-basic-offset: 8
# tab-width: 8
# End: