-
Notifications
You must be signed in to change notification settings - Fork 0
/
wimport.sh
executable file
·350 lines (316 loc) · 9.22 KB
/
wimport.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# workstation import for linuxmuster.net
#
# Thomas Schmitt <[email protected]>
# 13.06.2016
# GPL v3
#
# adapted for oss-linbo
# Frank Schütte <[email protected]>
# 17.03.2017
# workstations file (oss)
# raum;rechner;gruppe;mac;;;;;;;linbo=1;
RC=0
### functions begin ###
# get value from index in emulated associative array (bash3)
# (set value: declare "array_$index=$value")
#
arrayGet() {
local array=$1 index=$2
local i="${array}_$index"
printf '%s' "${!i}"
}
# check for unique entry: check_unique <item1 item2 ...>
check_unique() {
printf '%s\n' $1 | sort | uniq -c -d
}
# cancel with message
exitmsg() {
echo " > $1"
echo
echo "An error ocurred and import_workstations will be cancelled!"
echo "No modifications have been made to your system!"
rm -f $locker
RC=1
echo
exit $RC
}
# sets serverip in start.conf
set_serverip(){
local conf="$LINBODIR/start.conf.$1"
local serverip="$2"
local RC="0"
grep -qi ^"server = $serverip" $conf && return "$RC"
if grep -qwi ^server $conf; then
sed -e "s/^[Ss][Ee][Rr][Vv][Ee][Rr].*/Server = $serverip/" -i $conf || RC="1"
else
sed -e "/^\[LINBO\]/a\
Server = $serverip" -i $conf || RC="1"
fi
return "$RC"
}
# sets group in start.conf
set_group(){
local group="$1"
local conf="$LINBODIR/start.conf.$group"
local RC="0"
grep -qi ^"Group = $group" $conf && return "$RC"
if grep -qwi ^group $conf; then
sed -e "s/^[Gg][Rr][Oo][Uu][Pp].*/Group = $group/" -i $conf || RC="1"
else
sed -e "/^Server/a\
Group = $group" -i $conf || RC="1"
fi
return "$RC"
}
# get systemtype from start.conf
get_systemtype(){
local group="$1"
local conf="$LINBODIR/start.conf.$group"
[ -e "$conf" ] || return 1
grep -iw ^systemtype "$conf" | tail -1 | awk -F\= '{ print $2 }' | awk '{ print $1 }'
}
# compute and print grub2 compliant disk name
# args: partition
grubdisk(){
local partition="$1"
local partnr="$(echo "$partition" | sed -e 's|/dev/[hsv]d[abcdefgh]||' -e 's|/dev/xvd[abcdefgh]||' -e 's|/dev/mmcblk[0-9]p||')"
case "$partition" in
/dev/mmcblk*) local disknr="$(echo "$partition" | sed 's|/dev/mmcblk\([0-9]\)p[1-9]|\1|')" ;;
*:*|*//*|*\\\\*) echo "nocache" ; return 0 ;; # remote cache, no local cache, no cache partition
*)
local ord="$(printf "$(echo $partition | sed 's|/dev/*[hsv]d\([a-z]\)[0-9]|\1|')" | od -A n -t d1)"
local disknr=$(( $ord - 97 ))
;;
esac
echo "(hd${disknr},${partnr})"
}
# sets pxe config file, params: group kopts
set_pxeconfig(){
local group="$1"
local kopts="$2"
local RC="0"
local startconf="$LINBODIR/start.conf.$group"
local targetconf="$LINBODIR/boot/grub/$group.cfg"
local globaltpl="$LINBOTPLDIR/grub.cfg.global"
local cache="$(grep -i ^cache /$startconf | tail -1 | awk -F\= '{ print $2 }' | awk '{ print $1 }' 2> /dev/null)"
local cacheroot="$(grubdisk "$cache" "$group")"
local ostpl
local ostype
if ([ -s "$targetconf" ] && ! grep -q "$MANAGEDSTR" "$targetconf"); then
echo -e "\tkeeping pxe config."
return 0
fi
# create gobal part for group cfg
echo -e "\twriting pxe config."
sed -e "s|@@group@@|$group|g
s|@@cacheroot@@|$cacheroot|g
s|@@kopts@@|$kopts|g" "$globaltpl" > "$targetconf" || RC="1"
# collect boot parameters from start.conf and write os parts for group cfg
local line
local partnr
local root
local name
local osroot
local kernel
local initrd
local append
local ostpl="$LINBOTPLDIR/grub.cfg.os"
local osnr=0
echo "[EOF]" | cat "$startconf" - | grep -v '^$\|^\s*\#' | awk -F\# '{ print $1 }' | sed -e 's|^ *||g' -e 's| *$||g' -e 's| =|=|g' -e 's|= |=|g' | while read line; do
if [ "${line:0:1}" = "[" ]; then
if [ -n "$kernel" ]; then
osnr=$((osnr + 1))
if [ "$kernel" = "reboot" ]; then
kernel="nokernel_placeholder"
else
kernel="$(echo $kernel | sed 's|^\/||')"
fi
if [ -z "$initrd" ]; then
initrd="noinitrd_placeholder"
else
initrd="$(echo $initrd | sed 's|^\/||')"
fi
# convert partition to grub syntax
osroot="$(grubdisk "$root" "$group")"
# computer partition number from start.conf
partnr="$(grep -i ^dev "$startconf" | grep -n "$root" | awk -F\: '{ print $1 }')"
# get ostype from osname
case "$(echo "$name" | tr A-Z a-z)" in
*windows*) ostype="win" ;;
*kubuntu*) ostype="kubuntu" ;;
*lubuntu*) ostype="lubuntu" ;;
*xubuntu*) ostype="xubuntu" ;;
*ubuntu*|*trusty*|*wily*) ostype="ubuntu" ;;
*centos*) ostype="centos" ;;
*arch*) ostype="arch" ;;
*linuxmint*) ostype="linuxmint" ;;
*fedora*) ostype="fedora" ;;
*gentoo*) ostype="gentoo" ;;
*debian*) ostype="debian" ;;
*suse*) ostype="opensuse" ;;
*linux*) ostype="linux" ;;
*) ostype="unknown" ;;
esac
# create config from template
sed -e "s|@@osnr@@|$osnr|g
s|@@kernel@@|$kernel|g
s|@@initrd@@|$initrd|g
s|@@append@@|$append|g
s|@@partition@@|$root|g
s|@@partnr@@|$partnr|g
s|@@osroot@@|$osroot|g
s|@@osname@@|$name|g
s|@@ostype@@|$ostype|g
s|@@group@@|$group|g
s|@@cacheroot@@|$cacheroot|g
s|@@kopts@@|$kopts|g" "$ostpl" >> "$targetconf" || RC="1"
fi
name=""; root=""; kernel=""; initrd=""; append=""; osroot=""; ostype=""
continue
fi
case "$line" in
[Nn][Aa][Mm][Ee]=*) name="$(echo $line | awk -F\= '{ print $2 }')" ;;
[Aa][Pp][Pp][Ee][Nn][Dd]=*) append="$(echo $line | sed s'|^[Aa][Pp][Pp][Ee][Nn][Dd]=||')" ;;
[Rr][Oo][Oo][Tt]=*|[Kk][Ee][Rr][Nn][Ee][Ll]=*|[Ii][Nn][Ii][Tt][Rr][Dd]=*) eval "$(echo $line | tr A-Z a-z)" ;;
esac
done
return "$RC"
}
# process configs for pxe hosts
do_pxe(){
local group="$1"
local ip="$2"
local RC="0"
local server=""
local kopts=""
# copy default start.conf if there is none for this group
if [ ! -e "$LINBODIR/start.conf.$group" ]; then
echo " Creating new linbo group $group."
cp "$LINBODEFAULTCONF" "$LINBODIR/start.conf.$group" || RC="2"
fi
# process start.conf and pxelinux configfile for group
if ! echo "$groups_processed" | grep -qwi "#${group}#"; then
echo -en " * LINBO-Group\t$group"
groups_processed="$groups_processed #${group}#"
# get kernel options from start.conf
kopts="$(linbo_kopts "$LINBODIR/start.conf.$group")"
# get custom serverip from kernel opts if set
if echo "$kopts" | grep -qw server; then
for i in $kopts; do eval "$i" &> /dev/null; done
fi
# set custom server ip in start.conf if defined in kernel opts
if validip "$server"; then
set_serverip "$group" "$server" || RC="2"
else # set default server ip
set_serverip "$group" "$SCHOOL_SERVER" || RC="2"
fi
# set group in start.conf
set_group "$group" || RC="2"
# provide grub2 pxe configfile for group
set_pxeconfig "$group" "$kopts" || RC="2"
fi
case "$RC" in
1) echo " ERROR in pxe host configuration!" ;;
2) echo " ERROR in pxe group configuration!" ;;
*) ;;
esac
return "$RC"
}
### functions end ###
# adding new host entries from LINBO's registration
if ls $LINBODIR/*.new &> /dev/null; then
for i in $LINBODIR/*.new; do
if [ -s "$i" ]; then
hostname="$(basename "$i" | sed 's|.new||')"
echo "Importing new host $hostname:"
cat $i
echo
cat $i >> $WIMPORTDATA
fi
rm -f $i
done
fi
# case repair for workstation data
# all to lower case, macs to upper case
sed -e 's/\(^[A-Za-z0-9].*\)/\L\1/
s/\([a-fA-F0-9]\{2\}[:][a-fA-F0-9]\{2\}[:][a-fA-F0-9]\{2\}[:][a-fA-F0-9]\{2\}[:][a-fA-F0-9]\{2\}[:][a-fA-F0-9]\{2\}\)/\U\1/g' -i "$WIMPORTDATA"
echo -n " - checking hostgroups..."
# hostgroups
hostgroups="$(grep ^[a-zA-Z0-9] $WIMPORTDATA | awk -F\; '{ print "#"$3"#" }' | sort -u)"
echo "$hostgroups" | grep -q "##" && exitmsg "Empty hostgroup found! Check your data!"
hostgroups="${hostgroups//#/}"
for i in $hostgroups; do
check_string "$i" || exitmsg "$i is no valid hostgroup name!"
done
echo "done."
# tests are done
echo " Ok!"
echo
# sync host accounts
echo "Creating new workstations accounts..."
if [ -s $WIMPORTDATA ]; then
$LINBOSHAREDIR/linbo_sync_hosts.pl $WIMPORTDATA 2>> $TMPLOG; RC="$?"
if [ "$RC" = "0" ]; then
echo "Done!"
echo
echo "Updating workstations file..."
$LINBOSHAREDIR/linbo_update_workstations.pl
echo "Done."
else
echo "linbo_sync_hosts.pl exits with error!"
echo
rm -f $locker
exit "$RC"
fi # sync host accounts
else
echo " * no file $WIMPORTDATA"
echo "Done!"
echo
fi
# sync host groups
echo "Processing host groups..."
RC=
TMPFILE=$(mktemp /tmp/hwtypesXXXX)
rm -f $TMPFILE
>$TMPFILE
while read line; do
key=${line%%:*}
case "$key" in
description)
descr="$(echo "$line" | awk '{ print $2 }' )"
;;
configurationKey)
hwconf="$(echo "$line" | awk '{ print $2 }' )"
;;
dn)
if [ -n "$descr" -a -n "$hwconf" ]; then
do_pxe "$descr" || RC="1"
declare "hosttypearray_$hwconf=$(get_systemtype $descr)"
declare "hostgrouparray_$hwconf=$descr"
descr=
hwconf=
fi
;;
*)
;;
esac
done <$TMPFILE
if [ -n "$descr" -a -n "$hwconf" ]; then
do_pxe "$descr" || RC="1"
declare "hosttypearray_$hwconf=$(get_systemtype $descr)"
declare "hostgrouparray_$hwconf=$descr"
descr=
hwconf=
fi
rm -f $TMPFILE
if [ -n "$RC" ]; then
echo "Errors in host group processing!"
echo
rm -f $locker
exit "$RC"
else
echo "Done!"
echo
fi
# exit with return code
exit $RC