summaryrefslogtreecommitdiffstats
path: root/repoman
diff options
context:
space:
mode:
authorramon <ramongomez@us.es>2017-10-10 09:10:59 +0000
committerramon <ramongomez@us.es>2017-10-10 09:10:59 +0000
commit2b1ed11692595333173c6567e1f23f2fc43a472b (patch)
treec3d3ae7fb15264f27781793ea9b2faa75a5476e6 /repoman
parentf009c3dca271c59dbdbd9d2884b2a12d71778419 (diff)
#810: Crear script {{{checkrepo}}} para mantener información de imágenes del repositorio (se ejecutará en el cron tras el script {{{deletepreimage}}}.
git-svn-id: https://opengnsys.es/svn/branches/version1.1@5450 a21b9725-9963-47de-94b9-378ad31fedc9
Diffstat (limited to 'repoman')
-rwxr-xr-xrepoman/bin/checkrepo226
-rwxr-xr-xrepoman/bin/deletepreimage36
2 files changed, 245 insertions, 17 deletions
diff --git a/repoman/bin/checkrepo b/repoman/bin/checkrepo
new file mode 100755
index 00000000..b0ac1922
--- /dev/null
+++ b/repoman/bin/checkrepo
@@ -0,0 +1,226 @@
+#!/bin/bash
+
+#/**
+# checkrepo
+#@file checkrepo
+#@brief Generate repository information in a JSON file.
+#@warning This script uses "jq" command.
+#@version 1.1.0 - Initial version.
+#@author Ramón M. Gómez - ETSII Univ. Sevilla
+#@date 2017-09-27
+#*/ ##
+
+
+# Global constants definition.
+PROG=$(basename "$(realpath "$0")")
+OPENGNSYS=/opt/opengnsys
+IMAGESDIR=$OPENGNSYS/images
+INFOFILE=$OPENGNSYS/etc/repoinfo.json
+
+
+# Auxiliar functions.
+
+# Metafunction to check if JSON result exists.
+function jq() {
+ local OUTPUT
+ OUTPUT=$($JQ "$@") || return $?
+ [[ "$OUTPUT" = "null" ]] && return 1
+ echo "$OUTPUT"
+}
+
+# Create/edit JSON file about installed ogLive clients.
+function addToJson() {
+ # Parameters and variables.
+ local IMAGENAME="$1" IMAGETYPE="$2" DATA="$3" JSON i j n m OUNAME OUIND IMGIND
+ local CLONATOR COMPRESSOR FSTYPE DATASIZE CLIENT
+ IFS=":" read -r CLONATOR COMPRESSOR FSTYPE DATASIZE CLIENT <<<"$DATA"
+ # Check if image is restricted to an OU (subdir).
+ if [[ $IMAGENAME =~ / ]]; then
+ OUNAME="${IMAGENAME%/*}"
+ IMAGENAME="${IMAGENAME##*/}"
+ fi
+ # JSON-formatted new entry.
+ JSON=$(cat << EOT | jq .
+{
+ "name":"$IMAGENAME",
+ "type":"${IMAGETYPE,,}",
+ "clientname":"$CLIENT",
+ "clonator":"${CLONATOR,,}",
+ "compressor":"${COMPRESSOR,,}",
+ "filesystem":"${FSTYPE^^}",
+ "datasize":"$DATASIZE"
+}
+EOT
+ )
+ # Check JSON file consistency.
+ if [ "$(jq -c keys $INFOFILE 2>/dev/null)" == '["directory","images","ous"]' ]; then
+ # Common image.
+ if [ -z "$OUNAME" ]; then
+ # Check if the image is defined into JSON file.
+ n=$(jq ".images | length" $INFOFILE)
+ for ((i=0; i<n; i++)); do
+ [ "$(jq ".check=$JSON | .check.name==.images[$i].name" $INFOFILE)" == "true" ] && IMGIND=$i
+ done
+ # Check if it needs to update or insert data.
+ if [ -n "$IMGIND" ]; then
+ # Update if image data changes and info file exists.
+ [ $# -gt 2 -a "$(jq ".check=$JSON | .check==.images[$IMGIND]" $INFOFILE)" == "false" ] && jq ".images[$IMGIND]=$JSON" $INFOFILE | sponge $INFOFILE
+ else
+ # Append a new entry.
+ jq ".images |= (. + [$JSON])" $INFOFILE | sponge $INFOFILE
+ fi
+ else # OU image.
+ # Append a new OU entry if it does not exist.
+ if [ -z "$(jq -r ".ous[].subdir" $INFOFILE | grep "^$OUNAME$")" ]; then
+ JSON=$(cat << EOT | jq .
+{
+ "subdir": "$OUNAME",
+ "images": [ $JSON ]
+}
+EOT
+ )
+ jq ".ous |= (. + [$JSON])" $INFOFILE | sponge $INFOFILE
+ else
+ # Check if the image is defined in some OU.
+ m=$(jq ".ous | length" $INFOFILE)
+ for ((j=0; j<m; j++)); do
+ n=$(jq ".ous[$j].images | length" $INFOFILE)
+ for ((i=0; i<n; i++)); do
+ [ "$(jq ".check=$JSON | .check.name==.ous[$j].images[$i].name" $INFOFILE)" == "true" ] && OUIND=$j && IMGIND=$i
+ done
+ done
+ # Check if it needs to update or insert data.
+ if [ -n "$IMGIND" ]; then
+ # Update if image data changes and info file exists.
+ [ $# -gt 2 -a "$(jq ".check=$JSON | .check==.ous[$OUIND].images[$IMGIND]" $INFOFILE)" == "false" ] && jq ".ous[$OUIND].images[$IMGIND]=$JSON" $INFOFILE | sponge $INFOFILE
+ else
+ # Append a new entry.
+ jq ".ous[$OUIND].images |= (. + [$JSON])" $INFOFILE | sponge $INFOFILE
+ fi
+ fi
+ fi
+ else
+ # Create new JSON file.
+ if [ -z "$OUNAME" ]; then
+ cat << EOT | jq . > $INFOFILE
+{"directory":"$IMAGESDIR","images":[$JSON],"ous":[]}
+EOT
+ else
+ cat << EOT | jq . > $INFOFILE
+{"directory":"$IMAGESDIR","images":[],"ous":[{"subdir":"$OUNAME","images":[$JSON]}]}
+EOT
+ fi
+ fi
+}
+
+# Show an error message.
+function raiseError() {
+ case "$1" in
+ usage)
+ echo "$PROG: Usage error: Type \"$PROG help\"" >&2
+ exit 1 ;;
+ notfound)
+ echo "$PROG: Resource not found: $2" >&2
+ exit 2 ;;
+ access)
+ echo "$PROG: Access error: $2" >&2
+ exit 3 ;;
+ *)
+ echo "$PROG: Unknown error" >&2
+ exit 1 ;;
+ esac
+}
+
+# Command functions.
+
+# Show help message.
+function help() {
+ cat << EOT
+$PROG: maintain the repository information.
+Usage: $PROG
+EOT
+}
+
+# Check for file-based images to update the repository configuration file.
+function checkfiles() {
+ local IMAGES IMG INFO DATA
+
+ # File-stored images.
+ IMAGES=$(find $IMAGESDIR -maxdepth 2 -type f \( -name "*.img" -o -name "*.dsk" \) -print)
+ for IMG in $IMAGES; do
+ # Skip locked images.
+ [ -e "$IMG.lock" ] && continue
+ # Retrieve image creation data and delete temporary file.
+ INFO="$IMG.info"
+ [ -e "$INFO" -a "$INFO" -ot "$IMG" ] && rm -f "$INFO" && echo "Warning: Deleted outdated file $INFO"
+ [ -r "$INFO" ] && DATA=$(tr ':' ' ' < "$INFO")
+ # Add data to configuration file (name, type and data) and remove image info file.
+ IMG=${IMG#$IMAGESDIR/}
+ addToJson "${IMG%.*}" "${IMG##*.}" "$DATA" && rm -f "$INFO"
+ done
+}
+
+# Check for directory-based images to update the repository configuration file.
+function checkdirs() {
+ local IMAGES IMG INFO DATA
+
+ # Directory-based images.
+ IMAGES=$(find $IMAGESDIR -maxdepth 3 -type f -name ogimg.info -print)
+ for INFO in $IMAGES; do
+ IMG="$(dirname "${INFO#$IMAGESDIR/}")"
+ # Skip repository root directory and locked images.
+ [ "$IMG" == "$IMAGESDIR" -o -e "$IMG.lock" ] && continue
+ DATA=$(awk -F= '$1=="# fstype" {fs=$2} $1=="# sizedata" {sz=$2} END {printf "::%s:%s:",fs,sz}' "$INFO")
+ # Add data to configuration file (name, type and data).
+ addToJson "$IMG" "dir" "$DATA"
+ done
+}
+
+# Check if images are removed to update the repository configuration file.
+function checkremoved() {
+ local IMG TYPE OU i j n m
+ [ ! -w "$INFOFILE" ] && raiseError access "$INFOFILE"
+
+ # Check if global images are defined into JSON file.
+ n=$(jq ".images | length" $INFOFILE)
+ for ((i=0; i<n; i++)); do
+ # Image name and type.
+ IMG="$(jq -r ".images[$i].name" $INFOFILE)"
+ TYPE="$(jq -r ".images[$i].type" $INFOFILE)"
+ [ "$TYPE" != "dir" ] && IMG="$IMG.$TYPE"
+ # Delete entry if image does not exist and it's not locked.
+ [ ! -e "$IMAGESDIR/$IMG" -a ! -e "$IMAGESDIR/$IMG.lock" ] && jq "del(.images[$i])" $INFOFILE | sponge $INFOFILE
+ done
+ # Check if OU images are defined into JSON file.
+ m=$(jq ".ous | length" $INFOFILE)
+ for ((j=0; j<m; j++)); do
+ # OU subdir.
+ OU="$(jq -r ".ous[$i].subdir" $INFOFILE)"
+ # Delete OU's entries if its subdir does not exist.
+ if [ ! -e "$IMAGESDIR/$OU" ]; then
+ jq "del(.ous[[$j])" $INFOFILE | sponge $INFOFILE
+ else
+ n=$(jq ".images | length" $INFOFILE)
+ for ((i=0; i<n; i++)); do
+ # Image name and type.
+ IMG="$(jq -r ".images[$i].name" $INFOFILE)"
+ TYPE="$(jq -r ".images[$i].type" $INFOFILE)"
+ # Delete entry if image does not exist and it's not locked.
+ [ ! -e "$IMAGESDIR/$OU/$IMG" -a ! -e "$IMAGESDIR/$OU/$IMG.lock" ] && jq "del(.ous[[$j].images[$i])" $INFOFILE | sponge $INFOFILE
+ done
+ fi
+ done
+}
+
+
+# Main progrram.
+
+# Check dependencies.
+[ ! -w "$(dirname "$INFOFILE")" ] && raiseError access "$INFOFILE"
+JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"."
+which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"."
+
+checkfiles
+checkdirs
+checkremoved
+
diff --git a/repoman/bin/deletepreimage b/repoman/bin/deletepreimage
index b4020ba0..19d6f2c7 100755
--- a/repoman/bin/deletepreimage
+++ b/repoman/bin/deletepreimage
@@ -1,9 +1,10 @@
#!/bin/bash
-# Eliminar las imagenees del repositiro seg�raca de la consola web .img
-#Version 0.3 Ejecuci�n desde cron cada minuto.
+# Eliminar las imágenees del repositiro seg�raca de la consola web.
+#Version 0.3 Ejecución desde cron cada minuto.
#echo "* * * * * root /opt/opengnsys/bin/image-delete" > /etc/cron.d/imagedelete
+# Version 1.1.0 - Llamar a script "checkrepo".
-# Comprobar si el proceso ya est� en ejecuci�n.on.
+# Comprobar si el proceso ya está en ejecución.
PROG=$(basename $0)
[ "$(pgrep "$PROG")" != "$$" ] && exit
@@ -14,27 +15,24 @@ OGIMG="$OPENGNSYS/images"
REPOCFG="$OPENGNSYS/etc/ogAdmRepo.cfg"
LOGFILE="$OPENGNSYS/log/$PROG.log"
-# Error si no est� bien configurado el repositorio de im�genes.nes.
+# Error si no está bien configurado el repositorio de imágenes.
[ -d $OGIMG -a -f $REPOCFG ] || exit 1
-# Procesar ficheros de im�genes.s.
+# Procesar ficheros de imágenes.
trap 'echo "`date` : Proceso interrumpido" >> $LOGFILE; exit ' 1 2 3 6 9 15
-#TODO en LOCAL: si existe algun fichero *.delete lo movemos al repositorio
-ls /opt/opengnsys/www/tmp/*.delete &>/dev/null || exit
-#[ -f /opt/opengnsys/www/tmp/*.delete ] &&
-mv /opt/opengnsys/www/tmp/*.* /opt/opengnsys/images/
+#TODO en LOCAL: si existe algún fichero *.delete lo movemos al repositorio
+ls $OPENGNSYS/www/tmp/*.delete &>/dev/null || (checkrepo; exit)
+mv $OPENGNSYS/www/tmp/*.* $OGIMG
#TODO: iniciar blucle siempre y cuando haya algun delete
ls /opt/opengnsys/images/*.delete &>/dev/null || exit
+for IMG in `ls $OGIMG/*.delete`; do
+ # Obtenemos el nombre de la imagen
+ DELETEIMAGE=$(echo ${IMG%%.*} | awk -F"$OGIMG/" '{print $2}')
-for IMG in `ls /opt/opengnsys/images/*.delete`; do
- ## Obtenemos el nombre de la imagen
- # DELETEIMAGE=$(echo $IMG | awk -F"." '{print $1}' | awk -F"/opt/opengnsys/images/" '{print $2}')
- DELETEIMAGE=$(echo ${IMG%%.*} | awk -F"/opt/opengnsys/images/" '{print $2}')
-
- # Borramos marca .delete para que el proximo cron no trabaje sobre este conjunto.
- [ -f $IMG ] && rm $IMG
+ # Borramos marca .delete para que el próximo cron no trabaje sobre este conjunto.
+ [ -f $IMG ] && rm $IMG
## Comprobamos si es una imagen de backup
DELETEant=$(echo $IMG | awk -F"." '{print $3}') ## .ant
@@ -47,6 +45,10 @@ for IMG in `ls /opt/opengnsys/images/*.delete`; do
DELETEIMAGE=$(echo $DELETEIMAGE|tr : /)
## se llama al escript de borrado de imagen.
- /opt/opengnsys/bin/deleteimage $DELETEIMAGE
+ deleteimage $DELETEIMAGE
done
+
+# Actualizar información del repositorio.
+checkrepo
+