[c8fd992] | 1 | #!/bin/bash |
---|
| 2 | |
---|
[df7d8f0] | 3 | #/** |
---|
| 4 | #@file setserveraddr |
---|
[e020339] | 5 | #@brief Assign default IP address to OpenGnsys services. |
---|
| 6 | #@usage setserveraddr {str_ipaddress | str_netiface} |
---|
[df7d8f0] | 7 | #@param str_ipaddress IP address assigned to a network interface |
---|
[e020339] | 8 | #@param str_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. |
---|
| 22 | PROG="$(basename "$0")" |
---|
| 23 | OPENGNSYS=/opt/opengnsys |
---|
[e020339] | 24 | PXEDIRS="$OPENGNSYS/tftpboot/menu.lst $OPENGNSYS/tftpboot/grub" |
---|
[df7d8f0] | 25 | DEFAULTFILE=/etc/default/opengnsys |
---|
| 26 | |
---|
[e020339] | 27 | # Functions. |
---|
| 28 | source $OPENGNSYS/lib/ogfunctions.sh |
---|
[c8fd992] | 29 | |
---|
[e020339] | 30 | # Checking parameters. |
---|
| 31 | [ "$USER" != "root" ] && raiseError access "Need to be root" |
---|
| 32 | [ $# -ne 1 ] && raiseError usage |
---|
| 33 | [ -r $DEFAULTFILE ] || raiseError access "Cannot read default configuration file" |
---|
| 34 | for f in $OPENGNSYS/{etc/{ogAdmServer,ogAdmRepo,ogAdmAgent}.cfg,www/controlacceso.php,client/etc/ogAdmClient.cfg}; do |
---|
| 35 | [ -w $f ] || raiseError access "Cannot write to file: $f" |
---|
| 36 | done |
---|
| 37 | # Show help message. |
---|
| 38 | [ "$1" == "help" ] && help |
---|
[8f70b5b] | 39 | |
---|
[df7d8f0] | 40 | # Detecting network interfaces. |
---|
| 41 | DEVICES=$(ip -o link show up | awk -F: '$2!~/lo/ {print $2}') |
---|
[8f70b5b] | 42 | for 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] | 49 | done |
---|
| 50 | |
---|
[df7d8f0] | 51 | # Checking if IP address has been detected. |
---|
[8f70b5b] | 52 | if [ -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) |
---|
| 58 | MYCNF=$(mktemp /tmp/.my.cnf.XXXXX) |
---|
| 59 | trap "rm -f $tmpfile $MYCNF" 1 2 3 6 9 15 |
---|
| 60 | |
---|
[df7d8f0] | 61 | # Checking whether the DHCP settings need to be changed. |
---|
[c8fd992] | 62 | CHANGE=0 |
---|
[8f70b5b] | 63 | for f in /etc/{dhcp,hcp3}/dhcpd.conf; do |
---|
| 64 | if [ -f $f ]; then |
---|
[df7d8f0] | 65 | # Changing DHCP "next-server" parameter. |
---|
| 66 | file="${f/./-$SERVERDEV.}" |
---|
[e0314d93] | 67 | sed -e "s/next-server.*/next-server $SERVERIP;/" \ |
---|
| 68 | -e "s/option routers ;/option routers ${SERVERIP%.*}.1;/" $file >$tmpfile |
---|
[df7d8f0] | 69 | # Copying and linking file if there are changes. |
---|
[67d0d8c] | 70 | if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then |
---|
[8f70b5b] | 71 | mv $tmpfile $file |
---|
| 72 | chmod 644 $file |
---|
| 73 | ln -f $file $f |
---|
| 74 | CHANGE=1 |
---|
| 75 | fi |
---|
[c8fd992] | 76 | fi |
---|
| 77 | done |
---|
[df7d8f0] | 78 | # Restarting DHCP service if its configuration has changed. |
---|
[c8fd992] | 79 | if [ $CHANGE == 1 ]; then |
---|
| 80 | for f in /etc/init.d/{isc-dhcp-server,dhcp3-server,dhcpd}; do |
---|
| 81 | [ -x $f ] && $f restart |
---|
| 82 | done |
---|
| 83 | else |
---|
[2cbfd3f] | 84 | echo "DHCP configuration has not changed." |
---|
[c8fd992] | 85 | fi |
---|
[8f70b5b] | 86 | |
---|
[df7d8f0] | 87 | # Saving old IP address. |
---|
[e020339] | 88 | source $OPENGNSYS/etc/ogAdmServer.cfg |
---|
| 89 | OLDSERVERIP=$ServidorAdm |
---|
[df7d8f0] | 90 | # Checking if configuration files need to be modified. |
---|
[c8fd992] | 91 | CHANGE=0 |
---|
[8f70b5b] | 92 | for f in $OPENGNSYS/{etc/{ogAdmServer,ogAdmRepo,ogAdmAgent}.cfg,www/controlacceso.php,client/etc/ogAdmClient.cfg}; do |
---|
[e020339] | 93 | # Updating configuration variables (if URL does not contain "localhost"). |
---|
| 94 | sed -e "s,\(ServidorAdm\|IPlocal\)=.*,\1=$SERVERIP," \ |
---|
[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 | |
---|
[df7d8f0] | 106 | # Processing when something has changed. |
---|
[c8fd992] | 107 | if [ $CHANGE == 1 ]; then |
---|
[df7d8f0] | 108 | # Restart OpenGnsys services. |
---|
[c8fd992] | 109 | /etc/init.d/opengnsys restart |
---|
[df7d8f0] | 110 | source $DEFAULTFILE |
---|
| 111 | # If OpenGnsys Server is active, updating the database. |
---|
| 112 | if [ "$RUN_OGADMSERVER" == "yes" ]; then |
---|
| 113 | # Creating credentials file. |
---|
| 114 | cat << EOT > $MYCNF |
---|
[69fc529] | 115 | [client] |
---|
| 116 | user=$USUARIO |
---|
| 117 | password=$PASSWORD |
---|
| 118 | EOT |
---|
[e020339] | 119 | # Updating IP addresses and menu URLs. |
---|
| 120 | mysql --defaults-extra-file=$MYCNF -D "$CATALOG" << EOT |
---|
| 121 | UPDATE entornos |
---|
| 122 | SET ipserveradm='$SERVERIP' |
---|
| 123 | WHERE identorno=1; |
---|
| 124 | UPDATE repositorios |
---|
| 125 | SET ip='$SERVERIP' |
---|
| 126 | WHERE ip='$OLDSERVERIP'; |
---|
| 127 | UPDATE menus |
---|
| 128 | SET htmlmenupub = REPLACE(htmlmenupub, '$OLDSERVERIP', '$SERVERIP'), |
---|
| 129 | htmlmenupri = REPLACE(htmlmenupri, '$OLDSERVERIP', '$SERVERIP'); |
---|
| 130 | EOT |
---|
[df7d8f0] | 131 | # Updating all PXE files. |
---|
[e020339] | 132 | find $PXEDIRS -name "01-*" -exec sed -i -e "s/$OLDSERVERIP/$SERVERIP/g" {} \; |
---|
[df7d8f0] | 133 | fi |
---|
[8f70b5b] | 134 | |
---|
[df7d8f0] | 135 | # Showing manual task to do after execution. |
---|
[8f70b5b] | 136 | cat << EOT |
---|
[df7d8f0] | 137 | Default server interface set to: $SERVERDEV ($SERVERIP) |
---|
[8f70b5b] | 138 | |
---|
| 139 | Manual tasks: |
---|
[df7d8f0] | 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. |
---|
[dde2db1] | 144 | - Note: Run "settoken" script to update authentication tokens. |
---|
[8f70b5b] | 145 | EOT |
---|
[c8fd992] | 146 | else |
---|
[df7d8f0] | 147 | # Showing message if nothing changes. |
---|
[2cbfd3f] | 148 | echo "Default interface has not changed: $1" |
---|
[c8fd992] | 149 | fi |
---|
| 150 | else |
---|
[df7d8f0] | 151 | # Error if network interface is not found. |
---|
[e020339] | 152 | raiseError notfound "Network device" |
---|
[c8fd992] | 153 | fi |
---|
| 154 | |
---|
[df7d8f0] | 155 | # Removing temporary files. |
---|
[2cbfd3f] | 156 | rm -f $tmpfile $MYCNF |
---|
| 157 | |
---|