%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python3.9/site-packages/cloudinit/sources/__pycache__/
Upload File :
Create Path :
Current File : //usr/lib/python3.9/site-packages/cloudinit/sources/__pycache__/DataSourceAliYun.cpython-39.pyc

a

��h�<�@s�ddlZddlZddlmZmZddlmZmZddlmZ	ddlm
Z
ddlmZm
Z
ddlmZddlmZdd	lmZdd
lmZmZe�e�ZdZGdd
�d
ej�Zdd�Zdd�ZGdd�de�Zeej ffeej ej!ffgZ"dd�Z#dS)�N)�List�Union)�dmi�sources)�
url_helper)�util)�
EventScope�	EventType)�NoDHCPLeaseError)�EphemeralIPNetwork)�DataSourceHostname)�aliyun�ec2zAlibaba Cloud ECScsBeZdZUdZdgZdZgZeee	d<dZ
dZdZe
jZeeefe	d<d	Zed
d��Z�fdd
�Zedd��fdd�Zd:dd�Zdd�Zdd�Zedd��Zedd��Zedd��Zedd��Zedd ��Zed!d"��Z d#d$�Z!d%d&�Z"d'd(�Z#d)d*�Z$d+d,�Z%d-d.�Z&d/d0�Z'd1d2�Z(d;d3d4�Z)d<d6d7�Z*d=d8d9�Z+�Z,S)>�DataSourceAliYunZAliYunzhttp://100.100.100.200z
2016-01-01�extended_metadata_versions���2N�_network_configFcCsdS)NzX-aliyun-ecs-metadata-token���selfrr�F/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceAliYun.py�imdsv2_token_put_header+sz(DataSourceAliYun.imdsv2_token_put_headercs:tt|��|||�t�|j�|_|jtj�t	j
�dS�N)�superr�__init__�copy�deepcopyZdefault_update_eventsrZNETWORK�addr	ZBOOT)rZsys_cfg�distro�paths��	__class__rrr/szDataSourceAliYun.__init__)�ci_pkl_version�returncst��|�dSr)r�	_unpickle)rr#r!rrr%4szDataSourceAliYun._unpicklecCs*|j�d�}d}|dur d}d}t||�S)N�hostnameFzlocalhost.localdomainT)�metadata�getr)rZfqdnZ
resolve_ipZ
metadata_onlyr&Z
is_defaultrrr�get_hostname7szDataSourceAliYun.get_hostnamecCst|j�di��S)Nzpublic-keys)�parse_public_keysr'r(rrrr�get_public_ssh_keys?sz$DataSourceAliYun.get_public_ssh_keyscCst�r|j��SdS)NZNO_ALIYUN_METADATA)�
_is_aliyun�dsname�lowerrrrr�_get_cloud_nameBs
z DataSourceAliYun._get_cloud_namecCs
|j��Sr)r-r.rrrr�platformGszDataSourceAliYun.platformcCsdS)Nzlatest/api/tokenrrrrr�api_token_routeLsz DataSourceAliYun.api_token_routecCsdS)NZ21600rrrrr�imdsv2_token_ttl_secondsPsz)DataSourceAliYun.imdsv2_token_ttl_secondscCs|j|jgSr)r�imdsv2_token_req_headerrrrr�imdsv2_token_redactTsz$DataSourceAliYun.imdsv2_token_redactcCs
|jdS)Nz-ttl-seconds)rrrrrr3Xsz(DataSourceAliYun.imdsv2_token_req_headercCsn|jtjkr|jSi}|jj}|j�d�}t|t�rRt	j
||t�|j
dd�d�}nt�d|�|S||_|jS)z@Return a network config dict for rendering ENI or netplan files.�networkZapply_full_imds_network_configT)Zfallback_nicZfull_network_configz%Metadata 'network' key not valid: %s.)rr�UNSETr�fallback_interfacer'r(�
isinstance�dictr
Z#convert_ecs_metadata_network_configrZget_cfg_option_bool�ds_cfg�LOG�warning)r�resultZifaceZnet_mdrrr�network_config\s"
��zDataSourceAliYun.network_configc
Cs�g}i}|j}d}|D]"}d�||�}|�|�|||<qt�d�d}d}|��}	z0tj||	j|	j	tj
|j|j||j
dd�	\}}Wntjy�Yn0|r�|r�||_||SdS)a=Get an API token for ECS Instance Metadata Service.

        On ECS. IMDS will always answer an API token, set
        HttpTokens=optional (default) when create instance will not forcefully
        use the security-enhanced mode (IMDSv2).

        https://api.alibabacloud.com/api/Ecs/2014-05-26/RunInstances
        �PUTz{0}/{1}zFetching Ecs IMDSv2 API TokenNF)	�urlsZmax_wait�timeoutZ	status_cb�
