3 \me@sddlZddlmZddlmZmZddlmZddlm Z ddl m Z ej eZddiZd Zd Zd ZGd d d ejZGd ddeZeejffeejejffgZddZdS)N)net)sourcesutil)NoDHCPLeaseError)EphemeralDHCPv4)upcloud metadata_urlz'http://169.254.169.254/metadata/v1.jsonc@sHeZdZdZdZddZddZddZd d Zd d Z e d dZ dS)DataSourceUpCloudUpCloudFcCstjj||||||_t|_tjtj|ddgit g|_ |j d|_ |j j dt |_|j j dt|_|j j dt|_d|_dS)NZ datasourcer rretriestimeout wait_retry)r DataSource__init__distrodictmetadatarZ mergemanydictZget_cfg_by_pathBUILTIN_DS_CONFIGZds_cfgmetadata_addressget MD_RETRIESr MD_TIMEOUTr MD_WAIT_RETRYr_network_config)selfsys_cfgrpathsr'/usr/lib/python3.6/DataSourceUpCloud.pyr"s  zDataSourceUpCloud.__init__cCstjS)N) uc_helperZ read_sysinfo)rrrr _get_sysinfo2szDataSourceUpCloud._get_sysinfocCstj|j|j|j|jdS)N)rZ sec_betweenr )r!Z read_metadatarrrr )rrrr _read_metadata5s z DataSourceUpCloud._read_metadatacCs|j\}}|sdStjd||jryNtjdtj}tjd|t|j|t j tjd|j d}WdQRXWn6t t jfk r}zt jtt|dSd}~XnXndy"tjdt j tjd|j d}Wn@t jk r}z t jtt|tjd|dSd}~XnX||_|jd ||jd <|jd |jd <|jd |jd <|jd|jd<|jdd|jd<|jdd|_|jdd|_dS)NFz"Running on UpCloud. server_uuid=%szFinding a fallback NICz*Discovering metadata via DHCP interface %szReading from metadata service)Zlogfuncmsgfuncz7Discovering metadata without DHCP-configured networkingzENo DHCP-enabled interfaces available, unable to fetch metadata for %sZ instance_idz instance-idZhostnamezlocal-hostnamenetworkZ public_keysz public-keysZregiondefaultZavailability_zoneZ vendor_dataZ user_dataT)r"LOGinfoperform_dhcp_setupdebugcloudnetZfind_fallback_nicrrrZlog_timer#rrZInvalidMetaDataExceptionZlogexcstrZ metadata_fullrrZvendordata_rawZ userdata_raw)rZ is_upcloudZ server_uuidZnicZmderrr _get_data=sN    zDataSourceUpCloud._get_datacCstj|jS)N)rZinstance_id_matches_system_uuidZget_instance_id)rrrrr check_instance_idrsz#DataSourceUpCloud.check_instance_idcCs6|jr |jS|jjd}|s$tdtj||_|jS)z Configure the networking. This needs to be done each boot, since the IP and interface information might have changed due to reconfiguration. r&z/Unable to get network meta-data from server....)rrr RuntimeErrorr!Zconvert_network_config)rZraw_network_configrrr network_configus z DataSourceUpCloud.network_configN) __name__ __module__ __qualname__Zdsnamer*rr"r#r/r0propertyr2rrrr r s5r c@seZdZdZdZdS)DataSourceUpCloudLocalaz Run in init-local using a DHCP discovery prior to metadata crawl. In init-local, no network is available. This subclass sets up minimal networking with dhclient on a viable nic so that it can talk to the metadata service. If the metadata service provides network configuration then render the network configuration for that instance based on metadata. TN)r3r4r5__doc__r*rrrr r7sr7cCs tj|tS)N)rZlist_from_depends datasources)Zdependsrrr get_datasource_listsr:)ZloggingZ cloudinitrr,rrZcloudinit.net.dhcprZcloudinit.net.ephemeralrZcloudinit.sources.helpersrr!Z getLoggerr3r(rrrrrr r7ZDEP_FILESYSTEMZ DEP_NETWORKr9r:rrrr s     r