source: server/bin/oglivecli @ 550192f

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since 550192f was f60723c, checked in by ramon <ramongomez@…>, 7 years ago

#768: oglivecli check comprueba también si hay nuevas descargas de imágenes ISO después de haber sido instaladas.

git-svn-id: https://opengnsys.es/svn/branches/version1.1@5525 a21b9725-9963-47de-94b9-378ad31fedc9

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