source: repoman/bin/checkrepo @ 9215580

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

#810: Convertir datos a bytes para ruta REST /repository/images; propiedades de repositorio convierte tamaños a valor legible.

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

  • Property mode set to 100755
File size: 7.9 KB
Line 
1#!/bin/bash
2
3#/**
4#         checkrepo
5#@file    checkrepo
6#@brief   Generate repository information in a JSON file.
7#@warning This script uses "jq" command.
8#@version 1.1.0 - Initial version.
9#@author  Ramón M. Gómez - ETSII Univ. Sevilla
10#@date    2017-09-27
11#*/ ##
12
13
14# Global constants definition.
15PROG=$(basename "$(realpath "$0")")
16OPENGNSYS=/opt/opengnsys
17IMAGESDIR=$OPENGNSYS/images
18INFOFILE=$OPENGNSYS/etc/repoinfo.json
19
20
21# Auxiliar functions.
22
23# Metafunction to check if JSON result exists.
24function jq() {
25    local OUTPUT
26    OUTPUT=$($JQ "$@") || return $?
27    [[ "$OUTPUT" = "null" ]] && return 1
28    echo "$OUTPUT"
29}
30
31# Create/edit JSON file about installed ogLive clients.
32function addToJson() {
33    # Parameters and variables.
34    local IMAGENAME="$1" IMAGETYPE="$2" DATA="$3" JSON i j n m OUNAME OUIND IMGIND
35    local CLONATOR COMPRESSOR FSTYPE DATASIZE CLIENT
36    IFS=":" read -r CLONATOR COMPRESSOR FSTYPE DATASIZE CLIENT <<<"$DATA"
37    # Check if image is restricted to an OU (subdir).
38    if [[ $IMAGENAME =~ / ]]; then
39        OUNAME="${IMAGENAME%/*}"
40        IMAGENAME="${IMAGENAME##*/}"
41    fi
42    # Data size must be numeric (in KB).
43    [[ $DATASIZE =~ ^[0-9]*$ ]] || DATASIZE=0
44    # JSON-formatted new entry.
45    JSON=$(cat << EOT | jq .
46{
47  "name":"$IMAGENAME",
48  "type":"${IMAGETYPE,,}",
49  "clientname":"$CLIENT",
50  "clonator":"${CLONATOR,,}",
51  "compressor":"${COMPRESSOR,,}",
52  "filesystem":"${FSTYPE^^}",
53  "datasize":$[ DATASIZE * 1024]
54}
55EOT
56    )
57    # Check JSON file consistency.
58    if [ "$(jq -c keys $INFOFILE 2>/dev/null)" == '["directory","images","ous"]' ]; then
59        # Common image.
60        if [ -z "$OUNAME" ]; then
61            # Check if the image is defined into JSON file.
62            n=$(jq ".images | length" $INFOFILE)
63            for ((i=0; i<n; i++)); do
64                [ "$(jq ".check=$JSON | .check.name==.images[$i].name" $INFOFILE)" == "true" ] && IMGIND=$i
65            done
66            # Check if it needs to update or insert data.
67            if [ -n "$IMGIND" ]; then
68                # Update if image data changes and info file exists.
69                [ -n "$3" -a "$(jq ".check=$JSON | .check==.images[$IMGIND]" $INFOFILE)" == "false" ] && jq ".images[$IMGIND]=$JSON" $INFOFILE | sponge $INFOFILE
70            else
71                # Append a new entry.
72                jq ".images |= (. + [$JSON])" $INFOFILE | sponge $INFOFILE
73            fi
74        else    # OU image.
75            # Append a new OU entry if it does not exist.
76            if [ -z "$(jq -r ".ous[].subdir" $INFOFILE | grep "^$OUNAME$")" ]; then
77                JSON=$(cat << EOT | jq .
78{
79  "subdir": "$OUNAME",
80  "images": [ $JSON ]
81}
82EOT
83                )
84                jq ".ous |= (. + [$JSON])" $INFOFILE | sponge $INFOFILE
85            else
86                # Check if the image is defined in some OU.
87                m=$(jq ".ous | length" $INFOFILE)
88                for ((j=0; j<m; j++)); do
89                    n=$(jq ".ous[$j].images | length" $INFOFILE)
90                    for ((i=0; i<n; i++)); do
91                        [ "$(jq ".check=$JSON | .check.name==.ous[$j].images[$i].name" $INFOFILE)" == "true" ] && OUIND=$j && IMGIND=$i
92                    done
93                done
94                # Check if it needs to update or insert data.
95                if [ -n "$IMGIND" ]; then
96                    # Update if image data changes and info file exists.
97                    [ $# -gt 2 -a "$(jq ".check=$JSON | .check==.ous[$OUIND].images[$IMGIND]" $INFOFILE)" == "false" ] && jq ".ous[$OUIND].images[$IMGIND]=$JSON" $INFOFILE | sponge $INFOFILE
98                else
99                    # Append a new entry.
100                    jq ".ous[$OUIND].images |= (. + [$JSON])" $INFOFILE | sponge $INFOFILE
101                fi
102            fi
103        fi
104    else
105        # Create new JSON file.
106        if [ -z "$OUNAME" ]; then
107            cat << EOT | jq . > $INFOFILE
108{"directory":"$IMAGESDIR","images":[$JSON],"ous":[]}
109EOT
110        else
111            cat << EOT | jq . > $INFOFILE
112{"directory":"$IMAGESDIR","images":[],"ous":[{"subdir":"$OUNAME","images":[$JSON]}]}
113EOT
114        fi
115    fi
116}
117
118# Show an error message.
119function raiseError() {
120    case "$1" in
121        usage)
122            echo "$PROG: Usage error: Type \"$PROG help\"" >&2
123            exit 1 ;;
124        notfound)
125            echo "$PROG: Resource not found: $2" >&2
126            exit 2 ;;
127        access)
128            echo "$PROG: Access error: $2" >&2
129            exit 3 ;;
130        *)
131            echo "$PROG: Unknown error" >&2
132            exit 1 ;;
133    esac
134}
135
136# Command functions.
137
138# Show help message.
139function help() {
140    cat << EOT
141$PROG: maintain the repository information.
142Usage: $PROG
143EOT
144}
145
146# Check for file-based images to update the repository configuration file.
147function checkfiles() {
148    local IMAGES IMG INFO DATA
149
150    # File-stored images.
151    IMAGES=$(find $IMAGESDIR -maxdepth 2 -type f \( -name "*.img" -o -name "*.dsk" \) -print)
152    for IMG in $IMAGES; do
153        # Skip locked images.
154        [ -e "$IMG.lock" ] && continue
155        # Retrieve image creation data and delete temporary file.
156        INFO="$IMG.info"
157        [ -e "$INFO" -a "$INFO" -ot "$IMG" ] && rm -f "$INFO" && echo "Warning: Deleted outdated file $INFO"
158        DATA=""
159        [ -r "$INFO" ] && DATA=$(cat "$INFO")
160        # Add data to configuration file (name, type and data) and remove image info file.
161        IMG=${IMG#$IMAGESDIR/}
162        addToJson "${IMG%.*}" "${IMG##*.}" "$DATA" && rm -f "$INFO"
163    done
164}
165
166# Check for directory-based images to update the repository configuration file.
167function checkdirs() {
168    local IMAGES IMG INFO DATA
169
170    # Directory-based images.
171    IMAGES=$(find $IMAGESDIR -maxdepth 3 -type f -name ogimg.info -print)
172    for INFO in $IMAGES; do
173        IMG="$(dirname "${INFO#$IMAGESDIR/}")"
174        # Skip repository root directory and locked images.
175        [ "$IMG" == "$IMAGESDIR" -o -e "$IMG.lock" ] && continue
176        DATA=$(awk -F= '$1=="# fstype" {fs=$2} $1=="# sizedata" {sz=$2} END {printf "rsync::%s:%s:",fs,sz}' "$INFO")
177        # Add data to configuration file (name, type and data).
178        addToJson "$IMG" "dir" "$DATA"
179    done
180}
181
182# Check if images are removed to update the repository configuration file.
183function checkremoved() {
184    local IMG TYPE OU i j n m
185    [ ! -w "$INFOFILE" ] && raiseError access "$INFOFILE"
186
187    # Check if global images are defined into JSON file.
188    n=$(jq ".images | length" $INFOFILE)
189    for ((i=0; i<n; i++)); do
190        # Image name and type.
191        IMG="$(jq -r ".images[$i].name" $INFOFILE)"
192        TYPE="$(jq -r ".images[$i].type" $INFOFILE)"
193        [ "$TYPE" != "dir" ] && IMG="$IMG.$TYPE"
194        # Delete entry if image does not exist and it's not locked.
195        [ ! -e "$IMAGESDIR/$IMG" -a ! -e "$IMAGESDIR/$IMG.lock" ] && jq "del(.images[$i])" $INFOFILE | sponge $INFOFILE
196    done
197    # Check if OU images are defined into JSON file.
198    m=$(jq ".ous | length" $INFOFILE)
199    for ((j=0; j<m; j++)); do
200        # OU subdir.
201        OU="$(jq -r ".ous[$j].subdir" $INFOFILE)"
202        # Delete OU's entries if its subdir does not exist.
203        if [ ! -e "$IMAGESDIR/$OU" ]; then
204            jq "del(.ous[$j])" $INFOFILE | sponge $INFOFILE
205        else
206            n=$(jq ".images | length" $INFOFILE)
207            for ((i=0; i<n; i++)); do
208                # Image name and type.
209                IMG="$(jq -r ".ous[$j].images[$i].name" $INFOFILE)"
210                TYPE="$(jq -r ".ous[$j].images[$i].type" $INFOFILE)"
211                [ "$TYPE" != "dir" ] && IMG="$IMG.$TYPE"
212                # Delete entry if image does not exist and it's not locked.
213                [ ! -e "$IMAGESDIR/$OU/$IMG" -a ! -e "$IMAGESDIR/$OU/$IMG.lock" ] && jq "del(.ous[$j].images[$i])" $INFOFILE | sponge $INFOFILE
214            done
215        fi
216    done
217}
218
219
220# Main progrram.
221
222# Check dependencies.
223[ ! -w "$(dirname "$INFOFILE")" ] && raiseError access "$INFOFILE"
224JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"."
225which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"."
226
227checkfiles
228checkdirs
229checkremoved
230
Note: See TracBrowser for help on using the repository browser.