name : digitalocean.cpython-39.pyc
a

'�Dg��@s�ddlZddlZddlZddlmZddlmZddlmZmZddd�Z	e�
e�Zddd	�Z
d
d�Zddd
�Zdd�Zddd�Zdd�ZdS)�N)�dmi)�net)�
url_helper�utilZeth0Zeth1)�publicZprivatec	Cs�|st|�}t�d|�|s$td��d�t�dd�t�dd��}z,|j�||�|j�	|�t�d||�Wn$t
y�t�td	||��Yn0|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.z169.254.{0}.{1}/16��r�z&brought device '%s' up with address %szRip4LL address assignment of '%s' to '%s' failed. Droplet networking will be broken)
�get_link_local_nic�LOG�debug�RuntimeError�format�randomZrandint�net_opsZadd_addrZlink_up�	Exceptionr�logexc)�distro�nic�addr�r�J/usr/lib/python3.9/site-packages/cloudinit/sources/helpers/digitalocean.py�assign_ipv4_link_locals.���rcs.�fdd�t��D�}|sdSt|dd�d�S)Ncsg|]}�j�|�r|�qSr)Z
networkingZis_physical)�.0�f�rrr�
<listcomp>9s�z&get_link_local_nic.<locals>.<listcomp>cSst�|d�S)NZifindex)�cloudnetZread_sys_net_int)�drrr�<lambda>@�z$get_link_local_nic.<locals>.<lambda>)�key)rZget_devicelist�min)rZnicsrrrr
8s
�r
c
Csr|st�d�dSt�d�z|j�|�t�d|�Wn4tyl}zt�td||�WYd}~n
d}~00dS)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 cleanupNzcleaning up ipv4LL addresszremoved ip4LL addresses from %sz)failed to remove ip4LL address from '%s'.)rrrZ
flush_addrrrr)rr�errr�del_ipv4_link_localCs�
r$cCsVdd�}g}t��}t�d|�|D�]}||d}t�d|�|�d�}||vrdtd||f��|�|�}|�dd	�}	t�|	|�}
|
|kr�t�d
|	|||
�nd}t�|||�d||
d
�}g}
dD]>}|�|d�}|s�q�||�}|	dks�d|vr�|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'}
        ]
     }
    cSsXdd|�d�|�d�d�}d|�d�vrFd�|�d�|�d��|d	<n|�d
�|d
<|S)NZstatic�auto�
ip_address�gateway)�typeZcontrol�addressr'�:z{0}/{1}Zcidrr)�netmask)�getr)ZpcfgZsubpartrrr�_get_subnet_partus��
z7convert_network_configuration.<locals>._get_subnet_partznic mapping: %srzconsidering %sZmaczVDid 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.Zphysical)r(�mac_address�name)Zipv4Zipv6Zanchor_ipv4Zanchor_ipv6Nr�anchorr'�subnetsznic '%s' configuration: %szadded dns servers: %sZ
nameserver)r(r)r)�version�config)rZget_interfaces_by_macrrr,r
�NIC_MAP�append)r4Zdns_serversr-Znic_configsZmacs_to_nics�nrr/Z
sysfs_nameZnic_typeZif_name�msgZncfgr2ZnetdefZ
raw_subnetZsub_partrrr�convert_network_configurationXs`

��
�	��
r9��cCs6tj||||d�}|��s&td|��t�|j���S)N)�timeout�sec_between�retrieszunable to read metadata at %s)rZreadurl�okr
�json�loads�contents�decode)�urlr<r=r>Zresponserrr�
read_metadata�s�rEcCsPt�d�}|dkrdSt�d�}|r2t�d|�nd}t�|�t|��d|fS)Nzsystem-manufacturerZDigitalOcean)FNzsystem-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)rZ
read_dmi_datarrZcriticalr
)Zvendor_nameZ
droplet_idr8rrr�read_sysinfo�s

��
rF)N)N)r:r:r;)r@ZloggingrZ	cloudinitrrrrrr5Z	getLogger�__name__rrr
r$r9rErFrrrr�<module>s


&
n
	

© 2025 Cubjrnet7