Current File : //usr/lib/python3/dist-packages/cloudinit/sources/__pycache__/DataSourceScaleway.cpython-312.pyc
�

Ӈg�:���ddlZddlZddlZddlZddlZddlmZddlZddlm	Z	ddl
mZddlm
Z
ddlmZmZmZmZmZddlmZmZddlmZdd	lmZmZdd
lmZddlmZej@e!�Z"dd
gZ#dZ$dZ%dZ&Gd�dejNjP�Z)d�Z*d�Z+Gd�dejX�Z-e-ej\ffgZ/d�Z0y)�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]���
c�*��eZdZdZ�fd�Zdd�Z�xZS)�SourceAddressAdapterzF
    Adapter for requests to choose the local address to bind to.
    c�:��||_tt|�di|��y)N�)�source_address�superr�__init__)�selfr�kwargs�	__class__s   ��F/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceScaleway.pyrzSourceAddressAdapter.__init__+s���,���
�"�D�2�<�V�<�c��tjtjtjdfgz}t||||j|��|_y)N�)�	num_pools�maxsize�blockr�socket_options)r�default_socket_options�socket�
SOL_SOCKET�SO_REUSEPORTrr�poolmanager)r�connectionsr%r&r's     r �init_poolmanagerz%SourceAddressAdapter.init_poolmanager/sR��'�>�>�
�
�
�� 3� 3�Q�7�B
�
��'�!����.�.�)�
��r!)F)�__name__�
__module__�__qualname__�__doc__rr.�
__classcell__�rs@r rr&s����=�

r!rc���	tj|d|d|d���}tj|j�S#tj
$r}|jdk(rYd}~y�d}~wwxYw)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.
    Nrc��|jdk7xr/t|jtjj
�S)N�)�code�
isinstance�cause�requests�
exceptionsr)�excs r �<lambda>z%query_data_api_once.<locals>.<lambda>Ws3��S�X�X��_�&��s�y�y�(�*=�*=�*M�*M�N�N�r!)�data�timeout�retries�session�exception_cbr7)r
�readurlr�
decode_binary�contents�UrlErrorr8)�api_addressr@�requests_session�respr=s     r �query_data_api_oncerK<sk�� ��!�!�����$��
���!�!�$�-�-�0�0�������8�8�s�?��
��	�s�:=�A*�A%�$A%�%A*c��tdt|d��D]�}	tjd||�t	j
�}d}	t
|�j}|}|ddk(r|dd}tj|dtj�	�dd}	|	tjk(rd
}|jdt||f���t|||�
�}
tjd|�|
cS�#t$rY�TwxYw#t j"$r9}tj%d||�t'j(d�|}Yd}~��6d}~wwxYw)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)r@rIz%s-data downloadedz%Error while trying to get %s data: %s�)�range�max�LOG�debugr;�Sessionr�netlocr)�getaddrinfo�IPPROTO_TCP�AF_INET6�
ValueError�mountrrKr
rG�warning�time�sleep)
�api_typerHrAr@�portrI�	localhost�url_address�address�
addr_protor?r=�last_excs
             r �query_data_apirfdsa���a��W�a��)�#��"	��I�I�<�h��
� (�/�/�1��!�I�
�&�{�3�:�:��%���q�>�S�(�)�!�B�/�G�#�/�/��T��);�);������
�����0� %�I�
�"�"��$�Y��4E�F�
�'��W�?O��D�
�I�I�*�H�5��K�9#�L�N��)�
��
���"�"�	��K�K�?��3�O��J�J�q�M��H���	�s=�-D�	A%C7�.AD�7	D�D�D�D�E�.E
�
Ec�
��eZdZdZej
ejejejhiZ
�fd�Zdeddf�fd�Z
d�Zd�Zed	��Zd
�Zed��Zed��Zd
�Zd�Zdd�Zed��Zed��Z�xZS)�DataSourceScaleway�Scalewayc���tt|�|||�tjtj
|ddgi�g�|_t|jjdt��|_
t|jjdt��|_t|jjdt��|_tj |_t$|_d|_d|_d|_d|_d|_d|jj3�vr#|xj&|jdz
c_yy)N�
datasourcerirAr@�max_waitT�
metadata_urls)rrhrr�
mergemanydict�get_cfg_by_path�ds_cfg�int�get�DEF_MD_RETRIESrA�DEF_MD_TIMEOUTr@�DEF_MD_MAX_WAITrlr	�UNSET�_network_config�DS_BASE_URLSrm�metadata_url�userdata_url�vendordata_url�ephemeral_fixed_address�has_ipv4�keys)r�sys_cfg�distro�pathsrs    �r rzDataSourceScaleway.__init__�s���
� �$�0��&�%�H��(�(��$�$�W�|�Z�.H�"�M�
�
����4�;�;�?�?�9�n�E�F����4�;�;�?�?�9�n�E�F����D�K�K�O�O�J��H�I��
�&�}�}���)��� ��� ���"���'+��$���
��d�k�k�.�.�0�0����$�+�+�o�">�>��1r!�ci_pkl_version�returnNc���t�|�|�ddttddd�}|D]}t	||�r�t||||��!y)NT)r|r}rlrmrzr{)r�	_unpicklerurx�hasattr�setattr)rr��
attr_defaults�attrrs    �r r�zDataSourceScaleway._unpickle�sU���
���.�)�'+��'�)� �"�

