Closed fgeorgatos closed 6 years ago
OK, i have a 1st-draft for handling this:
[fotis@demo2 ~]$ ll parse_yaml_profile.*sh
-rw-rw-r-- 1 fotis fotis 322 Sep 20 11:48 parse_yaml_profile.csh
-rw-rw-r-- 1 fotis fotis 298 Sep 20 11:48 parse_yaml_profile.sh
[fotis@demo2 ~]$ cat !$
cat parse_yaml_profile.*sh
if ( ! $?__Init_Default_Profile ) then
setenv __Init_Default_Profile 1
setenv PARSEFILES "/etc/profile.definitions/{global,site/*,nodecategory/*,groups/`id -gn`,user/`id -un`}.yml"
cat $PARSEFILES 2>/dev/null|bash /etc/profile.definitions/parse_yaml.sh |sed 's/^/setenv /g;s/=\(.*\)/ \1/g'
unset PARSEFILES
endif
if [ -z "__Init_Default_Profile" ]; then
export __Init_Default_Profile=1;
PARSEFILES="/etc/profile.definitions/{global,site/*,nodecategory/*,groups/`id -gn`,user/`id -un`}.yml"
cat $PARSEFILES 2>/dev/null|bash /etc/profile.definitions/parse_yaml.sh |sed 's/^/export /g'
unset PARSEFILES
fi
[fotis@demo2 ~]$ ll /etc/profile.definitions/
total 8
-rw-r--r-- 1 root root 153 Sep 20 11:47 global.yml
drwxr-xr-x 2 root root 10 Sep 20 11:47 nodecategory
-rw-r--r-- 1 root root 695 Sep 20 11:29 parse_yaml.sh
drwxr-xr-x 2 root root 10 Sep 20 11:47 site
[fotis@demo2 ~]$ cat /etc/profile.definitions/parse_yaml.sh
## ref. https://stackoverflow.com/questions/5014632/how-can-i-parse-a-yaml-file-from-a-linux-shell-script
function parse_yaml {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
parse_yaml
OK, a prototyped working implementation for {ba|tc}sh
(and likely more shells) is:
[fgeorgatos@demo2 ~]$ cat /etc/profile.d/007_parse_yaml_profile.*
## /etc/profile.d/007_parse_yaml_profile.csh
if ( ! $?__Init_Default_Profile ) then
setenv __Init_Default_Profile 1
eval "`bash /etc/profile.d/007_parse_yaml_profile.sh OK|sed 's/^export /setenv /g;s/=\(.*\)/ \1/g'|tr '\n' ';'`"
endif
## /etc/profile.d/007_parse_yaml_profile.sh
PARSEFILES=$(ls /etc/profile.definitions/{global,site/*,nodecategory/*,groups/`id -gn`,user/`id -un`}.yml 2>/dev/null)
if [ -n "__Init_Default_Profile" ]; then
export __Init_Default_Profile=1;
if [ -z $1 ]; then
eval `cat /dev/null $PARSEFILES | bash /etc/profile.definitions/parse_yaml.sh |sed 's/^/export /g;s/[[:space:]]*#.*//g'`
else
cat /dev/null $PARSEFILES | bash /etc/profile.definitions/parse_yaml.sh|sed 's/^/export /g;s/[[:space:]]*#.*//g'
fi
fi
unset PARSEFILES
[fgeorgatos@demo2 ~]$ ll !$
ll /etc/profile.d/007_parse_yaml_profile.*
-rw-r--r-- 1 root root 195 Sep 21 15:51 /etc/profile.d/007_parse_yaml_profile.csh
-rw-r--r-- 1 root root 492 Sep 21 15:42 /etc/profile.d/007_parse_yaml_profile.sh
as before, this script may do the conversion (not the only solution to this issue):
$ cat /etc/profile.definitions/parse_yaml.sh
## ref. https://stackoverflow.com/questions/5014632/how-can-i-parse-a-yaml-file-from-a-linux-shell-script
function parse_yaml {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
parse_yaml
btw. this configuration appears to work well, note that hashes do the intended thing:
[fgeorgatos@demo2 ~]$ cat /etc/profile.definitions/site/Lmod.yml
LMOD:
RC: /etc/site/lmod/lmodrc.lua
SYSTEM_DEFAULT_MODULES: "settarg use.own.eb HPCBIOS/2015q2" # sge cluster"
##FULL_SETTARG_SUPPORT: full
TMOD_FIND_FIRST: yes
ADMIN_FILE: /dev/shm/lmod/lmod_admin_file
PACKAGE_PATH: /etc/site/lmod
TARGPATHLOC: last
SETTARG:
TITLE_BAR: yes
##MODULEPATH: /etc/modulefiles:/usr/share/modulefiles:/usr/share/Modules/modulefiles:/etc/site/modules
MODULEPATH: /etc/modulefiles:/usr/share/modulefiles:/usr/share/Modules/modulefiles:/etc/site/modules:/etc/modulefiles___
i'm resolving this, since PR #95 does everything initially asked and quite some more!
if this gets done fully, we should have a mechanism which would permit to variate the variable settings according to different values of the tuple in $subject
Some initial ideas:
$__Init_Default_Profile
is defined, exit; otherwise define it now/etc/profile.definitions/{global,site/*,nodecategory/*,groups/
id -gn,user/
id -un/*.yml
yaml files; possibly expand in the future with$HOME/.007_profile.definitions/*.yml