/
set-static-ip
executable file
·41 lines (36 loc) · 1.67 KB
/
set-static-ip
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
#!/bin/bash -xe
if [ -z "$PROVISIONING_IP" ]; then
echo "ERROR: PROVISIONING_IP environment variable unset."
exit 1
fi
if [ -z "$PROVISIONING_INTERFACE" ]; then
if [ -n "${PROVISIONING_MACS}" ]; then
for mac in ${PROVISIONING_MACS//,/ } ; do
if ip -br link show up | grep -q "$mac"; then
PROVISIONING_INTERFACE=$(ip -br link show up | grep "$mac" | cut -f 1 -d ' ')
break
fi
done
fi
fi
if [ -n "$PROVISIONING_INTERFACE" ]; then
# Check provisioning interface is already set to ip address
if [[ -n $(ip -o addr show dev "${PROVISIONING_INTERFACE}" scope global) ]]; then
# equality check can be done for IPv4, i.e. 172.22.0.3/24
# for IPv6 and dualstack(IPv4v6), fd00:1101::x/64 is used as fd00:1101::xxxx:xxxx:xxxx:xxxx/128
# Thus, we need to parse provisioning ip with '::'.
if ! ip -o addr show dev "${PROVISIONING_INTERFACE}" scope global | grep -q "${PROVISIONING_IP//::*/}" ; then
echo "ERROR: \"$PROVISIONING_INTERFACE\" is already set to ip address belong to different subset than \"$PROVISIONING_IP\""
exit 1
fi
fi
# Get rid of any DHCP addresses on the dedicated provisioning interface
/usr/sbin/ip address flush dev "$PROVISIONING_INTERFACE"
# Need this to be long enough to bring up the pod with the ip refresh in it.
# The refresh-static-ip container should lower this back to 10 seconds once it starts.
# The only time this will actually be set for 5 minutes is if the containers fail to come up.
/usr/sbin/ip addr add "$PROVISIONING_IP" dev "$PROVISIONING_INTERFACE" valid_lft 300 preferred_lft 300
else
echo "ERROR: Could not find suitable interface for \"$PROVISIONING_IP\""
exit 1
fi