source: server/bin/setserveraddr @ 4079733

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

Corregir errata en script setserveraddr al tratar algunas URL y forzar cambio a HTTPS.

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

  • Property mode set to 100755
File size: 4.3 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                        sed "s/next-server.*/next-server $SERVERIP;/" $f >$tmpfile
53                        file="${f/./-$1.}"
54                        # Copiar el fichero y enlazarlo si hay cambios.
55                        if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
56                                mv $tmpfile $file
57                                chmod 644 $file
58                                ln -f $file $f
59                                CHANGE=1
60                        fi
61                fi
62        done
63        # Si ha cambiado la configuración, reiniciar DHCP.
64        if [ $CHANGE == 1 ]; then
65                for f in /etc/init.d/{isc-dhcp-server,dhcp3-server,dhcpd}; do
66                        [ -x $f ] && $f restart
67                done
68        else
69                echo "DHCP configuration has not changed."
70        fi
71
72        # Guardar la IP anterior del repositorio.
73        OPENGNSYS=/opt/opengnsys
74        source $OPENGNSYS/etc/ogAdmRepo.cfg
75        OLDSERVERIP=$IPlocal
76
77        # Comprobar si hay que modificar la configuración de OpenGnsys.
78        CHANGE=0
79        # Procesar los ficheros de configuración de OpenGnSys.
80        for f in $OPENGNSYS/{etc/{ogAdmServer,ogAdmRepo,ogAdmAgent}.cfg,www/controlacceso.php,client/etc/ogAdmClient.cfg}; do
81                # Error si no existe algún fichero de configuración.
82                if [ ! -f $f ]; then
83                        echo "$PROG: File $file does not exist." >&2
84                        exit 2
85                fi
86                # Cambiar la IP del servidor:
87                # - variables  ServidorAdm  e  IPlocal,
88                # - servidor o IP en URLs excepto si contienen "localhost".
89                sed -e "s,ServidorAdm=.*,ServidorAdm=$SERVERIP," \
90                    -e "s,IPlocal=.*,IPlocal=$SERVERIP," \
91                    -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile
92                file="${f/./-$1.}"
93                # Si se usa otro interfaz o cambian los datos de red; ...
94                if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
95                        # Copiar el fichero y enlazarlo.
96                        cp $tmpfile $file
97                        ln -f $file $f
98                        CHANGE=1
99                fi
100        done
101
102        # Si ha cambiado la configuración, reiniciar OpenGnSys y actualizar la BD.
103        if [ $CHANGE == 1 ]; then
104                /etc/init.d/opengnsys restart
105                source $OPENGNSYS/etc/ogAdmServer.cfg
106                # Componer fichero con credenciales de conexión. 
107                cat << EOT > $MYCNF
108[client]
109user=$USUARIO
110password=$PASSWORD
111EOT
112                # Actualizar IP del servidor en la BD.
113                mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
114                         "UPDATE entornos
115                             SET ipserveradm='$SERVERIP'
116                           WHERE identorno=1"
117
118                # Actualizar IP del repositorio en la BD.
119                mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
120                         "UPDATE repositorios
121                             SET ip='$SERVERIP'
122                           WHERE ip='$OLDSERVERIP'"
123
124                # Mostrar instrucciones a realizar tras la ejecución.
125                cat << EOT
126Default server interface set to: $1 ($SERVERIP)
127
128Manual tasks:
129Check DHCP configuration file and restart service, if needed.
130Log-in as Web Console organization user.
131 - Check URLs in all menus.
132 - Run Advanced Netboot in all rooms.
133
134EOT
135        else
136                # Mensaje indicando que no se han cambiado datos.
137                echo "Default interface has not changed: $1"
138        fi
139else
140        # Error: interfaz de red no encontrado.
141        echo "$PROG: Network device not found. Format: $PROG interface" >&2
142        exit 1
143fi
144
145# Eliminar ficheros temporales.
146rm -f $tmpfile $MYCNF
147
Note: See TracBrowser for help on using the repository browser.