[2b1ed11] | 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. |
---|
| 15 | PROG=$(basename "$(realpath "$0")") |
---|
| 16 | OPENGNSYS=/opt/opengnsys |
---|
| 17 | IMAGESDIR=$OPENGNSYS/images |
---|
| 18 | INFOFILE=$OPENGNSYS/etc/repoinfo.json |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | # Auxiliar functions. |
---|
| 22 | |
---|
| 23 | # Metafunction to check if JSON result exists. |
---|
| 24 | function 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. |
---|
| 32 | function 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" |
---|
| 52 | } |
---|
| 53 | EOT |
---|
| 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 | [ $# -gt 2 -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 | } |
---|
| 80 | EOT |
---|
| 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":[]} |
---|
| 107 | EOT |
---|
| 108 | else |
---|
| 109 | cat << EOT | jq . > $INFOFILE |
---|
| 110 | {"directory":"$IMAGESDIR","images":[],"ous":[{"subdir":"$OUNAME","images":[$JSON]}]} |
---|
| 111 | EOT |
---|
| 112 | fi |
---|
| 113 | fi |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | # Show an error message. |
---|
| 117 | function 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. |
---|
| 137 | function help() { |
---|
| 138 | cat << EOT |
---|
| 139 | $PROG: maintain the repository information. |
---|
| 140 | Usage: $PROG |
---|
| 141 | EOT |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | # Check for file-based images to update the repository configuration file. |
---|
| 145 | function 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 | [ -r "$INFO" ] && DATA=$(tr ':' ' ' < "$INFO") |
---|
| 157 | # Add data to configuration file (name, type and data) and remove image info file. |
---|
| 158 | IMG=${IMG#$IMAGESDIR/} |
---|
| 159 | addToJson "${IMG%.*}" "${IMG##*.}" "$DATA" && rm -f "$INFO" |
---|
| 160 | done |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | # Check for directory-based images to update the repository configuration file. |
---|
| 164 | function checkdirs() { |
---|
| 165 | local IMAGES IMG INFO DATA |
---|
| 166 | |
---|
| 167 | # Directory-based images. |
---|
| 168 | IMAGES=$(find $IMAGESDIR -maxdepth 3 -type f -name ogimg.info -print) |
---|
| 169 | for INFO in $IMAGES; do |
---|
| 170 | IMG="$(dirname "${INFO#$IMAGESDIR/}")" |
---|
| 171 | # Skip repository root directory and locked images. |
---|
| 172 | [ "$IMG" == "$IMAGESDIR" -o -e "$IMG.lock" ] && continue |
---|
| 173 | DATA=$(awk -F= '$1=="# fstype" {fs=$2} $1=="# sizedata" {sz=$2} END {printf "::%s:%s:",fs,sz}' "$INFO") |
---|
| 174 | # Add data to configuration file (name, type and data). |
---|
| 175 | addToJson "$IMG" "dir" "$DATA" |
---|
| 176 | done |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | # Check if images are removed to update the repository configuration file. |
---|
| 180 | function checkremoved() { |
---|
| 181 | local IMG TYPE OU i j n m |
---|
| 182 | [ ! -w "$INFOFILE" ] && raiseError access "$INFOFILE" |
---|
| 183 | |
---|
| 184 | # Check if global images are defined into JSON file. |
---|
| 185 | n=$(jq ".images | length" $INFOFILE) |
---|
| 186 | for ((i=0; i<n; i++)); do |
---|
| 187 | # Image name and type. |
---|
| 188 | IMG="$(jq -r ".images[$i].name" $INFOFILE)" |
---|
| 189 | TYPE="$(jq -r ".images[$i].type" $INFOFILE)" |
---|
| 190 | [ "$TYPE" != "dir" ] && IMG="$IMG.$TYPE" |
---|
| 191 | # Delete entry if image does not exist and it's not locked. |
---|
| 192 | [ ! -e "$IMAGESDIR/$IMG" -a ! -e "$IMAGESDIR/$IMG.lock" ] && jq "del(.images[$i])" $INFOFILE | sponge $INFOFILE |
---|
| 193 | done |
---|
| 194 | # Check if OU images are defined into JSON file. |
---|
| 195 | m=$(jq ".ous | length" $INFOFILE) |
---|
| 196 | for ((j=0; j<m; j++)); do |
---|
| 197 | # OU subdir. |
---|
| 198 | OU="$(jq -r ".ous[$i].subdir" $INFOFILE)" |
---|
| 199 | # Delete OU's entries if its subdir does not exist. |
---|
| 200 | if [ ! -e "$IMAGESDIR/$OU" ]; then |
---|
| 201 | jq "del(.ous[[$j])" $INFOFILE | sponge $INFOFILE |
---|
| 202 | else |
---|
| 203 | n=$(jq ".images | length" $INFOFILE) |
---|
| 204 | for ((i=0; i<n; i++)); do |
---|
| 205 | # Image name and type. |
---|
| 206 | IMG="$(jq -r ".images[$i].name" $INFOFILE)" |
---|
| 207 | TYPE="$(jq -r ".images[$i].type" $INFOFILE)" |
---|
| 208 | # Delete entry if image does not exist and it's not locked. |
---|
| 209 | [ ! -e "$IMAGESDIR/$OU/$IMG" -a ! -e "$IMAGESDIR/$OU/$IMG.lock" ] && jq "del(.ous[[$j].images[$i])" $INFOFILE | sponge $INFOFILE |
---|
| 210 | done |
---|
| 211 | fi |
---|
| 212 | done |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | # Main progrram. |
---|
| 217 | |
---|
| 218 | # Check dependencies. |
---|
| 219 | [ ! -w "$(dirname "$INFOFILE")" ] && raiseError access "$INFOFILE" |
---|
| 220 | JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"." |
---|
| 221 | which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"." |
---|
| 222 | |
---|
| 223 | checkfiles |
---|
| 224 | checkdirs |
---|
| 225 | checkremoved |
---|
| 226 | |
---|