source: server/bin/setserveraddr @ 82e5b6c

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 82e5b6c was d7fe54a, checked in by ramon <ramongomez@…>, 8 years ago

#730: Sustitución del nombre del proyecto en ficheros del servidor.

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

  • Property mode set to 100755
File size: 4.5 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 "s,UrlMenu=https?://\([^/]*\)/\(.*\),UrlMenu=https://$SERVERIP/\2," \
93                    -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile
94                file="${f/./-$1.}"
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
99                        ln -f $file $f
100                        CHANGE=1
101                fi
102        done
103
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
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
137        else
138                # Mensaje indicando que no se han cambiado datos.
139                echo "Default interface has not changed: $1"
140        fi
141else
142        # Error: interfaz de red no encontrado.
143        echo "$PROG: Network device not found. Format: $PROG interface" >&2
144        exit 1
145fi
146
147# Eliminar ficheros temporales.
148rm -f $tmpfile $MYCNF
149
Note: See TracBrowser for help on using the repository browser.