source: server/bin/setserveraddr @ 5cef356

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-instalacion
Last change on this file since 5cef356 was e020339, checked in by Ramón M. Gómez <ramongomez@…>, 5 years ago

#856: Fix bug when updating repository IP address.

  • 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        if [ "${ANSWER,,}" != "y" ]; then
56                echo "Operation canceled."
57                exit 0
58        fi
59        # Temporary files.
60        tmpfile=$(mktemp /tmp/og.XXXXX)
61        MYCNF=$(mktemp /tmp/.my.cnf.XXXXX)
62        trap "rm -f $tmpfile $MYCNF" 1 2 3 6 9 15
63
64        # Checking whether the DHCP settings need to be changed.
65        CHANGE=0
66        for f in /etc/{dhcp,hcp3}/dhcpd.conf; do
67                if [ -f $f ]; then
68                        # Changing DHCP "next-server" parameter.
69                        file="${f/./-$SERVERDEV.}"
70                        sed -e "s/next-server.*/next-server $SERVERIP;/" \
71                            -e "s/option routers ;/option routers ${SERVERIP%.*}.1;/" $file >$tmpfile
72                        # Copying and linking file if there are changes.
73                        if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
74                                mv $tmpfile $file
75                                chmod 644 $file
76                                ln -f $file $f
77                                CHANGE=1
78                        fi
79                fi
80        done
81        # Restarting DHCP service if its configuration has changed.
82        if [ $CHANGE == 1 ]; then
83                for f in /etc/init.d/{isc-dhcp-server,dhcp3-server,dhcpd}; do
84                        [ -x $f ] && $f restart
85                done
86        else
87                echo "DHCP configuration has not changed."
88        fi
89
90        # Saving old IP address.
91        source $OPENGNSYS/etc/ogAdmServer.cfg
92        OLDSERVERIP=$ServidorAdm
93        # Checking if configuration files need to be modified.
94        CHANGE=0
95        for f in $OPENGNSYS/{etc/{ogAdmServer,ogAdmRepo,ogAdmAgent}.cfg,www/controlacceso.php,client/etc/ogAdmClient.cfg}; do
96                # Updating configuration variables (if URL does not contain "localhost").
97                sed -e "s,\(ServidorAdm\|IPlocal\)=.*,\1=$SERVERIP," \
98                    -e "s,UrlMenu=https?://\([^/]*\)/\(.*\),UrlMenu=https://$SERVERIP/\2," \
99                    -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile
100                file="${f/./-$SERVERDEV.}"
101                # Copying updated file, if needed.
102                if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
103                        cp $tmpfile $file
104                        ln -f $file $f
105                        CHANGE=1
106                fi
107        done
108
109        # Processing when something has changed.
110        if [ $CHANGE == 1 ]; then
111                # Restart OpenGnsys services.
112                /etc/init.d/opengnsys restart
113                source $DEFAULTFILE
114                # If OpenGnsys Server is active, updating the database.
115                if [ "$RUN_OGADMSERVER" == "yes" ]; then
116                        # Creating credentials file.
117                        cat << EOT > $MYCNF
118[client]
119user=$USUARIO
120password=$PASSWORD
121EOT
122                        # Updating IP addresses and menu URLs.
123                        mysql --defaults-extra-file=$MYCNF -D "$CATALOG" << EOT
124UPDATE entornos
125   SET ipserveradm='$SERVERIP'
126 WHERE identorno=1;
127UPDATE repositorios
128   SET ip='$SERVERIP'
129 WHERE ip='$OLDSERVERIP';
130UPDATE menus
131   SET htmlmenupub = REPLACE(htmlmenupub, '$OLDSERVERIP', '$SERVERIP'),
132       htmlmenupri = REPLACE(htmlmenupri, '$OLDSERVERIP', '$SERVERIP');
133EOT
134                        # Updating all PXE files.
135                        find $PXEDIRS -name "01-*" -exec sed -i -e "s/$OLDSERVERIP/$SERVERIP/g" {} \;
136                fi
137
138                # Showing manual task to do after execution.
139                cat << EOT
140Default server interface set to: $SERVERDEV ($SERVERIP)
141
142Manual tasks:
143- Check DHCP configuration file and restart service, if needed.
144- Check PXE files.
145- Log-in as Web Console user:
146  - Check menu URLs.
147- Note: Run "settoken" script to update authentication tokens.
148EOT
149        else
150                # Showing message if nothing changes.
151                echo "Default interface has not changed: $1"
152        fi
153else
154        # Error if network interface is not found.
155        raiseError notfound "Network device"
156fi
157
158# Removing temporary files.
159rm -f $tmpfile $MYCNF
160
Note: See TracBrowser for help on using the repository browser.