source: server/bin/setserveraddr @ 091a2b1

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 091a2b1 was 8948748, checked in by OpenGnSys Support Team <soporte-og@…>, 6 years ago

#915 update installer to add APITOKEN= to ogAdmServer.cfg

This is going to be used to protect calls to ogAdmServer

Example line for ogAdmServer.cfg:

APITOKEN=9b5fddfc5d4d1daa74ffd7ce7c3c9925

  • Property mode set to 100755
File size: 5.6 KB
RevLine 
[c8fd992]1#!/bin/bash
2
[df7d8f0]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.
[c8fd992]28if [ $# -ne 1 ]; then
[df7d8f0]29        echo "$PROG: Incorrect operand. Format: $PROG ipaddress|netiface" >&2
[c8fd992]30        exit 1
31fi
32if [ "$USER" != "root" ]; then
33        echo "$PROG: Need to be root." >&2
34        exit 1
35fi
36
[df7d8f0]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
[8f70b5b]40        echo "Operation canceled."
41        exit 0
42fi
43
[df7d8f0]44# Detecting network interfaces.
45DEVICES=$(ip -o link show up | awk -F: '$2!~/lo/ {print $2}')
[8f70b5b]46for DEV in $DEVICES; do
[df7d8f0]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
[c8fd992]53done
54
[df7d8f0]55# Checking if IP address has been detected.
[8f70b5b]56if [ -n "$SERVERIP" ]; then
[df7d8f0]57        # Temporary files.
[8f70b5b]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
[df7d8f0]62        # Checking whether the DHCP settings need to be changed.
[c8fd992]63        CHANGE=0
[8f70b5b]64        for f in /etc/{dhcp,hcp3}/dhcpd.conf; do
65                if [ -f $f ]; then
[df7d8f0]66                        # Changing DHCP "next-server" parameter.
67                        file="${f/./-$SERVERDEV.}"
[e0314d93]68                        sed -e "s/next-server.*/next-server $SERVERIP;/" \
69                            -e "s/option routers ;/option routers ${SERVERIP%.*}.1;/" $file >$tmpfile
[df7d8f0]70                        # Copying and linking file if there are changes.
[67d0d8c]71                        if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
[8f70b5b]72                                mv $tmpfile $file
73                                chmod 644 $file
74                                ln -f $file $f
75                                CHANGE=1
76                        fi
[c8fd992]77                fi
78        done
[df7d8f0]79        # Restarting DHCP service if its configuration has changed.
[c8fd992]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
[2cbfd3f]85                echo "DHCP configuration has not changed."
[c8fd992]86        fi
[8f70b5b]87
[df7d8f0]88        # Saving old IP address.
[8f70b5b]89        source $OPENGNSYS/etc/ogAdmRepo.cfg
90        OLDSERVERIP=$IPlocal
91
[df7d8f0]92        # Checking if configuration files need to be modified.
[c8fd992]93        CHANGE=0
[8f70b5b]94        for f in $OPENGNSYS/{etc/{ogAdmServer,ogAdmRepo,ogAdmAgent}.cfg,www/controlacceso.php,client/etc/ogAdmClient.cfg}; do
[df7d8f0]95                # Error if configuration file cannot be found.
[8f70b5b]96                if [ ! -f $f ]; then
[c8fd992]97                        echo "$PROG: File $file does not exist." >&2
98                        exit 2
99                fi
[df7d8f0]100                # Updating configuration variables (if URLs does not contain "localhost").
[8f70b5b]101                sed -e "s,ServidorAdm=.*,ServidorAdm=$SERVERIP," \
102                    -e "s,IPlocal=.*,IPlocal=$SERVERIP," \
[f535be9]103                    -e "s,UrlMenu=https?://\([^/]*\)/\(.*\),UrlMenu=https://$SERVERIP/\2," \
[4079733]104                    -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile
[df7d8f0]105                file="${f/./-$SERVERDEV.}"
106                # Copying updated file, if needed.
[2cbfd3f]107                if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
108                        cp $tmpfile $file
[8f70b5b]109                        ln -f $file $f
[c8fd992]110                        CHANGE=1
111                fi
112        done
[8f70b5b]113
[df7d8f0]114        # Processing when something has changed.
[c8fd992]115        if [ $CHANGE == 1 ]; then
[df7d8f0]116                # Restart OpenGnsys services.
[c8fd992]117                /etc/init.d/opengnsys restart
[df7d8f0]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
[8948748]123                        sed -i -e "s/APITOKEN=.*/APITOKEN=$REPOKEY/" $OPENGNSYS/etc/ogAdmServer.cfg
[df7d8f0]124                fi
125                # If OpenGnsys Server is active, updating the database.
126                if [ "$RUN_OGADMSERVER" == "yes" ]; then
127                        source $OPENGNSYS/etc/ogAdmServer.cfg
128                        # Creating credentials file.
129                        cat << EOT > $MYCNF
[69fc529]130[client]
131user=$USUARIO
132password=$PASSWORD
133EOT
[df7d8f0]134                        # Updating OpenGnsys Server IP address.
135                        mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
136                                 "UPDATE entornos
137                                     SET ipserveradm='$SERVERIP'
138                                   WHERE identorno=1"
139                        # If OpenGnsys Repository is active, updating IP address and API token.
140                        if [ "$RUN_OGADMREPO" == "yes" ]; then
141                                mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
142                                         "UPDATE repositorios
143                                             SET ip='$SERVERIP', apikey='$REPOKEY'
144                                           WHERE ip='$OLDSERVERIP'"
145                                unset REPOKEY
146                        fi
147                        # Updating all menu URLs.
148                        mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
149                                 "UPDATE menus
150                                     SET htmlmenupub = REPLACE(htmlmenupub, '$OLDSERVERIP', '$SERVERIP'),
151                                         htmlmenupri = REPLACE(htmlmenupri, '$OLDSERVERIP', '$SERVERIP');"
152                        # Updating all PXE files.
153                        find $PXEDIR -name "01-*" -exec sed -i -e "s/$OLDSERVERIP/$SERVERIP/g" {} \;
154                fi
[8f70b5b]155
[df7d8f0]156                # Showing manual task to do after execution.
[8f70b5b]157                cat << EOT
[df7d8f0]158Default server interface set to: $SERVERDEV ($SERVERIP)
[8f70b5b]159
160Manual tasks:
[df7d8f0]161- Check DHCP configuration file and restart service, if needed.
162- Check PXE files.
163- Log-in as Web Console user:
164  - Check menu URLs.
165${REPOKEY:+"  - Update repository API token"}
[8f70b5b]166EOT
[c8fd992]167        else
[df7d8f0]168                # Showing message if nothing changes.
[2cbfd3f]169                echo "Default interface has not changed: $1"
[c8fd992]170        fi
171else
[df7d8f0]172        # Error if network interface is not found.
173        echo "$PROG: Network device not found. Format: $PROG ipaddress|netiface" >&2
[c8fd992]174        exit 1
175fi
176
[df7d8f0]177# Removing temporary files.
[2cbfd3f]178rm -f $tmpfile $MYCNF
179
Note: See TracBrowser for help on using the repository browser.