source: server/bin/oglivecli @ 240a4dc

918-git-images-111dconfigure-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-instalacion
Last change on this file since 240a4dc was 240a4dc, checked in by Ramón M. Gómez <ramongomez@…>, 5 years ago

#957: Adapt oglivecli script to global functions.

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