�
�"�	9�D��4��&���d�M�$�$7�8�	9r!c�n�tj�}tj||j|j
d��\}}|r5tjd|�|�d�|_|�d�|_	|�d�|_
ytjd|ttj�|z
��t�)	zO
        Define metadata_url based upon api-metadata URL availability.
        F)�urlsrlr@�connect_synchronouslyz%s is reachablez/conf?format=jsonz/user_data/cloud-initz/vendor_data/cloud-initNz3Unable to reach api-metadata at %s after %s seconds)
r]�	monotonicr
�wait_for_urlrlr@rSrTryrzr{rqr)rr��
start_time�	avail_url�_s     r �_set_metadata_urlz$DataSourceScaleway._set_metadata_url�s���
�^�^�%�
�!�.�.���]�]��L�L�"'�	
��	�1���I�I�'��3�#,�+�->� ?�D��#,�+�-B� C�D��%.�K�/F�"G�D����I�I�E���D�N�N�$�z�1�2�
�
"�!r!c��tj|j|j|j��}tjtj|j��|_
td|j|j|j�|_
td|j|j|j�|_y)N)r@rAz	user-datazvendor-data)r
rDryr@rA�json�loadsrrErF�metadatarfrz�userdata_rawr{�vendordata_raw)rrJs  r �_crawl_metadataz"DataSourceScaleway._crawl_metadata�s����!�!����t�|�|�T�\�\�
���
�
�4�#5�#5�d�m�m�#D�E��
�*���*�*�D�L�L�$�,�,�
���-��4�.�.����d�l�l�
��r!c��tjd�}|dk(rytjj	d�rytj�}d|vryy)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-manufacturerriTz/var/run/scaleway�scalewayN)r�
read_dmi_data�os�path�existsr�get_cmdline)�vendor_name�cmdlines  r �	ds_detectzDataSourceScaleway.ds_detect�sR���'�'�(=�>���*�$��
�7�7�>�>�-�.���"�"�$���� ��!r!c��|jr�	t|j|jj�5}t	j
d�5|j
|j�ddd�|j�|d|_	d|jd<ddd�|js�	t%|j|jj�5t	j
d�5|j
|j�ddd�|j�d|jd<ddd�yy#1swY��xYw#1swY��xYw#tttf$r4}tjt t#|��d|_Yd}~��d}~wwxYw#1swY��xYw#1swYyxYw#t$rYywxYw)	Nz7Setting api-metadata URL depending on IPv4 availabilityz
fixed-address�ipv4�
net_in_useFz7Setting api-metadata URL depending on IPv6 availability�ipv6T)r}rr��fallback_interfacer�Timedr�rmr�r|r�rrrr�logexcrS�strr)rr��es   r �	_get_datazDataSourceScaleway._get_datas���
�=�=�
&�%��K�K��K�K�2�2��9��$�*�*�9��C��.�.�t�/A�/A�B�	C�
�(�(�*�37��3H�D�0�28�D�M�M�,�/�9�.�}�}�

�)��K�K��K�K�2�2��
9�%�*�*�,��C��.�.�t�/A�/A�B�	C�
�(�(�*�28�D�M�M�,�/�
9��EC�C��	9�9��!��%��
&�
���C��Q��(�!&��
�
��
&��"C�C��	
9���#�
��
�s��*E�E	�D=�*1E	�E�0*F8�F,�0F �'F,�3F8�=E	�E	�	E�E�F�)*F�F� F)	�%F,�,F5�1F8�5F8�8	G�Gc	�R�|j�9tjdtj�tj|_|jtjk7r|jS|j
d��i}i}|j
dD]�}|d|jk(r3d|d<dddd	�}d
|j�vr|d
xx|gz
cc<�A|g|d
<�Hd|j�vr|dxx|d�d|d
��fz
cc<n|d�d|d
��f|d<|ddk(s��|ddd�}d
|j�vr|d
xx|gz
cc<��|g|d
<��|||jj<d|d�|_n�dd|jjzd�}ddig}|j
drI|dd|j
ddzd|j
dd
zddd|j
ddzd�gd�gz
}||d<d |gd!�|_tjd"|j�|jS)#z`
        Configure networking according to data received from the
        metadata API.
        z5Found None as cached _network_config. Resetting to %s�
