source: client/engine/System.lib @ d1795da

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since d1795da was c4db9c1, checked in by irina <irinagomez@…>, 11 years ago

#565 Se crea codigo y mensaje de error para los problemas de transferencia de rsync.

git-svn-id: https://opengnsys.es/svn/branches/version1.0@4220 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 8.5 KB
Line 
1#!/bin/bash
2#/**
3#@file     System.lib
4#@brief    Librería o clase System
5#@class    System
6#@brief    Funciones básicas del sistema.
7#@version  1.0.5
8#@warning  License: GNU GPLv3+
9#*/
10
11
12#/**
13#         ogEcho [str_logtype ...] [str_loglevel] "str_message" ...
14#@brief   Muestra mensajes en consola y lo registra en fichero de incidencias.
15#@param   str_logtype  tipo de registro de incidencias.
16#@param   str_loglevel nivel de registro de incidencias.
17#@param   str_message  mensaje (puede recibir más de 1 parámetro.
18#@return  Mensaje mostrado.
19#@warning Si no se indica nivel de registro, solo muestra mensaje en pantalla.
20#@note    logfile = { log, command, session }; usa "log" si se indica nivel de registro.
21#@note    loglevel = { help, info, warning, error }
22#@note    El nivel de ayuda \c (help) no se registra en el fichero de incidencias.
23#@version 0.9 - Primera versión para OpenGnSys
24#@author  Ramon Gomez, ETSII Universidad de Sevilla
25#@date    2009-07-23
26#@version 1.0.5 - Elegir fichero de log.
27#@author  Ramon Gomez, ETSII Universidad de Sevilla
28#@date    2009-07-23
29#*/
30function ogEcho () {
31
32# Variables locales
33local CONT=1 LOGS LOGLEVEL DATETIME
34
35# Selección de ficheros de rgistro de incidencias.
36while [ $CONT ]; do
37    case "${1,,}" in
38        log)     LOGS="$LOGS $OGLOGFILE";    shift ;;
39        command) LOGS="$LOGS $OGLOGCOMMAND"; shift ;;
40        session) LOGS="$LOGS $OGLOGSESSION"; shift ;;
41        *)       CONT= ;;
42    esac
43done
44
45# Selección del nivel de registro (opcional).
46case "${1,,}" in
47     help)    shift ;;
48     info)    LOGLEVEL="$1"; shift ;;
49     warning) LOGLEVEL="$1"; shift ;;
50     error)   LOGLEVEL="$1"; shift ;;
51     *)       ;;
52esac
53
54if [ -n "$LOGLEVEL" ]; then
55    DATETIME=$(date +"%F %T")
56    logger -s -t "OpenGnSys $LOGLEVEL" "$DATETIME $*" 2>&1 | tee -a $OGLOGFILE $LOGS
57else
58    echo "$*" | tee -a $LOGS
59fi
60}
61
62
63#/**
64#         ogRaiseError [str_logtype ...] int_errcode ["str_errmessage" ...]
65#@brief   Devuelve el mensaje y el código de error correspondiente.
66#@param   str_logtype    tipo de registro de incidencias.
67#@param   int_errcode    código de error.
68#@param   str_errmessage mensajes complementarios de error.
69#@return  str_message - Mensaje de error, incluyendo las funciones relacionadas.
70#@warning No definidas
71#@note    Mensajes internacionales del fichero de idiomas.
72#@version 0.9 - Primera versión para OpenGnSys.
73#@author  Ramon Gomez, ETSII Universidad de Sevilla
74#@date    2009-07-21
75#@version 1.0.5 - Muestra en el mensaje todas las funciones relacionadas (separadas por <-).
76#@author  Ramon Gomez, ETSII Universidad de Sevilla
77#@date    2014-03-17
78#*/
79function ogRaiseError () {
80
81# Variables locales
82local CONT=1 LOGS MSG CODE FUNCS
83
84# Selección de rgistros de incidencias.
85while [ $CONT ]; do
86    case "${1,,}" in
87        log|command|session)  LOGS="$LOGS $1"; shift ;;
88        *)                    CONT= ;;
89    esac
90done
91
92# Obtener código y mensaje de error.
93CODE="$1"
94case "$CODE" in
95     $OG_ERR_FORMAT)     MSG="$MSG_ERR_FORMAT \"$2\"" ;;
96     $OG_ERR_NOTFOUND)   MSG="$MSG_ERR_NOTFOUND \"$2\"" ;;
97     $OG_ERR_OUTOFLIMIT) MSG="$MSG_ERR_OUTOFLIMIT \"$2\"" ;;
98     $OG_ERR_PARTITION)  MSG="$MSG_ERR_PARTITION \"$2\"" ;;
99     $OG_ERR_LOCKED)     MSG="$MSG_ERR_LOCKED \"$2\"" ;;
100     $OG_ERR_CACHE)      MSG="$MSG_ERR_CACHE \"$2\"" ;;
101     $OG_ERR_NOGPT)      MSG="$MSG_ERR_NOGPT \"$2\"" ;;
102     $OG_ERR_FILESYS)    MSG="$MSG_ERR_FILESYS \"$2\"" ;;
103     $OG_ERR_IMAGE)      MSG="$MSG_ERR_IMAGE \"$2\"" ;;
104     $OG_ERR_NOTOS)      MSG="$MSG_ERR_NOTOS \"$2\"" ;;
105     $OG_ERR_NOTEXEC)    MSG="$MSG_ERR_NOTEXEC \"$2\"" ;;
106     $OG_ERR_NOTWRITE)   MSG="$MSG_ERR_NOTWRITE \"$2\"" ;;
107     $OG_ERR_NOTCACHE)   MSG="$MSG_ERR_NOTCACHE \"$2\"" ;;
108     $OG_ERR_CACHESIZE)  MSG="$MSG_ERR_CACHESIZE \"$2\"" ;;
109     $OG_ERR_REDUCEFS)   MSG="$MSG_ERR_REDUCEFS \"$2\"" ;;
110     $OG_ERR_EXTENDFS)   MSG="$MSG_ERR_EXTENDFS \"$2\"" ;;
111     $OG_ERR_IMGSIZEPARTITION)   MSG="$MSG_ERR_IMGSIZEPARTITION \"$2\"" ;;   
112     $OG_ERR_UPDATECACHE)       MSG="$MSG_ERR_UPDATECACHE \"$2\"" ;;
113     $OG_ERR_UCASTSYNTAXT)   MSG="$MSG_ERR_UCASTSYNTAXT \"$2\"" ;;
114     $OG_ERR_UCASTSENDPARTITION)   MSG="$MSG_ERR_UCASTSENDPARTITION \"$2\"" ;;   
115     $OG_ERR_UCASTSENDFILE)   MSG="$MSG_ERR_UCASTSENDFILE \"$2\"" ;; 
116     $OG_ERR_UCASTRECEIVERPARTITION)   MSG="$MSG_ERR_UCASTRECEIVERPARTITION \"$2\"" ;;   
117     $OG_ERR_UCASTRECEIVERFILE)   MSG="$MSG_ERR_UCASTRECEIVERFILE \"$2\"" ;; 
118     $OG_ERR_MCASTSYNTAXT)   MSG="$MSG_ERR_MCASTSYNTAXT \"$2\"" ;;
119     $OG_ERR_MCASTSENDFILE)   MSG="$MSG_ERR_MCASTSENDFILE \"$2\"" ;;
120     $OG_ERR_MCASTRECEIVERFILE)   MSG="$MSG_ERR_MCASTRECEIVERFILE \"$2\"" ;;
121     $OG_ERR_MCASTSENDPARTITION)   MSG="$MSG_ERR_MCASTSENDPARTITION \"$2\"" ;;
122     $OG_ERR_MCASTRECEIVERPARTITION)   MSG="$MSG_ERR_MCASTRECEIVERPARTITION \"$2\"" ;;
123     $OG_ERR_PROTOCOLJOINMASTER)   MSG="$MSG_ERR_PROTOCOLJOINMASTER \"$2\"" ;;
124     $OG_ERR_DONTMOUNT_IMAGE)   MSG="$MSG_ERR_DONTMOUNT_IMAGE \"$2\"" ;;
125     $OG_ERR_DONTUNMOUNT_IMAGE)   MSG="$MSG_ERR_DONTUNMOUNT_IMAGE \"$2\"" ;;
126     $OG_ERR_DONTSYNC_IMAGE)    MSG="$MSG_ERR_DONTSYNC_IMAGE \"$2\"" ;;
127     $OG_ERR_NOTDIFFERENT)      MSG="$MSG_ERR_NOTDIFFERENT \"$2\"" ;;
128     $OG_ERR_SYNCHRONIZING)     MSG="$MSG_ERR_SYNCHRONIZING \"$2\"" ;;
129     *)                  MSG="$MSG_ERR_GENERIC"; CODE=$OG_ERR_GENERIC ;;
130esac
131
132# Obtener lista de funciones afectadas, incluyendo el script que las llama.
133FUNCS="${FUNCNAME[@]:1}"
134FUNCS="${FUNCS/main/$(basename $0 2>/dev/null)}"
135
136# Mostrar mensaje de error y salir con el código indicado.
137ogEcho $LOGS error "${FUNCS// /<-}: $MSG" >&2
138return $CODE
139}
140
141
142#/**
143#         ogGetCaller
144#@brief   Devuelve nombre del programa o script ejecutor (padre).
145#@param   No.
146#@return  str_name - Nombre del programa ejecutor.
147#@version 0.10 - Primera versión para OpenGnSys.
148#@author  Ramon Gomez, ETSII Universidad de Sevilla
149#@date    2011-01-17
150#*/
151function ogGetCaller () {
152
153# Obtener el nombre del programa o del script que ha llamado al proceso actual.
154basename "$(ps hlp $PPID | awk '{if ($13~/bash/ && $14!="") print $14;
155                                 else { sub(/^-/,"",$13); print $13; } }')"
156}
157
158
159#/**
160#         ogIsRepoLocked
161#@brief   Comprueba si el repositorio está siendo usado (tiene ficheros abiertos).
162#@param   No.
163#@return  Código de salida: 0 - bloqueado, 1 - sin bloquear o error.
164#@version 0.10 - Primera versión para OpenGnSys.
165#@author  Ramon Gomez, ETSII Universidad de Sevilla
166#@date    2011-01-17
167#@version 1.0.1 - Devolver falso en caso de error.
168#@author  Ramon Gomez, ETSII Universidad de Sevilla
169#@date    2011-05-18
170#*/
171function ogIsRepoLocked ()
172{
173# Variables locales.
174local f FILES
175
176# No hacer nada, si no está definido el punto de montaje del repositorio.
177[ -z "$OGIMG" ] && return 1
178
179# Comprobar si alguno de los ficheros abiertos por los procesos activos está en el
180# punto de montaje del repositorio de imágenes.
181FILES=$(for f in /proc/[0-9]*/fd/*; do readlink -f "$f"; done | grep "^$OGIMG")   # */ (comentario Doxygen)
182test -n "$FILES"
183}
184
185
186
187#/**
188#         ogHelp ["str_function" ["str_format" ["str_example" ... ]]]
189#@brief   Muestra mensaje de ayuda para una función determinda.
190#@param   str_function Nombre de la función.
191#@param   str_format   Formato de ejecución de la función.
192#@param   str_example  Ejemplo de ejecución de la función.
193#@return  str_help - Salida de ayuda.
194#@note    Si no se indican parámetros, la función se toma de la variable \c $FUNCNAME
195#@note    La descripción de la función se toma de la variable compuesta por \c MSG_FUNC_$función incluida en el fichero de idiomas.
196#@note    Pueden especificarse varios mensajes con ejemplos.
197#@version 0.9 - Primera versión para OpenGnSys.
198#@author  Ramon Gomez, ETSII Universidad de Sevilla
199#@date    2009-07-27
200#*/
201function ogHelp () {
202
203# Variables locales.
204local FUNC MSG
205
206# Mostrar función, descripción y formato.
207FUNC="${1:-${FUNCNAME[${#FUNCNAME[*]}-1]}}"
208MSG="MSG_HELP_$FUNC"
209ogEcho help "$MSG_FUNCTION $FUNC: ${!MSG}"
210[ -n "$2" ] && ogEcho help "    $MSG_FORMAT: $2"
211# Mostrar ejemplos (si existen).
212shift 2
213while [ $# -gt 0 ]; do
214    ogEcho help "    $MSG_EXAMPLE: $1"
215    shift
216done
217}
218
219
220function ogCheckProgram ()
221{
222# Si se solicita, mostrar ayuda.
223if [ "$*" == "help" ]; then
224    ogHelp "$FUNCNAME \"str_program ...\"" \
225           "$FUNCNAME \"partimage partclone mbuffer\""
226    return
227fi
228
229# Error si no se recibe 1 parámetro.
230[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
231
232local PERROR PLOG i
233PERROR=0
234PLOG=" "
235for i in `echo $1`
236do
237  if [ ! `which $i` ]
238     then
239        PERROR=1
240        PLOG="$PLOG $i"
241     fi
242done
243if [ "$PERROR" == "1" ]
244then
245        ogRaiseError $OG_ERR_NOTEXEC "$PLOG" || return $?
246else           
247        return 0
248fi
249}
250
Note: See TracBrowser for help on using the repository browser.