source: server/bin/setserveraddr @ 12e1eef

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

#856: Improvements in setserveraddr script:

  • If Server is active, updating IP address in PXE files and database tables.
  • If Repository is active, generating new API token.
  • Supporting an IP address as input parameter.
  • Property mode set to 100755
File size: 5.5 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                # If Repository is active, generating a new API token.
119                source $DEFAULTFILE
120                if [ "$RUN_OGADMREPO" == "yes" ]; then
121                        REPOKEY=$(php -r 'echo md5(uniqid(rand(), true));')
122                        sed -i -e "s/ApiToken=.*/ApiToken=$REPOKEY/" $OPENGNSYS/etc/ogAdmRepo.cfg
123                fi
124                # If OpenGnsys Server is active, updating the database.
125                if [ "$RUN_OGADMSERVER" == "yes" ]; then
126                        source $OPENGNSYS/etc/ogAdmServer.cfg
127                        # Creating credentials file.
128                        cat << EOT > $MYCNF
129[client]
130user=$USUARIO
131password=$PASSWORD
132EOT
133                        # Updating OpenGnsys Server IP address.
134                        mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
135                                 "UPDATE entornos
136                                     SET ipserveradm='$SERVERIP'
137                                   WHERE identorno=1"
138                        # If OpenGnsys Repository is active, updating IP address and API token.
139                        if [ "$RUN_OGADMREPO" == "yes" ]; then
140                                mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
141                                         "UPDATE repositorios
142                                             SET ip='$SERVERIP', apikey='$REPOKEY'
143                                           WHERE ip='$OLDSERVERIP'"
144                                unset REPOKEY
145                        fi
146                        # Updating all menu URLs.
147                        mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
148                                 "UPDATE menus
149                                     SET htmlmenupub = REPLACE(htmlmenupub, '$OLDSERVERIP', '$SERVERIP'),
150                                         htmlmenupri = REPLACE(htmlmenupri, '$OLDSERVERIP', '$SERVERIP');"
151                        # Updating all PXE files.
152                        find $PXEDIR -name "01-*" -exec sed -i -e "s/$OLDSERVERIP/$SERVERIP/g" {} \;
153                fi
154
155                # Showing manual task to do after execution.
156                cat << EOT
157Default server interface set to: $SERVERDEV ($SERVERIP)
158
159Manual tasks:
160- Check DHCP configuration file and restart service, if needed.
161- Check PXE files.
162- Log-in as Web Console user:
163  - Check menu URLs.
164${REPOKEY:+"  - Update repository API token"}
165EOT
166        else
167                # Showing message if nothing changes.
168                echo "Default interface has not changed: $1"
169        fi
170else
171        # Error if network interface is not found.
172        echo "$PROG: Network device not found. Format: $PROG ipaddress|netiface" >&2
173        exit 1
174fi
175
176# Removing temporary files.
177rm -f $tmpfile $MYCNF
178
Note: See TracBrowser for help on using the repository browser.