private_ip�
public_ipsrcT�dhcp4z169.254.42.42/32z
62.210.0.1)zon-link�to�via�routes�	addresses�/�netmask�family�inet6�gatewayz::/0)r�r�r)�version�	ethernets�physicalz%s)�type�namer�r��staticz::�0)�network�prefixr�)r�rcr�r��subnetsr#)r��configznetwork_config : %s)rwrSr\r	rvr�r|r~r�r�rT)r�netcfg�ip_cfg�ip�router�s      r �network_configz!DataSourceScaleway.network_config4s������'��K�K�G��
�
�
�$+�=�=�D� ����7�=�=�0��'�'�'��=�=��&�.��F��F��m�m�L�1�
7���i�=�D�$@�$@�@�&*�F�7�O�$(�0�+��E�
 �6�;�;�=�0��x�(�U�G�3�(�,1�7��x�(�"�f�k�k�m�3��{�+�!�)�}�o�Q�r�)�}�o�>�0��+�
 "�)�}�o�Q�r�)�}�o�>�/��{�+��(�|�w�.�(*�9�
�V� D��#�v�{�{�}�4�"�8�,���7�,�05�w�F�8�,�9
7�:6<�F�4�;�;�1�1�2�/0�v�#F�D� �#��t�{�{�=�=�=��F���(�)�G��}�}�V�$�� (�#'�$�-�-��*?�	�*J�#J�#'�$�-�-��*?�	�*J�#J�,0�*-�+/�"&�-�-��"7�	�"B�,C��#�	����!(�F�9��/0�V�H�#E�D� ��	�	�'��)=�)=�>��#�#�#r!c��y�Nr�rs r �launch_indexzDataSourceScaleway.launch_index����r!c� �|jdS)N�id)r�r�s r �get_instance_idz"DataSourceScaleway.get_instance_id�s���}�}�T�"�"r!c��|jdD�cgc]}|d��	}}d}t|�}|jjdg�D]8}|j|�s�|j	||djdd���:|Scc}w)N�ssh_public_keys�keyzAUTHORIZED_KEY=�tagsr�� )r��lenrr�
startswith�append�replace)rr��ssh_keys�akeypre�plen�tags      r �get_public_ssh_keysz&DataSourceScaleway.get_public_ssh_keys�s���*.�-�-�8I�*J�K�3�C��J�K��K�#���7�|���=�=�$�$�V�R�0�	:�C��>�>�'�*���O�O�C���J�.�.�s�C�8�9�	:�
���Ls�Bc�4�t|jdd�S)N�hostnameF)rr�)r�fqdn�
resolve_ip�
metadata_onlys    r �get_hostnamezDataSourceScaleway.get_hostname�s��!�$�-�-�
�";�U�C�Cr!c��yr�rr�s r �availability_zonez$DataSourceScaleway.availability_zone�r�r!c��yr�rr�s r �regionzDataSourceScaleway.region�r�r!)FFF)r/r0r1�dsnamer�NETWORKr
�BOOT_NEW_INSTANCE�BOOT�BOOT_LEGACY�default_update_eventsrrqr�r�r��staticmethodr�r��propertyr�r�r�r�r�r�r�r3r4s@r rhrh�s����
�F�����'�'��N�N��!�!�
���?�,9��9��9�"�4
�����(0�d�M$��M$�^����#�
�D��������r!rhc�6�tj|t�Sr�)r	�list_from_depends�datasources)�dependss r �get_datasource_listr��s���$�$�W�k�:�:r!)1r��loggingr�r)r]�urllib.parserr;�requests.exceptionsr�urllib3.connectionr�urllib3.poolmanagerr�	cloudinitrrr	r
r�cloudinit.eventrr
�cloudinit.net.dhcpr�cloudinit.net.ephemeralrr�cloudinit.sourcesr�cloudinit.subpr�	getLoggerr/rSrxrsrurt�adapters�HTTPAdapterrrKrf�
DataSourcerh�DEP_FILESYSTEMr�r�rr!r �<module>rs�����	�
��!��/�.�+�A�A�1�/�I�0�0��g����!��&�(>�?��������
�8�,�,�8�8�
�,%�P1�hH��+�+�H�X�'�0�0�2�3���
;r!