headers_cb�exception_cb�request_method�headers_redactZconnect_synchronously)r1�format�appendr;�debugZget_url_params�uhelpZwait_for_urlZmax_wait_secondsZtimeout_secondsr<�_get_headers�_imds_exception_cbr4�UrlError�
_api_token)
r�mdurlsr@Zurl2baseZurl_pathrD�urlZcur�responseZ
url_paramsrrr�_maybe_fetch_api_tokenss<



�z'DataSourceAliYun._maybe_fetch_api_tokencCsJ|j}|�d|j�}|�|�}|r8||_t�d|j�n
t�d�t|�S)N�
metadata_urlszUsing metadata source: '%s'z)IMDS's HTTP endpoint is probably disabled)	r:r(rRrQ�metadata_addressr;rHr<�bool)rZmcfgrNrSrrr�wait_for_metadata_service�s

z*DataSourceAliYun.wait_for_metadata_servicecCs|��siS|j}i}|j}|j}d}|j}z�tj|j|j|j	||dd�|d<tj|j|j|j	||dd�|d<z&tj
|j|j|j	||d�}||d<WnBty�t�
td|j�tj|j|j|j	|||d�|d<Yn0Wn(t�yt�
td	|j�iYS0|S)
z�Crawl metadata service when available.

        @returns: Dictionary of crawled metadata content containing the keys:
          meta-data, user-data, vendor-data and dynamic.
        N�	user-data)rBrErCZ	item_name�vendor-data)rBrErC�	meta-dataz@Faild read json meta-data from %s fall back directory tree style)rBrErCZretrieval_exception_ignore_cbz'Failed reading from metadata address %s)rUr4�_refresh_stale_aliyun_token_cb�&_skip_or_refresh_stale_aliyun_token_cb�#_skip_json_path_meta_path_aliyun_cbr
