source: server/bin/setserveraddr @ f5aad83

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 f5aad83 was e0314d93, checked in by ramon <ramongomez@…>, 9 years ago

#730 #739: Cambios aplicados:

  • Corregir errata en setserveraddr para tratar el fichero DHCP adecuado y establecer router por defecto si no existe.
  • Eliminar cambios temporales en Vagrantfile que corregían problemas de setserveraddr.
  • Actualizar lista de tickets resueltos.

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

  • Property mode set to 100755
File size: 4.4 KB
Line 
1#!/bin/bash
2# setserveraddr: modifica los ficheros de configuración para asignar los valores
3#       de la interfaz de red solicitada.
4# Nota: se enlazan los ficheros a los predefinidos detectados para la interfaz.
5# Uso:  setserveraddr iface
6# Autor: Ramon Gomez - Univ. Sevilla
7# Fecha: 2011-01-25
8# Versión: 1.0.5 - Regenerar ficheros de configuración.
9# Autor: Ramon Gomez - Univ. Sevilla
10# Fecha: 2014-06-06
11
12
13# Variables globales.
14PROG="$(basename $0)"
15
16# Comprobar parámetros.
17if [ $# -ne 1 ]; then
18        echo "$PROG: Incorrect operand. Format: $PROG interface" >&2
19        exit 1
20fi
21if [ "$USER" != "root" ]; then
22        echo "$PROG: Need to be root." >&2
23        exit 1
24fi
25
26# Aviso informando de que los clientes iniciados pueden quedarse colgados.
27read -p "WARNING: initiated clients can hang. Continue? (y/n): " ANSWER
28if [ "${ANSWER^^}" != "Y" ]; then
29        echo "Operation canceled."
30        exit 0
31fi
32
33# Detectar la interfaz de red.
34DEVICES=$(ip -o link show up|awk -F: '$2!~/lo/ {print $2}')
35for DEV in $DEVICES; do
36        # Si se encuentra la interfaz de red, obtener su dirección IP.
37        [ "$DEV" == "$1" ] && SERVERIP=$(ip -o addr show dev $DEV | awk '$3~/inet$/ {sub (/\/.*/, ""); print ($4)}')
38done
39
40# Comprobar si se ha detectado dirección IP.
41if [ -n "$SERVERIP" ]; then
42        # Ficheros temporales.
43        tmpfile=$(mktemp /tmp/og.XXXXX)
44        MYCNF=$(mktemp /tmp/.my.cnf.XXXXX)
45        trap "rm -f $tmpfile $MYCNF" 1 2 3 6 9 15
46
47        # Comprobar si hay que modificar la configuración de DHCP.
48        CHANGE=0
49        for f in /etc/{dhcp,hcp3}/dhcpd.conf; do
50                if [ -f $f ]; then
51                        # Cambiar el parámetro "next-server" de DHCP.
52                        file="${f/./-$1.}"
53                        sed -e "s/next-server.*/next-server $SERVERIP;/" \
54                            -e "s/option routers ;/option routers ${SERVERIP%.*}.1;/" $file >$tmpfile
55                        # Copiar el fichero y enlazarlo si hay cambios.
56                        if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
57                                mv $tmpfile $file
58                                chmod 644 $file
59                                ln -f $file $f
60                                CHANGE=1
61                        fi
62                fi
63        done
64        # Si ha cambiado la configuración, reiniciar DHCP.
65        if [ $CHANGE == 1 ]; then
66                for f in /etc/init.d/{isc-dhcp-server,dhcp3-server,dhcpd}; do
67                        [ -x $f ] && $f restart
68                done
69        else
70                echo "DHCP configuration has not changed."
71        fi
72
73        # Guardar la IP anterior del repositorio.
74        OPENGNSYS=/opt/opengnsys
75        source $OPENGNSYS/etc/ogAdmRepo.cfg
76        OLDSERVERIP=$IPlocal
77
78        # Comprobar si hay que modificar la configuración de OpenGnsys.
79        CHANGE=0
80        # Procesar los ficheros de configuración de OpenGnSys.
81        for f in $OPENGNSYS/{etc/{ogAdmServer,ogAdmRepo,ogAdmAgent}.cfg,www/controlacceso.php,client/etc/ogAdmClient.cfg}; do
82                # Error si no existe algún fichero de configuración.
83                if [ ! -f $f ]; then
84                        echo "$PROG: File $file does not exist." >&2
85                        exit 2
86                fi
87                # Cambiar la IP del servidor:
88                # - variables  ServidorAdm  e  IPlocal,
89                # - servidor o IP en URLs excepto si contienen "localhost".
90                sed -e "s,ServidorAdm=.*,ServidorAdm=$SERVERIP," \
91                    -e "s,IPlocal=.*,IPlocal=$SERVERIP," \
92                    -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile
93                file="${f/./-$1.}"
94                # Si se usa otro interfaz o cambian los datos de red; ...
95                if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
96                        # Copiar el fichero y enlazarlo.
97                        cp $tmpfile $file
98                        ln -f $file $f
99                        CHANGE=1
100                fi
101        done
102
103        # Si ha cambiado la configuración, reiniciar OpenGnSys y actualizar la BD.
104        if [ $CHANGE == 1 ]; then
105                /etc/init.d/opengnsys restart
106                source $OPENGNSYS/etc/ogAdmServer.cfg
107                # Componer fichero con credenciales de conexión. 
108                cat << EOT > $MYCNF
109[client]
110user=$USUARIO
111password=$PASSWORD
112EOT
113                # Actualizar IP del servidor en la BD.
114                mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
115                         "UPDATE entornos
116                             SET ipserveradm='$SERVERIP'
117                           WHERE identorno=1"
118
119                # Actualizar IP del repositorio en la BD.
120                mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
121                         "UPDATE repositorios
122                             SET ip='$SERVERIP'
123                           WHERE ip='$OLDSERVERIP'"
124
125                # Mostrar instrucciones a realizar tras la ejecución.
126                cat << EOT
127Default server interface set to: $1 ($SERVERIP)
128
129Manual tasks:
130Check DHCP configuration file and restart service, if needed.
131Log-in as Web Console organization user.
132 - Check URLs in all menus.
133 - Run Advanced Netboot in all rooms.
134
135EOT
136        else
137                # Mensaje indicando que no se han cambiado datos.
138                echo "Default interface has not changed: $1"
139        fi
140else
141        # Error: interfaz de red no encontrado.
142        echo "$PROG: Network device not found. Format: $PROG interface" >&2
143        exit 1
144fi
145
146# Eliminar ficheros temporales.
147rm -f $tmpfile $MYCNF
148
Note: See TracBrowser for help on using the repository browser.