forked from CFD-GO/TCLB_cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.bash
169 lines (155 loc) · 2.27 KB
/
lib.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
168
169
# BASH
agree=false
def=false
function source_conf {
if [ -f "$PP/conf.ini" ]
then
source $PP/conf.ini
fi
}
function save_conf {
OLD="$PP/conf.ini"
NEW="$PP/new.conf.ini"
echo -n "Saving conf... "
if [ -f "$NEW" ]
then
if [ -f "$OLD" ]
then
if diff "$NEW" "$OLD" >/dev/null
then
echo "nothing new"
else
mv "$NEW" "$OLD"
echo "ok"
fi
else
mv "$NEW" "$OLD"
echo "ok"
fi
else
echo "no new conf"
fi
}
function source_cluster {
for i in $PP/cluster/*.bash
do
hn=$(head -n 1 $i | sed 's/^ *# *//')
if hostname | grep "$hn" >/dev/null
then
source $i
fi
done
}
function : {
if test -z "$2"
then
var="$1"
else
var="$1_$2"
fi
echo ${!var}
}
function ask_init {
true >$PP/new.conf.ini
}
function ask {
VAR="$1"
shift
COMM="$1"
VAL="$(: $VAR)"
ASK="$(: $VAR ASK)"
DEF="$(: $VAR DEF)"
CHECK="${VAR}_CHECK"
#echo "var:$VAR | val:$VAL | ask:$ASK | def:$DEF | check:$CHECK"
if $def || test -z "$VAL"
then
VAL="$DEF"
fi
if test -z "$ASK"
then
if ! test -z "$VAL"
then
O=" [$VAL]"
fi
if $agree
then
echo "$COMM: $VAL"
else
read -e -p "$COMM: " -i "$VAL" NEW
VAL=$NEW
fi
elif test "$ASK" == "yn"
then
if ask_yn "$DEF" "$COMM"
then
VAL="y"
else
VAL="n"
fi
else
VAL="$DEF"
fi
eval "$VAR=\"$VAL\""
if test "x$(type -t "$CHECK")" == "xfunction"
then
if test "x$ASK" != "xfix"
then
if ! $CHECK "$VAL"
then
exit -1;
fi
fi
fi
echo "$VAR=\"$VAL\"" >>$PP/new.conf.ini
}
function defgrant {
sacctmgr -p --quiet list User $USER | sed -n 2p | cut -d '|' -f 2 | tail -n1
}
function checkgrant {
[ -z "$1" ] && return 1
NL=$(sacctmgr --quiet list Account "$1" | wc -l)
[ $NL -ne 3 ] && return 1
return 0
}
function ask_yn {
DEF=$1
shift
MSG=$1
if $agree
then
echo "$MSG: $DEF"
REP="$DEF"
else
MSG="$MSG [$DEF]"
echo -n "$MSG: "
read REP
fi
case "$REP" in
y|Y|yes|Yes|YES) RET=y;;
n|N|no|NO) RET=n;;
"") RET=$DEF;;
*)
echo "Say yes of no" >&2
exit -1
esac
if test "$RET" == "y"
then
return 0;
else
return 1;
fi
}
function ask_yes {
ask_yn y "$@"
}
function ask_no {
ask_yn n "$@"
}
function def {
eval "${1}_DEF=\"$2\""
eval "${1}_ASK=\"\""
}
function fix {
eval "${1}_DEF=\"$2\""
eval "${1}_ASK=\"no\""
}