Zget_instance_data�min_metadata_versionrSrJZget_instance_meta_data�	ExceptionrZlogexcr;rZget_instance_metadata)rZredactZcrawled_metadataZexc_cbZ	exc_cb_udZskip_cbZexe_cb_whole_metar=rrr�crawl_metadata�sp�
�
����
zDataSourceAliYun.crawl_metadatacCs*t|tj�r&|jdkr&t�d�d|_dS)z=Exception handler for Ecs to refresh token if token is stale.i�z+Clearing cached Ecs API token due to expiryNT)r8rIrL�coder;rHrM�r�msg�	exceptionrrrrY�s
z/DataSourceAliYun._refresh_stale_aliyun_token_cbcCs
|j|vS)z/Returns False if cause.code is in status_codes.)r_)rZstatus_codes�causerrr�_skip_retry_on_codes�sz%DataSourceAliYun._skip_retry_on_codescCs"|�tj|�}|sdS|�||�S)z^Callback will not retry on SKIP_USERDATA_VENDORDATA_CODES or
        if no token is available.F)rdrZSKIP_USERDATA_CODESrY)rrarbZretryrrrrZ�sz7DataSourceAliYun._skip_or_refresh_stale_aliyun_token_cbcCs0t|tj�r$|jdkr$t�d�dS|�||�S)z7Callback will not retry of whole meta_path is not foundi�z&whole meta_path is not found, skippingF)r8rIrLr_r;r<rYr`rrrr[s
z4DataSourceAliYun._skip_json_path_meta_path_aliyun_cbcCs�|j|j��krdS|jr�t��r0t�d�dSzbt|j	|j	j
ddd��8}|��|_t�d|j
rld|j
��nd�Wd�n1s�0YWq�ty�YdS0n
|��|_|jr�t|jt�s�dS|j�di�|_|j�d	i�|_|j�d
i�|_dS)NFz1FreeBSD doesn't support running dhclient with -sfT)Zipv4Zipv6zCrawled metadata service%s� �rXrVrW)Z
cloud_namer-r.�perform_dhcp_setuprZ
is_FreeBSDr;rHrrr7r^Z_crawled_metadataZ	state_msgr
r8r9r(r'Zuserdata_rawZvendordata_raw)rZnetwrrr�	_get_datas<
�
�&

�zDataSourceAliYun._get_datac
Cs�|dur|j}t�d�|j|i}d�|j|j�}ztj|||j	dd�}Wn6tj
y�}zt�d||�WYd}~dSd}~00|jS)z�Request new metadata API token.
        @param seconds: The lifetime of the token in seconds

        @return: The API token or None if unavailable.
        Nz!Refreshing Ecs metadata API tokenz{}/{}r?)ZheadersrErDz/Unable to get API token: %s raised exception %s)
r2r;rHr3rFrSr1rIZreadurlr4rLr<�contents)rZsecondsZrequest_headerZ	token_urlrP�errr�_refresh_api_token/s$

�
�z#DataSourceAliYun._refresh_api_tokenrfcCs@|j|ji}|j|vr|S|js4|��|_|js4iS|j|jiS)z�Return a dict of headers for accessing a url.

        If _api_token is unset on AWS, attempt to refresh the token via a PUT
        and then return the updated token header.
        )r3r2r1rMrkr)rrOZrequest_token_headerrrrrJJs�

zDataSourceAliYun._get_headerscCsDt|tj�r@|jr@|jdkr@|jdkr2t�d�n
t�d�|�dS)a2Fail quickly on proper AWS if IMDSv2 rejects API token request

        Guidance from Amazon is that if IMDSv2 had disabled token requests
        by returning a 403, or cloud-init malformed requests resulting in
        other 40X errors, we want the datasource detection to fail quickly
        without retries as those symptoms will likely not be resolved by
        retries.

        Exceptions such as requests.ConnectionError due to IMDS being
        temporarily unroutable or unavailable will still retry due to the
        callsite wait_for_url.
        i�i�zLEcs IMDS endpoint returned a 403 error. HTTP endpoint is disabled. Aborting.z2Fatal error while requesting Ecs IMDSv2 API tokensN)r8rIrLr_r;r<r`rrrrK_s

��z#DataSourceAliYun._imds_exception_cb)FFF)N)rf)N)-�__name__�
__module__�__qualname__r-rRr\rr�str�__annotations__Zurl_max_waitZurl_timeoutrMrr6rrr9rg�propertyrr�intr%r)r+r/r0r1r2r4r3r>rQrUr^rYrdrZr[rhrkrJrK�
__classcell__rrr!rrsN








2@!

rcCst�d�tkS)Nzsystem-product-name)rZ
read_dmi_data�ALIYUN_PRODUCTrrrrr,{sr,cCs�g}|��D]|\}}t|t�r.|�|���qt|t�rD|�|�qt|t�r|�dg�}t|t�rt|�|���qt|t�r|�|�q|S)Nzopenssh-key)	�itemsr8rorG�strip�list�extendr9r()Zpublic_keys�keysZ_key_idZkey_body�keyrrrr*s




r*c@seZdZdZdZdS)�DataSourceAliYunLocalayDatasource run at init-local which sets up network to query metadata.

    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)rlrmrn�__doc__rgrrrrr{�sr{cCst�|t�Sr)rZlist_from_depends�datasources)Zdependsrrr�get_datasource_list�sr~)$rZlogging�typingrrZ	cloudinitrrrrIrZcloudinit.eventrr	Zcloudinit.net.dhcpr
Zcloudinit.net.ephemeralrZcloudinit.sourcesrZcloudinit.sources.helpersr
rZ	getLoggerrlr;rtZ
DataSourcerr,r*r{ZDEP_FILESYSTEMZDEP_NETWORKr}r~rrrr�<module>s,
h
�

Zerion Mini Shell 1.0