source: server/bin/setserveraddr @ 0fd42f4

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 0fd42f4 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
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                        sed -i -e "s/APITOKEN=.*/APITOKEN=$REPOKEY/" $OPENGNSYS/etc/ogAdmServer.cfg
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
130[client]
131user=$USUARIO
132password=$PASSWORD
133EOT
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
155
156                # Showing manual task to do after execution.
157                cat << EOT
158Default server interface set to: $SERVERDEV ($SERVERIP)
159
160Manual tasks:
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"}
166EOT
167        else
168                # Showing message if nothing changes.
169                echo "Default interface has not changed: $1"
170        fi
171else
172        # Error if network interface is not found.
173        echo "$PROG: Network device not found. Format: $PROG ipaddress|netiface" >&2
174        exit 1
175fi
176
177# Removing temporary files.
178rm -f $tmpfile $MYCNF
179
Note: See TracBrowser for help on using the repository browser.