[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. |
---|
| 15 | PROG=$(basename "$(realpath "$0")") |
---|
| 16 | OPENGNSYS=/opt/opengnsys |
---|
| 17 | IMAGESDIR=$OPENGNSYS/images |
---|
| 18 | INFOFILE=$OPENGNSYS/etc/repoinfo.json |
---|
| 19 | |
---|
| 20 | |
---|
[115525a] | 21 | # Functions. |
---|
| 22 | source $OPENGNSYS/lib/ogfunctions.sh |
---|
[2b1ed11] | 23 | |
---|
| 24 | # Create/edit JSON file about installed ogLive clients. |
---|
| 25 | function 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 | } |
---|
| 48 | EOT |
---|
| 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 | } |
---|
| 75 | EOT |
---|
| 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. |
---|
| 99 | if [ -z "$OUNAME" ]; then |
---|
| 100 | cat << EOT | jq . > $INFOFILE |
---|
[b136012] | 101 | {"directory":"$IMAGESDIR","images":[${IMAGENAME:+$JSON}],"ous":[]} |
---|
[2b1ed11] | 102 | EOT |
---|
| 103 | else |
---|
| 104 | cat << EOT | jq . > $INFOFILE |
---|
| 105 | {"directory":"$IMAGESDIR","images":[],"ous":[{"subdir":"$OUNAME","images":[$JSON]}]} |
---|
| 106 | EOT |
---|
| 107 | fi |
---|
| 108 | fi |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | # Check for file-based images to update the repository configuration file. |
---|
| 112 | function checkfiles() { |
---|
| 113 | local IMAGES IMG INFO DATA |
---|
| 114 | |
---|
| 115 | # File-stored images. |
---|
| 116 | IMAGES=$(find $IMAGESDIR -maxdepth 2 -type f \( -name "*.img" -o -name "*.dsk" \) -print) |
---|
| 117 | for IMG in $IMAGES; do |
---|
| 118 | # Skip locked images. |
---|
| 119 | [ -e "$IMG.lock" ] && continue |
---|
| 120 | # Retrieve image creation data and delete temporary file. |
---|
| 121 | INFO="$IMG.info" |
---|
| 122 | [ -e "$INFO" -a "$INFO" -ot "$IMG" ] && rm -f "$INFO" && echo "Warning: Deleted outdated file $INFO" |
---|
[9d773c0] | 123 | DATA="" |
---|
[3b43d89] | 124 | [ -r "$INFO" ] && DATA=$(cat "$INFO") |
---|
[2b1ed11] | 125 | # Add data to configuration file (name, type and data) and remove image info file. |
---|
| 126 | IMG=${IMG#$IMAGESDIR/} |
---|
| 127 | addToJson "${IMG%.*}" "${IMG##*.}" "$DATA" && rm -f "$INFO" |
---|
| 128 | done |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | # Check for directory-based images to update the repository configuration file. |
---|
| 132 | function checkdirs() { |
---|
| 133 | local IMAGES IMG INFO DATA |
---|
| 134 | |
---|
| 135 | # Directory-based images. |
---|
| 136 | IMAGES=$(find $IMAGESDIR -maxdepth 3 -type f -name ogimg.info -print) |
---|
| 137 | for INFO in $IMAGES; do |
---|
| 138 | IMG="$(dirname "${INFO#$IMAGESDIR/}")" |
---|
| 139 | # Skip repository root directory and locked images. |
---|
| 140 | [ "$IMG" == "$IMAGESDIR" -o -e "$IMG.lock" ] && continue |
---|
[9d773c0] | 141 | DATA=$(awk -F= '$1=="# fstype" {fs=$2} $1=="# sizedata" {sz=$2} END {printf "rsync::%s:%s:",fs,sz}' "$INFO") |
---|
[2b1ed11] | 142 | # Add data to configuration file (name, type and data). |
---|
| 143 | addToJson "$IMG" "dir" "$DATA" |
---|
| 144 | done |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | # Check if images are removed to update the repository configuration file. |
---|
| 148 | function checkremoved() { |
---|
| 149 | local IMG TYPE OU i j n m |
---|
| 150 | [ ! -w "$INFOFILE" ] && raiseError access "$INFOFILE" |
---|
| 151 | |
---|
| 152 | # Check if global images are defined into JSON file. |
---|
| 153 | n=$(jq ".images | length" $INFOFILE) |
---|
| 154 | for ((i=0; i<n; i++)); do |
---|
| 155 | # Image name and type. |
---|
| 156 | IMG="$(jq -r ".images[$i].name" $INFOFILE)" |
---|
| 157 | TYPE="$(jq -r ".images[$i].type" $INFOFILE)" |
---|
| 158 | [ "$TYPE" != "dir" ] && IMG="$IMG.$TYPE" |
---|
| 159 | # Delete entry if image does not exist and it's not locked. |
---|
| 160 | [ ! -e "$IMAGESDIR/$IMG" -a ! -e "$IMAGESDIR/$IMG.lock" ] && jq "del(.images[$i])" $INFOFILE | sponge $INFOFILE |
---|
| 161 | done |
---|
| 162 | # Check if OU images are defined into JSON file. |
---|
| 163 | m=$(jq ".ous | length" $INFOFILE) |
---|
| 164 | for ((j=0; j<m; j++)); do |
---|
| 165 | # OU subdir. |
---|
[b444fa7] | 166 | OU="$(jq -r ".ous[$j].subdir" $INFOFILE)" |
---|
[2b1ed11] | 167 | # Delete OU's entries if its subdir does not exist. |
---|
| 168 | if [ ! -e "$IMAGESDIR/$OU" ]; then |
---|
[b444fa7] | 169 | jq "del(.ous[$j])" $INFOFILE | sponge $INFOFILE |
---|
[2b1ed11] | 170 | else |
---|
| 171 | n=$(jq ".images | length" $INFOFILE) |
---|
| 172 | for ((i=0; i<n; i++)); do |
---|
| 173 | # Image name and type. |
---|
[b444fa7] | 174 | IMG="$(jq -r ".ous[$j].images[$i].name" $INFOFILE)" |
---|
| 175 | TYPE="$(jq -r ".ous[$j].images[$i].type" $INFOFILE)" |
---|
| 176 | [ "$TYPE" != "dir" ] && IMG="$IMG.$TYPE" |
---|
[2b1ed11] | 177 | # Delete entry if image does not exist and it's not locked. |
---|
[b444fa7] | 178 | [ ! -e "$IMAGESDIR/$OU/$IMG" -a ! -e "$IMAGESDIR/$OU/$IMG.lock" ] && jq "del(.ous[$j].images[$i])" $INFOFILE | sponge $INFOFILE |
---|
[2b1ed11] | 179 | done |
---|
| 180 | fi |
---|
| 181 | done |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | # Main progrram. |
---|
| 186 | |
---|
| 187 | # Check dependencies. |
---|
[115525a] | 188 | [ -w "$(dirname "$INFOFILE")" ] || raiseError access "$INFOFILE" |
---|
| 189 | [ -e "$INFOFILE" ] || addToJson |
---|
[2b1ed11] | 190 | which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"." |
---|
| 191 | |
---|
| 192 | checkfiles |
---|
| 193 | checkdirs |
---|
| 194 | checkremoved |
---|
| 195 | |
---|