source: server/bin/oglivecli @ b50031b

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 b50031b was b17e87e, checked in by ramon <ramongomez@…>, 7 years ago

#739 #768: Cambios menores:

  • Crear entradas correctas en DHCP de entorno Vagrant.
  • Permisos correctos en todos los clientes ogLive.
  • Obtener datos correctos al ejecutar oglivecli convert.

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

  • Property mode set to 100755
File size: 19.8 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)"
134    addToJson "$(echo $OGLIVEISO|cut -f2 -d-)" "$OGLIVEKRNL" "i386" "${OGLIVEISO##*-}" "$OGLIVEDIR" "$OGLIVEISO.iso"
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=$(realpath $DOWNLOADDIR/$1)
293    # Only 1 file in pathname expansion.
294    [ $(echo $OGLIVEFILE | wc -w) -gt 1 ] && raiseError usage
295    [ ! -f $OGLIVEFILE ] && raiseError notfound "Downloaded file: \"$1\"."
296    [ ! -r $OGLIVEFILE ] && raiseError access "Downloaded file: \"$1\"."
297    [ ! -w $(dirname $INFOFILE) ] && raiseError access "Configuration directory."
298    [ ! -w $TFTPDIR ] && raiseError access "Installation directory."
299    [ -z "$(file -b $OGLIVEFILE | grep "ISO.*ogClient")" ] && raiseError access "File is not an ogLive ISO image."
300    # Working directory (ogLive-Distribution-KernelVersion-Architecture-CodeRevision).
301    OGLIVEDIST="$(echo $OGLIVEFILE|cut -f2 -d-)"
302    OGLIVEREV="${OGLIVEFILE##*-}"; OGLIVEREV="${OGLIVEREV%.*}"
303    OGLIVEKRNL="$(echo $OGLIVEFILE|cut -f3- -d-)"; OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEREV.*}"
304    OGLIVEARCH="$(echo $OGLIVEFILE|awk -F- '{print $(NF-1)}')"
305    case "$OGLIVEARCH" in
306        i386|amd64) # Get architecture.
307            OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEARCH}" ;;
308        *)          # 32-bit by default.
309            OGLIVEARCH="i386" ;;
310    esac
311    OGLIVEDIR="$TFTPDIR/$DEFOGLIVE-$OGLIVEDIST-${OGLIVEKRNL%%-*}-$OGLIVEARCH-$OGLIVEREV"
312    # Get current or default Samba key.
313    OGINITRD=$OGLIVEDIR/oginitrd.img
314    [ ! -r $OGINITRD ] && OGINITRD=$TFTPDIR/$DEFOGLIVE/oginitrd.img
315    if [ -r $OGINITRD ]; then
316    SAMBAPASS=$(gzip -dc $OGINITRD | \
317    cpio -i --to-stdout scripts/ogfunctions 2>&1 | \
318            grep "^[    ].*OPTIONS=" | \
319            sed 's/\(.*\)pass=\(\w*\)\(.*\)/\2/')
320    fi
321    # Make ogLive backup.
322    rm -fr ${OGLIVEDIR}.old
323    mv -fv $OGLIVEDIR ${OGLIVEDIR}.old 2>/dev/null
324    # Mount ogLive ISO image, update its files and unmount it.
325    TMPDIR=/tmp/${OGLIVEFILE%.iso}
326    mkdir -p $OGLIVEDIR $TMPDIR
327    trap "umount $TMPDIR; rm -fr $TMPDIR" 1 2 3 6 9 15
328    mount -o loop,ro $OGLIVEFILE $TMPDIR
329    cp -va $TMPDIR/ogclient/* $OGLIVEDIR || raiseError access "Cannot copy files to ogLive directory."
330    umount $TMPDIR
331    # Link to default directory if it's the first ogLive.
332    if [ ! -f $INFOFILE ]; then
333        rm -f $TFTPDIR/$DEFOGLIVE $TFTPDIR/$OGCLIENT
334        ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE
335        ln -vfs $DEFOGLIVE $TFTPDIR/$OGCLIENT
336    fi
337    # Recover or ask for a new Samba access key.
338    if [ -n "$SAMBAPASS" ]; then
339        echo -ne "$SAMBAPASS\n$SAMBAPASS\n" | $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)"
340    else
341        $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)"
342    fi
343    # Set permissions.
344    find -L $OGLIVEDIR -type d -exec chmod 755 {} \;
345    find -L $OGLIVEDIR -type f -exec chmod 644 {} \;
346    chown -R :opengnsys $OGLIVEDIR
347    # Mount SquashFS and check Rsync version.
348    OGSQFS=$OGLIVEDIR/ogclient.sqfs
349    mount -o loop,ro $OGSQFS $TMPDIR
350    # If Rsync server version > client version, link to compiled file.
351    RSYNCSERV=$(rsync --version 2>/dev/null | awk '/protocol/ {print $6}')
352    RSYNCCLNT=$(chroot $TMPDIR /usr/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}')
353    if [ -z "$RSYNCSERV" -o ${RSYNCSERV:-0} -gt ${RSYNCCLNT:-1} ]; then
354        [ -e $OPENGNSYS/client/bin/rsync-$RSYNCSERV ] && mv -f $OPENGNSYS/client/bin/rsync-$RSYNCSERV $OPENGNSYS/client/bin/rsync
355    else
356        # Else, rename compiled file using Rsync protocol number.
357        [ -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}')
358    fi
359    # Unmount SquashFS.
360    umount $TMPDIR
361    rmdir $TMPDIR
362    # Update JSON file.
363    addToJson "$OGLIVEDIST" "$OGLIVEKRNL" "$OGLIVEARCH" "$OGLIVEREV" "$OGLIVEDIR" "$OGLIVEFILE"
364}
365
366# Uninstall an ogLive client.
367function uninstall() {
368    local ISO DIR INDEX DEFINDEX
369    [ $# -ne 1 ] && raiseError usage
370    [ ! -r $INFOFILE ] && raiseError access "Configuration file."
371    [ ! -w $TFTPDIR ] && raiseError access "Installation directory."
372    # Get index and directory for the entry.
373    case "$1" in
374        */*)    # Error (access to other directory).
375            raiseError access "Cannot access outside installation directory."
376            ;;
377        *.iso)  # ISO file.
378            ISO="$1"
379            # Working directory (ogLive-Distribution-KernelVersion-CodeRevision).
380            DIR="$(echo $ISO|cut -f1,3 -d-)-${ISO##*-}"; DIR=${DIR%.*}
381            INDEX=$(search $DIR 2>/dev/null)
382            ;;
383        [0-9]*) # Index.
384            INDEX=$1; DIR=$(search $INDEX 2>/dev/null)
385            ;;
386        *)      # Directory.
387            DIR="$1"; INDEX=$(search $DIR 2>/dev/null)
388            ;;
389    esac
390    DEFINDEX=$(getdefault)
391    [[ $INDEX = $DEFINDEX ]] && raiseError access "Cannot uninstall default ogLive."
392    # Remove files and delete index entry.
393    rm -vfr ${ISO:+$DOWNLOADDIR/$ISO} ${DIR:+$TFTPDIR/$DIR}    ### Remove $TFTPDIR/$DIR.old ?
394    if [ -n "$INDEX" ]; then
395        jq "del(.oglive[$INDEX])" $INFOFILE | sponge $INFOFILE
396        # Decrement default index if needed (removed < default).
397        [[ $INDEX < $DEFINDEX ]] && jq ".default=$((DEFINDEX-1))" $INFOFILE | sponge $INFOFILE
398    fi
399}
400
401# Get default ogLive index.
402function getdefault() {
403    [ $# -ne 0 ] && raiseError usage
404    [ ! -r $INFOFILE ] && raiseError access "Configuration file."
405    # Read default parameter.
406    jq -r .default $INFOFILE || raiseError notfound "Undefined default index."
407}
408
409# Set default ogLive index.
410function setdefault() {
411    local INDEX OGLIVEDIR
412    [ $# -ne 1 ] && raiseError usage
413    [ ! -w $INFOFILE ] && raiseError access "Configuration file."
414    INDEX=$1
415    # Check if index entry exists.
416    jq ".oglive[$INDEX]" $INFOFILE || raiseError notfound "Index \"$INDEX\"."
417    # Get ogLive directory.
418    OGLIVEDIR=$(jq -r ".oglive[$INDEX].directory" $INFOFILE) || raiseError notfound "Directory for index \"$INDEX\"."
419    # Update default parameter.
420    jq ".default=$INDEX" $INFOFILE | sponge $INFOFILE
421    # Link to default directory.
422    rm -f $TFTPDIR/$DEFOGLIVE
423    ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE
424}
425
426# Rebuild a lost configuration file.
427function rebuild() {
428    local INST i NF DEF
429    [ $# -ne 0 ] && raiseError usage
430    [ -f $INFOFILE ] && raiseError access "Configuration file exists."
431    INST=$(find $TFTPDIR/ -type d -name "$DEFOGLIVE-*" -a ! -name "*.old" -printf "%f\n" | sort)
432    for i in $INST; do
433        NF=$(echo $i | awk -F- '{print NF-1}')
434        case $NF in
435            1)  addToJson "" "$(echo $i|cut -f2 -d-)" "i386" "" "$i" "" ;;
436            4)  addToJson $(echo $i | awk -F- '{printf "%s %s i386 %s %s '""'",$2,$3,$4,$0}') ;;
437            5)  addToJson $(echo $i | awk -F- '{printf "%s %s %s %s %s '""'",$2,$3,$4,$5,$0}') ;;
438        esac
439        # Check for is default oglive.
440        [ -n "$(stat -c "%N" $TFTPDIR/$DEFOGLIVE | awk '$3~/'$i'/ {print}')" ] && DEF="$i"
441    done
442    # Set default ogLive.
443    [ -n "$DEF" ] && setdefault $(search $DEF)
444}
445
446# Assign an ISO file to a JSON entry.
447function assign() {
448local ISOFILE DIR
449    [ $# -ne 2 ] && raiseError usage
450    [ ! -w $INFOFILE ] && raiseError access "Configuration file."
451    # Check if exist ISO file and index directory.
452    ISOFILE=$DOWNLOADFILE/$1
453    [ ! -f $DOWNLOADDIR/$ISOFILE ] && raiseError notfound "ISO file \"$1\"."
454    DIR=$(search $2 2>/dev/null)
455    [ ! -d $TFTPDIR/$DIR ] && raiseError notfound "Directory for index \"$2\"."
456    # Assign ISO file to JSON entry.
457    jq ".oglive[$2].iso=\"$1\"" $INFOFILE | sponge $INFOFILE && jq ".oglive[$2]" $INFOFILE
458}
459
460
461# Main progrram.
462
463# Access control.
464[ -r $OPENGNSYS/www/controlacceso.php ] && ACCESS="web"
465[ "$USER" = "root" ] && ACCESS="root"
466[ -z "$ACCESS" ] && raiseError access "Need to be root."
467# Check dependencies.
468JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"."
469which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"."
470# Commands control.
471shopt -s extglob
472case "$ACCESS" in
473    root)   CMDS='+(help|convert|config|check|list|show|search|download|install|uninstall|get-default|set-default|rebuild|assign)' ;;
474    web)    CMDS='+(list|show|search|get-default)' ;;
475esac
476case "$1" in
477    $CMDS)  COMMAND="${1/-/}"; shift; $COMMAND "$@" ;;
478    *)      raiseError usage ;;
479esac
480
481exit $?
482
Note: See TracBrowser for help on using the repository browser.