source: repoman/bin/checkrepo @ 4073d14

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 4073d14 was 9d773c0, checked in by ramon <ramongomez@…>, 8 years ago

#810: Indicar si existen backups de imágenes en ruta RREST /repository/images.

git-svn-id: https://opengnsys.es/svn/branches/version1.1@5460 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    # JSON-formatted new entry.
43    JSON=$(cat << EOT | jq .
44{
45  "name":"$IMAGENAME",
46  "type":"${IMAGETYPE,,}",
47  "clientname":"$CLIENT",
48  "clonator":"${CLONATOR,,}",
49  "compressor":"${COMPRESSOR,,}",
50  "filesystem":"${FSTYPE^^}",
51  "datasize":${DATASIZE:-0}
52}
53EOT
54    )
55    # Check JSON file consistency.
56    if [ "$(jq -c keys $INFOFILE 2>/dev/null)" == '["directory","images","ous"]' ]; then
57        # Common image.
58        if [ -z "$OUNAME" ]; then
59            # Check if the image is defined into JSON file.
60            n=$(jq ".images | length" $INFOFILE)
61            for ((i=0; i<n; i++)); do
62                [ "$(jq ".check=$JSON | .check.name==.images[$i].name" $INFOFILE)" == "true" ] && IMGIND=$i
63            done
64            # Check if it needs to update or insert data.
65            if [ -n "$IMGIND" ]; then
66                # Update if image data changes and info file exists.
67                [ -n "$3" -a "$(jq ".check=$JSON | .check==.images[$IMGIND]" $INFOFILE)" == "false" ] && jq ".images[$IMGIND]=$JSON" $INFOFILE | sponge $INFOFILE
68            else
69                # Append a new entry.
70                jq ".images |= (. + [$JSON])" $INFOFILE | sponge $INFOFILE
71            fi
72        else    # OU image.
73            # Append a new OU entry if it does not exist.
74            if [ -z "$(jq -r ".ous[].subdir" $INFOFILE | grep "^$OUNAME$")" ]; then
75                JSON=$(cat << EOT | jq .
76{
77  "subdir": "$OUNAME",
78  "images": [ $JSON ]
79}
80EOT
81                )
82                jq ".ous |= (. + [$JSON])" $INFOFILE | sponge $INFOFILE
83            else
84                # Check if the image is defined in some OU.
85                m=$(jq ".ous | length" $INFOFILE)
86                for ((j=0; j<m; j++)); do
87                    n=$(jq ".ous[$j].images | length" $INFOFILE)
88                    for ((i=0; i<n; i++)); do
89                        [ "$(jq ".check=$JSON | .check.name==.ous[$j].images[$i].name" $INFOFILE)" == "true" ] && OUIND=$j && IMGIND=$i
90                    done
91                done
92                # Check if it needs to update or insert data.
93                if [ -n "$IMGIND" ]; then
94                    # Update if image data changes and info file exists.
95                    [ $# -gt 2 -a "$(jq ".check=$JSON | .check==.ous[$OUIND].images[$IMGIND]" $INFOFILE)" == "false" ] && jq ".ous[$OUIND].images[$IMGIND]=$JSON" $INFOFILE | sponge $INFOFILE
96                else
97                    # Append a new entry.
98                    jq ".ous[$OUIND].images |= (. + [$JSON])" $INFOFILE | sponge $INFOFILE
99                fi
100            fi
101        fi
102    else
103        # Create new JSON file.
104        if [ -z "$OUNAME" ]; then
105            cat << EOT | jq . > $INFOFILE
106{"directory":"$IMAGESDIR","images":[$JSON],"ous":[]}
107EOT
108        else
109            cat << EOT | jq . > $INFOFILE
110{"directory":"$IMAGESDIR","images":[],"ous":[{"subdir":"$OUNAME","images":[$JSON]}]}
111EOT
112        fi
113    fi
114}
115
116# Show an error message.
117function raiseError() {
118    case "$1" in
119        usage)
120            echo "$PROG: Usage error: Type \"$PROG help\"" >&2
121            exit 1 ;;
122        notfound)
123            echo "$PROG: Resource not found: $2" >&2
124            exit 2 ;;
125        access)
126            echo "$PROG: Access error: $2" >&2
127            exit 3 ;;
128        *)
129            echo "$PROG: Unknown error" >&2
130            exit 1 ;;
131    esac
132}
133
134# Command functions.
135
136# Show help message.
137function help() {
138    cat << EOT
139$PROG: maintain the repository information.
140Usage: $PROG
141EOT
142}
143
144# Check for file-based images to update the repository configuration file.
145function checkfiles() {
146    local IMAGES IMG INFO DATA
147
148    # File-stored images.
149    IMAGES=$(find $IMAGESDIR -maxdepth 2 -type f \( -name "*.img" -o -name "*.dsk" \) -print)
150    for IMG in $IMAGES; do
151        # Skip locked images.
152        [ -e "$IMG.lock" ] && continue
153        # Retrieve image creation data and delete temporary file.
154        INFO="$IMG.info"
155        [ -e "$INFO" -a "$INFO" -ot "$IMG" ] && rm -f "$INFO" && echo "Warning: Deleted outdated file $INFO"
156        DATA=""
157        [ -r "$INFO" ] && DATA=$(cat "$INFO")
158        # Add data to configuration file (name, type and data) and remove image info file.
159        IMG=${IMG#$IMAGESDIR/}
160        addToJson "${IMG%.*}" "${IMG##*.}" "$DATA" && rm -f "$INFO"
161    done
162}
163
164# Check for directory-based images to update the repository configuration file.
165function checkdirs() {
166    local IMAGES IMG INFO DATA
167
168    # Directory-based images.
169    IMAGES=$(find $IMAGESDIR -maxdepth 3 -type f -name ogimg.info -print)
170    for INFO in $IMAGES; do
171        IMG="$(dirname "${INFO#$IMAGESDIR/}")"
172        # Skip repository root directory and locked images.
173        [ "$IMG" == "$IMAGESDIR" -o -e "$IMG.lock" ] && continue
174        DATA=$(awk -F= '$1=="# fstype" {fs=$2} $1=="# sizedata" {sz=$2} END {printf "rsync::%s:%s:",fs,sz}' "$INFO")
175        # Add data to configuration file (name, type and data).
176        addToJson "$IMG" "dir" "$DATA"
177    done
178}
179
180# Check if images are removed to update the repository configuration file.
181function checkremoved() {
182    local IMG TYPE OU i j n m
183    [ ! -w "$INFOFILE" ] && raiseError access "$INFOFILE"
184
185    # Check if global images are defined into JSON file.
186    n=$(jq ".images | length" $INFOFILE)
187    for ((i=0; i<n; i++)); do
188        # Image name and type.
189        IMG="$(jq -r ".images[$i].name" $INFOFILE)"
190        TYPE="$(jq -r ".images[$i].type" $INFOFILE)"
191        [ "$TYPE" != "dir" ] && IMG="$IMG.$TYPE"
192        # Delete entry if image does not exist and it's not locked.
193        [ ! -e "$IMAGESDIR/$IMG" -a ! -e "$IMAGESDIR/$IMG.lock" ] && jq "del(.images[$i])" $INFOFILE | sponge $INFOFILE
194    done
195    # Check if OU images are defined into JSON file.
196    m=$(jq ".ous | length" $INFOFILE)
197    for ((j=0; j<m; j++)); do
198        # OU subdir.
199        OU="$(jq -r ".ous[$j].subdir" $INFOFILE)"
200        # Delete OU's entries if its subdir does not exist.
201        if [ ! -e "$IMAGESDIR/$OU" ]; then
202            jq "del(.ous[$j])" $INFOFILE | sponge $INFOFILE
203        else
204            n=$(jq ".images | length" $INFOFILE)
205            for ((i=0; i<n; i++)); do
206                # Image name and type.
207                IMG="$(jq -r ".ous[$j].images[$i].name" $INFOFILE)"
208                TYPE="$(jq -r ".ous[$j].images[$i].type" $INFOFILE)"
209                [ "$TYPE" != "dir" ] && IMG="$IMG.$TYPE"
210                # Delete entry if image does not exist and it's not locked.
211                [ ! -e "$IMAGESDIR/$OU/$IMG" -a ! -e "$IMAGESDIR/$OU/$IMG.lock" ] && jq "del(.ous[$j].images[$i])" $INFOFILE | sponge $INFOFILE
212            done
213        fi
214    done
215}
216
217
218# Main progrram.
219
220# Check dependencies.
221[ ! -w "$(dirname "$INFOFILE")" ] && raiseError access "$INFOFILE"
222JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"."
223which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"."
224
225checkfiles
226checkdirs
227checkremoved
228
Note: See TracBrowser for help on using the repository browser.