1 | #!/bin/bash |
---|
2 | |
---|
3 | #/** |
---|
4 | # oglivecli command [options ...] |
---|
5 | #@file oglivecli |
---|
6 | #@brief Command line tool to manage ogLive clients. |
---|
7 | #@param $1 command Command to execute. |
---|
8 | #@param $2 options Parameters and options. |
---|
9 | #@warning This script uses "jq" command. |
---|
10 | #@version 1.1.0 - Initial version. |
---|
11 | #@author Ramón M. Gómez - ETSII Univ. Sevilla |
---|
12 | #@date 2016-12-05 |
---|
13 | #*/ ## |
---|
14 | |
---|
15 | |
---|
16 | # Global constants definition. |
---|
17 | PROG=$(basename "$(realpath "$0")") # Program name. |
---|
18 | OPENGNSYS=/opt/opengnsys # OpenGnsys main directory. |
---|
19 | DOWNLOADDIR=$OPENGNSYS/lib # Directory to store ogLive images. |
---|
20 | DOWNLOADURL="https://opengnsys.es/trac/downloads" # Download URL. |
---|
21 | TFTPDIR=$OPENGNSYS/tftpboot # TFTP directory. |
---|
22 | DEFOGLIVE="ogLive" # Default ogLive directory. |
---|
23 | MINREL=20190601 # Mininum ogLive compatibility release. |
---|
24 | INFOFILE=$OPENGNSYS/etc/ogliveinfo.json # Configuration file. |
---|
25 | |
---|
26 | |
---|
27 | # Auxiliar functions. |
---|
28 | |
---|
29 | # Metafunction to check if JSON result exists. |
---|
30 | function jq() { |
---|
31 | local OUTPUT |
---|
32 | OUTPUT=$($JQ "$@") || return $? |
---|
33 | [[ "$OUTPUT" = "null" ]] && return 1 |
---|
34 | echo "$OUTPUT" |
---|
35 | } |
---|
36 | |
---|
37 | # Create/edit JSON file about installed ogLive clients. |
---|
38 | function addToJson() { |
---|
39 | local DATA OGLIVEDIST="$1" OGLIVEKRNL="$2" OGLIVEARCH="$3" OGLIVEREV="$4" |
---|
40 | local OGLIVEDIR=$(basename $5 2>/dev/null) OGLIVEISO=$(basename $6 2>/dev/null) |
---|
41 | # JSON data for installed ogLive. |
---|
42 | DATA=$(cat << EOT | jq . |
---|
43 | {"distribution":"$OGLIVEDIST","kernel":"$OGLIVEKRNL","architecture":"$OGLIVEARCH","revision":"$OGLIVEREV","directory":"$OGLIVEDIR","iso":"$OGLIVEISO"} |
---|
44 | EOT |
---|
45 | ) |
---|
46 | # Check JSON file consistency. |
---|
47 | if [ "$(jq -c keys $INFOFILE 2>/dev/null)" == '["default","oglive"]' ]; then |
---|
48 | # Check if ogLive is defined into JSON file. |
---|
49 | n=$(jq ".oglive | length" $INFOFILE) |
---|
50 | for ((i=0; i<n; i++)); do |
---|
51 | [ "$(jq ".check=$DATA | .check==.oglive[$i]" $INFOFILE)" == "true" ] && INDEX=$i |
---|
52 | done |
---|
53 | # Check if it needs to insert data. |
---|
54 | if [ -z "$INDEX" ]; then |
---|
55 | INDEX=$n |
---|
56 | jq ".oglive |= (. + [$DATA])" $INFOFILE | sponge $INFOFILE |
---|
57 | fi |
---|
58 | # Show JSON entry. |
---|
59 | jq ".oglive[$INDEX]" $INFOFILE |
---|
60 | else |
---|
61 | # Create new JSON file. |
---|
62 | cat << EOT | jq . | tee $INFOFILE |
---|
63 | {"oglive":[$DATA],"default":0} |
---|
64 | EOT |
---|
65 | fi |
---|
66 | } |
---|
67 | |
---|
68 | # Show an error message. |
---|
69 | function raiseError() { |
---|
70 | case "$1" in |
---|
71 | usage) |
---|
72 | echo "$PROG: Usage error: Type \"$PROG help\"" >&2 |
---|
73 | exit 1 ;; |
---|
74 | notfound) |
---|
75 | echo "$PROG: Resource not found: $2" >&2 |
---|
76 | exit 2 ;; |
---|
77 | access) |
---|
78 | echo "$PROG: Access error: $2" >&2 |
---|
79 | exit 3 ;; |
---|
80 | download) |
---|
81 | echo "$PROG: Download error: $2" >&2 |
---|
82 | exit 4 ;; |
---|
83 | *) |
---|
84 | echo "$PROG: Unknown error" >&2 |
---|
85 | exit 1 ;; |
---|
86 | esac |
---|
87 | } |
---|
88 | |
---|
89 | # Command functions. |
---|
90 | |
---|
91 | # Show help message. |
---|
92 | function help() { |
---|
93 | cat << EOT |
---|
94 | $PROG: manage ogLive cleints. |
---|
95 | Usage: $PROG command [options] |
---|
96 | Commands: |
---|
97 | help show this help |
---|
98 | config show configuration parameters |
---|
99 | check check system consistency |
---|
100 | convert convert old ogclient to new default ogLive client |
---|
101 | list list installed ogLive clients |
---|
102 | show all show JSON information about all installed ogLive clients |
---|
103 | show default show JSON information about ogLive client marked as default |
---|
104 | show Index|Dir show JSON information about an installed ogLive client |
---|
105 | search Index|Dir show corresponding index or directory |
---|
106 | download show a menu to download an ogLive ISO image from the OpenGnsys website |
---|
107 | download Iso download an specific ogLive ISO image from the OpenGnsys website |
---|
108 | install Iso install a new ogLive client from a downloaded ISO image |
---|
109 | uninstall Iso remove ISO image and uninstall its ogLive client |
---|
110 | uninstall Index|Dir uninstall an ogLive client |
---|
111 | get-default get index value for default ogLive client |
---|
112 | set-default Index set default ogLive client |
---|
113 | rebuild rebuild a lost configuration file |
---|
114 | assign Iso Index assign an ISO file to a JSON entry |
---|
115 | Parameters: |
---|
116 | Index a number, starting by 0 |
---|
117 | Dir directory (relative to installation directory) |
---|
118 | Iso ISO file name (relative to download URL or download directory) |
---|
119 | EOT |
---|
120 | } |
---|
121 | |
---|
122 | # Convert default ogclient to a new ogLive format. |
---|
123 | function convert() { |
---|
124 | local OGCLIENT=ogclient OLDINFOFILE=$OPENGNSYS/doc/veroglive.txt |
---|
125 | local OGLIVEKRNL OGLIVEDIR OGLIVEISO |
---|
126 | [ $# -ne 0 ] && raiseError usage |
---|
127 | [ ! -w $(dirname $INFOFILE) ] && raiseError access "Configuration file." |
---|
128 | [ -n "$(stat -c "%N" $TFTPDIR/ogclient | awk '$3~/'$DEFOGLIVE'/ {print}')" ] && raiseError access "ogLive is already converted." |
---|
129 | pushd $TFTPDIR >/dev/null || raiseError access "Installation directory." |
---|
130 | [ ! -f $OGCLIENT/ogvmlinuz ] && raiseError notfound "ogclient" |
---|
131 | # Add entry to JSON file using ogclient kernel version. |
---|
132 | OGLIVEKRNL=$(file -bkr $OGCLIENT/ogvmlinuz | awk '/Linux/ {for(i=1;i<=NF;i++) if($i~/version/) {v=$(i+1);sub(/-.*/,"",v);print v}}') |
---|
133 | OGLIVEDIR=$DEFOGLIVE-$OGLIVEKRNL |
---|
134 | [ -r $OLDINFOFILE ] && OGLIVEISO="$(head -1 $OLDINFOFILE)" |
---|
135 | addToJson "$(echo $OGLIVEISO|cut -f2 -d-)" "$OGLIVEKRNL" "i386" "${OGLIVEISO##*-}" "$OGLIVEDIR" "$OGLIVEISO.iso" |
---|
136 | # Rename directory, link to default and clean old files. |
---|
137 | mv -v $OGCLIENT $OGLIVEDIR |
---|
138 | ln -vfs $OGLIVEDIR $DEFOGLIVE |
---|
139 | rm -f $OGCLIENT |
---|
140 | ln -vfs $DEFOGLIVE $OGCLIENT |
---|
141 | mv -v $OGCLIENT.old $OGLIVEDIR.old 2>/dev/null |
---|
142 | rm -fv {ogvmlinuz,oginitrd.img}{,.sum} $OLDINFOFILE |
---|
143 | popd >/dev/null |
---|
144 | # Delete old config file. |
---|
145 | rm -f $OLDINFOFILE |
---|
146 | } |
---|
147 | |
---|
148 | # Show script configuration parameters. |
---|
149 | function config() { |
---|
150 | case $# in |
---|
151 | 0) # Show all parameters. |
---|
152 | cat << EOT |
---|
153 | Configuration file: $INFOFILE |
---|
154 | ogLive download URL: $DOWNLOADURL |
---|
155 | ogLive download directory: $DOWNLOADDIR |
---|
156 | ogLive installation directory: $TFTPDIR |
---|
157 | Default ogLive name: $DEFOGLIVE |
---|
158 | Mainimum compatibility release: r$MINREL |
---|
159 | EOT |
---|
160 | ;; |
---|
161 | 1) # Show specified parameter. |
---|
162 | case "$1" in |
---|
163 | config-file) echo "$INFOFILE" ;; |
---|
164 | download-url) echo "$DOWNLOADURL" ;; |
---|
165 | download-dir) echo "$DOWNLOADDIR" ;; |
---|
166 | install-dir) echo "$TFTPDIR" ;; |
---|
167 | default-name) echo "$DEFOGLIVE" ;; |
---|
168 | min-release) echo "r$MINREL" ;; |
---|
169 | *) raiseError notfound "$1" ;; |
---|
170 | esac |
---|
171 | ;; |
---|
172 | *) # Usage error. |
---|
173 | raiseError usage |
---|
174 | ;; |
---|
175 | esac |
---|
176 | } |
---|
177 | |
---|
178 | # Check consistency, showing configuration problems. |
---|
179 | function check() { |
---|
180 | local ERR=0 AUX INST DEF |
---|
181 | [ $# -ne 0 ] && raiseError usage |
---|
182 | # Check for old system that needs conversion. |
---|
183 | if [ -z "$(stat -c "%N" $TFTPDIR/ogclient | awk '$3~/'$DEFOGLIVE'/ {print}')" ]; then |
---|
184 | echo "This server uses old ogclient, please run \"$PROG convert\" to update." |
---|
185 | let ERR++ |
---|
186 | [ ! -f $INFOFILE ] && return $ERR |
---|
187 | fi |
---|
188 | # Check for other problems. |
---|
189 | [ ! -f $INFOFILE ] && echo "Configuration file does not exists: $INFOFILE" && let ERR++ |
---|
190 | [ -f $INFOFILE -a "$(jq -c keys $INFOFILE 2>/dev/null)" != "[\"default\",\"oglive\"]" ] && echo "Format error in configuration file: $INFOFILE" && let ERR++ |
---|
191 | [ ! -e $TFTPDIR ] && echo "TFTP directory does not exist: $TFTPDIR." && let ERR++ |
---|
192 | # Check for installed ogLive clients. |
---|
193 | INST=( $(find $TFTPDIR/ -type d -name "$DEFOGLIVE-*" -a ! -name "*.old" -printf "%f\n" | sort) ) |
---|
194 | [[ ${#INST[@]} -eq 0 ]] && echo "No ogLive clients are installed." && let ERR++ |
---|
195 | DEF=( $(jq -r .oglive[].directory $INFOFILE 2>/dev/null | sort) ) |
---|
196 | # Compare installed and defined ogLive clients. |
---|
197 | AUX=$(comm -23 <(printf "%s\n" ${INST[*]}) <(printf "%s\n" ${DEF[*]})) |
---|
198 | [ -n "$AUX" ] && echo "Some ogLive are installed but not defined: ${AUX//$'\n'/, }" && let ERR++ |
---|
199 | AUX=$(comm -13 <(printf "%s\n" ${INST[*]}) <(printf "%s\n" ${DEF[*]})) |
---|
200 | [ -n "$AUX" ] && echo "Some ogLive are defined but not installed: ${AUX//$'\n'/, }" && let ERR++ |
---|
201 | # Compare downloaded and defined ISO images. |
---|
202 | INST=( $(find $DOWNLOADDIR/ -type f -name "$DEFOGLIVE-*.iso" -printf "%f\n" | sort) ) |
---|
203 | DEF=( $(jq -r .oglive[].iso $INFOFILE 2>/dev/null | sort) ) |
---|
204 | AUX=$(comm -23 <(printf "%s\n" ${INST[*]}) <(printf "%s\n" ${DEF[*]})) |
---|
205 | [ -n "$AUX" ] && echo "Some ISOs are downloaded but not defined: ${AUX//$'\n'/, }" && let ERR++ |
---|
206 | AUX=$(comm -13 <(printf "%s\n" ${INST[*]}) <(printf "%s\n" ${DEF[*]})) |
---|
207 | [ -n "$AUX" ] && echo "Some ISOs are defined but not downloaded: ${AUX//$'\n'/, }" && let ERR++ |
---|
208 | # Check for new ISO files downloaded after installation. |
---|
209 | AUX=$(jq -r '.oglive[] as $og | $og.iso + ":" + $og.directory' $INFOFILE 2>/dev/null | \ |
---|
210 | while IFS=":" read -r DEF INST; do |
---|
211 | [ $DOWNLOADDIR/$DEF -nt $TFTPDIR/$INST ] && echo "$DEF" |
---|
212 | done) |
---|
213 | [ -n "$AUX" ] && echo "Some ISOs are downloaded after installation: ${AUX//$'\n'/, }" && let ERR++ |
---|
214 | AUX=$(jq -r '.oglive[] as $og | if ($og.revision[1:9] | tonumber) < '$MINREL' then $og.directory else "" end' $INFOFILE 2>/dev/null) |
---|
215 | [ -n "$AUX" ] && echo "Some installed ogLive aren't fully compatible: ${AUX//$'\n'/, }" && let ERR++ |
---|
216 | # Print result. |
---|
217 | [ $ERR -eq 0 ] && echo "OK!" || echo "Problems detected: $ERR" |
---|
218 | return $ERR |
---|
219 | } |
---|
220 | |
---|
221 | # List installed ogLive clients. |
---|
222 | function list() { |
---|
223 | [ $# -ne 0 ] && raiseError usage |
---|
224 | [ ! -r $INFOFILE ] && raiseError access "Configuration file." |
---|
225 | # List all defined indexes, directories and check if missing. |
---|
226 | jq -r .oglive[].directory $INFOFILE | nl -v 0 | \ |
---|
227 | awk '{system("echo -n "$0"; test -d '$TFTPDIR'/"$2" || echo -n \" (missing)\"; echo")}' | column -t |
---|
228 | } |
---|
229 | |
---|
230 | # Show information about an installed ogLive client. |
---|
231 | function show() { |
---|
232 | local INDEX |
---|
233 | [ $# -ne 1 ] && raiseError usage |
---|
234 | [ ! -r $INFOFILE ] && raiseError access "Configuration file." |
---|
235 | # Show JSON entries. |
---|
236 | case "$1" in |
---|
237 | default) # Default index. |
---|
238 | INDEX="[$(jq -r .default $INFOFILE)]" ;; |
---|
239 | all) # All intries. |
---|
240 | ;; |
---|
241 | [0-9]*) # Index. |
---|
242 | INDEX="[$1]" ;; |
---|
243 | *) # Directory. |
---|
244 | INDEX="[$(search "$1" 2>/dev/null)]" || raiseError notfound "Directory \"$1\"." |
---|
245 | ;; |
---|
246 | esac |
---|
247 | jq ".oglive$INDEX" $INFOFILE || raiseError notfound "Index \"$1\"." |
---|
248 | } |
---|
249 | |
---|
250 | # Show index or directory corresponding to searching parameter. |
---|
251 | function search() { |
---|
252 | [ $# -ne 1 ] && raiseError usage |
---|
253 | [ ! -r $INFOFILE ] && raiseError access "Configuration file." |
---|
254 | # Show corresponding index or directory. |
---|
255 | list | awk -v d="$1" '{if ($2==d) print $1; if ($1==d) print $2}' | grep . || raiseError notfound "Index/Directory \"$1\"" |
---|
256 | } |
---|
257 | |
---|
258 | # Show a menu to select and download an ogLive ISO image from the OpenGnsys website. |
---|
259 | function download() { |
---|
260 | local OGLIVE NISOS i SOURCELENGTH TARGETFILE |
---|
261 | local ISOREL |
---|
262 | [ $# -gt 1 ] && raiseError usage |
---|
263 | [ ! -d $DOWNLOADDIR ] && raiseError notfound "Download directory" |
---|
264 | [ ! -w $DOWNLOADDIR ] && raiseError access "Download directory" |
---|
265 | # Check parameter. |
---|
266 | if [ -n "$1" ]; then |
---|
267 | # ogLive to download. |
---|
268 | OGLIVEFILE="$1" |
---|
269 | else |
---|
270 | # Show download menu. |
---|
271 | OGLIVE=( $(curl --silent $DOWNLOADURL | grep "$DEFOGLIVE.*iso") ) |
---|
272 | NISOS=${#OGLIVE[@]} |
---|
273 | echo "Available downloads (+ = installed, * = full compatibility):" |
---|
274 | for i in $(seq 1 $NISOS); do |
---|
275 | [ -e $DOWNLOADDIR/${OGLIVE[i-1]} ] && OGLIVE[i-1]="(+) ${OGLIVE[i-1]}" |
---|
276 | ISOREL=${OGLIVE[i-1]##*-r}; ISOREL=${ISOREL%%.*} |
---|
277 | [ $ISOREL -ge $MINREL ] && OGLIVE[i-1]="(*) ${OGLIVE[i-1]}" |
---|
278 | done |
---|
279 | select opt in "${OGLIVE[@]}"; do |
---|
280 | [ -n "$opt" ] && OGLIVEFILE=${opt##* } && break |
---|
281 | done |
---|
282 | fi |
---|
283 | # Get download size. |
---|
284 | SOURCELENGTH=$(curl --head --silent $DOWNLOADURL/$OGLIVEFILE | awk -F: '/Content-Length:/ {print $2}') |
---|
285 | [ -n "$SOURCELENGTH" ] || raiseError download "$OGLIVEFILE" |
---|
286 | # Download ogLive. |
---|
287 | TARGETFILE=$DOWNLOADDIR/$OGLIVEFILE |
---|
288 | trap "rm -f $TARGETFILE" 1 2 3 6 9 15 |
---|
289 | curl $DOWNLOADURL/$OGLIVEFILE -o $TARGETFILE || raiseError download "$OGLIVEFILE" |
---|
290 | } |
---|
291 | |
---|
292 | # Install an ogLive client from a previously downloaded ISO image. |
---|
293 | function install() { |
---|
294 | local OGLIVEFILE OGLIVEDIST OGLIVEREV OGLIVEKRNL OGLIVEDIR OGINITRD OGSQFS OGCLIENT=ogclient |
---|
295 | local SAMBAPASS TMPDIR RSYNCSERV RSYNCCLNT |
---|
296 | [ $# -ne 1 ] && raiseError usage |
---|
297 | OGLIVEFILE=$(realpath $DOWNLOADDIR/$1) |
---|
298 | # Only 1 file in pathname expansion. |
---|
299 | [ $(echo $OGLIVEFILE | wc -w) -gt 1 ] && raiseError usage |
---|
300 | [ ! -f $OGLIVEFILE ] && raiseError notfound "Downloaded file: \"$1\"." |
---|
301 | [ ! -r $OGLIVEFILE ] && raiseError access "Downloaded file: \"$1\"." |
---|
302 | [ ! -w $(dirname $INFOFILE) ] && raiseError access "Configuration directory." |
---|
303 | [ ! -w $TFTPDIR ] && raiseError access "Installation directory." |
---|
304 | [ -z "$(file -b $OGLIVEFILE | grep "ISO.*ogClient")" ] && raiseError access "File is not an ogLive ISO image." |
---|
305 | # Working directory (ogLive-Distribution-KernelVersion-Architecture-CodeRevision). |
---|
306 | OGLIVEDIST="$(echo $OGLIVEFILE|cut -f2 -d-)" |
---|
307 | OGLIVEREV="${OGLIVEFILE##*-}"; OGLIVEREV="${OGLIVEREV%.*}" |
---|
308 | OGLIVEKRNL="$(echo $OGLIVEFILE|cut -f3- -d-)"; OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEREV.*}" |
---|
309 | OGLIVEARCH="$(echo $OGLIVEFILE|awk -F- '{print $(NF-1)}')" |
---|
310 | case "$OGLIVEARCH" in |
---|
311 | i386|amd64) # Get architecture. |
---|
312 | OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEARCH}" ;; |
---|
313 | *) # 32-bit by default. |
---|
314 | OGLIVEARCH="i386" ;; |
---|
315 | esac |
---|
316 | OGLIVEDIR="$TFTPDIR/$DEFOGLIVE-$OGLIVEDIST-${OGLIVEKRNL%%-*}-$OGLIVEARCH-$OGLIVEREV" |
---|
317 | # Get current or default Samba key. |
---|
318 | OGINITRD=$OGLIVEDIR/oginitrd.img |
---|
319 | [ ! -r $OGINITRD ] && OGINITRD=$TFTPDIR/$DEFOGLIVE/oginitrd.img |
---|
320 | if [ -r $OGINITRD ]; then |
---|
321 | SAMBAPASS=$(gzip -dc $OGINITRD | \ |
---|
322 | cpio -i --to-stdout scripts/ogfunctions 2>&1 | \ |
---|
323 | grep "^[ ].*OPTIONS=" | \ |
---|
324 | sed 's/\(.*\)pass=\(\w*\)\(.*\)/\2/') |
---|
325 | fi |
---|
326 | # Make ogLive backup. |
---|
327 | rm -fr ${OGLIVEDIR}.old |
---|
328 | mv -fv $OGLIVEDIR ${OGLIVEDIR}.old 2>/dev/null |
---|
329 | # Mount ogLive ISO image, update its files and unmount it. |
---|
330 | TMPDIR=/tmp/${OGLIVEFILE%.iso} |
---|
331 | mkdir -p $OGLIVEDIR $TMPDIR |
---|
332 | trap "umount $TMPDIR; rm -fr $TMPDIR" 1 2 3 6 9 15 |
---|
333 | mount -o loop,ro $OGLIVEFILE $TMPDIR |
---|
334 | cp -va $TMPDIR/ogclient/* $OGLIVEDIR || raiseError access "Cannot copy files to ogLive directory." |
---|
335 | umount $TMPDIR |
---|
336 | # Link to default directory if it's the first ogLive. |
---|
337 | if [ ! -f $INFOFILE ]; then |
---|
338 | rm -f $TFTPDIR/$DEFOGLIVE $TFTPDIR/$OGCLIENT |
---|
339 | ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE |
---|
340 | ln -vfs $DEFOGLIVE $TFTPDIR/$OGCLIENT |
---|
341 | fi |
---|
342 | # Recover or ask for a new Samba access key. |
---|
343 | if [ -n "$SAMBAPASS" ]; then |
---|
344 | echo -ne "$SAMBAPASS\n$SAMBAPASS\n" | $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" |
---|
345 | else |
---|
346 | $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" |
---|
347 | fi |
---|
348 | # Set permissions. |
---|
349 | find -L $OGLIVEDIR -type d -exec chmod 755 {} \; |
---|
350 | find -L $OGLIVEDIR -type f -exec chmod 644 {} \; |
---|
351 | chown -R :opengnsys $OGLIVEDIR |
---|
352 | # Mount SquashFS and check Rsync version. |
---|
353 | OGSQFS=$OGLIVEDIR/ogclient.sqfs |
---|
354 | mount -o loop,ro $OGSQFS $TMPDIR |
---|
355 | # If Rsync server version > client version, link to compiled file. |
---|
356 | RSYNCSERV=$(rsync --version 2>/dev/null | awk '/protocol/ {print $6}') |
---|
357 | RSYNCCLNT=$(chroot $TMPDIR /usr/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}') |
---|
358 | if [ -z "$RSYNCSERV" -o ${RSYNCSERV:-0} -gt ${RSYNCCLNT:-1} ]; then |
---|
359 | [ -e $OPENGNSYS/client/bin/rsync-$RSYNCSERV ] && mv -f $OPENGNSYS/client/bin/rsync-$RSYNCSERV $OPENGNSYS/client/bin/rsync |
---|
360 | else |
---|
361 | # Else, rename compiled file using Rsync protocol number. |
---|
362 | [ -e $OPENGNSYS/client/bin/rsync ] && mv -f $OPENGNSYS/client/bin/rsync $OPENGNSYS/client/bin/rsync-$($OPENGNSYS/client/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}') |
---|
363 | fi |
---|
364 | # Unmount SquashFS. |
---|
365 | umount $TMPDIR |
---|
366 | rmdir $TMPDIR |
---|
367 | # Update JSON file. |
---|
368 | addToJson "$OGLIVEDIST" "$OGLIVEKRNL" "$OGLIVEARCH" "$OGLIVEREV" "$OGLIVEDIR" "$OGLIVEFILE" |
---|
369 | } |
---|
370 | |
---|
371 | # Uninstall an ogLive client. |
---|
372 | function uninstall() { |
---|
373 | local ISO DIR INDEX DEFINDEX |
---|
374 | [ $# -ne 1 ] && raiseError usage |
---|
375 | [ ! -r $INFOFILE ] && raiseError access "Configuration file." |
---|
376 | [ ! -w $TFTPDIR ] && raiseError access "Installation directory." |
---|
377 | # Get index and directory for the entry. |
---|
378 | case "$1" in |
---|
379 | */*) # Error (access to other directory). |
---|
380 | raiseError access "Cannot access outside installation directory." |
---|
381 | ;; |
---|
382 | *.iso) # ISO file. |
---|
383 | ISO="$1" |
---|
384 | # Working directory (ogLive-Distribution-KernelVersion-CodeRevision). |
---|
385 | DIR="$(echo $ISO|cut -f1,3 -d-)-${ISO##*-}"; DIR=${DIR%.*} |
---|
386 | INDEX=$(search $DIR 2>/dev/null) |
---|
387 | ;; |
---|
388 | [0-9]*) # Index. |
---|
389 | INDEX=$1; DIR=$(search $INDEX 2>/dev/null) |
---|
390 | ;; |
---|
391 | *) # Directory. |
---|
392 | DIR="$1"; INDEX=$(search $DIR 2>/dev/null) |
---|
393 | ;; |
---|
394 | esac |
---|
395 | DEFINDEX=$(getdefault) |
---|
396 | [[ $INDEX = $DEFINDEX ]] && raiseError access "Cannot uninstall default ogLive." |
---|
397 | # Remove files and delete index entry. |
---|
398 | rm -vfr ${ISO:+$DOWNLOADDIR/$ISO} ${DIR:+$TFTPDIR/$DIR} ### Remove $TFTPDIR/$DIR.old ? |
---|
399 | if [ -n "$INDEX" ]; then |
---|
400 | jq "del(.oglive[$INDEX])" $INFOFILE | sponge $INFOFILE |
---|
401 | # Decrement default index if needed (removed < default). |
---|
402 | [[ $INDEX < $DEFINDEX ]] && jq ".default=$((DEFINDEX-1))" $INFOFILE | sponge $INFOFILE |
---|
403 | fi |
---|
404 | } |
---|
405 | |
---|
406 | # Get default ogLive index. |
---|
407 | function getdefault() { |
---|
408 | [ $# -ne 0 ] && raiseError usage |
---|
409 | [ ! -r $INFOFILE ] && raiseError access "Configuration file." |
---|
410 | # Read default parameter. |
---|
411 | jq -r .default $INFOFILE || raiseError notfound "Undefined default index." |
---|
412 | } |
---|
413 | |
---|
414 | # Set default ogLive index. |
---|
415 | function setdefault() { |
---|
416 | local INDEX OGLIVEDIR |
---|
417 | [ $# -ne 1 ] && raiseError usage |
---|
418 | [ ! -w $INFOFILE ] && raiseError access "Configuration file." |
---|
419 | INDEX=$1 |
---|
420 | # Check if index entry exists. |
---|
421 | jq ".oglive[$INDEX]" $INFOFILE || raiseError notfound "Index \"$INDEX\"." |
---|
422 | # Get ogLive directory. |
---|
423 | OGLIVEDIR=$(jq -r ".oglive[$INDEX].directory" $INFOFILE) || raiseError notfound "Directory for index \"$INDEX\"." |
---|
424 | # Update default parameter. |
---|
425 | jq ".default=$INDEX" $INFOFILE | sponge $INFOFILE |
---|
426 | # Link to default directory. |
---|
427 | rm -f $TFTPDIR/$DEFOGLIVE |
---|
428 | ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE |
---|
429 | } |
---|
430 | |
---|
431 | # Rebuild a lost configuration file. |
---|
432 | function rebuild() { |
---|
433 | local INST i NF DEF |
---|
434 | [ $# -ne 0 ] && raiseError usage |
---|
435 | [ -f $INFOFILE ] && raiseError access "Configuration file exists." |
---|
436 | INST=$(find $TFTPDIR/ -type d -name "$DEFOGLIVE-*" -a ! -name "*.old" -printf "%f\n" | sort) |
---|
437 | for i in $INST; do |
---|
438 | NF=$(echo $i | awk -F- '{print NF-1}') |
---|
439 | case $NF in |
---|
440 | 1) addToJson "" "$(echo $i|cut -f2 -d-)" "i386" "" "$i" "" ;; |
---|
441 | 4) addToJson $(echo $i | awk -F- '{printf "%s %s i386 %s %s '""'",$2,$3,$4,$0}') ;; |
---|
442 | 5) addToJson $(echo $i | awk -F- '{printf "%s %s %s %s %s '""'",$2,$3,$4,$5,$0}') ;; |
---|
443 | esac |
---|
444 | # Check for is default oglive. |
---|
445 | [ -n "$(stat -c "%N" $TFTPDIR/$DEFOGLIVE | awk '$3~/'$i'/ {print}')" ] && DEF="$i" |
---|
446 | done |
---|
447 | # Set default ogLive. |
---|
448 | [ -n "$DEF" ] && setdefault $(search $DEF) |
---|
449 | } |
---|
450 | |
---|
451 | # Assign an ISO file to a JSON entry. |
---|
452 | function assign() { |
---|
453 | local ISOFILE DIR |
---|
454 | [ $# -ne 2 ] && raiseError usage |
---|
455 | [ ! -w $INFOFILE ] && raiseError access "Configuration file." |
---|
456 | # Check if exist ISO file and index directory. |
---|
457 | ISOFILE=$DOWNLOADFILE/$1 |
---|
458 | [ ! -f $DOWNLOADDIR/$ISOFILE ] && raiseError notfound "ISO file \"$1\"." |
---|
459 | DIR=$(search $2 2>/dev/null) |
---|
460 | [ ! -d $TFTPDIR/$DIR ] && raiseError notfound "Directory for index \"$2\"." |
---|
461 | # Assign ISO file to JSON entry. |
---|
462 | jq ".oglive[$2].iso=\"$1\"" $INFOFILE | sponge $INFOFILE && jq ".oglive[$2]" $INFOFILE |
---|
463 | } |
---|
464 | |
---|
465 | |
---|
466 | # Main progrram. |
---|
467 | |
---|
468 | # Access control. |
---|
469 | [ -r $OPENGNSYS/www/controlacceso.php ] && ACCESS="web" |
---|
470 | [ "$USER" = "root" ] && ACCESS="root" |
---|
471 | [ -z "$ACCESS" ] && raiseError access "Need to be root." |
---|
472 | # Check dependencies. |
---|
473 | JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"." |
---|
474 | which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"." |
---|
475 | # Commands control. |
---|
476 | shopt -s extglob |
---|
477 | case "$ACCESS" in |
---|
478 | root) CMDS='+(help|convert|config|check|list|show|search|download|install|uninstall|get-default|set-default|rebuild|assign)' ;; |
---|
479 | web) CMDS='+(list|show|search|get-default)' ;; |
---|
480 | esac |
---|
481 | case "$1" in |
---|
482 | $CMDS) COMMAND="${1/-/}"; shift; $COMMAND "$@" ;; |
---|
483 | *) raiseError usage ;; |
---|
484 | esac |
---|
485 | |
---|
486 | exit $? |
---|
487 | |
---|