source: client/engine/System.lib @ 46f7d6f

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

#537 #636: Redirecciones correctas de mensajes de error en función ogRaiseError.

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

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