o
    x[hê  ã                   @   sd   d dl Z d dlZd dlmZ d dlmZ d dlmZ e e¡Z	dd„ Z
dd„ Zddd„Zdd„ ZdS )é    N)Údmi)Únet)Ú
url_helperc              
   C   s  dd„ }g }t  ¡ }t d|¡ |  d¡D ]U}t d|¡ | d¡}||vr.td||f ƒ‚| d¡}| |¡}t d	|||| d
¡¡ d||dœ}g }	| d¡D ]}
||
|  d¡ƒ}|	 |¡ qQ|	|d< | |¡ q|  d¡r„t d|  d¡¡ | d|  d¡dœ¡ d|dœS )aC
  
    Convert the UpCloud network metadata description into
    Cloud-init's version 1 netconfig format.

    Example JSON:
    {
      "interfaces": [
        {
          "index": 1,
          "ip_addresses": [
            {
              "address": "94.237.105.53",
              "dhcp": true,
              "dns": [
                "94.237.127.9",
                "94.237.40.9"
              ],
              "family": "IPv4",
              "floating": false,
              "gateway": "94.237.104.1",
              "network": "94.237.104.0/22"
            },
            {
              "address": "94.237.105.50",
              "dhcp": false,
              "dns": [],
              "family": "IPv4",
              "floating": true,
              "gateway": "",
              "network": "94.237.105.50/32"
            }
          ],
          "mac": "32:d5:ba:4a:36:e7",
          "network_id": "031457f4-0f8c-483c-96f2-eccede02909c",
          "type": "public"
        },
        {
          "index": 2,
          "ip_addresses": [
            {
              "address": "10.6.3.27",
              "dhcp": true,
              "dns": [],
              "family": "IPv4",
              "floating": false,
              "gateway": "10.6.0.1",
              "network": "10.6.0.0/22"
            }
          ],
          "mac": "32:d5:ba:4a:84:cc",
          "network_id": "03d82553-5bea-4132-b29a-e1cf67ec2dd1",
          "type": "utility"
        },
        {
          "index": 3,
          "ip_addresses": [
            {
              "address": "2a04:3545:1000:720:38d6:baff:fe4a:63e7",
              "dhcp": true,
              "dns": [
                "2a04:3540:53::1",
                "2a04:3544:53::1"
              ],
              "family": "IPv6",
              "floating": false,
              "gateway": "2a04:3545:1000:720::1",
              "network": "2a04:3545:1000:720::/64"
            }
          ],
          "mac": "32:d5:ba:4a:63:e7",
          "network_id": "03000000-0000-4000-8046-000000000000",
          "type": "public"
        },
        {
          "index": 4,
          "ip_addresses": [
            {
              "address": "172.30.1.10",
              "dhcp": true,
              "dns": [],
              "family": "IPv4",
              "floating": false,
              "gateway": "172.30.1.1",
              "network": "172.30.1.0/24"
            }
          ],
          "mac": "32:d5:ba:4a:8a:e1",
          "network_id": "035a0a4a-77b4-4de5-820d-189fc8135714",
          "type": "private"
        }
      ],
      "dns": [
        "94.237.127.9",
        "94.237.40.9"
      ]
    }
    c                 S   s¸   |   d¡rd}|   d¡dkrd}d|iS d}|   d¡dkrd}|d|   d	¡d
œ}|   d¡r4|   d¡|d< d|   d¡v rG|   d¡ d¡d |d< ||   d¡krZ|   d¡rZ|   d¡|d< |S )NÚdhcpÚfamilyÚIPv6zipv6_dhcpv6-statelessÚtypeÚstaticÚstatic6ÚautoÚaddress)r   Úcontrolr   Úgatewayú/Únetworké   ÚnetmaskÚdnsÚdns_nameservers)ÚgetÚsplit)Úip_addrr   Ú	dhcp_typeÚstatic_typeÚsubpart© r   úC/usr/lib/python3/dist-packages/cloudinit/sources/helpers/upcloud.pyÚ_get_subnet_configr   s&   
ý
z8convert_to_network_config_v1.<locals>._get_subnet_configzNIC mapping: %sÚ
interfaceszConsidering %sÚmaczVDid not find network interface on system with mac '%s'. Cannot apply configuration: %sr   z4Found %s interface '%s' with address '%s' (index %d)ÚindexÚphysical)r   ÚnameÚmac_addressÚip_addressesr   ÚsubnetszSetting DNS nameservers to %sÚ
nameserver)r   r   r   )ÚversionÚconfig)ÚcloudnetÚget_interfaces_by_macÚLOGÚdebugr   ÚRuntimeErrorÚappend)r(   r   Únic_configsÚmacs_to_interfacesÚ	raw_ifacer#   Ú
iface_typeÚ
sysfs_nameÚ	interfacer%   Ú
ip_addressÚsub_partr   r   r   Úconvert_to_network_config_v1   sL   c
þÿ

û	ý
ÿ
r7   c                 C   s   t | ƒS )N)r7   )r(   r   r   r   Úconvert_network_configÀ   s   r8   é   é   c                 C   s6   t j| |||d�}| ¡ std|  ƒ‚t |j ¡ ¡S )N)ÚtimeoutÚsec_betweenÚretrieszunable to read metadata at %s)r   ÚreadurlÚokr-   ÚjsonÚloadsÚcontentsÚdecode)Úurlr;   r<   r=   Úresponser   r   r   Úread_metadataÄ   s   ÿrF   c                  C   sN   t  d¡} | dkrdS t  d¡}|rt d|¡ d|fS d}t |¡ t|ƒ‚)Nzsystem-manufacturerÚUpCloud)FNzsystem-uuidz2system identified via SMBIOS as UpCloud server: %sz¤system identified via SMBIOS as a UpCloud server, but did not provide an ID. Please contact support viahttps://hub.upcloud.com or via email with support@upcloud.comT)r   Úread_dmi_datar+   r,   Úcriticalr-   )Úvendor_nameÚserver_uuidÚmsgr   r   r   Úread_sysinfoÍ   s   

ÿùÿ
rM   )r9   r9   r:   )r@   ÚloggingÚ	cloudinitr   r   r)   r   Ú	getLoggerÚ__name__r+   r7   r8   rF   rM   r   r   r   r   Ú<module>   s   
 2
	