source: server/bin/setserveraddr @ 084fdc1

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

#988 Fix setserveraddr json handling

setserveraddr script modified ogserver.json values with "sed". This
caused unwanted changes in database.ip.

This commit replaces "sed" with "jq" for modifying ogserver.json,
ensuring only rest.ip and wol.interface values change.

  • Property mode set to 100755
File size: 5.4 KB
RevLine 
[c8fd992]1#!/bin/bash
2
[df7d8f0]3#/**
4#@file    setserveraddr
[e020339]5#@brief   Assign default IP address to OpenGnsys services.
[995b512]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
[df7d8f0]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
[e020339]15#@version 1.1.1 - Updating menu URLs and PXE files.
[df7d8f0]16#@author  Ramón M. Gómez - ETSII Univ. Sevilla
17#@date    2018-11-15
18#*/ ##
19
20
21# Variables.
[995b512]22OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"}
[e020339]23PXEDIRS="$OPENGNSYS/tftpboot/menu.lst $OPENGNSYS/tftpboot/grub"
[df7d8f0]24DEFAULTFILE=/etc/default/opengnsys
25
[e020339]26# Functions.
[995b512]27source $OPENGNSYS/lib/ogfunctions.sh || exit 1
[c8fd992]28
[995b512]29# Show help or version number.
30[ "$*" == "help" ] && help
31[ "$*" == "version" ] && version
[e020339]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"
[4972d15]36for f in $OPENGNSYS/{etc/ogserver.json,etc/ogAdmRepo.cfg,www/controlacceso.php,client/ogClient/cfg/ogclient.json}; do
[e020339]37        [ -w $f ] || raiseError access "Cannot write to file: $f"
38done
[8f70b5b]39
[df7d8f0]40# Detecting network interfaces.
41DEVICES=$(ip -o link show up | awk -F: '$2!~/lo/ {print $2}')
[8f70b5b]42for DEV in $DEVICES; do
[df7d8f0]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
[c8fd992]49done
50
[df7d8f0]51# Checking if IP address has been detected.
[8f70b5b]52if [ -n "$SERVERIP" ]; then
[e020339]53        # Showing warning to inform that initiated clients may hang.
54        read -rp "WARNING: initiated clients can hang. Continue? (y/n): " ANSWER
[940b1c7f]55        [ "${ANSWER,,}" != "y" ] && raiseError cancel "Do nothing"
[df7d8f0]56        # Temporary files.
[8f70b5b]57        tmpfile=$(mktemp /tmp/og.XXXXX)
[995b512]58        trap "rm -f $tmpfile" 1 2 3 6 9 15
[8f70b5b]59
[df7d8f0]60        # Checking whether the DHCP settings need to be changed.
[c8fd992]61        CHANGE=0
[8f70b5b]62        for f in /etc/{dhcp,hcp3}/dhcpd.conf; do
63                if [ -f $f ]; then
[df7d8f0]64                        # Changing DHCP "next-server" parameter.
65                        file="${f/./-$SERVERDEV.}"
[e0314d93]66                        sed -e "s/next-server.*/next-server $SERVERIP;/" \
67                            -e "s/option routers ;/option routers ${SERVERIP%.*}.1;/" $file >$tmpfile
[df7d8f0]68                        # Copying and linking file if there are changes.
[67d0d8c]69                        if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
[8f70b5b]70                                mv $tmpfile $file
71                                chmod 644 $file
72                                ln -f $file $f
73                                CHANGE=1
74                        fi
[c8fd992]75                fi
76        done
[df7d8f0]77        # Restarting DHCP service if its configuration has changed.
[c8fd992]78        if [ $CHANGE == 1 ]; then
[6640e2cf]79                for s in isc-dhcp-server dhcp3-server dhcpd; do
80                        restart $s &>/dev/null && break
[c8fd992]81                done
82        else
[2cbfd3f]83                echo "DHCP configuration has not changed."
[c8fd992]84        fi
[8f70b5b]85
[df7d8f0]86        # Saving old IP address.
[4972d15]87        source_json_config $OPENGNSYS/etc/ogserver.json
[e020339]88        OLDSERVERIP=$ServidorAdm
[df7d8f0]89        # Checking if configuration files need to be modified.
[c8fd992]90        CHANGE=0
[084fdc1]91        for f in $OPENGNSYS/{etc/ogAdmRepo.cfg,www/controlacceso.php}; do
[e020339]92                # Updating configuration variables (if URL does not contain "localhost").
93                sed -e "s,\(ServidorAdm\|IPlocal\)=.*,\1=$SERVERIP," \
[9d3b079]94                    -e "s,^INTERFACE=.*,INTERFACE=$SERVERDEV," \
[f535be9]95                    -e "s,UrlMenu=https?://\([^/]*\)/\(.*\),UrlMenu=https://$SERVERIP/\2," \
[4079733]96                    -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile
[df7d8f0]97                file="${f/./-$SERVERDEV.}"
98                # Copying updated file, if needed.
[2cbfd3f]99                if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
100                        cp $tmpfile $file
[8f70b5b]101                        ln -f $file $f
[c8fd992]102                        CHANGE=1
103                fi
104        done
[8f70b5b]105
[084fdc1]106        f="$OPENGNSYS"/etc/ogserver.json
107        file="${f/./-$SERVERDEV.}"
108        step_one=$(mktemp)
109        jq --arg ip "$SERVERIP" '.rest.ip = $ip' "$file" > "$step_one"
110        step_two=$(mktemp)
111        jq --arg iface "$SERVERDEV" '.wol.interface = $iface' "$step_one" > "$step_two"
112        if [ ! "$f" -ef "$file" ] || ! diff -q "$step_two" "$file" &>/dev/null; then
113                cp "$step_two" "$file"
114                ln -f "$file" "$f"
115                CHANGE=1
116        fi
117
[df7d8f0]118        # Processing when something has changed.
[c8fd992]119        if [ $CHANGE == 1 ]; then
[51ce65c]120                # Config ogClient.
[41f8ff6]121                IP_PATTERN="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
122                sed -i -e "s/$IP_PATTERN/$SERVERIP/g" \
123                          "$OPENGNSYS"/client/ogClient/cfg/ogclient.json
[df7d8f0]124                # Restart OpenGnsys services.
[6640e2cf]125                echo "Restarting services..."
126                restart opengnsys >/dev/null
[df7d8f0]127                source $DEFAULTFILE
128                # If OpenGnsys Server is active, updating the database.
129                if [ "$RUN_OGADMSERVER" == "yes" ]; then
[e020339]130                        # Updating IP addresses and menu URLs.
[995b512]131                        dbexec "
[e020339]132UPDATE entornos
133   SET ipserveradm='$SERVERIP'
134 WHERE identorno=1;
135UPDATE repositorios
136   SET ip='$SERVERIP'
137 WHERE ip='$OLDSERVERIP';
138UPDATE menus
139   SET htmlmenupub = REPLACE(htmlmenupub, '$OLDSERVERIP', '$SERVERIP'),
[995b512]140       htmlmenupri = REPLACE(htmlmenupri, '$OLDSERVERIP', '$SERVERIP');"
[df7d8f0]141                        # Updating all PXE files.
[e020339]142                        find $PXEDIRS -name "01-*" -exec sed -i -e "s/$OLDSERVERIP/$SERVERIP/g" {} \;
[df7d8f0]143                fi
[8f70b5b]144
[df7d8f0]145                # Showing manual task to do after execution.
[8f70b5b]146                cat << EOT
[df7d8f0]147Default server interface set to: $SERVERDEV ($SERVERIP)
[8f70b5b]148
149Manual tasks:
[df7d8f0]150- Check DHCP configuration file and restart service, if needed.
151- Check PXE files.
[6640e2cf]152- Log-in as Web Console user:
[df7d8f0]153  - Check menu URLs.
[dde2db1]154- Note: Run "settoken" script to update authentication tokens.
[8f70b5b]155EOT
[c8fd992]156        else
[df7d8f0]157                # Showing message if nothing changes.
[2cbfd3f]158                echo "Default interface has not changed: $1"
[c8fd992]159        fi
160else
[df7d8f0]161        # Error if network interface is not found.
[e020339]162        raiseError notfound "Network device"
[c8fd992]163fi
164
[df7d8f0]165# Removing temporary files.
[995b512]166rm -f $tmpfile
[2cbfd3f]167
Note: See TracBrowser for help on using the repository browser.