Skip to content

Instantly share code, notes, and snippets.

@NeverBehave
Last active January 10, 2019 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeverBehave/6bc0a99826928a045041fb2e80b5cff6 to your computer and use it in GitHub Desktop.
Save NeverBehave/6bc0a99826928a045041fb2e80b5cff6 to your computer and use it in GitHub Desktop.
Script for Dynamic IP with Rancher Agent (v1.6)
#!/bin/bash
# https://rancher.com/docs/rancher/v1.6/en/faqs/agents/#how-does-the-host-determine-ip-address-and-how-can-i-change-it-what-do-i-do-if-the-ip-of-my-host-has-changed-due-to-reboot
LOCATION="/tmp/ip.txt"
RANCHER_URL=""
function getIPAndSave() {
ip=$(curl -s https://api-ipv4.ip.sb/ip)
rc=$?
if [ "$rc" != "0" ]; then
echo "curl exit with non 0, exiting"
exit $?
fi
echo "My IP address is: $ip"
echo "Saving to temp file..."
echo "$ip" > $LOCATION
}
function runRancherRegister() {
docker run -e CATTLE_AGENT_IP="$1" --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.2.11 $RANCHER_URL
}
# Check existing file and ip
if [ ! -f $LOCATION ]; then
echo "File not found!"
echo "Run IP Process"
getIPAndSave
echo "Wait for next Call..."
else
read -r previousIP < $LOCATION
echo "Acquire previous IP: $previousIP"
getIPAndSave
read -r currentIP < $LOCATION
if [ "$previousIP" = "$currentIP" ]; then
echo "IP does not changed, exit and wait for next call..."
else
echo "IP changed, start re-register process"
runRancherRegister $currentIP
echo "finished, wait for next call..."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment