source: client/engine/System.lib @ 3294b83

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 3294b83 was 3294b83, checked in by ramon <ramongomez@…>, 11 years ago

#616 #637: Actualizar lista de tickets cerrados; función ogRaiseError muestra traza de llamada de funciones afectadas.

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

  • Property mode set to 100755
File size: 8.3 KB
RevLine 
[9f29ba6]1#!/bin/bash
2#/**
3#@file     System.lib
[9f57de01]4#@brief    Librería o clase System
[9f29ba6]5#@class    System
6#@brief    Funciones básicas del sistema.
[e7e3140]7#@version  1.0.5
[9f29ba6]8#@warning  License: GNU GPLv3+
9#*/
10
[2e15649]11
[9f29ba6]12#/**
[4bf0ddf]13#         ogEcho [str_logtype ...] [str_loglevel] "str_message" ...
[9f57de01]14#@brief   Muestra mensajes en consola y lo registra en fichero de incidencias.
[e7e3140]15#@param   str_logtype  tipo de registro de incidencias.
[42669ebf]16#@param   str_loglevel nivel de registro de incidencias.
17#@param   str_message  mensaje (puede recibir más de 1 parámetro.
[9f57de01]18#@return  Mensaje mostrado.
[e2923e1]19#@warning Si no se indica nivel de registro, solo muestra mensaje en pantalla.
[4bf0ddf]20#@note    logfile = { log, command, session }; usa "log" si se indica nivel de registro.
21#@note    loglevel = { help, info, warning, error }
[aae34f6]22#@note    El nivel de ayuda \c (help) no se registra en el fichero de incidencias.
[ead38fb]23#@version 0.9 - Primera versión para OpenGnSys
[aae34f6]24#@author  Ramon Gomez, ETSII Universidad de Sevilla
[9f57de01]25#@date    2009-07-23
[4bf0ddf]26#@version 1.0.5 - Elegir fichero de log.
27#@author  Ramon Gomez, ETSII Universidad de Sevilla
28#@date    2009-07-23
[9f29ba6]29#*/
[9f57de01]30function ogEcho () {
[9f29ba6]31
[59f9ad2]32# Variables locales
[4bf0ddf]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
[9f57de01]44
[42669ebf]45# Selección del nivel de registro (opcional).
[4bf0ddf]46case "${1,,}" in
[d071d9b]47     help)    shift ;;
[e2923e1]48     info)    LOGLEVEL="$1"; shift ;;
49     warning) LOGLEVEL="$1"; shift ;;
50     error)   LOGLEVEL="$1"; shift ;;
[d071d9b]51     *)       ;;
[9f57de01]52esac
[42669ebf]53
[cfeabbf]54if [ -n "$LOGLEVEL" ]; then
[4bf0ddf]55    DATETIME=$(date +"%F %T")
56    logger -s -t "OpenGnSys $LOGLEVEL" "$DATETIME $*" 2>&1 | tee -a $OGLOGFILE $LOGS
[cfeabbf]57else
[4bf0ddf]58    echo "$*" | tee -a $LOGS
[9f57de01]59fi
[9f29ba6]60}
61
62
63#/**
[4bf0ddf]64#         ogRaiseError [str_logtype ...] int_errcode ["str_errmessage" ...]
[9f57de01]65#@brief   Devuelve el mensaje y el código de error correspondiente.
[e7e3140]66#@param   str_logtype    tipo de registro de incidencias.
[42669ebf]67#@param   int_errcode    código de error.
68#@param   str_errmessage mensajes complementarios de error.
[3294b83]69#@return  str_message - Mensaje de error, incluyendo las funciones relacionadas.
[9f57de01]70#@warning No definidas
[aae34f6]71#@note    Mensajes internacionales del fichero de idiomas.
[ead38fb]72#@version 0.9 - Primera versión para OpenGnSys.
[aae34f6]73#@author  Ramon Gomez, ETSII Universidad de Sevilla
[9f57de01]74#@date    2009-07-21
[3294b83]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
[9f29ba6]78#*/
[9f57de01]79function ogRaiseError () {
[9f29ba6]80
[59f9ad2]81# Variables locales
[3294b83]82local CONT=1 LOGS MSG CODE FUNCS
[4bf0ddf]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
[2e15649]91
[42669ebf]92# Obtener código y mensaje de error.
[4bf0ddf]93CODE="$1"
[a5df9b9]94case "$CODE" in
[315aaca]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\"" ;;
[4e1dc53]101     $OG_ERR_NOGPT)      MSG="$MSG_ERR_NOGPT \"$2\"" ;;
[315aaca]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\"" ;;
[eb9424f]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_UCASTSYNTAXT)   MSG="$MSG_ERR_UCASTSYNTAXT \"$2\"" ;;
113     $OG_ERR_UCASTSENDPARTITION)   MSG="$MSG_ERR_UCASTSENDPARTITION \"$2\"" ;;   
114     $OG_ERR_UCASTSENDFILE)   MSG="$MSG_ERR_UCASTSENDFILE \"$2\"" ;; 
115     $OG_ERR_UCASTRECEIVERPARTITION)   MSG="$MSG_ERR_UCASTRECEIVERPARTITION \"$2\"" ;;   
116     $OG_ERR_UCASTRECEIVERFILE)   MSG="$MSG_ERR_UCASTRECEIVERFILE \"$2\"" ;; 
117     $OG_ERR_MCASTSYNTAXT)   MSG="$MSG_ERR_MCASTSYNTAXT \"$2\"" ;;
118     $OG_ERR_MCASTSENDFILE)   MSG="$MSG_ERR_MCASTSENDFILE \"$2\"" ;;
119     $OG_ERR_MCASTRECEIVERFILE)   MSG="$MSG_ERR_MCASTRECEIVERFILE \"$2\"" ;;
120     $OG_ERR_MCASTSENDPARTITION)   MSG="$MSG_ERR_MCASTSENDPARTITION \"$2\"" ;;
121     $OG_ERR_MCASTRECEIVERPARTITION)   MSG="$MSG_ERR_MCASTRECEIVERPARTITION \"$2\"" ;;
[4e1dc53]122     $OG_ERR_PROTOCOLJOINMASTER)   MSG="$MSG_ERR_PROTOCOLJOINMASTER \"$2\"" ;;
[1ee5d4d3]123     $OG_ERR_DONTMOUNT_IMAGE)   MSG="$MSG_ERR_DONTMOUNT_IMAGE \"$2\"" ;;
[cd1f048]124     $OG_ERR_DONTUNMOUNT_IMAGE)   MSG="$MSG_ERR_DONTUNMOUNT_IMAGE \"$2\"" ;;
[e27c4f4]125     $OG_ERR_DONTSYNC_IMAGE)    MSG="$MSG_ERR_DONTSYNC_IMAGE \"$2\"" ;;
[a4f7a1a0]126     $OG_ERR_NOTDIFFERENT)      MSG="$MSG_ERR_NOTDIFFERENT \"$2\"" ;;
[1cd64e6]127     *)                  MSG="$MSG_ERR_GENERIC"; CODE=$OG_ERR_GENERIC ;;
[2e15649]128esac
129
[42669ebf]130# Mostrar mensaje de error y salir con el código indicado.
[3294b83]131FUNCS="${FUNCNAME[@]:1}"
132ogEcho $LOGS error "${FUNCS// /<-}: $MSG" >&2
[a5df9b9]133return $CODE
[9f29ba6]134}
135
136
[aae34f6]137#/**
[b1384da]138#         ogGetCaller
139#@brief   Devuelve nombre del programa o script ejecutor (padre).
140#@param   No.
141#@return  str_name - Nombre del programa ejecutor.
142#@version 0.10 - Primera versión para OpenGnSys.
143#@author  Ramon Gomez, ETSII Universidad de Sevilla
144#@date    2011-01-17
145#*/
146function ogGetCaller () {
147
148# Obtener el nombre del programa o del script que ha llamado al proceso actual.
[130408f]149basename "$(ps hlp $PPID | awk '{if ($13~/bash/ && $14!="") print $14;
150                                 else { sub(/^-/,"",$13); print $13; } }')"
[b1384da]151}
152
153
154#/**
155#         ogIsRepoLocked
156#@brief   Comprueba si el repositorio está siendo usado (tiene ficheros abiertos).
157#@param   No.
[7685100]158#@return  Código de salida: 0 - bloqueado, 1 - sin bloquear o error.
[b1384da]159#@version 0.10 - Primera versión para OpenGnSys.
160#@author  Ramon Gomez, ETSII Universidad de Sevilla
161#@date    2011-01-17
[7685100]162#@version 1.0.1 - Devolver falso en caso de error.
163#@author  Ramon Gomez, ETSII Universidad de Sevilla
164#@date    2011-05-18
[b1384da]165#*/
[7685100]166function ogIsRepoLocked ()
167{
[b1384da]168# Variables locales.
169local f FILES
170
171# No hacer nada, si no está definido el punto de montaje del repositorio.
[7685100]172[ -z "$OGIMG" ] && return 1
[b1384da]173
174# Comprobar si alguno de los ficheros abiertos por los procesos activos está en el
175# punto de montaje del repositorio de imágenes.
[697a9ee]176FILES=$(for f in /proc/[0-9]*/fd/*; do readlink -f "$f"; done | grep "^$OGIMG")   # */ (comentario Doxygen)
[7685100]177test -n "$FILES"
[b1384da]178}
179
180
181
182#/**
[1e7eaab]183#         ogHelp ["str_function" ["str_format" ["str_example" ... ]]]
[aae34f6]184#@brief   Muestra mensaje de ayuda para una función determinda.
[42669ebf]185#@param   str_function Nombre de la función.
186#@param   str_format   Formato de ejecución de la función.
187#@param   str_example  Ejemplo de ejecución de la función.
188#@return  str_help - Salida de ayuda.
[aae34f6]189#@note    Si no se indican parámetros, la función se toma de la variable \c $FUNCNAME
190#@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.
191#@note    Pueden especificarse varios mensajes con ejemplos.
[ead38fb]192#@version 0.9 - Primera versión para OpenGnSys.
[aae34f6]193#@author  Ramon Gomez, ETSII Universidad de Sevilla
194#@date    2009-07-27
195#*/
196function ogHelp () {
197
[59f9ad2]198# Variables locales.
[aae34f6]199local FUNC MSG
200
[42669ebf]201# Mostrar función, descripción y formato.
[aae34f6]202FUNC="${1:-${FUNCNAME[${#FUNCNAME[*]}-1]}}"
203MSG="MSG_HELP_$FUNC"
204ogEcho help "$MSG_FUNCTION $FUNC: ${!MSG}"
205[ -n "$2" ] && ogEcho help "    $MSG_FORMAT: $2"
[42669ebf]206# Mostrar ejemplos (si existen).
[aae34f6]207shift 2
208while [ $# -gt 0 ]; do
209    ogEcho help "    $MSG_EXAMPLE: $1"
210    shift
211done
212}
[914d834]213
[1cd64e6]214
215function ogCheckProgram ()
[914d834]216{
217# Si se solicita, mostrar ayuda.
218if [ "$*" == "help" ]; then
[1cd64e6]219    ogHelp "$FUNCNAME \"str_program ...\"" \
220           "$FUNCNAME \"partimage partclone mbuffer\""
[914d834]221    return
222fi
223
224# Error si no se recibe 1 parámetro.
225[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
226
[1cd64e6]227local PERROR PLOG i
[914d834]228PERROR=0
229PLOG=" "
230for i in `echo $1`
231do
232  if [ ! `which $i` ]
233     then
234        PERROR=1
235        PLOG="$PLOG $i"
236     fi
237done
238if [ "$PERROR" == "1" ]
239then
240        ogRaiseError $OG_ERR_NOTEXEC "$PLOG" || return $?
241else           
242        return 0
243fi
244}
[1cd64e6]245
Note: See TracBrowser for help on using the repository browser.