#@IgnoreInspection BashAddShebang # Environment handling functionality (Koalephant Shell Script Library) # Version: 2.0.0 # Copyright: 2014, Koalephant Co., Ltd # Author: Stephen Reay , Koalephant Packaging Team # Prefix to use when setting environment variables setglobal KOALEPHANT_ENVIRONMENT_PREFIX = ''"" # Make sure a name is safe for use as a variable name # # Input: # $1 - the name to make safe # # Output: # The save variable name proc k_env_safename { echo "$(KOALEPHANT_ENVIRONMENT_PREFIX)$(1)" | tr -cd '[:alnum:]_' } # Set a local variable # # Input: # $1 - the name of the variable to set # $2 - the value to set proc k_env_set { var name = '', value = '', msg = '', tmpN = '', existing = '', nsName = '' if test $Argc -lt 2 { echo "Not enough arguments passed to k_env_set: '$ifsjoin(Argv)'" > !2 return 1 } set name = $[k_env_safename $(1)] set value = $(2) k_debug 2 "Setting '$(name)' environment variable to '$(value)'" export "$(name)=$(value)" } # Unset one or more environment variables # # Input: # $1...n variables to unset from the environment proc k_env_unset { var name = '' for name in [@Argv] { k_debug 2 "Unsetting '$(name)' environment variable" unset $[k_env_safename $(name)] } } # Export one or more environment variables # # Input: # $1...n variables to export for child processes proc k_env_export { var name = '' for name in [@Argv] { echo "export \"$(name)=$[eval "echo \${$(name)}]\"" } } # Un-Export one or more environment variables # # Input: # $1...n - variables to un-export proc k_env_unexport { echo "unset $ifsjoin(Argv)" } # Import environment variables from the GECOS field # # Input: # $1 the user to import from. Defaults to the current logged in user. proc k_env_import_gecos { var userName = '', passwdEntry = '', gecos = '', i = '', name = '', value = '' k_debug 2 "Importing Environment Variables from GECOS field" # If no argument is provided, use $USER set userName = $(1:-${USER}) set passwdEntry = $[getent passwd $(userName)] if test "$Status" != "0" { return $? } set gecos = $[echo $(passwdEntry) | cut -d ":" -f 5] set i = '1' for name in [NAME ROOM TELEPHONE_WORK TELEPHONE_HOME EMAIL] { k_env_set $(name) $[echo $(gecos) | cut -d "," -f $(i)] if ! k_string_contains $(name) $(KOALEPHANT_ENV_NS_GECOS) { export KOALEPHANT_ENV_NS_GECOS="$(KOALEPHANT_ENV_NS_GECOS) $(name)" } set i = $shExpr('${i} + 1') } } # Export environment variables previously imported with (#k_env_import_gecos) proc k_env_export_gecos { k_env_export $(KOALEPHANT_ENV_NS_GECOS) } # Unset environment variables previously imported with {k_env_import_gecos} proc k_env_unset_gecos { k_env_unset $(KOALEPHANT_ENV_NS_GECOS) KOALEPHANT_ENV_NS_GECOS } # Import environment variables from an apache config file # # Input: # $1 - the apache config file to set environment variables from proc k_env_import_apache { var lines = '', line = '', name = '', value = '' var IFS = $[printf '\n ] && set IFS = $(IFS% ) var file = $(1) var setMode = $(2:-true) if test ! -f $(file) { echo "Invalid file supplied: '$(file)'" > !2 exit 1 } k_debug 2 "Importing Environment Variables from Apache Config: '$(file)'" set lines = $[grep -o -E "SetEnv[[:space:]].*" $(file)] if test $Status != 0 { return $? } for line in [$(lines)] { set name = $[echo $(line) | cut -d " " -f 2] set value = $[echo $(line) | cut -d " " -f 3- | perl -e 's/^"//g; s/(? !2 exit 1 } k_debug 2 "Importing Environment Variables from LDAP with Filter: '$(filter)'" # attribute renaming maps: if test -d "$(KOALEPHANT_LIB_PATH)/ldap-attr-maps" { for i in ["$(KOALEPHANT_LIB_PATH)/ldap-attr-maps/"*.attrs] { if test -r $(i) { for line in [$[grep --only-matching '^\(ATTR_[a-zA-Z0-9\-]\+\)=\([a-zA-Z0-9\-]\+\)$' $(i)]] { eval "local $(line)" } } } } set result = $[ldapsearch -x -LLL -o ldif-wrap=no "($(filter))" @Argv] for line in [$(result)] { set name = $[echo $(line) | grep --only-matching "^[^:]*] set origName = $(name) set longName = $[eval echo "\$ATTR_$(name)] if test ! -z $(longName) { set name = $(longName) } set name = $[k_string_upper $[k_string_split_camelcase $(name) "_]] set value = $[echo $(line) | cut -b "$shExpr('${#origName} + 3')-] k_env_set $(name) $(value) "LDAP" if ! k_string_contains $(name) $(KOALEPHANT_ENV_NS_LDAP) { export KOALEPHANT_ENV_NS_LDAP="$(KOALEPHANT_ENV_NS_LDAP) $(name)" } } } # Export environment variables previously imported with @link k_env_import_ldap @/link proc k_env_export_ldap { k_env_export $(KOALEPHANT_ENV_NS_LDAP) } # Unset environment variables previously imported with @link k_env_import_ldap @/link proc k_env_unset_ldap { k_env_unset $(KOALEPHANT_ENV_NS_LDAP) KOALEPHANT_ENV_NS_LDAP }