source: client/engine/Net.lib @ b351d8a

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 b351d8a was 348b5c9, checked in by Irina Gómez <irinagomez@…>, 6 years ago

#855 #679 Los script de crear o restaurar imágenes sincronizadas permiten cambiar de repositorio. ogChangeRepo: se resuelve errata por la que no tomaba los permisos (ro|rw) del repositorio.

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