4096
     ˆ¿Èƒt-µÂÅ ò    # Author: Ben Howard  <bh@digitalocean.com>
#
# This file is part of cloud-init. See LICENSE file for license information.

import json
import random

from cloudinit import log as logging
from cloudinit import net as cloudnet
from cloudinit import url_helper
from cloudinit import util

NIC_MAP = {'public': 'eth0', 'private': 'eth1'}

LOG = logging.getLogger(__name__)


def assign_ipv4_link_local(nic=None):
    """Bring up NIC using an address using link-local (ip4LL) IPs. On
       DigitalOcean, the link-local domain is per-droplet routed, so there
       is no risk of collisions. However, to be more safe, the ip4LL
       address is random.
    """

    if not nic:
        nic = get_link_local_nic()
        LOG.debug("selected interface '%s' for reading metadata", nic)

    if not nic:
        raise RuntimeError("unable to find interfaces to access the"
                           "meta-data server. This droplet is broken.")

    addr = "169.254.{0}.{1}/16".format(random.randint(1, 168),
                                       random.randint(0, 255))

    ip_addr_cmd = ['ip', 'addr', 'add', addr, 'dev', nic]
    ip_link_cmd = ['ip', 'link', 'set', 'dev', nic, 'up']

    if not util.which('ip'):
        raise RuntimeError("No 'ip' command available to configure ip4LL "
                           "address")

    try:
        util.subp(ip_addr_cmd)
        LOG.debug("assigned ip4LL address '%s' to '%s'", addr, nic)
        util.subp(ip_link_cmd)
        LOG.debug("brought device '%s' up", nic)
    except Exception:
        util.logexc(LOG, "ip4LL address assignment of '%s' to '%s' failed."
                         " Droplet networking will be broken", addr, nic)
        raise

    return nic


def get_link_local_nic():
    nics = [f for f in cloudnet.get_devicelist() if cloudnet.is_physical(f)]
    if not nics:
        return None
    return min(nics, key=lambda d: cloudnet.read_sys_net_int(d, 'ifindex'))


def del_ipv4_link_local(nic=None):
    """Remove the ip4LL address. While this is not necessary, the ip4LL
       address is extraneous and confusing to users.
    """
    if not nic:
        LOG.debug("no link_local address interface defined, skipping link "
                  "local address cleanup")
        return

    LOG.debug("cleaning up ipv4LL address")

    ip_addr_cmd = ['ip', 'addr', 'flush', 'dev', nic]

    try:
        util.subp(ip_addr_cmd)
        LOG.debug("removed ip4LL addresses from %s", nic)

    except Exception as e:
        util.logexc(LOG, "failed to remove ip4LL address from '%s'.", nic, e)


def convert_network_configuration(config, dns_servers):
    """Convert the DigitalOcean Network description into Cloud-init's netconfig
       format.

       Example JSON:
        {'public': [
              {'mac': '04:01:58:27:7f:01',
               'ipv4': {'gateway': '45.55.32.1',
                        'netmask': '255.255.224.0',
                        'ip_address': '45.55.50.93'},
               'anchor_ipv4': {
                        'gateway': '10.17.0.1',
                        'netmask': '255.255.0.0',
                        'ip_address': '10.17.0.9'},
               'type': 'public',
               'ipv6': {'gateway': '....',
                        'ip_address': '....',
                        'cidr': 64}}
           ],
          'private': [
              {'mac': '04:01:58:27:7f:02',
               'ipv4': {'gateway': '10.132.0.1',
                        'netmask': '255.255.0.0',
                        'ip_address': '10.132.75.35'},
               'type': 'private'}
           ]
        }
    """

    def _get_subnet_part(pcfg):
        subpart = {'type': 'static',
                   'control': 'auto',
                   'address': pcfg.get('ip_address'),
                   'gateway': pcfg.get('gateway')}

        if ":" in pcfg.get('ip_address'):
            subpart['address'] = "{0}/{1}".format(pcfg.get('ip_address'),
                                                  pcfg.get('cidr'))
        else:
            subpart['netmask'] = pcfg.get('netmask')

  