3 \meE@sddlZddlZddlZddlmZddlmZddlmZmZm Z dddZ ej e Z ddd Zd d Zdd d ZddZdddZddZdS)N)dmi)net)subp url_helperutilZeth0Zeth1)publicZprivatec Cs|st|}tjd||s$tddjtjddtjdd}dd d |d |g}dd d d |dg}tjdsrtdy2tj|tjd||tj|tjd|Wn&t k rt j td||YnX|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.z169.254.{0}.{1}/16ripaddradddevlinksetZupz4No 'ip' command available to configure ip4LL addressz#assigned ip4LL address '%s' to '%s'zbrought device '%s' upzRip4LL address assignment of '%s' to '%s' failed. Droplet networking will be broken) get_link_local_nicLOGdebug RuntimeErrorformatrandomZrandintrZwhich Exceptionrlogexc)distronicr ip_addr_cmdZ ip_link_cmdr"/usr/lib/python3.6/digitalocean.pyassign_ipv4_link_locals4    rcs.fddtjD}|sdSt|dddS)Ncsg|]}jj|r|qSr)Z networkingZ is_physical).0f)rrr Csz&get_link_local_nic..cSs tj|dS)NZifindex)cloudnetZread_sys_net_int)drrrIsz$get_link_local_nic..)key)r"Zget_devicelistmin)rZnicsr)rrrAs   rcCs||stjddStjddddd|g}ytj|tjd|Wn2tk rv}ztjtd ||WYdd}~XnXdS) 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 addressr r flushrzremoved ip4LL addresses from %sz)failed to remove ip4LL address from '%s'.)rrrrrr)rrerrrdel_ipv4_link_localLs  r)cCsddd}g}tj}tjd|x|D] }||d}tjd||jd}||krhtd||f|j|}|jdd } tj| |} | |krtjd | ||| nd } tj| ||d || d } g} xJdD]B}|j|d}|sq||}| dksd|kr|d=| j|qW| | d<|j| tjd| | q(W|rZtjd||jd|dd|dS)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|jd|jdd}d|jdkrFdj|jd|jd|d <n|jd |d <|S) NZstaticautoZ ip_addressgateway)typeZcontroladdressr+:z{0}/{1}Zcidrr-Znetmask)getr)ZpcfgZsubpartrrr_get_subnet_partsz7convert_network_configuration.._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_addressnameipv4ipv6 anchor_ipv4 anchor_ipv6NrZanchorr+subnetsznic '%s' configuration: %szadded dns servers: %sZ nameserver)r,r-r)versionconfig)r4r5r6r7)r"Zget_interfaces_by_macrrr/rNIC_MAPappend)r:Z dns_serversr0Z nic_configsZ macs_to_nicsnrr2Z sysfs_nameZnic_typeZif_namemsgZncfgr8ZnetdefZ raw_subnetZsub_partrrrconvert_network_configurationcsV            r?cCs6tj||||d}|js&td|tj|jjS)N)timeout sec_betweenretrieszunable to read metadata at %s)rZreadurlokrjsonloadscontentsdecode)ZurlrBrCrDZresponserrr read_metadatas  rJcCsPtjd}|dkrdStjd}|r2tjd|nd}tj|t|d|fS) Nzsystem-manufacturerZ DigitalOceanFzsystem-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)rZ read_dmi_datarrZcriticalr)Z vendor_nameZ droplet_idr>rrr read_sysinfos   rK)N)N)r@r@rA)rFZloggingrZ cloudinitrrr"rrrr;Z getLogger__name__rrrr)r?rJrKrrrrs     / n