source: server/bin/setserveraddr @ b0c7586

918-git-images-111dconfigure-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-instalacion
Last change on this file since b0c7586 was 9d3b079, checked in by Ramón M. Gómez <ramongomez@…>, 5 years ago

#946: Fix bug in commit 6640e2c when setservderaddr parses configuration parameters.

  • Property mode set to 100755
File size: 5.0 KB
Line 
1#!/bin/bash
2
3#/**
4#@file    setserveraddr
5#@brief   Assign default IP address to OpenGnsys services.
6#@usage   setserveraddr {str_ipaddress | str_netiface}
7#@param   str_ipaddress  IP address assigned to a network interface
8#@param   str_netiface   network interface name defined by the operating system
9#@version Initial version.
10#@author  Ramón M. Gómez - ETSII Univ. Sevilla
11#@date    2011-01-25
12#@version 1.0.5 - Regenerate configuration files.
13#@author  Ramón M. Gómez - ETSII Univ. Sevilla
14#@date    2014-06-06
15#@version 1.1.1 - Updating menu URLs and PXE files.
16#@author  Ramón M. Gómez - ETSII Univ. Sevilla
17#@date    2018-11-15
18#*/ ##
19
20
21# Variables.
22PROG="$(basename "$0")"
23OPENGNSYS=/opt/opengnsys
24PXEDIRS="$OPENGNSYS/tftpboot/menu.lst $OPENGNSYS/tftpboot/grub"
25DEFAULTFILE=/etc/default/opengnsys
26
27# Functions.
28source $OPENGNSYS/lib/ogfunctions.sh
29
30# Checking parameters.
31[ "$USER" != "root" ] && raiseError access "Need to be root"
32[ $# -ne 1 ] && raiseError usage
33[ -r $DEFAULTFILE ] || raiseError access "Cannot read default configuration file"
34for f in $OPENGNSYS/{etc/{ogAdmServer,ogAdmRepo,ogAdmAgent}.cfg,www/controlacceso.php,client/etc/ogAdmClient.cfg}; do
35        [ -w $f ] || raiseError access "Cannot write to file: $f"
36done
37# Show help message.
38[ "$1" == "help" ] && help
39
40# Detecting network interfaces.
41DEVICES=$(ip -o link show up | awk -F: '$2!~/lo/ {print $2}')
42for DEV in $DEVICES; do
43        # If the network interface is found, get its IP address.
44        IP=$(ip -o addr show dev "$DEV" | awk '$3~/inet$/ {sub (/\/.*/, ""); print ($4)}')
45        if [ "$DEV" == "$1" ] || [ "$IP" == "$1" ]; then
46                SERVERIP="$IP"
47                SERVERDEV="$DEV"
48        fi
49done
50
51# Checking if IP address has been detected.
52if [ -n "$SERVERIP" ]; then
53        # Showing warning to inform that initiated clients may hang.
54        read -rp "WARNING: initiated clients can hang. Continue? (y/n): " ANSWER
55        [ "${ANSWER,,}" != "y" ] && raiseError cancel "Do nothing"
56        # Temporary files.
57        tmpfile=$(mktemp /tmp/og.XXXXX)
58        MYCNF=$(mktemp /tmp/.my.cnf.XXXXX)
59        trap "rm -f $tmpfile $MYCNF" 1 2 3 6 9 15
60
61        # Checking whether the DHCP settings need to be changed.
62        CHANGE=0
63        for f in /etc/{dhcp,hcp3}/dhcpd.conf; do
64                if [ -f $f ]; then
65                        # Changing DHCP "next-server" parameter.
66                        file="${f/./-$SERVERDEV.}"
67                        sed -e "s/next-server.*/next-server $SERVERIP;/" \
68                            -e "s/option routers ;/option routers ${SERVERIP%.*}.1;/" $file >$tmpfile
69                        # Copying and linking file if there are changes.
70                        if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
71                                mv $tmpfile $file
72                                chmod 644 $file
73                                ln -f $file $f
74                                CHANGE=1
75                        fi
76                fi
77        done
78        # Restarting DHCP service if its configuration has changed.
79        if [ $CHANGE == 1 ]; then
80                for s in isc-dhcp-server dhcp3-server dhcpd; do
81                        restart $s &>/dev/null && break
82                done
83        else
84                echo "DHCP configuration has not changed."
85        fi
86
87        # Saving old IP address.
88        source $OPENGNSYS/etc/ogAdmServer.cfg
89        OLDSERVERIP=$ServidorAdm
90        # Checking if configuration files need to be modified.
91        CHANGE=0
92        for f in $OPENGNSYS/{etc/{ogAdmServer,ogAdmRepo,ogAdmAgent}.cfg,www/controlacceso.php,client/etc/ogAdmClient.cfg}; do
93                # Updating configuration variables (if URL does not contain "localhost").
94                sed -e "s,\(ServidorAdm\|IPlocal\)=.*,\1=$SERVERIP," \
95                    -e "s,^INTERFACE=.*,INTERFACE=$SERVERDEV," \
96                    -e "s,UrlMenu=https?://\([^/]*\)/\(.*\),UrlMenu=https://$SERVERIP/\2," \
97                    -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile
98                file="${f/./-$SERVERDEV.}"
99                # Copying updated file, if needed.
100                if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
101                        cp $tmpfile $file
102                        ln -f $file $f
103                        CHANGE=1
104                fi
105        done
106
107        # Processing when something has changed.
108        if [ $CHANGE == 1 ]; then
109                # Restart OpenGnsys services.
110                echo "Restarting services..."
111                restart opengnsys >/dev/null
112                source $DEFAULTFILE
113                # If OpenGnsys Server is active, updating the database.
114                if [ "$RUN_OGADMSERVER" == "yes" ]; then
115                        # Creating credentials file.
116                        cat << EOT > $MYCNF
117[client]
118user=$USUARIO
119password=$PASSWORD
120EOT
121                        # Updating IP addresses and menu URLs.
122                        mysql --defaults-extra-file=$MYCNF -D "$CATALOG" << EOT
123UPDATE entornos
124   SET ipserveradm='$SERVERIP'
125 WHERE identorno=1;
126UPDATE repositorios
127   SET ip='$SERVERIP'
128 WHERE ip='$OLDSERVERIP';
129UPDATE menus
130   SET htmlmenupub = REPLACE(htmlmenupub, '$OLDSERVERIP', '$SERVERIP'),
131       htmlmenupri = REPLACE(htmlmenupri, '$OLDSERVERIP', '$SERVERIP');
132EOT
133                        # Updating all PXE files.
134                        find $PXEDIRS -name "01-*" -exec sed -i -e "s/$OLDSERVERIP/$SERVERIP/g" {} \;
135                fi
136
137                # Showing manual task to do after execution.
138                cat << EOT
139Default server interface set to: $SERVERDEV ($SERVERIP)
140
141Manual tasks:
142- Check DHCP configuration file and restart service, if needed.
143- Check PXE files.
144- Log-in as Web Console user:
145  - Check menu URLs.
146- Note: Run "settoken" script to update authentication tokens.
147EOT
148        else
149                # Showing message if nothing changes.
150                echo "Default interface has not changed: $1"
151        fi
152else
153        # Error if network interface is not found.
154        raiseError notfound "Network device"
155fi
156
157# Removing temporary files.
158rm -f $tmpfile $MYCNF
159
Note: See TracBrowser for help on using the repository browser.