source: repoman/bin/checkrepo

lgromero-new-oglive
Last change on this file was c28eefa, checked in by Natalia Serrano <natalia.serrano@…>, 19 months ago

Log to syslog in a number of shell scripts

  • Property mode set to 100755
File size: 7.4 KB
RevLine 
[2b1ed11]1#!/bin/bash
2
3#/**
4#@file    checkrepo
[115525a]5#@brief   Maintain repository information in a JSON file.
6#@usage   checkrepo
[2b1ed11]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
[115525a]21# Functions.
22source $OPENGNSYS/lib/ogfunctions.sh
[2b1ed11]23
24# Create/edit JSON file about installed ogLive clients.
25function addToJson() {
26    # Parameters and variables.
27    local IMAGENAME="$1" IMAGETYPE="$2" DATA="$3" JSON i j n m OUNAME OUIND IMGIND
28    local CLONATOR COMPRESSOR FSTYPE DATASIZE CLIENT
29    IFS=":" read -r CLONATOR COMPRESSOR FSTYPE DATASIZE CLIENT <<<"$DATA"
30    # Check if image is restricted to an OU (subdir).
31    if [[ $IMAGENAME =~ / ]]; then
32        OUNAME="${IMAGENAME%/*}"
33        IMAGENAME="${IMAGENAME##*/}"
34    fi
[d610135]35    # Data size must be numeric (in KB).
36    [[ $DATASIZE =~ ^[0-9]*$ ]] || DATASIZE=0
[2b1ed11]37    # JSON-formatted new entry.
38    JSON=$(cat << EOT | jq .
39{
40  "name":"$IMAGENAME",
41  "type":"${IMAGETYPE,,}",
42  "clientname":"$CLIENT",
43  "clonator":"${CLONATOR,,}",
44  "compressor":"${COMPRESSOR,,}",
45  "filesystem":"${FSTYPE^^}",
[d610135]46  "datasize":$[ DATASIZE * 1024]
[2b1ed11]47}
48EOT
49    )
50    # Check JSON file consistency.
51    if [ "$(jq -c keys $INFOFILE 2>/dev/null)" == '["directory","images","ous"]' ]; then
52        # Common image.
53        if [ -z "$OUNAME" ]; then
54            # Check if the image is defined into JSON file.
55            n=$(jq ".images | length" $INFOFILE)
56            for ((i=0; i<n; i++)); do
57                [ "$(jq ".check=$JSON | .check.name==.images[$i].name" $INFOFILE)" == "true" ] && IMGIND=$i
58            done
59            # Check if it needs to update or insert data.
60            if [ -n "$IMGIND" ]; then
61                # Update if image data changes and info file exists.
[3b43d89]62                [ -n "$3" -a "$(jq ".check=$JSON | .check==.images[$IMGIND]" $INFOFILE)" == "false" ] && jq ".images[$IMGIND]=$JSON" $INFOFILE | sponge $INFOFILE
[2b1ed11]63            else
64                # Append a new entry.
65                jq ".images |= (. + [$JSON])" $INFOFILE | sponge $INFOFILE
66            fi
67        else    # OU image.
68            # Append a new OU entry if it does not exist.
69            if [ -z "$(jq -r ".ous[].subdir" $INFOFILE | grep "^$OUNAME$")" ]; then
70                JSON=$(cat << EOT | jq .
71{
72  "subdir": "$OUNAME",
73  "images": [ $JSON ]
74}
75EOT
76                )
77                jq ".ous |= (. + [$JSON])" $INFOFILE | sponge $INFOFILE
78            else
79                # Check if the image is defined in some OU.
80                m=$(jq ".ous | length" $INFOFILE)
81                for ((j=0; j<m; j++)); do
82                    n=$(jq ".ous[$j].images | length" $INFOFILE)
83                    for ((i=0; i<n; i++)); do
84                        [ "$(jq ".check=$JSON | .check.name==.ous[$j].images[$i].name" $INFOFILE)" == "true" ] && OUIND=$j && IMGIND=$i
85                    done
86                done
87                # Check if it needs to update or insert data.
88                if [ -n "$IMGIND" ]; then
89                    # Update if image data changes and info file exists.
90                    [ $# -gt 2 -a "$(jq ".check=$JSON | .check==.ous[$OUIND].images[$IMGIND]" $INFOFILE)" == "false" ] && jq ".ous[$OUIND].images[$IMGIND]=$JSON" $INFOFILE | sponge $INFOFILE
91                else
92                    # Append a new entry.
93                    jq ".ous[$OUIND].images |= (. + [$JSON])" $INFOFILE | sponge $INFOFILE
94                fi
95            fi
96        fi
97    else
98        # Create new JSON file.
[c28eefa]99        echolog "creating new infofile '$INFOFILE'"
[2b1ed11]100        if [ -z "$OUNAME" ]; then
101            cat << EOT | jq . > $INFOFILE
[b136012]102{"directory":"$IMAGESDIR","images":[${IMAGENAME:+$JSON}],"ous":[]}
[2b1ed11]103EOT
104        else
105            cat << EOT | jq . > $INFOFILE
106{"directory":"$IMAGESDIR","images":[],"ous":[{"subdir":"$OUNAME","images":[$JSON]}]}
107EOT
108        fi
109    fi
110}
111
112# Check for file-based images to update the repository configuration file.
113function checkfiles() {
114    local IMAGES IMG INFO DATA
115
116    # File-stored images.
117    IMAGES=$(find $IMAGESDIR -maxdepth 2 -type f \( -name "*.img" -o -name "*.dsk" \) -print)
118    for IMG in $IMAGES; do
119        # Skip locked images.
120        [ -e "$IMG.lock" ] && continue
121        # Retrieve image creation data and delete temporary file.
122        INFO="$IMG.info"
123        [ -e "$INFO" -a "$INFO" -ot "$IMG" ] && rm -f "$INFO" && echo "Warning: Deleted outdated file $INFO"
[9d773c0]124        DATA=""
[3b43d89]125        [ -r "$INFO" ] && DATA=$(cat "$INFO")
[2b1ed11]126        # Add data to configuration file (name, type and data) and remove image info file.
127        IMG=${IMG#$IMAGESDIR/}
128        addToJson "${IMG%.*}" "${IMG##*.}" "$DATA" && rm -f "$INFO"
129    done
130}
131
132# Check for directory-based images to update the repository configuration file.
133function checkdirs() {
134    local IMAGES IMG INFO DATA
135
136    # Directory-based images.
137    IMAGES=$(find $IMAGESDIR -maxdepth 3 -type f -name ogimg.info -print)
138    for INFO in $IMAGES; do
139        IMG="$(dirname "${INFO#$IMAGESDIR/}")"
140        # Skip repository root directory and locked images.
141        [ "$IMG" == "$IMAGESDIR" -o -e "$IMG.lock" ] && continue
[9d773c0]142        DATA=$(awk -F= '$1=="# fstype" {fs=$2} $1=="# sizedata" {sz=$2} END {printf "rsync::%s:%s:",fs,sz}' "$INFO")
[2b1ed11]143        # Add data to configuration file (name, type and data).
144        addToJson "$IMG" "dir" "$DATA"
145    done
146}
147
148# Check if images are removed to update the repository configuration file.
149function checkremoved() {
150    local IMG TYPE OU i j n m
151    [ ! -w "$INFOFILE" ] && raiseError access "$INFOFILE"
152
153    # Check if global images are defined into JSON file.
154    n=$(jq ".images | length" $INFOFILE)
155    for ((i=0; i<n; i++)); do
156        # Image name and type.
157        IMG="$(jq -r ".images[$i].name" $INFOFILE)"
158        TYPE="$(jq -r ".images[$i].type" $INFOFILE)"
159        [ "$TYPE" != "dir" ] && IMG="$IMG.$TYPE"
160        # Delete entry if image does not exist and it's not locked.
161        [ ! -e "$IMAGESDIR/$IMG" -a ! -e "$IMAGESDIR/$IMG.lock" ] && jq "del(.images[$i])" $INFOFILE | sponge $INFOFILE
162    done
163    # Check if OU images are defined into JSON file.
164    m=$(jq ".ous | length" $INFOFILE)
165    for ((j=0; j<m; j++)); do
166        # OU subdir.
[b444fa7]167        OU="$(jq -r ".ous[$j].subdir" $INFOFILE)"
[2b1ed11]168        # Delete OU's entries if its subdir does not exist.
169        if [ ! -e "$IMAGESDIR/$OU" ]; then
[b444fa7]170            jq "del(.ous[$j])" $INFOFILE | sponge $INFOFILE
[2b1ed11]171        else
172            n=$(jq ".images | length" $INFOFILE)
173            for ((i=0; i<n; i++)); do
174                # Image name and type.
[b444fa7]175                IMG="$(jq -r ".ous[$j].images[$i].name" $INFOFILE)"
176                TYPE="$(jq -r ".ous[$j].images[$i].type" $INFOFILE)"
177                [ "$TYPE" != "dir" ] && IMG="$IMG.$TYPE"
[2b1ed11]178                # Delete entry if image does not exist and it's not locked.
[b444fa7]179                [ ! -e "$IMAGESDIR/$OU/$IMG" -a ! -e "$IMAGESDIR/$OU/$IMG.lock" ] && jq "del(.ous[$j].images[$i])" $INFOFILE | sponge $INFOFILE
[2b1ed11]180            done
181        fi
182    done
183}
184
185
186# Main progrram.
[118e2a2]187[ "$*" == "help" ] && help
188[ "$*" == "version" ] && version
[2b1ed11]189
190# Check dependencies.
[115525a]191[ -w "$(dirname "$INFOFILE")" ] || raiseError access "$INFOFILE"
192[ -e "$INFOFILE" ] || addToJson
[2b1ed11]193which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"."
194
[c28eefa]195echolog "calling checkfiles()";   checkfiles
196echolog "calling checkdirs()";    checkdirs
197echolog "calling checkremoved()"; checkremoved
[2b1ed11]198
Note: See TracBrowser for help on using the repository browser.