-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserconfig-lib.sh
290 lines (268 loc) · 5.6 KB
/
userconfig-lib.sh
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
Red="\e[31m"
Green="\e[32m"
Yellow="\e[33m"
Blue="\e[34m"
Magenta="\e[35m"
None="\e[0m"
export CONFLN_DATE=${CONFLN_DATE:-$(date +%Y-%m-%d--%H-%M-%S)}
if [[ "$USERCONFIG_ROOT" == "" ]]
then
USERCONFIG_ROOT=$(git rev-parse --show-toplevel)
[[ $? != 0 ]] && exit 1
fi
if ! grep jdskjcnjknksncfdvvndfiuvifd $USERCONFIG_ROOT/userconfig-lib.sh >/dev/null
then
echo "$USERCONFIG_ROOT don't look like userconfig git root."
exit 1
fi
version(){
version=$1
}
default_version(){
[[ "$version" == "" ]] && version 0
}
err()
{
echo -en $Red
echo -n ERROR $@
echo -en $None
echo
if [[ "${state_run_dir:-}" != "" ]]
then
echo faild > $state_run_dir/state
echo $@ > $state_run_dir/error
echo -e "${Blue}INSTALL FAIL $install_name$None"
fi
exit 1
}
need_root()
{
default_version
if [[ $UID != 0 ]]
then
echo -e "\e[31mROOT REQUIRE\e[0m"
exit 1
fi
echo -e "\e[32mROOT REQUIRE\e[0m"
}
confln()
{
$USERCONFIG_ROOT/confln "$@" || err confln faild
}
init-service()
{
(
. $USERCONFIG_ROOT/init-service.sh "$@"
) || err init-service faild
}
git_clupdate()
{
if [[ -d $2 ]]
then
(cd $2 ; git pull $1)
else
git clone $1 $2
fi
}
r_exit_on_err=true
r_pipe_output=true
r_confirm=false
r()
{
# Run commant with nice log
#
exit_on_err=$r_exit_on_err
pipe_output=$r_pipe_output
confirm=$r_confirm
do_eval=false
while [[ "$1" == "-"* ]]
do
if [[ "$1" == *b* ]]; then do_eval=true; fi
if [[ "$1" == *B* ]]; then do_eval=false; fi
if [[ "$1" == *c* ]]; then confirm=true; fi
if [[ "$1" == *C* ]]; then confirm=false; fi
if [[ "$1" == *e* ]]; then exit_on_err=true; fi
if [[ "$1" == *E* ]]; then exit_on_err=false; fi
if [[ "$1" == *p* ]]; then pipe_output=true; fi
if [[ "$1" == *P* ]]; then pipe_output=false; fi
if [[ "$1" == "-" ]] || [[ "$1" == "--" ]]
then
break
fi
shift
done
echo -n ">"
if $do_eval
then
echo "" "$@"
else
printf " %q" "$@"
echo
fi
if $confirm
then
in=""
while [[ "$in" != y ]]
do
echo -n -e " ╞ $Yellow Run?(Yes/Skip/Exit):$None "
read -n 1 in
echo
[[ "$in" == "e" ]] && echo -e " └─ $Magenta Without run (exit) \e[0m" && exit 1
[[ "$in" == "s" ]] && echo -e " └─ $Magenta Without run (skip) \e[0m" && return 0
done
fi
if $pipe_output
then
(
if $do_eval
then
eval "$@"
else
"$@"
fi
) 2>&1 | (
while read l
do
echo " │ $l"
done
)
ret="${PIPESTATUS[0]}"
else
if $do_eval
then
eval "$@"
ret="$?"
else
"$@"
ret="$?"
fi
fi
if [[ "$ret" == 0 ]]
then
echo -e " └─ \e[32mOK\e[0m"
else
if $exit_on_err
then
echo -e " └─ \e[31mRETURNED $ret => EXIT\e[0m"
err subprocess faild
else
echo -e " └─ \e[31mRETURNED $ret\e[0m"
fi
fi
return $ret
}
install_try_push(){
$USERCONFIG_ROOT/state_push.sh
}
install_config_load(){
install_config_path=$USERCONFIG_ROOT/state/install_config
mkdir -p $USERCONFIG_ROOT/state
if [[ ! -f $install_config_path ]]
then
echo -e "${Red}No install config, creating${None}"
(
echo "ic_version=0"
echo "ic_name=$([[ "$(pwd)" == /etc/* ]] && echo SYS || echo "$USER")@$(hostname)"
echo "ic_push='ssh jirikalvoda@ucw.cz'"
echo "ic_pull='movingssh -xd $USER@$(hostname)'"
echo "ic_userconfig_path='$(cd $USERCONFIG_ROOT ; pwd)'"
echo "ic_sysconfig='$([[ "$(pwd)" == /etc/* ]] && echo true || echo false)'"
) > $install_config_path
vim $install_config_path || exit 1
fi
ic_sysconfig=false
. $install_config_path
}
reln(){
[[ -h $2 ]] && (rm $2 || err rm faild)
ln -sr $1 $2 || err reln faild
}
clean_userinstall(){
if [[ -e ~/userconfig/state/$install_name/last ]]
then
echo -e "${Blue}Removing userconfig installation${None}"
rm ~/userconfig/state/$install_name/last
rm ~/userconfig/state/$install_name/last_ok
bash <<AMEN
cd ~/userconfig
. userconfig-lib.sh
install_try_push
AMEN
fi
}
install_begin(){
is_sysconfig=${is_sysconfig:-false}
default_version
install_config_load
if $ic_sysconfig && ! $is_sysconfig
then
err "This script should be called only from userconfig"
fi
if ! $ic_sysconfig && $is_sysconfig
then
err "This script should be called only from sysconfig"
fi
if [[ "$install_name" == "" ]]
then
install_name=$(pwd)/$(basename $0)
install_name=${install_name#$USERCONFIG_ROOT/}
[[ "$install_name" == *"/init.sh" ]] && install_name=${install_name%/init.sh}
install_name=${install_name//\//-}
fi
state_dir=$USERCONFIG_ROOT/state/$install_name
state_run_dir=$state_dir/$(date +%Y-%m-%d--%H-%M-%S)
mkdir -p $state_dir
if [[ -e $state_run_dir ]]
then
state_run_dir=""
err "State run dir exist, wait a second"
exit 1
fi
mkdir -p $state_run_dir
mkdir -p $state_run_dir/files
echo -e "${Blue}INSTALLING $install_name$None (version $version)"
echo $version > $state_run_dir/version
echo installing > $state_run_dir/state
printf "%s" "$global_args" > $state_run_dir/args
date -Iseconds > $state_run_dir/date
git rev-parse --verify HEAD > $state_run_dir/commit
git diff > $state_run_dir/git_diff
reln $state_run_dir $state_dir/last
}
install_ok(){
echo ok > $state_run_dir/state
echo -e "${Blue}INSTALL DONE $install_name$None"
reln $state_run_dir $state_dir/last_ok
unset state_dir
unset state_run_dir
unset install_name
install_try_push
}
need_state_server(){
userconfig_state_server=~/userconfig_state
[[ ! -d $userconfig_state_server ]] && err This is not userconfig state server
true
}
global_args="$(printf "%q " "$0" "$@")"
if [[ $1 == -v ]]
then
version(){
echo $1
exit 0
}
fi
if [[ $1 == -t ]]
then
install_begin(){
if ${is_sysconfig:-false}
then
echo sysconfig
else
echo userconfig
fi
exit 0
}
need_root(){
true
}
fi