[914d834] | 1 | #!/bin/bash |
---|
| 2 | |
---|
| 3 | |
---|
[841ce50] | 4 | #/** |
---|
| 5 | # updateCache |
---|
| 6 | #@brief Actualiza la cache del cliente con imagen o fichero iso. |
---|
| 7 | #@param 1 REPO Origen del fichero. -accesible por nfs-samba- |
---|
[a323383] | 8 | #@param 2 str_fichero nombre del fichero a actualizar. |
---|
[841ce50] | 9 | #@param 3 str_protoco. TORRENT | MULTICAST | UNICAST. |
---|
| 10 | #@param 4 str_opcionesprotocolo |
---|
| 11 | #@param 4 str_opcionesupdatecache |
---|
| 12 | #@ejemplo: oneRemoteFromMaster 172.17.36.11 CACHE /imagen1 9000:full-duplex:239.194.17.36:70M:50:100 1 1 partclone lzop |
---|
| 13 | #@return |
---|
| 14 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
| 15 | #@note |
---|
| 16 | #@todo: |
---|
| 17 | #@version 0.9.1 - integracion EAC |
---|
| 18 | #@author Antonio J. Doblas Viso. Universidad de Malaga. |
---|
| 19 | #@date 2008/03/17 |
---|
| 20 | #@version 0.9.2 - integracion OpenGnsys |
---|
| 21 | #@author Antonio J. Doblas Viso. Universidad de Malaga. |
---|
| 22 | #@date 2010/07/27 |
---|
[1547c12] | 23 | #@version 1.0.1 - Control de espacio requerido |
---|
| 24 | #@author Antonio J.Doblas Viso |
---|
| 25 | #@date 2011-05-10 |
---|
[841ce50] | 26 | #*/ ## |
---|
[914d834] | 27 | |
---|
| 28 | PROG="$(basename $0)" |
---|
[cea6d44] | 29 | if [ $# -lt 3 ]; then |
---|
[914d834] | 30 | ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $PROG str_REPO _str_Relative_Path_OGIMG_with_/ PROTOCOLO OPCIONES_PROTOCOLO OPCIONES_UPDATECACHE" |
---|
| 31 | exit $? |
---|
| 32 | fi |
---|
| 33 | |
---|
[a323383] | 34 | REPOSITORIO="$1" |
---|
| 35 | PROTOCOLO="$3" |
---|
| 36 | OPTPROTOCOLO="$4" |
---|
[9b6e62c] | 37 | REPOIP=$(ogGetRepoIp) |
---|
| 38 | echo $REPOSITORIO $REPOIP $PROTOCOLO $OPTPROTOCOLO |
---|
[914d834] | 39 | # Si el repositorio local CACHE no existe salimos. |
---|
| 40 | if ! $(ogFindCache >/dev/null); then |
---|
[5c44f86] | 41 | ogRaiseError $OG_ERR_NOTCACHE "CACHE" |
---|
[914d834] | 42 | exit $? |
---|
| 43 | fi |
---|
| 44 | |
---|
[9b6e62c] | 45 | #Comprobamos si Actualizacion - existe una imagen con igual nombre pero distinto sum- |
---|
| 46 | #TODO: ogUpdateCacheIsNecesary debe borrar la imagen que tenga igual nombre pero distinto sum- |
---|
[db8fdd2] | 47 | ogUpdateCacheIsNecesary $1 $2; RETVAL=$? |
---|
| 48 | # si RETVAL=0 => actualizamos si RETVAL=1 no actaulizasmo-exit 0 || si RETVAL>2 exit 1 |
---|
| 49 | [ "$RETVAL" == "1" ] && exit 0 |
---|
| 50 | [ "$RETVAL" -gt "1" ] && exit 1 |
---|
| 51 | |
---|
[914d834] | 52 | |
---|
| 53 | CACHESIZEFREE=$(ogGetFreeSize `ogFindCache`) |
---|
[a323383] | 54 | FILESIZE=$(ls -sk $(ogGetPath $REPOSITORIO "$2") | cut -f1 -d" ") |
---|
[914d834] | 55 | |
---|
| 56 | if [ "$FILESIZE" -ge "$CACHESIZEFREE" ] |
---|
| 57 | then |
---|
| 58 | echo "el tamanio del fichero $2 = $FILESIZE es mayor que el espacio dispinible en la cache = $CACHESIZEFREE" |
---|
[5c44f86] | 59 | ogRaiseError $OG_ERR_CACHESIZE "CACHE" |
---|
| 60 | exit $? |
---|
[914d834] | 61 | fi |
---|
| 62 | |
---|
[a323383] | 63 | ogUpdateCacheIsNecesary $REPOSITORIO "$2"; RETVAL=$? |
---|
[db8fdd2] | 64 | # si RETVAL=0 => actualizamos si RETVAL=1 no actaulizasmo-exit 0 || si RETVAL>2 exit 1 |
---|
| 65 | [ "$RETVAL" == "1" ] && exit 0 |
---|
| 66 | [ "$RETVAL" -gt "1" ] && exit 1 |
---|
[914d834] | 67 | |
---|
[a323383] | 68 | ogMountCache >/dev/null |
---|
[914d834] | 69 | |
---|
| 70 | ## Si no existe, crear subdirectorio para el fichero en la cache. |
---|
[a323383] | 71 | IMGDIR=$(ogGetParentPath CACHE "/$2") |
---|
[914d834] | 72 | if [ $? != 0 ]; then |
---|
| 73 | echo "[5] Crear subdirectorio del fichero \"$2 $(dirname "$2")." |
---|
[a323383] | 74 | ogMakeDir CACHE $(dirname "/$2") |
---|
| 75 | IMGDIR=$(ogGetParentPath CACHE "/$2") || exit $? |
---|
[914d834] | 76 | fi |
---|
| 77 | |
---|
[0c42ccf] | 78 | TIME1=$SECONDS |
---|
[914d834] | 79 | |
---|
| 80 | case "$PROTOCOLO" in |
---|
| 81 | torrent | TORRENT ) |
---|
[a323383] | 82 | echo "ogCopyFile $1 $2.torrent absolute $OGCAC/$OGIMG" |
---|
[ec051d0] | 83 | # tiempos |
---|
| 84 | timewait=$(expr $(printf '%d\n' 0x$(ogGetMacAddress | awk -F: '{print $5$6}')) \* 120 / 65535) |
---|
[a323383] | 85 | ogCopyFile $REPOSITORIO "$2.torrent" "$IMGDIR" |
---|
[9b6e62c] | 86 | #TODO: comprobar que el tracker definido en el fichero es correcto. |
---|
| 87 | #TODO comprobar que el fichero torrent está en cache |
---|
[ec051d0] | 88 | # retardamos el inicio -aleatorio de 0 a 120 segundos- al tracker para gestionar mas de +-40 equipos |
---|
| 89 | P2PWAIT=$[ ( $RANDOM % 120 ) + 1 ] |
---|
| 90 | echo "Esperando $P2PWAIT segundos para iniciar ogTorrentStart CACHE $2.torrent $4" |
---|
| 91 | sleep $P2PWAIT |
---|
[a323383] | 92 | ogTorrentStart CACHE "$2.torrent" $4 |
---|
[914d834] | 93 | ;; |
---|
| 94 | multicast | MULTICAST ) |
---|
[9b6e62c] | 95 | echo "determinando puerto principal y auxiliar." |
---|
| 96 | PORT=$(echo $OPTPROTOCOLO | cut -f1 -d":") |
---|
| 97 | let PORTAUX=$PORT+1 |
---|
[e8460d2] | 98 | #TODO: ticket 379 |
---|
| 99 | NUMBER=$[ ( $RANDOM % 30 ) + 1 ] |
---|
| 100 | sleep $NUMBER |
---|
| 101 | #FIN TODO |
---|
[9b6e62c] | 102 | echo "comprobando puerto $PORTAUX en $REPOIP (sesion multicast en puerto $PORT) " |
---|
| 103 | if (nmap -n -sU -p $PORTAUX $REPOIP | grep open) |
---|
| 104 | then |
---|
[a323383] | 105 | ogMcastReceiverFile $PORT CACHE "$2" |
---|
[9b6e62c] | 106 | else |
---|
[e8460d2] | 107 | # TODO ticket 379 Realizar la petición basada en identificador de operacion |
---|
[9b6e62c] | 108 | echo "solicita la apertura: hose $REPOIP 2009 --out sh -c "echo -ne START_MULTICAST $2 $OPTPROTOCOLO"" |
---|
| 109 | hose $REPOIP 2009 --out sh -c "echo -ne START_MULTICAST $2 $OPTPROTOCOLO" |
---|
| 110 | #echo "espero y llamo a: ogMcastReceiverFile $PORT CACHE $2" |
---|
[e8460d2] | 111 | sleep 10 |
---|
| 112 | if (nmap -n -sU -p $PORTAUX $REPOIP | grep open) |
---|
| 113 | then |
---|
[a323383] | 114 | ogMcastReceiverFile $PORT CACHE "$2" |
---|
[e8460d2] | 115 | else |
---|
| 116 | echo "la peticion ha fallado: hose $REPOIP 2009 --out sh -c echo -ne START_MULTICAST $2 $OPTPROTOCOLO" |
---|
| 117 | exit 1 |
---|
| 118 | fi |
---|
| 119 | fi |
---|
[914d834] | 120 | ;; |
---|
| 121 | unicast | UNICAST ) |
---|
[9b6e62c] | 122 | echo "unicast" |
---|
[a323383] | 123 | ogCopyFile $REPOSITORIO "$2" "$IMGDIR" |
---|
[914d834] | 124 | ;; |
---|
| 125 | esac |
---|
| 126 | |
---|
[0c42ccf] | 127 | TIME1=$[SECONDS-TIME1] |
---|
| 128 | echo "tiempo de updateCache (descarga) $[TIME1/60]m $[TIME1%60]s" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
[914d834] | 129 | |
---|
[0c42ccf] | 130 | echo "iniciando el calculo del crc de la imagen descargada" |
---|
| 131 | TIME2=$SECONDS |
---|
[a323383] | 132 | ogUpdateCacheIsNecesary $REPOSITORIO "$2"; RETVAL=$? |
---|
[0c42ccf] | 133 | TIME2=$[SECONDS-TIME2] |
---|
| 134 | echo "tiempo de calculo crc $[TIME2/60]m $[TIME2%60]s" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 135 | |
---|
[db8fdd2] | 136 | # si RETVAL=0 => actualizamos si RETVAL=1 no actaulizasmo-exit 0 || si RETVAL>2 exit 1 |
---|
| 137 | [ "$RETVAL" == "0" ] && exit 1 |
---|
| 138 | [ "$RETVAL" == "1" ] && exit 0 |
---|
[a323383] | 139 | [ "$RETVAL" -gt "1" ] && exit 1 |
---|
| 140 | |
---|