summaryrefslogtreecommitdiffstats
path: root/server/bin/setserveraddr3
blob: a979595f5ab6b1aaa1adae3551366a02c5ff6de2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash

#/**
#         setserveraddr {str_ipaddress | str_netiface}
#@file    setserveraddr
#@brief   Command the modifies configuration files to assign the default network interface.
#@param   str_ipaddress  IP address assigned to a network interface
#@param   str_netiface   network interface name defined by the operating system.
#@version Initial version.
#@author  Ramón M. Gómez - ETSII Univ. Sevilla
#@date    2011-01-25
#@version 1.0.5 - Regenerate configuration files.
#@author  Ramón M. Gómez - ETSII Univ. Sevilla
#@date    2014-06-06
#@version 1.1.1 - Updating menu URLs, PXE files, and repository API key.
#@author  Ramón M. Gómez - ETSII Univ. Sevilla
#@date    2018-11-15
#*/ ##


# Variables.
PROG="$(basename "$0")"
OPENGNSYS=/opt/opengnsys
PXEDIR=$OPENGNSYS/tftpboot/menu.lst
CONFIGFILE=$OPENGNSYS/etc/opengnsys.json

# Checking parameters.
if [ $# -ne 1 ]; then
	echo "$PROG: Incorrect operand. Format: $PROG ipaddress|netiface" >&2
	exit 1
fi
if [ "$USER" != "root" ]; then
	echo "$PROG: Need to be root." >&2
	exit 1
fi

# Showing warning to inform that initiated clients may hang.
read -rp "WARNING: initiated clients can hang. Continue? (y/n): " ANSWER
if [ "${ANSWER,,}" != "y" ]; then
	echo "Operation canceled."
	exit 0
fi

# Detecting network interfaces.
DEVICES=$(ip -o link show up | awk -F: '$2!~/lo/ {print $2}')
for DEV in $DEVICES; do
	# If the network interface is found, get its IP address.
	IP=$(ip -o addr show dev "$DEV" | awk '$3~/inet$/ {sub (/\/.*/, ""); print ($4)}')
	if [ "$DEV" == "$1" ] || [ "$IP" == "$1" ]; then
		SERVERIP="$IP"
		SERVERDEV="$DEV"
	fi
done

# Checking if IP address has been detected.
if [ -n "$SERVERIP" ]; then
	# Temporary files.
	tmpfile=$(mktemp /tmp/og.XXXXX)
	trap "rm -f $tmpfile" 1 2 3 6 9 15

	# Checking whether the DHCP settings need to be changed.
	CHANGE=0
	for f in /etc/{dhcp,hcp3}/dhcpd.conf; do
		if [ -f $f ]; then
			# Changing DHCP "next-server" parameter.
			file="${f/./-$SERVERDEV.}"
			sed -e "s/next-server.*/next-server $SERVERIP;/" \
			    -e "s/option routers ;/option routers ${SERVERIP%.*}.1;/" $file >$tmpfile
			# Copying and linking file if there are changes.
			if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
				mv $tmpfile $file
				chmod 644 $file
				ln -f $file $f
				CHANGE=1
			fi
		fi
	done
	# Restarting DHCP service if its configuration has changed.
	if [ $CHANGE == 1 ]; then
		for f in /etc/init.d/{isc-dhcp-server,dhcp3-server,dhcpd}; do
			[ -x $f ] && $f restart
		done
	else
		echo "DHCP configuration has not changed."
	fi

	OLDSERVERIP=$(jq -r .server.ip $CONFIGFILE)
	if [ "$SERVERIP" != "$OLDSERVERIP" ]; then
		# Updating configuration file.
		jq ".server.ip=\"$SERVERIP\"" $CONFIGFILE | sponge $CONFIGFILE
		# Updating all PXE files.
		find $PXEDIR -name "01-*" -exec sed -i -e "s/$OLDSERVERIP/$SERVERIP/g" {} \;
		# Showing manual task to do after execution.
		cat << EOT
Default server interface set to: $SERVERDEV ($SERVERIP)

Manual tasks:
- Check DHCP configuration file and restart service, if needed.
- Check PXE files.
- Log-in as Web Console user: 
  - Check menu URLs.
EOT
	else
		# Showing message if nothing changes.
		echo "Default interface has not changed: $1"
	fi
else
	# Error if network interface is not found.
	echo "$PROG: Network device not found. Format: $PROG ipaddress|netiface" >&2
	exit 1
fi

# Removing temporary file.
rm -f $tmpfile