source: client/engine/Net.lib @ 79a4175

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 79a4175 was cead9e3, checked in by irina <irinagomez@…>, 9 years ago

#679 Varios repositorios: en caso de error al montar el nuevo repositorio se deja montado el que tuviera el cliente. Se crean mensajes de error correspondientes

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

  • Property mode set to 100755
File size: 9.4 KB
Line 
1#!/bin/bash
2#/**
3#@file     Net.lib
4#@brief    Librería o clase Net
5#@class    Net
6#@brief    Funciones básicas de red.
7#@version  1.0.6
8#@warning  License: GNU GPLv3+
9#*/
10
11
12#/**
13# ogChangeRepo  IPREPO [ OgUnit ]
14#@brief   Cambia el repositorio para el recurso remoto images.
15#@param 1 Ip Repositorio
16#@param 2 Abreviatura Unidad Organizativa
17#@return  Cambio recurso remoto en OGIMG.
18#@version 1.1 - Primera versión para OpenGnSys.
19#@author  Irina Gomez, ETSII Universidad de Sevilla
20#@date    2015-06-16
21#*/
22function ogChangeRepo {
23local SRCIMG
24
25if [ "$*" == "help" ]; then
26    ogHelp "$FUNCNAME" "$FUNCNAME IPREPO [ OgUnit ]" \
27           "$FUNCNAME 10.1.120.3" \
28           "$FUNCNAME 10.1.120.3 cdc"
29    return
30fi
31
32
33if [ $# -lt 1 ]; then
34    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME IPREPO [ OgUnit ]"
35    return $?
36fi
37
38REPO="${1^^}"
39# Si REPO tomamos el valor por defecto.
40[ "$REPO" == "REPO" ] && REPO=$ogrepo
41
42OLDREPO=$(ogGetRepoIp)
43# Si $1 y $2 son el repositorio y la OU actual me salgo
44[ "$REPO" == "$OLDREPO" ] && [ "$2" == "$ogunit" ]  && return 0
45
46source /scripts/functions
47source /scripts/ogfunctions
48umount $OGIMG
49[ "$2" == "" ] && SRCIMG="ogimages" || SRCIMG="ogimages/$2"
50eval $(grep "OPTIONS=" /scripts/ogfunctions)
51[ "$ogactiveadmin" == "true" ] && RW=",rw" || RW=",ro"
52
53ogEcho session log "$MSG_HELP_ogChangeRepo $REPO"
54ogConnect $REPO $ogprotocol $SRCIMG $OGIMG $RW
55
56# Si da error volvemos a montar el inicial
57if [ $? -ne 0 ]; then
58    ogConnect $OLDREPO $ogprotocol $SRCIMG $OGIMG $RW
59    ogRaiseError session $OG_ERR_REPO "$REPO"
60    return $?
61fi
62
63}
64
65
66#/**
67#         ogGetGroupDir [ str_repo ]
68#@brief   Devuelve el cmaino del directorio para el grupo del cliente.
69#@param   str_repo     repositorio de imágenes (opcional)
70#@return  path_dir  -  Camino al directorio del grupo.
71#@note    repo = { REPO, CACHE }     REPO por defecto
72#@exception OG_ERR_FORMAT    formato incorrecto.
73#@version 1.0.2 - Primera versión para OpenGnSys.
74#@author  Ramon Gomez, ETSII Universidad de Sevilla
75#@date    2011-10-03
76#*/
77function ogGetGroupDir ()
78{
79local REPO DIR GROUP
80if [ "$*" == "help" ]; then
81    ogHelp "$FUNCNAME" "$FUNCNAME str_repo" \
82           "$FUNCNAME REPO  ==>  /opt/opengnsys/images/groups/Grupo1"
83    return
84fi
85# Error si se recibe más de 1 parámetro.
86case $# in
87    0)  REPO="REPO" ;;
88    1)  REPO="$1" ;;
89    *)  ogRaiseError $OG_ERR_FORMAT "$*"
90        return $? ;;
91esac
92
93GROUP="$(ogGetGroupName)"
94if [ -n "$GROUP" ]; then
95    DIR=$(ogGetPath "$REPO" "/groups/$GROUP" 2>/dev/null)
96    [ -d "$DIR" ] && echo "$DIR"
97fi
98}
99
100
101#/**
102#         ogGetGroupName
103#@brief   Devuelve el nombre del grupo al que pertenece el cliente.
104#@return  str_group - Nombre de grupo.
105#@version 1.0.2 - Primera versión para OpenGnSys.
106#@author  Ramon Gomez, ETSII Universidad de Sevilla
107#@date    2011-10-03
108#*/
109function ogGetGroupName ()
110{
111if [ "$*" == "help" ]; then
112    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  Grupo1"
113    return
114fi
115[ -n "$group" ] && echo "$group"
116}
117
118
119#/**
120#         ogGetHostname
121#@brief   Muestra el nombre del cliente.
122#@return  str_host - nombre de máquina
123#@version 0.10 - Integración en OpenGnSys 0.10
124#@author  Ramon Gomez, ETSII Universidad de Sevilla
125#@date    2010-02-11
126#*/ ##
127function ogGetHostname ()
128{
129local HOST
130if [ "$*" == "help" ]; then
131    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  pc1"
132    return
133fi
134# Tomar nombre de la variable HOSTNAME
135HOST="$HOSTNAME"
136# Si no, tomar del DHCP, opción host-name       /* (comentario para Doxygen)
137[ -z "$HOST" ] && HOST=$(awk -F\" '/option host-name/ {gsub(/;/,""); host=$2}
138                                   END {print host}
139                                  ' /var/lib/dhcp3/dhclient.leases)
140# Si no, leer el parámetro del kernel hostname  (comentario para Doxygen) */
141[ -z "$HOST" ] && HOST=$(awk 'BEGIN {RS=""; FS="="}
142                              $1~/hostname/ {print $2}' /proc/cmdline)
143[ "$HOSTNAME" != "$HOST" ] && export HOSTNAME="$HOST"
144[ -n "$HOST" ] && echo $HOST
145}
146
147
148#/**
149#         ogGetIpAddress
150#@brief   Muestra la dirección IP del sistema
151#@return  str_ip - Dirección IP
152#@version 0.10 - Integración en OpenGnSys 0.10
153#@author  Ramon Gomez, ETSII Universidad de Sevilla
154#@date    2010-02-11
155#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
156#@note    Usa las variables utilizadas por el initrd "/etc/net-ethX.conf
157#@author  Antonio J. Doblas Viso. Universidad de Malaga.
158#@date    2011-02-24
159#@version 1.0.2 - Soporte para varias tarjetas de red
160#@author  Ramon Gomez, ETSII Universidad de Sevilla
161#@date    2011-06-17
162#*/ ##
163function ogGetIpAddress ()
164{
165local IP
166if [ "$*" == "help" ]; then
167    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  192.168.0.10"
168    return
169fi
170if [ -n "$IPV4ADDR" ]; then
171     IP="$IPV4ADDR"
172else
173    # Obtener direcciones IP.
174    if [ -n "$DEVICE" ]; then
175        IP=$(ip -o address show up dev "$DEVICE" 2>/dev/null | awk '{if ($3~/inet$/) {printf ("%s ", $4)}}')
176    else
177        IP=$(ip -o address show up | awk '$2!~/lo/ {if ($3~/inet$/) {printf ("%s ", $4)}}')
178    fi
179fi
180# Mostrar solo la primera.
181echo "${IP%%/*}"       # (comentario para Doxygen) */
182}
183
184
185#/**
186#         ogGetMacAddress
187#@brief   Muestra la dirección Ethernet del cliente.
188#@return  str_ether - Dirección Ethernet
189#@version 0.10 - Integración en OpenGnSys 0.10
190#@author  Ramon Gomez, ETSII Universidad de Sevilla
191#@date    2010-02-11
192#@version 1.0.2 - Soporte para varias tarjetas de red
193#@author  Ramon Gomez, ETSII Universidad de Sevilla
194#@date    2011-06-17
195#*/ ##
196function ogGetMacAddress ()
197{
198local MAC
199if [ "$*" == "help" ]; then
200    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  00:11:22:33:44:55"
201    return
202fi
203# Obtener direcciones Ethernet.
204if [ -n "$DEVICE" ]; then
205    MAC=$(ip -o link show up dev "$DEVICE" 2>/dev/null | awk '{sub (/.*\\/, ""); if ($1~/ether/) printf ("%s ", toupper($2));}')
206else
207    MAC=$(ip -o link show up | awk '$2!~/lo/ {sub (/.*\\/, ""); if ($1~/ether/) printf ("%s ", toupper($2));}')
208fi
209# Mostrar sólo la primera.
210echo ${MAC%% *}
211}
212
213
214#/**
215#         ogGetNetInterface
216#@brief   Muestra la interfaz de red del sistema
217#@return  str_interface - interfaz de red
218#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
219#@note    Usa las variables utilizadas por el initrd "/etc/net-ethX.conf
220#@author  Antonio J. Doblas Viso. Universidad de Malaga.
221#@date    2011-02-24
222#*/ ##
223function ogGetNetInterface ()
224{
225if [ "$*" == "help" ]; then
226    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  eth0"
227    return
228fi
229[ -n "$DEVICE" ] && echo "$DEVICE"
230}
231
232
233#/**
234#         ogGetRepoIp
235#@brief   Muestra la dirección IP del repositorio de datos.
236#@return  str_ip - Dirección IP
237#@version 0.10 - Integración en OpenGnSys 0.10
238#@author  Ramon Gomez, ETSII Universidad de Sevilla
239#@date    2011-01-13
240#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
241#@note   Comprobacion segun protocolo de conexion al Repo
242#@author  Antonio J. Doblas Viso. Universidad de Malaga.
243#@date    2011-02-24
244#@version 1.0.6 - Obtener datos del punto de montaje, evitando fallo si $ogprotocol está vacía.
245#@author  Ramon Gomez, ETSII Universidad de Sevilla
246#@date    2014-08-27
247#*/ ##
248function ogGetRepoIp ()
249{
250# Variables locales.
251local SOURCE FSTYPE
252
253# Mostrar ayuda.
254if [ "$*" == "help" ]; then
255    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  192.168.0.2"
256    return
257fi
258
259# Obtener direcciones IP, según el tipo de montaje.
260eval $(findmnt -P -o SOURCE,FSTYPE $OGIMG)
261case "$FSTYPE" in
262    nfs)   echo "$SOURCE" | cut -f1 -d: ;;
263    cifs)  echo "$SOURCE" | cut -f3 -d/ ;;
264esac
265}
266
267
268#/**
269#         ogGetServerIp
270#@brief   Muestra la dirección IP del Servidor de OpenGnSys.
271#@return  str_ip - Dirección IP
272#@version 0.10 - Integración en OpenGnSys 0.10
273#@author  Ramon Gomez, ETSII Universidad de Sevilla
274#@date    2011-01-13
275#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
276#@note   Comprobacion segun protocolo de conexion al Repo
277#@author  Antonio J. Doblas Viso. Universidad de Malaga.
278#@date    2011-02-24
279#@version 1.0.6 - Obtener datos del punto de montaje, evitando fallo si $ogprotocol está vacía.
280#@author  Ramon Gomez, ETSII Universidad de Sevilla
281#@date    2014-08-27
282#*/ ##
283function ogGetServerIp ()
284{
285# Variables locales.
286local SOURCE FSTYPE
287
288# Mostrar ayuda.
289if [ "$*" == "help" ]; then
290    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  192.168.0.2"
291    return
292fi
293
294# Obtener direcciones IP, según el tipo de montaje.
295eval $(findmnt -P -o SOURCE,FSTYPE $OPENGNSYS)
296case "$FSTYPE" in
297    nfs)   echo "$SOURCE" | cut -f1 -d: ;;
298    cifs)  echo "$SOURCE" | cut -f3 -d/ ;;
299esac
300}
301
302
303#/**
304#         ogMakeGroupDir [ str_repo ]
305#@brief   Crea el directorio para el grupo del cliente.
306#@param   str_repo     repositorio de imágenes (opcional)
307#@return  (nada)
308#@note    repo = { REPO, CACHE }     REPO por defecto
309#@exception OG_ERR_FORMAT    formato incorrecto.
310#@version 1.0.5 - Primera versión para OpenGnSys.
311#@author  Ramon Gomez, ETSII Universidad de Sevilla
312#@date    2013-09-26
313#*/
314function ogMakeGroupDir ()
315{
316local REPO DIR GROUP
317if [ "$*" == "help" ]; then
318    ogHelp "$FUNCNAME" "$FUNCNAME str_repo" \
319           "$FUNCNAME" "$FUNCNAME REPO"
320    return
321fi
322# Error si se recibe más de 1 parámetro.
323case $# in
324    0)  REPO="REPO" ;;
325    1)  REPO="$1" ;;
326    *)  ogRaiseError $OG_ERR_FORMAT "$*"
327        return $? ;;
328esac
329# Comprobar tipo de repositorio.
330DIR=$(ogGetPath "$REPO" / 2>/dev/null)
331[ -n "$DIR" ] || ogRaiseError $OG_ERR_FORMAT "$1"
332GROUP="$(ogGetGroupName)"
333if [ -n "$GROUP" ]; then
334    mkdir -p "$DIR/groups/$GROUP" 2>/dev/null
335fi
336}
337
Note: See TracBrowser for help on using the repository browser.