source: server/bin/setserveraddr @ 4972d15

Last change on this file since 4972d15 was 4972d15, checked in by OpenGnSys Support Team <soporte-og@…>, 5 years ago

#988 Read json config file in scripts

This patch implements the function source_json_config() that is used to
read the new ogserver.json config file in the scripts. This replaces the
old 'source .../ogserver.cfg'.

Co-authored-by: Javier Sánchez Parra <jsanchez@…>

  • Property mode set to 100755
File size: 5.1 KB
Line 
1#!/bin/bash
2
3#/**
4#@file    setserveraddr
5#@brief   Assign default IP address to OpenGnsys services.
6#@usage   setserveraddr { IPAddress | NetIface }
7#@param   IPAddress  IP address assigned to a network interface
8#@param   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.
22OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"}
23PXEDIRS="$OPENGNSYS/tftpboot/menu.lst $OPENGNSYS/tftpboot/grub"
24DEFAULTFILE=/etc/default/opengnsys
25
26# Functions.
27source $OPENGNSYS/lib/ogfunctions.sh || exit 1
28
29# Show help or version number.
30[ "$*" == "help" ] && help
31[ "$*" == "version" ] && version
32# Checking parameters.
33[ "$USER" != "root" ] && raiseError access "Need to be root"
34[ $# -ne 1 ] && raiseError usage
35[ -r $DEFAULTFILE ] || raiseError access "Cannot read default configuration file"
36for f in $OPENGNSYS/{etc/ogserver.json,etc/ogAdmRepo.cfg,www/controlacceso.php,client/ogClient/cfg/ogclient.json}; do
37        [ -w $f ] || raiseError access "Cannot write to file: $f"
38done
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        trap "rm -f $tmpfile" 1 2 3 6 9 15
59
60        # Checking whether the DHCP settings need to be changed.
61        CHANGE=0
62        for f in /etc/{dhcp,hcp3}/dhcpd.conf; do
63                if [ -f $f ]; then
64                        # Changing DHCP "next-server" parameter.
65                        file="${f/./-$SERVERDEV.}"
66                        sed -e "s/next-server.*/next-server $SERVERIP;/" \
67                            -e "s/option routers ;/option routers ${SERVERIP%.*}.1;/" $file >$tmpfile
68                        # Copying and linking file if there are changes.
69                        if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
70                                mv $tmpfile $file
71                                chmod 644 $file
72                                ln -f $file $f
73                                CHANGE=1
74                        fi
75                fi
76        done
77        # Restarting DHCP service if its configuration has changed.
78        if [ $CHANGE == 1 ]; then
79                for s in isc-dhcp-server dhcp3-server dhcpd; do
80                        restart $s &>/dev/null && break
81                done
82        else
83                echo "DHCP configuration has not changed."
84        fi
85
86        # Saving old IP address.
87        source_json_config $OPENGNSYS/etc/ogserver.json
88        OLDSERVERIP=$ServidorAdm
89        # Checking if configuration files need to be modified.
90        CHANGE=0
91        for f in $OPENGNSYS/{etc/ogserver.json,etc/ogAdmRepo.cfg,www/controlacceso.php}; do
92                # Updating configuration variables (if URL does not contain "localhost").
93                sed -e "s,\(ServidorAdm\|IPlocal\)=.*,\1=$SERVERIP," \
94                    -e "s,^INTERFACE=.*,INTERFACE=$SERVERDEV," \
95                    -e "s,UrlMenu=https?://\([^/]*\)/\(.*\),UrlMenu=https://$SERVERIP/\2," \
96                    -e "s/\"ip\": \".*\"/\"ip\": \"$SERVERIP\"/" $SERVERCFG \
97                    -e "s/\"interface\": \".*\"/\"interface\": \"$SERVERDEV\"/" $SERVERCFG \
98                    -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile
99                file="${f/./-$SERVERDEV.}"
100                # Copying updated file, if needed.
101                if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
102                        cp $tmpfile $file
103                        ln -f $file $f
104                        CHANGE=1
105                fi
106        done
107
108        # Processing when something has changed.
109        if [ $CHANGE == 1 ]; then
110                # Config ogClient.
111                IP_PATTERN="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
112                sed -i -e "s/$IP_PATTERN/$SERVERIP/g" \
113                          "$OPENGNSYS"/client/ogClient/cfg/ogclient.json
114                # Restart OpenGnsys services.
115                echo "Restarting services..."
116                restart opengnsys >/dev/null
117                source $DEFAULTFILE
118                # If OpenGnsys Server is active, updating the database.
119                if [ "$RUN_OGADMSERVER" == "yes" ]; then
120                        # Updating IP addresses and menu URLs.
121                        dbexec "
122UPDATE entornos
123   SET ipserveradm='$SERVERIP'
124 WHERE identorno=1;
125UPDATE repositorios
126   SET ip='$SERVERIP'
127 WHERE ip='$OLDSERVERIP';
128UPDATE menus
129   SET htmlmenupub = REPLACE(htmlmenupub, '$OLDSERVERIP', '$SERVERIP'),
130       htmlmenupri = REPLACE(htmlmenupri, '$OLDSERVERIP', '$SERVERIP');"
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
157
Note: See TracBrowser for help on using the repository browser.