source: server/bin/setserveraddr @ 062ea34

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 062ea34 was 940b1c7f, checked in by Ramón M. Gómez <ramongomez@…>, 5 years ago

#816 856: Scripts setserveraddr and etclientmode` use common Shell function file.

  • Property mode set to 100755
File size: 4.9 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 f in /etc/init.d/{isc-dhcp-server,dhcp3-server,dhcpd}; do
81                        [ -x $f ] && $f restart
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,UrlMenu=https?://\([^/]*\)/\(.*\),UrlMenu=https://$SERVERIP/\2," \
96                    -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile
97                file="${f/./-$SERVERDEV.}"
98                # Copying updated file, if needed.
99                if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
100                        cp $tmpfile $file
101                        ln -f $file $f
102                        CHANGE=1
103                fi
104        done
105
106        # Processing when something has changed.
107        if [ $CHANGE == 1 ]; then
108                # Restart OpenGnsys services.
109                /etc/init.d/opengnsys restart
110                source $DEFAULTFILE
111                # If OpenGnsys Server is active, updating the database.
112                if [ "$RUN_OGADMSERVER" == "yes" ]; then
113                        # Creating credentials file.
114                        cat << EOT > $MYCNF
115[client]
116user=$USUARIO
117password=$PASSWORD
118EOT
119                        # Updating IP addresses and menu URLs.
120                        mysql --defaults-extra-file=$MYCNF -D "$CATALOG" << EOT
121UPDATE entornos
122   SET ipserveradm='$SERVERIP'
123 WHERE identorno=1;
124UPDATE repositorios
125   SET ip='$SERVERIP'
126 WHERE ip='$OLDSERVERIP';
127UPDATE menus
128   SET htmlmenupub = REPLACE(htmlmenupub, '$OLDSERVERIP', '$SERVERIP'),
129       htmlmenupri = REPLACE(htmlmenupri, '$OLDSERVERIP', '$SERVERIP');
130EOT
131                        # Updating all PXE files.
132                        find $PXEDIRS -name "01-*" -exec sed -i -e "s/$OLDSERVERIP/$SERVERIP/g" {} \;
133                fi
134
135                # Showing manual task to do after execution.
136                cat << EOT
137Default server interface set to: $SERVERDEV ($SERVERIP)
138
139Manual tasks:
140- Check DHCP configuration file and restart service, if needed.
141- Check PXE files.
142- Log-in as Web Console user:
143  - Check menu URLs.
144- Note: Run "settoken" script to update authentication tokens.
145EOT
146        else
147                # Showing message if nothing changes.
148                echo "Default interface has not changed: $1"
149        fi
150else
151        # Error if network interface is not found.
152        raiseError notfound "Network device"
153fi
154
155# Removing temporary files.
156rm -f $tmpfile $MYCNF
157
Note: See TracBrowser for help on using the repository browser.