Created
September 14, 2018 12:46
-
-
Save ndrut/8e8ecc6d295c5a8ce495388000761c9e to your computer and use it in GitHub Desktop.
vultr-ubuntu18-docker-startup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
log () { | |
level=$1 | |
log=$2 | |
t=`date +%F/%T` | |
echo "[${t} ${level}] ${log}" | |
} | |
METADATA=http://169.254.169.254/v1 | |
IP6Prefix="fda1:0000:0000:0da1" | |
LANIPPARENT="fda1::/48" | |
LANIP6GATEWAY="fda1::da1:a:a:a:a" | |
LANNAME="dfw-core" | |
WANIP="" | |
WANIP6="" | |
LANIP="" | |
LANIP6="" | |
LANMAC="" | |
getRandomIP6 () { | |
array=( 1 2 3 4 5 6 7 8 9 0 a b c d e f ) | |
addr=$IP6Prefix | |
x=0 | |
while [ $x -lt 4 ]; do | |
addr=${addr}:${array[$RANDOM%16]}${array[$RANDOM%16]}${array[$RANDOM%16]}${array[$RANDOM%16]} | |
x=$(($x+1)) | |
done | |
echo "$addr" | |
} | |
getLanConfig () { | |
interfaces=$(curl -s ${METADATA}/interfaces/); ec=$? | |
if [[ $ec != 0 ]]; then log error "Unable to reach interfaces via metadata (${METADATA})" ; fi | |
log "info" "Loading interfaces..." | |
for interface in $interfaces; | |
do | |
ntype="$(curl -s ${METADATA}/interfaces/${interface}network-type)" | |
case "$ntype" in | |
"public") | |
WANIP="$(curl -s ${METADATA}/interfaces/${interface}ipv4/address)" | |
WANIP6="$(curl -s ${METADATA}/interfaces/${interface}ipv6/address)" | |
log "info" "Identified WAN ${WANIP} ${WANIP6}" | |
;; | |
"private") | |
LANMAC="$(curl -s ${METADATA}/interfaces/${interface}mac)" | |
LANIP="$(curl -s ${METADATA}/interfaces/${interface}ipv4/address)" | |
LANIP6="$(curl -s ${METADATA}/interfaces/${interface}ipv6/address)" | |
if [ -z "$LANIP6" ]; then | |
# generate new random ip | |
LANIP6="$(getRandomIP6)" | |
fi | |
log "info" "Identified LAN ${LANMAC} ${LANIP} ${LANIP6}" | |
;; | |
"*") | |
log "error" "Type not found (${type})" | |
exit 1 #TODO this doesn't exit "now" also tried return oh well I guess | |
;; | |
esac | |
done | |
} | |
writeLanConfig () { | |
if [ ! -f /etc/netplan/10-${LANNAME}.yaml ]; then | |
cat <<- EOF > /etc/netplan/10-${LANNAME}.yaml | |
network: | |
version: 2 | |
renderer: networkd | |
ethernets: | |
ens7: | |
match: | |
macaddress: "$LANMAC" | |
mtu: 1450 | |
dhcp4: no | |
dhcp6: no | |
addresses: ["${LANIP}/24", "${LANIP6}/64"] | |
routes: | |
- to: "${LANIPPARENT}" | |
via: "${LANIP6GATEWAY}" | |
EOF | |
log "info" "LAN Configuration installed, applying network changes..." | |
netplan --debug apply | |
fi | |
} | |
createConsulIface () { | |
# Wow, this had to use systemd. https://askubuntu.com/a/1037551 | |
if [ ! -f /etc/systemd/network/consul0.netdev ]; then | |
cat <<- EOF > /etc/systemd/network/consul0.netdev | |
[NetDev] | |
Name=consul0 | |
Kind=dummy | |
EOF | |
cat <<- EOF > /etc/systemd/network/consul0.network | |
[Match] | |
Name=consul0 | |
[Network] | |
Address=169.254.1.1/32 | |
EOF | |
log "info" "Wrote files /etc/systemd/network/consul0.netdev and consul0.network. Reloading systemd-networkd." | |
systemctl restart systemd-networkd | |
systemctl status systemd-networkd | |
fi | |
} | |
log "info" "Starting..." | |
getLanConfig | |
log "info" "Getting LAN config..." | |
writeLanConfig | |
log "info" "LAN config written to /etc/netplan/10-${LANNAME}.yaml" | |
log "info" "Creating consul dummy interface (consul0)" | |
createConsulIface | |
log "info" "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment