forked from metal3-io/static-ip-manager-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-static-ip
executable file
·37 lines (31 loc) · 1.5 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
#!/bin/bash -xe
if [ -z "$PROVISIONING_IP" ]; then
echo "ERROR: PROVISIONING_IP environment variable unset."
exit 1
fi
if [ -z "$PROVISIONING_INTERFACE" ]; then
# If no provisioning interface is specified, then we're probably in
# the mode where the provisioning network is optional, so to avoid a
# hard dependency of forcing users to specify the interface, we detect
# which network interface has the IP's from the machine network.
IP_ONLY=$(echo "$PROVISIONING_IP" | cut -d/ -f1)
# See if we already have the IP on an interface
PROVISIONING_INTERFACE=$(ip -j addr | jq -r -c ".[].addr_info[] | select(.local == \"$IP_ONLY\") | .label")
# If not, let's figure out what interface it should go on
if [ -z "$PROVISIONING_INTERFACE" ]; then
PROVISIONING_INTERFACE=$(ip -j route get "$IP_ONLY" | jq -r '.[] | select(.dev != "lo") | .dev')
fi
else
# Get rid of any DHCP addresses only if we have a dedicated
# provisioning interface
/usr/sbin/ip address flush dev "$PROVISIONING_INTERFACE"
fi
if [ -z "$PROVISIONING_INTERFACE" ]; then
ip addr show
echo "ERROR: Could not find suitable interface for \"$PROVISIONING_IP\""
exit 1
fi
# 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