source: server/bin/oglivecli @ 6934ad12

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 6934ad12 was b2d7943, checked in by ramon <ramongomez@…>, 8 years ago

Usar nueva URL de descarga en script oglivecli (modificar #801).

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

  • Property mode set to 100755
File size: 19.3 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 ogLives.
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    # Print result.
206    [ $ERR -eq 0 ] && echo "OK!" || echo "Problems detected: $ERR"
207    return $ERR
208}
209
210# List installed ogLive clients.
211function list() {
212    [ $# -ne 0 ] && raiseError usage
213    [ ! -r $INFOFILE ] && raiseError access "Configuration file."
214    # List all defined indexes, directories and check if missing.
215    jq -r .oglive[].directory $INFOFILE | nl -v 0 | \
216            awk '{system("echo -n "$0"; test -d '$TFTPDIR'/"$2" || echo -n \" (missing)\"; echo")}' | column -t
217}
218
219# Show information about an installed ogLive client.
220function show() {
221    local INDEX
222    [ $# -ne 1 ] && raiseError usage
223    [ ! -r $INFOFILE ] && raiseError access "Configuration file."
224    # Show JSON entries.
225    case "$1" in
226        default)    # Default index.
227            INDEX="[$(jq -r .default $INFOFILE)]" ;;
228        all)        # All intries.
229            ;;
230        [0-9]*)     # Index.
231            INDEX="[$1]" ;;
232        *)          # Directory.
233            INDEX="[$(search "$1" 2>/dev/null)]" || raiseError notfound "Directory \"$1\"."
234            ;;
235    esac
236    jq ".oglive$INDEX" $INFOFILE || raiseError notfound "Index \"$1\"."
237}
238
239# Show index or directory corresponding to searching parameter.
240function search() {
241    [ $# -ne 1 ] && raiseError usage
242    [ ! -r $INFOFILE ] && raiseError access "Configuration file."
243    # Show corresponding index or directory.
244    list | awk -v d="$1" '{if ($2==d) print $1; if ($1==d) print $2}' | grep . || raiseError notfound "Index/Directory \"$1\""
245}
246
247# Show a menu to select and download an ogLive ISO image from the OpenGnsys website.
248function download() {
249    local OGLIVE NISOS i SOURCELENGTH TARGETFILE
250    local ISOREL MINREL=5000            # Mininum compatibility release.
251    [ $# -gt 1 ] && raiseError usage
252    [ ! -d $DOWNLOADDIR ] && raiseError notfound "Download directory"
253    [ ! -w $DOWNLOADDIR ] && raiseError access "Download directory"
254    # Check parameter.
255    if [ -n "$1" ]; then
256        # ogLive to download.
257        OGLIVEFILE="$1"
258    else
259        # Show download menu.
260        OGLIVE=( $(curl --silent $DOWNLOADURL | grep "$DEFOGLIVE.*iso") )
261        NISOS=${#OGLIVE[@]}
262        echo "Available downloads (+ = installed, * = full compatibility):"
263        for i in $(seq 1 $NISOS); do
264            [ -e $DOWNLOADDIR/${OGLIVE[i-1]} ] && OGLIVE[i-1]="(+) ${OGLIVE[i-1]}"
265            ISOREL=${OGLIVE[i-1]##*-r}; ISOREL=${ISOREL%.*}
266            [ $ISOREL -ge $MINREL ] && OGLIVE[i-1]="(*) ${OGLIVE[i-1]}"
267        done
268        select opt in "${OGLIVE[@]}"; do
269            [ -n "$opt" ] && OGLIVEFILE=${opt#* } && break
270        done
271    fi
272    # Get download size.
273    SOURCELENGTH=$(curl --head --silent $DOWNLOADURL/$OGLIVEFILE | awk -F: '/Content-Length:/ {print $2}')
274    [ -n "$SOURCELENGTH" ] || raiseError download "$OGLIVEFILE"
275    # Download ogLive.
276    TARGETFILE=$DOWNLOADDIR/$OGLIVEFILE
277    trap "rm -f $TARGETFILE" 1 2 3 6 9 15
278    curl $DOWNLOADURL/$OGLIVEFILE -o $TARGETFILE || raiseError download "$OGLIVEFILE"
279}
280
281# Install an ogLive client from a previously downloaded ISO image.
282function install() {
283    local OGLIVEFILE OGLIVEDIST OGLIVEREV OGLIVEKRNL OGLIVEDIR OGINITRD OGSQFS OGCLIENT=ogclient
284    local SAMBAPASS TMPDIR RSYNCSERV RSYNCCLNT
285    [ $# -ne 1 ] && raiseError usage
286    OGLIVEFILE=$DOWNLOADDIR/"$1"
287    [ ! -f $OGLIVEFILE ] && raiseError notfound "Downloaded file: \"$1\"."
288    [ ! -r $OGLIVEFILE ] && raiseError access "Downloaded file: \"$1\"."
289    [ ! -w $(dirname $INFOFILE) ] && raiseError access "Configuration directory."
290    [ ! -w $TFTPDIR ] && raiseError access "Installation directory."
291    [ -z "$(file -b $OGLIVEFILE | grep "ISO.*ogClient")" ] && raiseError access "File is not an ogLive ISO image."
292    # Working directory (ogLive-Distribution-KernelVersion-Architecture-CodeRevision).
293    OGLIVEDIST="$(echo $OGLIVEFILE|cut -f2 -d-)"
294    OGLIVEREV="${OGLIVEFILE##*-}"; OGLIVEREV="${OGLIVEREV%.*}"
295    OGLIVEKRNL="$(echo $OGLIVEFILE|cut -f3- -d-)"; OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEREV.*}"
296    OGLIVEARCH="$(echo $OGLIVEFILE|awk -F- '{print $(NF-1)}')"
297    case "$OGLIVEARCH" in
298        i386|amd64) # Get architecture.
299            OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEARCH}" ;;
300        *)          # 32-bit by default.
301            OGLIVEARCH="i386" ;;
302    esac
303    OGLIVEDIR="$TFTPDIR/$DEFOGLIVE-$OGLIVEDIST-${OGLIVEKRNL%%-*}-$OGLIVEARCH-$OGLIVEREV"
304    # Get current or default Samba key.
305    OGINITRD=$OGLIVEDIR/oginitrd.img
306    [ ! -r $OGINITRD ] && OGINITRD=$TFTPDIR/$DEFOGLIVE/oginitrd.img
307    if [ -r $OGINITRD ]; then
308    SAMBAPASS=$(gzip -dc $OGINITRD | \
309    cpio -i --to-stdout scripts/ogfunctions 2>&1 | \
310            grep "^[    ].*OPTIONS=" | \
311            sed 's/\(.*\)pass=\(\w*\)\(.*\)/\2/')
312    fi
313    # Make ogLive backup.
314    rm -fr ${OGLIVEDIR}.old
315    mv -fv $OGLIVEDIR ${OGLIVEDIR}.old 2>/dev/null
316    # Mount ogLive ISO image, update its files and unmount it.
317    TMPDIR=/tmp/${OGLIVEFILE%.iso}
318    mkdir -p $OGLIVEDIR $TMPDIR
319    trap "umount $TMPDIR; rm -fr $TMPDIR" 1 2 3 6 9 15
320    mount -o loop,ro $OGLIVEFILE $TMPDIR
321    cp -va $TMPDIR/ogclient/* $OGLIVEDIR || raiseError access "Cannot copy files to ogLive directory."
322    umount $TMPDIR
323    # Link to default directory if it's the first ogLive.
324    if [ ! -f $INFOFILE ]; then
325        rm -f $TFTPDIR/$DEFOGLIVE $TFTPDIR/$OGCLIENT
326        ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE
327        ln -vfs $DEFOGLIVE $TFTPDIR/$OGCLIENT
328    fi
329    # Recover or ask for a new Samba access key.
330    if [ -n "$SAMBAPASS" ]; then
331        echo -ne "$SAMBAPASS\n$SAMBAPASS\n" | $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)"
332    else
333        $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)"
334    fi
335    # Set permissions.
336    find -L $OGLIVEDIR -type d -exec chmod 755 {} \;
337    find -L $OGLIVEDIR -type f -exec chmod 644 {} \;
338    chown -R :opengnsys $OGLIVEDIR
339    # Mount SquashFS and check Rsync version.
340    OGSQFS=$OGLIVEDIR/ogclient.sqfs
341    mount -o loop,ro $OGSQFS $TMPDIR
342    # If Rsync server version > client version, link to compiled file.
343    RSYNCSERV=$(rsync --version 2>/dev/null | awk '/protocol/ {print $6}')
344    RSYNCCLNT=$(chroot $TMPDIR /usr/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}')
345    if [ -z "$RSYNCSERV" -o ${RSYNCSERV:-0} -gt ${RSYNCCLNT:-1} ]; then
346        [ -e $OPENGNSYS/client/bin/rsync-$RSYNCSERV ] && mv -f $OPENGNSYS/client/bin/rsync-$RSYNCSERV $OPENGNSYS/client/bin/rsync
347    else
348        # Else, rename compiled file using Rsync protocol number.
349        [ -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}')
350    fi
351    # Unmount SquashFS.
352    umount $TMPDIR
353    rmdir $TMPDIR
354    # Update JSON file.
355    addToJson "$OGLIVEDIST" "$OGLIVEKRNL" "$OGLIVEARCH" "$OGLIVEREV" "$OGLIVEDIR" "$OGLIVEFILE"
356}
357
358# Uninstall an ogLive client.
359function uninstall() {
360    local ISO DIR INDEX DEFINDEX
361    [ $# -ne 1 ] && raiseError usage
362    [ ! -r $INFOFILE ] && raiseError access "Configuration file."
363    [ ! -w $TFTPDIR ] && raiseError access "Installation directory."
364    # Get index and directory for the entry.
365    case "$1" in
366        */*)    # Error (access to other directory).
367            raiseError access "Cannot access outside installation directory."
368            ;;
369        *.iso)  # ISO file.
370            ISO="$1"
371            # Working directory (ogLive-Distribution-KernelVersion-CodeRevision).
372            DIR="$(echo $ISO|cut -f1,3 -d-)-${ISO##*-}"; DIR=${DIR%.*}
373            INDEX=$(search $DIR 2>/dev/null)
374            ;;
375        [0-9]*) # Index.
376            INDEX=$1; DIR=$(search $INDEX 2>/dev/null)
377            ;;
378        *)      # Directory.
379            DIR="$1"; INDEX=$(search $DIR 2>/dev/null)
380            ;;
381    esac
382    DEFINDEX=$(getdefault)
383    [[ $INDEX = $DEFINDEX ]] && raiseError access "Cannot uninstall default ogLive."
384    # Remove files and delete index entry.
385    rm -vfr ${ISO:+$DOWNLOADDIR/$ISO} ${DIR:+$TFTPDIR/$DIR}    ### Remove $TFTPDIR/$DIR.old ?
386    if [ -n "$INDEX" ]; then
387        jq "del(.oglive[$INDEX])" $INFOFILE | sponge $INFOFILE
388        # Decrement default index if needed (removed < default).
389        [[ $INDEX < $DEFINDEX ]] && jq ".default=$((DEFINDEX-1))" $INFOFILE | sponge $INFOFILE
390    fi
391}
392
393# Get default ogLive index.
394function getdefault() {
395    [ $# -ne 0 ] && raiseError usage
396    [ ! -r $INFOFILE ] && raiseError access "Configuration file."
397    # Read default parameter.
398    jq -r .default $INFOFILE || raiseError notfound "Undefined default index."
399}
400
401# Set default ogLive index.
402function setdefault() {
403    local INDEX OGLIVEDIR
404    [ $# -ne 1 ] && raiseError usage
405    [ ! -w $INFOFILE ] && raiseError access "Configuration file."
406    INDEX=$1
407    # Check if index entry exists.
408    jq ".oglive[$INDEX]" $INFOFILE || raiseError notfound "Index \"$INDEX\"."
409    # Get ogLive directory.
410    OGLIVEDIR=$(jq -r ".oglive[$INDEX].directory" $INFOFILE) || raiseError notfound "Directory for index \"$INDEX\"."
411    # Update default parameter.
412    jq ".default=$INDEX" $INFOFILE | sponge $INFOFILE
413    # Link to default directory.
414    rm -f $TFTPDIR/$DEFOGLIVE
415    ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE
416}
417
418# Rebuild a lost configuration file.
419function rebuild() {
420    local INST i NF DEF
421    [ $# -ne 0 ] && raiseError usage
422    [ -f $INFOFILE ] && raiseError access "Configuration file exists."
423    INST=$(find $TFTPDIR/ -type d -name "$DEFOGLIVE-*" -a ! -name "*.old" -printf "%f\n" | sort)
424    for i in $INST; do
425        NF=$(echo $i | awk -F- '{print NF-1}')
426        case $NF in
427            1)  addToJson "" "$(echo $i|cut -f2 -d-)" "i386" "" "$i" "" ;;
428            4)  addToJson $(echo $i | awk -F- '{printf "%s %s i386 %s %s '""'",$2,$3,$4,$0}') ;;
429            5)  addToJson $(echo $i | awk -F- '{printf "%s %s %s %s %s '""'",$2,$3,$4,$5,$0}') ;;
430        esac
431        # Check for is default oglive.
432        [ -n "$(stat -c "%N" $TFTPDIR/$DEFOGLIVE | awk '$3~/'$i'/ {print}')" ] && DEF="$i"
433    done
434    # Set default ogLive.
435    [ -n "$DEF" ] && setdefault $(search $DEF)
436}
437
438# Assign an ISO file to a JSON entry.
439function assign() {
440local ISOFILE DIR
441    [ $# -ne 2 ] && raiseError usage
442    [ ! -w $INFOFILE ] && raiseError access "Configuration file."
443    # Check if exist ISO file and index directory.
444    ISOFILE=$DOWNLOADFILE/$1
445    [ ! -f $DOWNLOADDIR/$ISOFILE ] && raiseError notfound "ISO file \"$1\"."
446    DIR=$(search $2 2>/dev/null)
447    [ ! -d $TFTPDIR/$DIR ] && raiseError notfound "Directory for index \"$2\"."
448    # Assign ISO file to JSON entry.
449    jq ".oglive[$2].iso=\"$1\"" $INFOFILE | sponge $INFOFILE && jq ".oglive[$2]" $INFOFILE
450}
451
452
453# Main progrram.
454
455# Access control.
456[ -r $OPENGNSYS/www/controlacceso.php ] && ACCESS="web"
457[ "$USER" = "root" ] && ACCESS="root"
458[ -z "$ACCESS" ] && raiseError access "Need to be root."
459# Check dependencies.
460JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"."
461which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"."
462# Commands control.
463shopt -s extglob
464case "$ACCESS" in
465    root)   CMDS='+(help|convert|config|check|list|show|search|download|install|uninstall|get-default|set-default|rebuild|assign)' ;;
466    web)    CMDS='+(list|show|search|get-default)' ;;
467esac
468case "$1" in
469    $CMDS)  COMMAND="${1/-/}"; shift; $COMMAND "$@" ;;
470    *)      raiseError usage ;;
471esac
472
473exit $?
474
Note: See TracBrowser for help on using the repository browser.