source: server/bin/setserveraddr @ 138234e

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 138234e was dde2db1, checked in by Ramón M. Gómez <ramongomez@…>, 6 years ago

#925: Only the installer uses settoken script to generate authentication tokens.

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