| 1 | #!/bin/bash | 
|---|
| 2 | #/** | 
|---|
| 3 | #@file    restoreImage | 
|---|
| 4 | #@brief   Script de ejemplo para restaurar una imagen. | 
|---|
| 5 | #@param   $1 Repositorio (CACHE, REPO o dirección IP) | 
|---|
| 6 | #@param   $2 Nombre canónico de la imagen (sin extensión) | 
|---|
| 7 | #@param   $3 Número de disco | 
|---|
| 8 | #@param   $4 Número de particion | 
|---|
| 9 | #@param   $5 Protocolo (UNICAST, UNICAST-DIRECT, MULTICAST o MULTICAST-DIRECT) | 
|---|
| 10 | #@param   $6 Opciones del protocolo | 
|---|
| 11 | #@exception OG_ERR_FORMAT   1 formato incorrecto. | 
|---|
| 12 | #@exception OG_ERR_NOTFOUND  2 cambio de repositorio: repositorio no encontrado | 
|---|
| 13 | #@exception OG_ERR_NOTFOUND  2 fichero de imagen o partición no detectados. | 
|---|
| 14 | #@exception $OG_ERR_MCASTRECEIVERFILE  57 Error en la recepción Multicast de un fichero | 
|---|
| 15 | #@exception $OG_ERR_PROTOCOLJOINMASTER 60 Error en la conexión de una sesión Unicast|Multicast con el Master | 
|---|
| 16 | #@version 1.1 - Cambio de repositorio para el recurso remoto images si es necesario | 
|---|
| 17 | #@author  Irina Gomez, ETSII Universidad de Sevilla | 
|---|
| 18 | #@date    2015-06-16 | 
|---|
| 19 | #@version 1.1 - Control de errores en transferencia multicast (ticket #781) | 
|---|
| 20 | #@author  Irina Gomez, ETSII Universidad de Sevilla | 
|---|
| 21 | #@date    2017/04/20 | 
|---|
| 22 | #**/ | 
|---|
| 23 |  | 
|---|
| 24 | TIME1=$SECONDS | 
|---|
| 25 | PROG="$(basename $0)" | 
|---|
| 26 | if [ $# -lt 4 ]; then | 
|---|
| 27 |     ogRaiseError session $OG_ERR_FORMAT "$MSG_FORMAT: $PROG REPO|CACHE imagen ndisco nparticion [ UNICAST|MULTICAST opciones protocolo]" | 
|---|
| 28 |     exit $? | 
|---|
| 29 | fi | 
|---|
| 30 |  | 
|---|
| 31 | #Load engine configurator from engine.cfg file. | 
|---|
| 32 | #Carga el configurador del engine desde el fichero engine.cfg | 
|---|
| 33 | # Valores por defecto: #IMGPROG="partclone" ; #IMGCOMP="lzop" ; #IMGEXT="img" #IMGREDUCE="TRUE" | 
|---|
| 34 | [ -z $OGENGINECONFIGURATE ] && source /opt/opengnsys/etc/engine.cfg | 
|---|
| 35 |  | 
|---|
| 36 | # Clear temporary file used as log track by httpdlog | 
|---|
| 37 | # Limpia los ficheros temporales usados como log de seguimiento para httpdlog | 
|---|
| 38 | echo " " > $OGLOGCOMMAND | 
|---|
| 39 | ogCheckStringInGroup "$(ogGetCaller)" "deployImage restoreImageCustom" || echo -n "" > $OGLOGSESSION;  | 
|---|
| 40 |  | 
|---|
| 41 | ogEcho log session "[1] $MSG_SCRIPTS_START $0 $*" | 
|---|
| 42 |  | 
|---|
| 43 | # Procesar parámetros de entrada | 
|---|
| 44 | REPO="${1^^}" | 
|---|
| 45 | IMGNAME="$2" | 
|---|
| 46 | DISK="$3" | 
|---|
| 47 | PART="$4" | 
|---|
| 48 | PROTO="${5^^}" | 
|---|
| 49 | PROTO=${PROTO:-"UNICAST"} | 
|---|
| 50 | PROTOOPT="$6" | 
|---|
| 51 | # Si MCASTWAIT menos que tiempo de espera del servidor lo aumento | 
|---|
| 52 | if [ "${PROTO%-*}" == "MULTICAST" ] && [[ ${PROTOOPT##*:} =~ ^-?[0-9]+$ ]]; then | 
|---|
| 53 |     [ ${MCASTWAIT:-0} -lt ${PROTOOPT##*:} ] && let MCASTWAIT=${PROTOOPT##*:}+5 | 
|---|
| 54 | fi | 
|---|
| 55 | IMGTYPE="${IMGTYPE:-"img"}" | 
|---|
| 56 |  | 
|---|
| 57 | # Unidad organizativa | 
|---|
| 58 | [ "$ogunit" != "" ] && OGUNIT="$ogunit/" | 
|---|
| 59 |  | 
|---|
| 60 | # Si es una ip y es igual a la del equipo restaura desde cache | 
|---|
| 61 | [ "$REPO" == "$(ogGetIpAddress)" ] && REPO="CACHE" | 
|---|
| 62 | # Si es una ip y es distinta a la del recurso samba cambiamos de REPO. | 
|---|
| 63 | ogCheckIpAddress $REPO  | 
|---|
| 64 | if [ $? == 0 -o $REPO == "REPO" ] ; then | 
|---|
| 65 |         # Si falla el cambio -> salimos con error repositorio no valido | 
|---|
| 66 |         ogChangeRepo $REPO ${OGUNIT%/} || exit $(ogRaiseError $OG_ERR_NOTFOUND '$REPO $OGUNIT'; echo $?) | 
|---|
| 67 |         REPO="REPO" | 
|---|
| 68 | fi | 
|---|
| 69 |  | 
|---|
| 70 | # Comprobar que existe la imagen del origen. | 
|---|
| 71 | IMGFILE=$(ogGetPath "$REPO" "$IMGNAME.$IMGTYPE") | 
|---|
| 72 | IMGDIR=$(ogGetParentPath "$REPO" "$IMGNAME") | 
|---|
| 73 | if [ "$IMGFILE" == "" -o "$IMGDIR" == "" ]; then | 
|---|
| 74 |         ogRaiseError session $OG_ERR_NOTFOUND "$REPO, ${IMGNAME%/*}" | 
|---|
| 75 |         exit $? | 
|---|
| 76 | fi | 
|---|
| 77 |  | 
|---|
| 78 | # Procesar protocolos de transferencia. | 
|---|
| 79 | case "$PROTO" in | 
|---|
| 80 |     UNICAST|UNICAST-DIRECT) | 
|---|
| 81 |         # Restaurar la imagen. | 
|---|
| 82 |         ogEcho log session "[40] ogRestoreImage $REPO $IMGNAME $DISK $PART UNICAST" | 
|---|
| 83 |         ogExecAndLog command ogRestoreImage "$REPO" "$IMGNAME" "$DISK" "$PART" | 
|---|
| 84 |         RETVAL=$? | 
|---|
| 85 |         ;; | 
|---|
| 86 |     MULTICAST|MULTICAST-DIRECT) | 
|---|
| 87 |         PORT=$(echo $PROTOOPT | cut -f1 -d":") | 
|---|
| 88 |         TOOL=$(ogGetImageProgram REPO $IMGNAME) | 
|---|
| 89 |         COMPRESS=$(ogGetImageCompressor REPO $IMGNAME) | 
|---|
| 90 |         #TODO comprobar parametros anteriores | 
|---|
| 91 |         ogEcho log session "[40] ogMcastReceiverPartition $DISK $PART $PORT $TOOL $COMPRESS" | 
|---|
| 92 |         ogMcastRequest "$IMGNAME.img" "$PROTOOPT" || exit $? | 
|---|
| 93 |         ogExecAndLog command ogMcastReceiverPartition "$DISK" "$PART" "$PORT" "$TOOL" "$COMPRESS" | 
|---|
| 94 |         RETVAL=$? | 
|---|
| 95 |         ;; | 
|---|
| 96 |     *)  # Protocolo desconocido. | 
|---|
| 97 |         ogRaiseError session $OG_ERR_FORMAT "$MSG_FORMAT: $PROG REPO|CACHE imagen ndisco nparticion [ UNICAST|MULTICAST opciones ]"  | 
|---|
| 98 |         exit $? | 
|---|
| 99 | esac | 
|---|
| 100 |  | 
|---|
| 101 | TIME=$[SECONDS-TIME1] | 
|---|
| 102 | ogEcho log session "[100] Duracion de la operacion $[TIME/60]m $[TIME%60]s" | 
|---|
| 103 |  | 
|---|
| 104 | # Código de salida del comando prinicpal de restauración. | 
|---|
| 105 | exit $RETVAL | 
|---|
| 106 |  | 
|---|