U
    L¬÷dr  ã                   @   s–   d dl Z d dlZd dlm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 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)Úlog)Únet)ÚsubpÚ
url_helperÚutilZeth0Zeth1)ÚpublicZprivatec              	   C   sÐ   |st | ƒ}t d|¡ |s$tdƒ‚d t dd¡t dd¡¡}dd	d
|d|g}dddd|dg}t d¡srtdƒ‚z2t |¡ t d||¡ t |¡ t d|¡ W n& t	k
rÊ   t
 td||¡ ‚ Y nX |S )zä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.
    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   úH/usr/lib/python3/dist-packages/cloudinit/sources/helpers/digitalocean.pyÚassign_ipv4_link_local   s>    ÿ
 
ÿ
ÿ

ûr   c                    s.   ‡ fdd„t  ¡ D ƒ}|sd S t|dd„ dS )Nc                    s   g | ]}ˆ j  |¡r|‘qS r   )Z
networkingZis_physical)Ú.0Úf©r   r   r   Ú
<listcomp>B   s   þz&get_link_local_nic.<locals>.<listcomp>c                 S   s   t  | d¡S )NZifindex)ÚcloudnetZread_sys_net_int)Údr   r   r   Ú<lambda>I   ó    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  d¡ dS t  d¡ dddd| g}zt |¡ t  d| ¡ W n2 tk
rv } zt t d	| |¡ W 5 d}~X Y 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   sV  dd„ }g }t  ¡ }t d|¡ | D ]}| | d }t d|¡ | d¡}||krdtd||f ƒ‚| |¡}| dd	¡}	t |	|¡}
|
|kr¢t d
|	|||
¡ nd}t |||¡ d||
dœ}g }dD ]>}| |d¡}|sÞqÈ||ƒ}|	dksöd|krü|d= | |¡ qÈ||d< | |¡ t d|
|¡ q$|rLt d|¡ | 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|   d¡|   d¡dœ}d|   d¡krFd |   d¡|   d¡¡|d	< n|   d
¡|d
< |S )NZstaticÚautoZ
ip_addressÚgateway)ÚtypeZcontrolÚaddressr.   ú:z{0}/{1}Zcidrr0   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)Zipv4Zipv6Zanchor_ipv4Zanchor_ipv6Nr   Zanchorr.   Úsubnetsznic '%s' configuration: %szadded dns servers: %sZ
nameserver)r/   r0   r	   )ÚversionÚconfig)r$   Zget_interfaces_by_macr   r   r2   r   ÚNIC_MAPÚappend)r9   Zdns_serversr3   Znic_configsZmacs_to_nicsÚnr   r5   Z
sysfs_nameZnic_typeZif_nameÚmsgZncfgr7   ZnetdefZ
raw_subnetZsub_partr   r   r   Úconvert_network_configurationc   s`    

þÿ
û	ÿý
r>   é   é   c                 C   s6   t j| |||d}| ¡ s&td|  ƒ‚t |j ¡ ¡S )N)ÚtimeoutÚsec_betweenÚretrieszunable to read metadata at %s)r   ZreadurlÚokr   ÚjsonÚloadsÚcontentsÚdecode)ZurlrA   rB   rC   Zresponser   r   r   Úread_metadataÑ   s       ÿrI   c                  C   sP   t  d¡} | dkrdS t  d¡}|r2t d|¡ nd}t |¡ t|ƒ‚d|fS )Nzsystem-manufacturerZDigitalOcean)FNzsystem-serial-numberz8system identified via SMBIOS as DigitalOcean Droplet: %sz¦system 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)r   Zread_dmi_datar   r   Zcriticalr   )Zvendor_nameZ
droplet_idr=   r   r   r   Úread_sysinfoÚ   s    

þÿ
rJ   )N)N)r?   r?   r@   )rE   r   Z	cloudinitr   r   Zloggingr   r$   r   r   r   r:   Z	getLoggerÚ__name__r   r   r   r,   r>   rI   rJ   r   r   r   r   Ú<module>   s   


/
n
	