source: server/bin/setserveraddr @ f8a75f18

opengnsys-1.0.6b
Last change on this file since f8a75f18 was f2ac15a, checked in by ramon <ramongomez@…>, 8 years ago

#754: Congelar versión de mantenimiento en rama tags/opengnsys-1.0.6b.

git-svn-id: https://opengnsys.es/svn/tags/opengnsys-1.0.6b@5144 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 4.5 KB
RevLine 
[91aaf03]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
[1602040]8# Versión: 1.0.5 - Regenerar ficheros de configuración.
9# Autor: Ramon Gomez - Univ. Sevilla
10# Fecha: 2014-06-06
[91aaf03]11
12
[1602040]13# Variables globales.
[91aaf03]14PROG="$(basename $0)"
[1602040]15
16# Comprobar parámetros.
[91aaf03]17if [ $# -ne 1 ]; then
[1602040]18        echo "$PROG: Incorrect operand. Format: $PROG interface" >&2
[91aaf03]19        exit 1
20fi
21if [ "$USER" != "root" ]; then
22        echo "$PROG: Need to be root." >&2
23        exit 1
24fi
25
[1602040]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
[91aaf03]33# Detectar la interfaz de red.
34DEVICES=$(ip -o link show up|awk -F: '$2!~/lo/ {print $2}')
[1602040]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)}')
[91aaf03]38done
39
[1602040]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
[91aaf03]47        # Comprobar si hay que modificar la configuración de DHCP.
48        CHANGE=0
[1602040]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.}"
[9c05bc7]53                        sed -e "s/next-server.*/next-server $SERVERIP;/" \
54                            -e "s/option routers ;/option routers ${SERVERIP%.*}.1;/" $file >$tmpfile
[1602040]55                        # Copiar el fichero y enlazarlo si hay cambios.
[de687e3]56                        if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
[1602040]57                                mv $tmpfile $file
58                                chmod 644 $file
59                                ln -f $file $f
60                                CHANGE=1
61                        fi
[91aaf03]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
[de687e3]70                echo "DHCP configuration has not changed."
[91aaf03]71        fi
[1602040]72
73        # Guardar la IP anterior del repositorio.
[91aaf03]74        OPENGNSYS=/opt/opengnsys
[1602040]75        source $OPENGNSYS/etc/ogAdmRepo.cfg
76        OLDSERVERIP=$IPlocal
77
78        # Comprobar si hay que modificar la configuración de OpenGnsys.
[91aaf03]79        CHANGE=0
[1602040]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
[91aaf03]84                        echo "$PROG: File $file does not exist." >&2
85                        exit 2
86                fi
[1602040]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," \
[f2ac15a]92                    -e "s,UrlMenu=https?://\([^/]*\)/\(.*\),UrlMenu=https://$SERVERIP/\2," \
[9c05bc7]93                    -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile
[1602040]94                file="${f/./-$1.}"
[de687e3]95                # Si se usa otro interfaz o cambian los datos de red; ...
96                if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
97                        # Copiar el fichero y enlazarlo.
98                        cp $tmpfile $file
[1602040]99                        ln -f $file $f
[91aaf03]100                        CHANGE=1
101                fi
102        done
[1602040]103
[91aaf03]104        # Si ha cambiado la configuración, reiniciar OpenGnSys y actualizar la BD.
105        if [ $CHANGE == 1 ]; then
106                /etc/init.d/opengnsys restart
107                source $OPENGNSYS/etc/ogAdmServer.cfg
[1602040]108                # Componer fichero con credenciales de conexión. 
109                cat << EOT > $MYCNF
110[client]
111user=$USUARIO
112password=$PASSWORD
113EOT
114                # Actualizar IP del servidor en la BD.
115                mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
116                         "UPDATE entornos
117                             SET ipserveradm='$SERVERIP'
118                           WHERE identorno=1"
119
120                # Actualizar IP del repositorio en la BD.
121                mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
122                         "UPDATE repositorios
123                             SET ip='$SERVERIP'
124                           WHERE ip='$OLDSERVERIP'"
125
126                # Mostrar instrucciones a realizar tras la ejecución.
127                cat << EOT
128Default server interface set to: $1 ($SERVERIP)
129
130Manual tasks:
131Check DHCP configuration file and restart service, if needed.
132Log-in as Web Console organization user.
133 - Check URLs in all menus.
134 - Run Advanced Netboot in all rooms.
135
136EOT
[91aaf03]137        else
[1602040]138                # Mensaje indicando que no se han cambiado datos.
[de687e3]139                echo "Default interface has not changed: $1"
[91aaf03]140        fi
141else
[1602040]142        # Error: interfaz de red no encontrado.
143        echo "$PROG: Network device not found. Format: $PROG interface" >&2
[91aaf03]144        exit 1
145fi
146
[de687e3]147# Eliminar ficheros temporales.
148rm -f $tmpfile $MYCNF
149
Note: See TracBrowser for help on using the repository browser.