a '¡Dg.ã@sâddlZddlZddlmZddlmZmZmZmZddl Z ddl m Z ddl m Z ddlmZmZe e¡ZdZdd „ZGd d „d ƒZd d dœeeeeedœdd„Zeeedœdd„Zdeeeedœdd„Zedœdd„ZdS)éN)Ú monotonic)ÚDictÚOptionalÚTypeÚUnion)Úutil)Úreport_diagnostic_event)ÚUrlErrorÚreadurlzhttp://169.254.169.254/metadatacCsdtt ¡ƒdœS)NÚtrue)ZMetadatazx-ms-client-request-id)ÚstrÚuuidZuuid4)Z_url©rú@/usr/lib/python3.9/site-packages/cloudinit/sources/azure/imds.pyÚ headers_cbs þrc@sFeZdZdZdddddœeeeeeddœdd„Zed œd d „Z dS) ÚReadUrlRetryHandleraKManager for readurl retry behavior using exception_callback(). :param logging_backoff: Backoff to limit logging. :param max_connection_errors: Number of connection errors to retry on. :param retry_codes: Set of http codes to retry on. :param retry_deadline: Optional monotonic()-based deadline to retry until. çð?N)é”éšé­iô©Úlogging_backoffÚmax_connection_errorsÚ retry_codesÚretry_deadline)rrrÚreturncCs.||_||_||_||_d|_d|_d|_dS)Nrr)rrrrÚ_logging_thresholdÚ_request_countÚ _last_error)ÚselfrrrrrrrÚ__init__%s zReadUrlRetryHandler.__init__©rcCsJ|jd7_t|tƒs.td|tjd�dSd}|jdurNtƒ|jkrNd}nd}|jdurˆt|j t j ƒrˆ|jd8_|jdkr¢d}n|j dur¢|j |j vr¢d}|j|jkrÀ|j|j9_nd}|j durè|j|j kræd}|j |_n:|jdu�st|jtƒ�rt|j |jƒ�s"d}t|j ƒ|_|�s.|�sFtd|j|ftjd�|S)Néz1Polling IMDS failed with unexpected exception: %r©Z logger_funcFTrz1Polling IMDS failed attempt %d with exception: %r)rÚ isinstancer rÚLOGÚwarningrrrÚcauseÚrequestsÚConnectionErrorÚcoderrrrÚtype)rZreq_argsÚ exceptionÚlogZretryrrrÚexception_callback:sd ÿýÿ þ ÿ ÿ þ    þ ü ú  ÿýz&ReadUrlRetryHandler.exception_callback) Ú__name__Ú __module__Ú __qualname__Ú__doc__ÚfloatrÚintr Úboolr.rrrrrs õô rTé)Ú log_responseÚtimeout)ÚurlÚ retry_handlerr7r8rc CsZzt||jtd||d�}Wn8tyR}z td|tjd�‚WYd}~n d}~00|jS)a'Fetch URL from IMDS. :param url: url to fetch. :param log_response: log responses in readurl(). :param retry_deadline: time()-based deadline to retry until. :param timeout: Read/connection timeout in seconds for readurl(). :raises UrlError: on error fetching metadata. T©Z exception_cbrZinfiniteZ log_req_respr8z&Failed to fetch metadata from IMDS: %sr#N)r r.rr rr%r&Úcontents)r9r:r7r8ÚresponseÚerrorrrrÚ _fetch_url{s ú þr?)r9r:rc Cs\t||d�}zt | d¡¡WStyV}z td|tjd�‚WYd}~n d}~00dS)zßFetch IMDS metadata. :param url: url to fetch. :param retry_deadline: time()-based deadline to retry until. :raises UrlError: on error fetching metadata. :raises ValueError: on error parsing metadata. ©r:zutf-8z&Failed to parse metadata from IMDS: %sr#N)r?rZ load_jsonÚdecodeÚ ValueErrorrr%r&)r9r:Úmetadatar>rrrÚ_fetch_metadatažs þrD)rrrc Cs’t||d�}ztd}t||d�WStyŒ}zR|jdkrvtdtjd�t||d�}td}t||d�WYd}~S‚WYd}~n d}~00dS) zïFetch extended metadata, falling back to non-extended as required. :param retry_deadline: time()-based deadline to retry until. :raises UrlError: on error fetching metadata. :raises ValueError: on error parsing metadata. )rrz./instance?api-version=2021-08-01&extended=truer@i�z,Falling back to IMDS api-version: 2019-06-01r#z /instance?api-version=2019-06-01N)rÚIMDS_URLrDr r*rr%r&)rrr:r9r>rrrÚ fetch_metadata_with_api_fallback·s( þ þþrFr!cCsPtd}tddddd�}t||jtddd d �}td |jd›d �tjd �|j S)zhFetch extended metadata, falling back to non-extended as required. :raises UrlError: on error. z'/reprovisiondata?api-version=2019-06-01g@r")rrrNrTFr6r;z Polled IMDS z time(s)r#) rErr r.rrrr%Údebugr<)r9Zhandlerr=rrrÚfetch_reprovision_data×s(ø ú þrH)N) Zloggingr ÚtimerÚtypingrrrrr(Z cloudinitrZcloudinit.sources.helpers.azurerZcloudinit.url_helperr r Z getLoggerr/r%rErrr r5r4Úbytesr?rDr3rFrHrrrrÚs<    cûú $ü ÿþ