
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\meE                 @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZmZm	Z	 dddZ
ejeZddd	Zd
d ZdddZdd ZdddZdd ZdS )    N)dmi)net)subp
url_helperutilZeth0Zeth1)publicZprivatec             C   s   |st | }tjd| |s$tddjtjddtjdd}dd	d
|d|g}dddd|dg}tjdsrtdy2tj| tjd|| tj| tjd| W n& t	k
r   t
jtd||  Y nX |S )zBring 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.
    z,selected interface '%s' for reading metadatazPunable to find interfaces to access themeta-data server. This droplet is broken.z169.254.{0}.{1}/16      r      ipaddradddevlinksetZupz4No 'ip' command available to configure ip4LL addressz#assigned ip4LL address '%s' to '%s'zbrought device '%s' upzRip4LL address assignment of '%s' to '%s' failed. Droplet networking will be broken)get_link_local_nicLOGdebugRuntimeErrorformatrandomZrandintr   Zwhich	Exceptionr   logexc)distronicr   ip_addr_cmdZip_link_cmd r   "/usr/lib/python3.6/digitalocean.pyassign_ipv4_link_local   s4    


r   c                s.    fddt j D }|sd S t|dd dS )Nc                s   g | ]} j j|r|qS r   )Z
networkingZis_physical).0f)r   r   r   
<listcomp>C   s   z&get_link_local_nic.<locals>.<listcomp>c             S   s   t j| dS )NZifindex)cloudnetZread_sys_net_int)dr   r   r   <lambda>I   s    z$get_link_local_nic.<locals>.<lambda>)key)r"   Zget_devicelistmin)r   Znicsr   )r   r   r   A   s
    
r   c             C   s|   | st jd dS t jd dddd| g}ytj| t jd|  W n2 tk
rv } ztjt d	| | W Y dd}~X nX dS )
zwRemove the ip4LL address. While this is not necessary, the ip4LL
    address is extraneous and confusing to users.
    zLno link_local address interface defined, skipping link local address cleanupNzcleaning up ipv4LL addressr   r   flushr   zremoved ip4LL addresses from %sz)failed to remove ip4LL address from '%s'.)r   r   r   r   r   r   )r   r   er   r   r   del_ipv4_link_localL   s    

r)   c             C   sd  dd }g }t j }tjd| x| D ]
}| | d }tjd| |jd}||krhtd||f |j|}|jdd	}	tj|	|}
|
|krtjd
|	|||
 nd}tj||| d||
d}g }xJdD ]B}|j|d}|sq||}|	dks d|kr|d= |j| qW ||d< |j| tjd|
| q(W |rZtjd| |jd|d d|dS )a}  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'}
        ]
     }
    c             S   sX   dd| j d| j dd}d| j dkrFdj| j d| j d|d	< n| j d
|d
< |S )NZstaticautoZ
ip_addressgateway)typeZcontroladdressr+   :z{0}/{1}Zcidrr-   Znetmask)getr   )ZpcfgZsubpartr   r   r   _get_subnet_part   s    z7convert_network_configuration.<locals>._get_subnet_partznic mapping: %sr   zconsidering %sZmaczVDid not find network interface on system with mac '%s'. Cannot apply configuration: %sr,   unknownz6Found %s interface '%s' on '%s', assigned name of '%s'zfFound interface '%s' on '%s', which is not a public or private interface. Using default system naming.Zphysical)r,   mac_addressnameipv4ipv6anchor_ipv4anchor_ipv6Nr   Zanchorr+   subnetsznic '%s' configuration: %szadded dns servers: %sZ
nameserver)r,   r-   r   )versionconfig)r4   r5   r6   r7   )r"   Zget_interfaces_by_macr   r   r/   r   NIC_MAPappend)r:   Zdns_serversr0   Znic_configsZmacs_to_nicsnr   r2   Z
sysfs_nameZnic_typeZif_namemsgZncfgr8   ZnetdefZ
raw_subnetZsub_partr   r   r   convert_network_configurationc   sV    



r?         c             C   s6   t j| |||d}|j s&td|  tj|jj S )N)timeoutsec_betweenretrieszunable to read metadata at %s)r   Zreadurlokr   jsonloadscontentsdecode)ZurlrB   rC   rD   Zresponser   r   r   read_metadata   s
    rJ   c              C   sP   t jd} | dkrdS t jd}|r2tjd| nd}tj| t|d|fS )	Nzsystem-manufacturerZDigitalOceanFzsystem-serial-numberz8system identified via SMBIOS as DigitalOcean Droplet: %szsystem identified via SMBIOS as a DigitalOcean Droplet, but did not provide an ID. Please file a support ticket at: https://cloud.digitalocean.com/support/tickets/newT)FN)r   Zread_dmi_datar   r   Zcriticalr   )Zvendor_nameZ
droplet_idr>   r   r   r   read_sysinfo   s    


rK   )N)N)r@   r@   rA   )rF   Zloggingr   Z	cloudinitr   r   r"   r   r   r   r;   Z	getLogger__name__r   r   r   r)   r?   rJ   rK   r   r   r   r   <module>   s   


/
n
	