
    fE                         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Zd	 Zdd
Zd ZddZd Zy)    N)dmi)net)subp
url_helperutileth0eth1)publicprivatec                 >   |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      st        d      	 t        j                  |       t        j                  d||       t        j                  |       t        j                  d|       |S # t        $ r t        j                  t        d||        w xY w)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set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randomrandintr   which	Exceptionr   logexc)distronicr   ip_addr_cmdip_link_cmds        H/usr/lib/python3/dist-packages/cloudinit/sources/helpers/digitalocean.pyassign_ipv4_link_localr&      s     (		@#F8
 	

  &&q#q# 6D eS9KsD9K::dB
 	
		+		7sC		+		*C0 J  1	
 	s   AC5 5'Dc                     t        j                         D cg c]  }| j                  j                  |      r|! }}|sy t	        |d       S c c}w )Nc                 .    t        j                  | d      S )Nifindex)cloudnetread_sys_net_int)ds    r%   <lambda>z$get_link_local_nic.<locals>.<lambda>I   s    8#<#<Q	#J     )key)r*   get_devicelist
networkingis_physicalmin)r!   fnicss      r%   r   r   A   sY     ((*((+ 	
D 
 tJKKs   $Ac                 (   | st         j                  d       yt         j                  d       dddd| g}	 t        j                  |       t         j                  d|        y# t        $ r&}t	        j
                  t         d	| |       Y d}~yd}~ww xY w)
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#   es      r%   del_ipv4_link_localr9   L   s     		$	
 	II*+%5KN		+		3S9 NCDc1MMNs   +A" "	B+BBc                 *   d }g }t        j                         }t        j                  d|       | D ].  }| |   d   }t        j                  d|       |j	                  d      }||vrt        d|d|      |j	                  |      }|j	                  dd	      }	t        j	                  |	|      }
|
|k7  rt        j                  d
|	|||
       nd}t        j                  |||       d||
d}g }dD ]<  }|j	                  |d      }|s ||      }|	dk7  sd|v r|d= |j                  |       > ||d<   |j                  |       t        j                  d|
|       1 |r*t        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                    dd| j                  d      | j                  d      d}d| j                  d      v r5dj                  | j                  d      | j                  d            |d	<   |S | j                  d
      |d
<   |S )Nstaticauto
ip_addressgateway)typecontroladdressr?   :z{0}/{1}cidrrB   netmask)getr   )pcfgsubparts     r%   _get_subnet_partz7convert_network_configuration.<locals>._get_subnet_part   s    xx-xx	*	
 $((<((!*!1!1&(8"GI  "&)!4GIr.   znic mapping: %sr   zconsidering %smacz3Did not find network interface on system with mac 'z'. Cannot apply configuration: r@   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.physical)r@   mac_addressname)ipv4ipv6anchor_ipv4anchor_ipv6Nr
   anchorr?   subnetsznic '%s' configuration: %szadded dns servers: %s
nameserver)r@   rB   r   )versionconfig)r*   get_interfaces_by_macr   r   rF   r   NIC_MAPappend)rW   dns_serversrI   nic_configsmacs_to_nicsnr"   rM   
sysfs_namenic_typeif_namemsgncfgrT   netdef
raw_subnetsub_parts                    r%   convert_network_configurationrg   c   s   :" K113LII. 3?Qil		"C(ggenl* %  "%%k2
7769-++h
3j IIHE  IIc;
3 &
 D 		%F.J'
3H8#x6'9Y'NN8$		% "Y4 		.>g3?j 		);7L[IJK00r.   c                     t        j                  | |||      }|j                         st        d| z        t	        j
                  |j                  j                               S )N)timeoutsec_betweenretrieszunable to read metadata at %s)r   readurlokr   jsonloadscontentsdecode)urlri   rj   rk   responses        r%   read_metadatart      sU    !!W+wH ;;=:S@AA::h''..011r.   c                      t        j                  d      } | dk7  ryt        j                  d      }|rt        j                  d|       d|fS d}t        j	                  |       t        |      )Nzsystem-manufacturerDigitalOcean)FNz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)r   read_dmi_datar   r   criticalr   )vendor_name
droplet_idrb   s      r%   read_sysinfor{      sw    
 ##$9:Kn$""#9:J		F	
 *A 	 	S3r.   )N)   r|      )rn   loggingr   	cloudinitr   r   r*   r   r   r   rY   	getLogger__name__r   r&   r   r9   rg   rt   r{    r.   r%   <module>r      s[   
     % , ,
/g!,^LN.k1\2r.   