cloudcalvin
12/23/2014 - 1:04 PM

Coreos bare metal network environment setup for IPv4 variable access

Coreos bare metal network environment setup for IPv4 variable access

#!/bin/bash

ENV="/etc/environment"
HOSTS="/etc/hosts"

# Test for RW access to $1
touch $HOSTS
touch $ENV
if [ $? -ne 0 ]; then
    echo exiting, unable to modify: $ENV
    exit 1
fi

# Setup environment target
sed -i -e '/^COREOS_PUBLIC_IPV4=/d' \
    -e '/^COREOS_PRIVATE_IPV4=/d' \
    "${ENV}"

# We spin loop until the the IP addresses are set
get_ip () {
    IF=$1
    IP=
    i=20
    # sometimes network devices don't acquire IP's on boot, so we give it
    # a finite time...
    while [ $i -gt 0 ]; do
        IP=$(ifconfig $IF | awk '/inet / {print $2}')
        if [ "$IP" != "" ]; then
            break
        fi
        sleep 1
        i=$(( $i - 1 ))
    done
    #...then restart if fails to try again.
    if [ "$IP" = "" ]; then
        reboot
    fi
    echo $IP
}

# For different sets of hardware, dynamicaly acquire network interface name
ethInterface=$(ifconfig -s | grep -v "lo" | grep -v "Iface" | awk '{print $1}')
echo NODE_NET_INTERFACE=$ethInterface >> $ENV

# Echo results of IP queries to environment file as soon as network interfaces '
# get assigned IPs
echo COREOS_PUBLIC_IPV4=$(get_ip $ethInterface) >> $ENV # Also assigned to same IP
echo COREOS_PRIVATE_IPV4=$(get_ip $ethInterface) >> $ENV
echo COREOS_HOSTNAME=$(/usr/bin/hostname) >> $ENV
source /etc/environment
echo "$COREOS_PRIVATE_IPV4 localhost" >> $HOSTS
echo "$COREOS_PUBLIC_IPV4 publichost" >> $HOSTS