a '¡Dgê:ã@s.ddlZddlZddlZddlZddlZddlmZddlZddlm Z ddl m Z ddl m Z ddlmZmZmZmZmZddlmZmZddlmZdd lmZmZdd lmZdd lmZe  e!¡Z"d d gZ#dZ$dZ%dZ&Gdd„dej'j(ƒZ)dd„Z*dd„Z+Gdd„dej,ƒZ-e-ej.ffgZ/dd„Z0dS)éN)Úurlparse)ÚConnectionError)ÚHTTPConnection)Ú PoolManager)ÚdmiÚ performanceÚsourcesÚ url_helperÚutil)Ú EventScopeÚ EventType)ÚNoDHCPLeaseError)ÚEphemeralDHCPv4ÚEphemeralIPv6Network)ÚDataSourceHostname)ÚProcessExecutionErrorzhttp://169.254.42.42zhttp://[fd00:42::42]ééé cs*eZdZdZ‡fdd„Zddd„Z‡ZS)ÚSourceAddressAdapterzF Adapter for requests to choose the local address to bind to. c s ||_tt|ƒjfi|¤ŽdS©N)Úsource_addressÚsuperrÚ__init__)ÚselfrÚkwargs©Ú __class__©úH/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceScaleway.pyr+szSourceAddressAdapter.__init__FcCs0tjtjtjdfg}t||||j|d�|_dS)Né)Z num_poolsÚmaxsizeÚblockrÚsocket_options)rZdefault_socket_optionsÚsocketZ SOL_SOCKETZ SO_REUSEPORTrrZ poolmanager)rZ connectionsr!r"r#rrrÚinit_poolmanager/s ÿûz%SourceAddressAdapter.init_poolmanager)F)Ú__name__Ú __module__Ú __qualname__Ú__doc__rr%Ú __classcell__rrrrr&s rc Csnz(tj|d|d|dd„d�}t |j¡WStjyh}z&|jdkrRWYd}~dS‚WYd}~n d}~00dS)aQ Retrieve user data or vendor data. Scaleway user/vendor data API returns HTTP/404 if user/vendor data is not set. This function calls `url_helper.readurl` but instead of considering HTTP/404 as an error that requires a retry, it considers it as empty user/vendor data. Also, be aware the user data/vendor API requires the source port to be below 1024 to ensure the client is root (since non-root users can't bind ports below 1024). If requests raises ConnectionError (EADDRINUSE), the caller should retry to call this function on an other port. NrcSs|jdkot|jtjjƒ S)Né”)ÚcodeÚ isinstanceÚcauseÚrequestsÚ exceptionsr)Ú_ÚexcrrrÚWs z%query_data_api_once..)ÚdataÚtimeoutÚretriesZsessionZ exception_cbr+)r Úreadurlr Ú decode_binaryÚcontentsÚUrlErrorr,)Ú api_addressr5Úrequests_sessionÚrespr2rrrÚquery_data_api_once<sö r>c Cs,tdt|dƒƒD�]}zºt d||¡t ¡}d}zRt|ƒj}|}|ddkrZ|dd…}tj |dtj d �dd} | tj kr‚d }Wnt y–Yn0|  d t||fd �¡t|||d �} t d|¡| WStj�y } z4t d|| ¡t d¡| } WYd} ~ qWYd} ~ qd} ~ 00q| ‚dS)a/Get user or vendor data. Handle the retrying logic in case the source port is used. Scaleway metadata service requires the source port of the client to be a privileged port (<1024). This is done to ensure that only a privileged user on the system can access the metadata service. r rz*Trying to get %s data (bind on port %d)...z0.0.0.0rú[éÿÿÿÿN)Úprotoz0::zhttp://)r)r5r<z%s-data downloadedz%Error while trying to get %s data: %sé)ÚrangeÚmaxÚLOGÚdebugr/ZSessionrÚnetlocr$Z getaddrinfoZ IPPROTO_TCPZAF_INET6Ú ValueErrorZmountrr>r r:ÚwarningÚtimeÚsleep) Zapi_typer;r6r5Úportr<Z localhostZ url_addressÚaddressZ addr_protor4r2Úlast_excrrrÚquery_data_apidsL ÿ   ÿþþ   þÿ   $rOcs¸eZdZdZejejejej hiZ ‡fdd„Z e ddœ‡fdd„ Z dd „Zd d „Zed d „ƒZdd„Zedd„ƒZedd„ƒZdd„Zdd„Zddd„Zedd„ƒZedd„ƒZ‡ZS) ÚDataSourceScalewayÚScalewaycs¾tt|ƒ |||¡t t |ddgi¡g¡|_t|j dt ¡ƒ|_ t|j dt ¡ƒ|_ t|j dt ¡ƒ|_tj|_t|_d|_d|_d|_d|_d|_d|j ¡vrº|j|jd7_dS)NZ datasourcerQr6r5Úmax_waitTÚ metadata_urls)rrPrr Z mergemanydictZget_cfg_by_pathZds_cfgÚintÚgetÚDEF_MD_RETRIESr6ÚDEF_MD_TIMEOUTr5ÚDEF_MD_MAX_WAITrRrÚUNSETÚ_network_configÚ DS_BASE_URLSrSÚ metadata_urlÚ userdata_urlÚvendordata_urlÚephemeral_fixed_addressÚhas_ipv4Úkeys)rZsys_cfgÚdistroÚpathsrrrr¢s"ÿÿzDataSourceScaleway.__init__N)Úci_pkl_versionÚreturncsFtƒ |¡ddttdddœ}|D]}t||ƒs"t||||ƒq"dS)NT)r_r`rRrSr]r^)rÚ _unpicklerXr[ÚhasattrÚsetattr)rrdZ attr_defaultsÚattrrrrrf¸s ú zDataSourceScaleway._unpicklecCs|t ¡}tj||j|jdd�\}}|rZt d|¡|›d�|_|›d�|_ |›d�|_ dSt d|t t ¡|ƒ¡t ‚dS) zO Define metadata_url based upon api-metadata URL availability. F)ÚurlsrRr5Zconnect_synchronouslyz%s is reachablez/conf?format=jsonz/user_data/cloud-initz/vendor_data/cloud-initNz3Unable to reach api-metadata at %s after %s seconds) rJÚ monotonicr Z wait_for_urlrRr5rErFr\r]r^rTr)rrjZ start_timeZ avail_urlr1rrrÚ_set_metadata_urlÆs&ü     ýz$DataSourceScaleway._set_metadata_urlcCsZtj|j|j|jd�}t t |j ¡¡|_ t d|j |j|jƒ|_ t d|j|j|jƒ|_dS)N)r5r6z user-dataz vendor-data)r r7r\r5r6ÚjsonÚloadsr r8r9ÚmetadatarOr]Z userdata_rawr^Zvendordata_raw)rr=rrrÚ_crawl_metadataàs ÿÿÿz"DataSourceScaleway._crawl_metadatacCs>t d¡}|dkrdStj d¡r&dSt ¡}d|vr:dSdS)a  There are three ways to detect if you are on Scaleway: * check DMI data: not yet implemented by Scaleway, but the check is made to be future-proof. * the initrd created the file /var/run/scaleway. * "scaleway" is in the kernel cmdline. zsystem-manufacturerrQTz/var/run/scalewayZscalewayN)rZ read_dmi_dataÚosÚpathÚexistsr Z get_cmdline)Z vendor_nameZcmdlinerrrÚ ds_detectís  zDataSourceScaleway.ds_detectc Csr|jrÎz†t|j|jjƒ�b}t d¡�| |j¡Wdƒn1sF0Y| ¡|d|_ d|j d<Wdƒn1s€0YWn@t t t fyÌ}z"t tt|ƒ¡d|_WYd}~n d}~00|j�snz€t|j|jjƒ�Zt d¡�| |j¡Wdƒn1�s0Y| ¡d|j d<Wdƒn1�sJ0YWnt �ylYdS0dS) Nz7Setting api-metadata URL depending on IPv4 availabilityz fixed-addressÚipv4Z net_in_useFz7Setting api-metadata URL depending on IPv6 availabilityÚipv6T)r`rrbÚfallback_interfacerZTimedrlrSrpr_ror rrr ZlogexcrEÚstrr)rruÚerrrÚ _get_datasHþÿ* ,ý þÿ,.zDataSourceScaleway._get_datac Csð|jdur t dtj¡tj|_|jtjkr2|jS|jddu�rVi}i}|jdD]æ}|d|jkr¦d|d<ddd d œ}d | ¡vrš|d |g7<n |g|d <qTd | ¡vrØ|d |d›d |d›�f7<n|d›d |d›�f|d <|ddkrT|dddœ}d | ¡v�r0|d |g7<qT|g|d <qT|||jj <d|dœ|_n†dd|jj dœ}ddig}|jd�rÆ|dd|jddd|jddddd|jdddœgdœg7}||d <d!|gd"œ|_t  d#|j¡|jS)$z` Configure networking according to data received from the metadata API. Nz5Found None as cached _network_config. Resetting to %sZ private_ipZ public_ipsrMTZdhcp4z169.254.42.42/32z 62.210.0.1)zon-linkÚtoÚviaÚroutesÚ addressesú/ÚnetmaskZfamilyZinet6Úgatewayz::/0)r|r{r)ÚversionZ ethernetsZphysicalz%s)ÚtypeÚnamerƒrvZstaticz::Ú0)ÚnetworkÚprefixr�)rƒrMr€r}Úsubnetsr )r‚Zconfigznetwork_config : %s) rZrErIrrYror_rarbrwrF)rZnetcfgZip_cfgÚipZrouterˆrrrÚnetwork_config4sp þ ý   ÿ ÿ    þ   ÿýÿüÿz!DataSourceScaleway.network_configcCsdSrr©rrrrÚ launch_index„szDataSourceScaleway.launch_indexcCs |jdS)NÚid)ror‹rrrÚget_instance_idˆsz"DataSourceScaleway.get_instance_idcCs^dd„|jdDƒ}d}t|ƒ}|j dg¡D]*}| |¡s>q.| ||d… dd¡¡q.|S)NcSsg|] }|d‘qS)Úkeyr)Ú.0r�rrrÚ Œóz:DataSourceScaleway.get_public_ssh_keys..Zssh_public_keyszAUTHORIZED_KEY=Útagsr1ú )roÚlenrUÚ startswithÚappendÚreplace)rZssh_keysZakeypreÚplenÚtagrrrÚget_public_ssh_keys‹s z&DataSourceScaleway.get_public_ssh_keysFcCst|jddƒS)NÚhostnameF)rro)rZfqdnZ resolve_ipZ metadata_onlyrrrÚ get_hostname—szDataSourceScaleway.get_hostnamecCsdSrrr‹rrrÚavailability_zonešsz$DataSourceScaleway.availability_zonecCsdSrrr‹rrrÚregionžszDataSourceScaleway.region)FFF)r&r'r(Zdsnamer ZNETWORKr ZBOOT_NEW_INSTANCEZBOOTZ BOOT_LEGACYZdefault_update_eventsrrTrfrlrpÚ staticmethodrtrzÚpropertyrŠrŒrŽr›r�ržrŸr*rrrrrP˜s2ýÿ   2 O   rPcCs t |t¡Sr)rZlist_from_dependsÚ datasources)ZdependsrrrÚget_datasource_list¨sr£)1rmZloggingrqr$rJÚ urllib.parserr/Zrequests.exceptionsrZurllib3.connectionrZurllib3.poolmanagerrZ cloudinitrrrr r Zcloudinit.eventr r Zcloudinit.net.dhcpr Zcloudinit.net.ephemeralrrZcloudinit.sourcesrZcloudinit.subprZ getLoggerr&rEr[rVrXrWZadaptersZ HTTPAdapterrr>rOZ DataSourcerPZDEP_FILESYSTEMr¢r£rrrrÚs:        (4 ÿ