kylemanna
7/6/2015 - 7:30 AM

digitalocean-synchronize.sh

process_interface() {
    local url=$1
    local attrs=$2
    local mac=$(curl -Ssf ${url}mac)
    local type=$(curl -Ssf ${url}type)
    local interface=
    local cand path
    for cand in $(ls /sys/class/net); do
        path=/sys/class/net/${cand}/address
        if [ -e ${path} ] && [ "$(<${path})" = "${mac}" ]; then
            interface=${cand}
            break
        fi
    done
    [ -n "${interface}" ] || return 0
    mkdir -p /run/systemd/network
    {
        cat <<-EOF
            # Generated by digitalocean-synchronize
            [Match]
            Name=${interface}
            [Network]
        EOF
        if [[ " ${attrs} " =~ " ipv4/ " ]]; then
            local address=$(curl -sf ${url}ipv4/address)
            local prefix=$(netmask_to_prefix $(curl -sf ${url}ipv4/netmask))
            echo "Address=${address}/${prefix}"
            if [ "${type}" != "private" ]; then
                echo "Gateway=$(curl -sf ${url}ipv4/gateway)"

                if [[ ${interface} = "eth0" ]]; then
                    echo "Tunnel=he-ipv6"
                fi
            fi
            log "Added IPv4 address ${address}/${prefix} on ${interface}."
        fi
        if [[ " ${attrs} " =~ " ipv6/ " ]]; then
            local address=$(curl -sf ${url}ipv6/address)
            local prefix=$(curl -sf ${url}ipv6/cidr)
            echo "Address=${address}/${prefix}"
            if [ "${type}" != "private" ]; then
                echo "Gateway=$(curl -sf ${url}ipv6/gateway)"
            fi
            log "Added IPv6 address ${address}/${prefix} on ${interface}."
        fi
    } > /run/systemd/network/dosync-${interface}.network
}