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

S�g>���,�ddlZddlZddlZddlZddlZddlZddlZddlmZddl	m
Z
mZddlm
Z
mZmZmZmZmZmZddlmZmZmZmZmZmZmZddlmZddlmZddlm Z dd	l!m"Z"dd
l#m$Z$m%Z%ddl&m'Z'ddl(m)Z)dd
l*m+Z+ddl,m-Z-dZ.dZ/dZ0dZ1e.e/e0gZ2dZ3dZ4dZ5dZ6dZ7dZ8dZ9dZ:ejve<�Z=dd�fdd�fdd �fd!�Z>eGd"�d#e
��Z?Gd$�d%e
�Z@Gd&�d'eA�ZBGd(�d)eA�ZCGd*�d+eA�ZDdId,�ZEe7fd-�ZFed.gd/��ZGed0d1d2g�ZHGd3�d4e�ZIGd5�de+ej��6�ZKd7�ZLd8eeKeMffd9�ZNd:�ZO	dJd;eMd8ePfd<�ZQd=�ZRdKd>�ZSGd?�d@eT�ZUdA�ZVdBeKdCeMd8ePfdD�ZWdCeMd8eeKfdE�ZXd8eMfdF�ZYdGeMd8eMfdH�ZZy)L�N)�
namedtuple)�Enum�unique)�Any�Dict�List�
NamedTuple�Optional�Tuple�Union)�
atomic_helper�dmi�importer�	lifecycle�net�performance�
type_utils)�	user_data)�util)�
write_json)�Distro)�
EventScope�	EventType)�launch_index)�Paths)�CloudInitPickleMixin)�events�disabled�localr�pass�
FILESYSTEM�NETWORK�
DataSourcez|EXPERIMENTAL: The structure and format of content scoped under the 'ds' key may change in subsequent releases of cloud-init.zredacted for non-root user�
cloud-name�_unset�unknownz	aws-chinac��|dk(S�N�aws���cs �</usr/lib/python3/dist-packages/cloudinit/sources/__init__.py�<lambda>r.Gs
��1��:��zaws-govc��|dk(Sr(r*r+s r-r.r.Hs
��Q�%�Z�r/zazure-chinac��|dk(S)N�azurer*r+s r-r.r.Is
��q�G�|�r/)zcn-zus-gov-�chinac�0�eZdZdZdZdZdZdZdZde	fd�Z
y	)
�NetworkConfigSourcezb
    Represents the canonical list of network config sources that cloud-init
    knows about.
    �cmdline�ds�
system_cfg�fallback�	initramfs�returnc��|jS�N��value��selfs r-�__str__zNetworkConfigSource.__str__Z����z�z�r/N)�__name__�
__module__�__qualname__�__doc__�CMD_LINE�DS�
SYSTEM_CFG�FALLBACK�	INITRAMFS�strrBr*r/r-r5r5Ms.���
�H�	
�B��J��H��I���r/r5c�$�eZdZdZdZdZdefd�Zy)�NicOrderzRepresents ways to sort NICs�mac�nic_namer;c��|jSr=r>r@s r-rBzNicOrder.__str__drCr/N)rDrErFrG�MAC�NIC_NAMErMrBr*r/r-rOrO^s��&�
�C��H���r/rOc��eZdZdZy)�DatasourceUnpickleUserDataErrorzERaised when userdata is unable to be unpickled due to python upgradesN�rDrErFrGr*r/r-rVrVhs��Or/rVc��eZdZy)�DataSourceNotFoundExceptionN�rDrErFr*r/r-rYrYl���r/rYc��eZdZdZy)�InvalidMetaDataExceptionz8Raised when metadata is broken, unavailable or disabled.NrWr*r/r-r]r]ps��Br/r]c�x�tj|�}g}g}|j�D]�\}}|r	|dz|z}n|}|j�|vs|j�|vr|j	|�t|t�r7|jd�r&|j	|�|jdd�||<t|t�s��t|||�}	|j|	jd��|j|	jd��|	||<��t|�|d<t|�|d<|S)z�Process all instance metadata cleaning it up for persisting as json.

    Strip ci-b64 prefix and catalog any 'base64_encoded_keys' as a list

    @return Dict copy of processed metadata.
    �/zci-b64:��base64_encoded_keys�sensitive_keys)�copy�deepcopy�items�lower�append�
isinstancerM�
startswith�replace�dict�process_instance_metadata�extend�pop�sorted)
�metadata�key_pathrb�md_copyra�	sens_keys�key�val�sub_key_path�
return_vals
          r-rlrlts/���m�m�H�%�G����I��N�N�$�&���S��#�c�>�C�/�L��L��I�I�K�>�)��!�!�#�~�5����\�*��c�3��C�N�N�9�$=��&�&�|�4��;�;�y�"�5�G�C�L��c�4� �2��\�>��J�
 �&�&�z�~�~�6K�'L�M����Z�^�^�,<�=�>�%�G�C�L�'&�(&,�,?�%@�G�!�"� &�y� 1�G����Nr/c��|jdg�s|Stj|�}|jd�D]M}|jd�}|}|D])}||vs�t	||t
�s�||dk7s�%||}�+|vs�I|||<�O|S)z�Redact any sensitive keys from to provided metadata dictionary.

    Replace any keys values listed in 'sensitive_keys' with redact_value.
    rbr_���)�getrcrd�splitrhrk)rp�redact_valuerrrq�
path_parts�obj�paths       r-�redact_sensitive_keysr��s����<�<�(�"�-����m�m�H�%�G��L�L�!1�2�%���^�^�C�(�
����	 �D�����s�4�y�$�/��J�r�N�*��$�i��
	 ��3�;�$�C��I�%��Nr/�	URLParams)�max_wait_seconds�timeout_seconds�num_retries�sec_between_retries�DataSourceHostname�hostname�
is_defaultc�0�eZdZUdZeed<eed<eed<y)�HotplugRetrySettingsz
in seconds�force_retry�sleep_period�sleep_totalN)rDrErFrG�bool�__annotations__�intr*r/r-r�r��s��������r/r�c	���eZdZUeZdZdZdZee	e
d<dZdZdZ
eeee	fe
d<ej"ej$ej&ej(fZeedfe
d<dZd	Zd
ZdZej8ej<ej>ej@ejBhiZ"ej8ej<hiZ#de$fd
e$fdifddddddf	Z%eee	e&fdfe
d<dZ'dZ(ee	dfe
d<dZ)e*ddd�Z+dZ,ee	e
d<dZ-dOde.de/fd�Z0de1ddfd �Z2d!�Z3de4fd"�Z5de4fd#�Z6d$�Z7d%�Z8dPd&�Z9e:jvd'd(�)�de4fd*��Z<dQd+�Z=de4fd,�Z>d-�Z?dRd.�Z@d/�ZAd0�ZBeCd1��ZDeCd2��ZEd3�ZFeCd4��ZGd5�ZHeCd6��ZId7�ZJd8�ZKd9�ZLd:�ZMd;�ZNd<�ZOd=�ZPd>�ZQd?�ZRd@�ZSeCdA��ZTeCdB��ZUdC�ZVdSdD�ZWdE�ZXdFeYefdG�ZZdFeYede4fdH�Z[dI�Z\dJ�Z]e^dTdK��Z_eCdL��Z`dM�ZadN�Zby)Ur#zen_US.UTF-8�_undefN�_cloud_name�_crawled_metadata.�network_config_sourcesry�
���ec2_metadata�network_jsonrp)�userdataN)�userdata_rawN)�
vendordataN)�vendordata_rawN)�vendordata2N)�vendordata2_rawN�cached_attr_defaultsF)
�combined_cloud_config�
merged_cfg�merged_system_cfgzsecurity-credentialsr��	user-datarr��vendor-datazds/vendor_data�sensitive_metadata_keysr�extra_hotplug_udev_rules�distro�pathsc��||_||_||_d|_i|_d|_d|_d|_d|_d|_	d|_
t|_t|_
tj|jd|j fi�|_|j"si|_|s%t%j&|j�|_y||_y)N�
datasource)�sys_cfgr�r�r�rpr�r�r�r�r��metadata_address�UNSETr�r�r�get_cfg_by_path�dsname�ds_cfg�ud�UserDataProcessor�ud_proc)rAr�r�r�r�s     r-�__init__zDataSource.__init__Ts�����������
�'+��
� ��
�9=���������"���#���/3���+0���!����*�*��L�L�<����5�r�
����{�{��D�K���/�/��
�
�;�D�L�"�D�Lr/�ci_pkl_versionr;c��dddtddtdddtddd�d�}|j�D]\}}t||�r�t	|||��!t|d�st	|dd��t|d�r$|j
�	t
|j
�yyy#t$r&}tjd|�t�|�d}~wwxYw)	z(Perform deserialization fixes for Paths.NFr)r��_platform_type�_subplatformr�r�r�r��skip_hotplug_detectr�r��hotplug_retry_settings�check_if_fallback_is_allowedc��y�NFr*r*r/r-r.z&DataSource._unpickle.<locals>.<lambda>���r/r�z:Unable to unpickle datasource: %s. Ignoring current cache.)r�r�re�hasattr�setattrr�rM�AttributeError�LOG�debugrV)rAr��expected_attrsrtr?�es      r-�	_unpicklezDataSource._unpicklens���"&�"� �!�(,� $�!�#(��#�&:�5�!�Q�&G�
��)�.�.�0�	*�J�C���4��%���c�5�)�	*��t�;�<��D�8�-�H��4��$����)B�
?��D�M�M�"�*C�$��"�
?��	�	�/���
6�7�Q�>��

?�s�B�	C�(!C	�	Cc�,�tj|�Sr=)r�obj_namer@s r-rBzDataSource.__str__�s���"�"�4�(�(r/c��y)z#Check if running on this datasourceTr*r@s r-�	ds_detectzDataSource.ds_detect����r/c��|jj�t�j�k(rtj	d|�y|j
j
dg�|jgk(rtj	d|�yy)aJOverride if either:
        - only a single datasource defined (nothing to fall back to)
        - command line argument is used (ci.ds=OpenStack)

        Note: get_cmdline() is required for the general case - when ds-identify
        does not run, _something_ needs to detect the kernel command line
        definition.
        z6Kernel command line set to use a single datasource %s.T�datasource_listz2Datasource list set to use a single datasource %s.F)r�rf�
parse_cmdliner�r�r�rzr@s r-�override_ds_detectzDataSource.override_ds_detect�st���;�;����-�/�"7�"7�"9�9��I�I�H��
��
�\�\�
�
�/��
4����
�
E��I�I�D�d�
��r/c���|j�r|j�S|j�r&tj	d|�|j�Stj	d|�y)z&Overrides runtime datasource detectionzDetected %szDid not detect %sF)r��	_get_datar�r�r�r@s r-�_check_and_get_datazDataSource._check_and_get_data�sX���"�"�$��>�>�#�#�
�^�^�
��I�I���
��>�>�#�#��I�I�)�4�0�r/c��|j�j}|j�}|j}|d}diddg�d|�d|�dt	|j
|j|j��d|j
�d	|j
�d
|dd�d
|dd�d|dd�d|j�d|j��d|d�d|�d|�d|dd�d|�d|�|dd|j|j|d|dd��iS)z2Return a dictionary of standardized metadata keys.�sys_info�v1�
_beta_keys�subplatform�availability-zone�availability_zone�cloud_idr$�
cloud_namer��distr�distro_versionr��distro_release��platform�public_ssh_keys�python_version�python�instance-id�instance_id�kernel_release�uname�local-hostname�local_hostname��variant)�machine�regionr��system_platformr�)
�get_hostnamer��get_instance_idr��canonical_cloud_idr�r��
platform_type�get_public_ssh_keysr�)rA�
instance_datar�r�r��sysinfos      r-�_get_standardized_metadataz%DataSource._get_standardized_metadata�s����*�*�,�5�5���*�*�,�� �2�2�� �
�+�����}�o��#�%6��$�%6���.��O�O�T�[�[�$�2D�2D��	��d�o�o���d�o�o���'�&�/�!�,��!�'�&�/�!�"4��!�'�&�/�!�"4���D�.�.��"�4�#;�#;�#=��!�'�(�"3���{�� �{�!�"!�'�'�"2�1�"5�#�$!�.�%�&!�.�'�(#�7�+�A�.��+�+�#�/�/�#*�:�#6�"�9�-�1�
�	
r/c��|jsy|r|}n|j}|D]\}}t||�s�t|||��!|sd|_yy)z�Reset any cached metadata attributes to datasource defaults.

        @param attr_defaults: Optional tuple of (attr, value) pairs to
           set instead of cached_attr_defaults.
        NF)�_dirty_cacher�r�r�)rA�
attr_defaults�attr_values�	attributer?s     r-�clear_cached_attrszDataSource.clear_cached_attrs�s_��� � ���'�K��3�3�K� +�	0��I�u��t�Y�'���i��/�	0�� %�D��r/zGetting metadata�always)�log_modec�\�d|_|j�}|s|S|j�|S)z�Datasources implement _get_data to setup metadata and userdata_raw.

        Minimally, the datasource should return a boolean True on success.
        T)r�r��persist_instance_data)rA�return_values  r-�get_datazDataSource.get_data�s7��!����/�/�1������"�"�$��r/c���|rXtjj|jj�r%t||jj
d��|j�Htj|j�}|jdd�|jdd�d|i}nZdd|jii}|jtk7r|j|dd<|jtk7r|j|dd<t|dd	<tj|j �|d
<d|d
d	<tj|d
�|d<d
|dd	<t#j$�|d<|j'|j)|��	t+j,|�}t/t1j2|�|j4��}|jjAd�}|djCdd�}tjjE|jjFd�}	t#jH|	�d|��|�d��d}
|	�d|��}tjjK|	�rtjjM|	�}
t#jN||	d��|
r|
|k7rt#jP|
�tS||d��|jjAd�}tS|tU|��y#t6$r)}t8j;dt=|��Yd}~yd}~wt>$r)}t8j;dt=|��Yd}~yd}~wwxYw)aPProcess and write INSTANCE_JSON_FILE with all instance metadata.

        Replace any hyphens with underscores in key names for use in template
        processing.

        :param write_cache: boolean set True to persist obj.pkl when
            instance_link exists.

        @return True on successful write, False otherwise.
        �obj_pklNr�r�r7�	meta_datar�r��_docr�z<DEPRECATED: Use merged_system_cfg. Will be dropped from 24.1r�zUMerged cloud-init system config from /etc/cloud/cloud.cfg and /etc/cloud/cloud.cfg.d/r�)rbz'Error persisting instance-data.json: %sF�instance_data_sensitiver�r��nonezcloud-id�-�
T)�forcei�)�moder�)+�osr�lexistsr��
instance_link�	pkl_store�
get_ipath_curr�rcrdrnrpr�r�r��EXPERIMENTAL_TEXTr�r�system_info�updater�r
�
json_dumpsrl�json�loadsr��	TypeErrorr��warningrM�UnicodeDecodeError�get_runpathrz�join�run_dir�
write_file�exists�realpath�sym_link�del_filerr�)
rA�write_cache�crawled_metadatar��content�processed_datar��json_sensitive_filer��
cloud_id_file�prev_cloud_id_file�new_cloud_id_file�	json_files
             r-rz DataSource.persist_instance_data
s���2�7�7�?�?�4�:�:�+C�+C�D��d�D�J�J�4�4�Y�?�@��!�!�-� $�}�}�T�-C�-C�D��� � ��d�3�� � ���5�!�#3�4�M�!�K����#?�@�M�� � �E�)�6:�6G�6G�
�d�#�N�3�� � �E�)�6:�6G�6G�
�d�#�N�3�&7�
�d��F�#�&*�m�m�D�L�L�&A�
�l�#�
K�	�l�#��	
�.2�]�]��,�'�.
�
�)�*�
'�	�)�*�6�2�%)�$4�$4�$6�
�j�!����T�<�<�]�K�L�
	�#�.�.�}�=�G�6��
�
�7�#�#�;�;��N�#�j�j�4�4�5N�O�� ��&�*�*�:�v�>�������T�Z�Z�%7�%7��D�
����=�/��8�*�5�(��2��G�!��,�o�Q�x�j�9��
�7�7�>�>�-�(�!#���!1�!1�-�!@���
�
�'��d�C��"4�8I�"I��M�M�,�-��&��U�C��J�J�*�*�?�;�	��9�3�N�C�D���1�	��K�K�A�3�q�6�J���!�	��K�K�A�3�q�6�J���	�s$�?L�	M7�M�M7�M2�2M7c��td��)z@Walk metadata sources, process crawled data and save attributes.zlSubclasses of DataSource must implement _get_data which sets self.metadata, vendordata_raw and userdata_raw.)�NotImplementedErrorr@s r-r�zDataSource._get_dataVs��!�
D�
�	
r/c	�R�|j}	t|jjd|j��}|j}	tdt|jjd|j���}|j}	t|jjd|j��}|j}	t|jjd|j��}t||||�S#t$r9tjtd|jjd�|�Y��
wxYw#t$rE|j}tjtd|jjd�|�Y��wxYw#t$r9tjtd|jjd�|�Y��!wxYw#t$r9tjtd	|jjd�|�Y��*wxYw)
z�Return the Datasource's preferred url_read parameters.

        Subclasses may override url_max_wait, url_timeout, url_retries.

        @return: A URLParams object with max_wait_seconds, timeout_seconds,
            num_retries.
        �max_waitz6Config max_wait '%s' is not an int, using default '%s'r�timeoutz5Config timeout '%s' is not an int, using default '%s'�retriesz5Config retries '%s' is not an int, using default '%s'r�zAConfig sec_between_retries '%s' is not an int, using default '%s')�url_max_waitr�r�rz�
ValueErrorr�logexcr��url_timeout�max�url_retries�	Exception�url_sec_between_retriesr�)rAr1r2r3r�s     r-�get_url_paramszDataSource.get_url_params]s����$�$��	��4�;�;�?�?�:�t�7H�7H�I�J�H��"�"��		��!�S�������D�<L�<L�!M�N�O�G��"�"��	��$�+�+�/�/�)�T�5E�5E�F�G�G�#�:�:��
	�"%������)�4�+G�+G��#����7�G�5H�I�I��_�	��K�K��H������
�+��	
�	���	��&�&�G��K�K��G������	�*��	
�	���	��K�K��G������	�*��	
�	���	��K�K��&������ 5�6�#�
�	�sH�/D	�
9E�/F�/G$�	>E�
E�A
F�F�>G!� G!�$>H&�%H&c���|j�.|jj|j��|_|r|j	|j�S|jSr=)r�r��process�get_userdata_raw�
_filter_xdata)rA�apply_filters  r-�get_userdatazDataSource.get_userdata�sM���=�=� � �L�L�0�0��1F�1F�1H�I�D�M���%�%�d�m�m�4�4��}�}�r/c��|j�.|jj|j��|_|jSr=)r�r�r>�get_vendordata_rawr@s r-�get_vendordatazDataSource.get_vendordata�s5���?�?�"�"�l�l�2�2�4�3J�3J�3L�M�D�O����r/c��|j�.|jj|j��|_|jSr=)r�r�r>�get_vendordata2_rawr@s r-�get_vendordata2zDataSource.get_vendordata2�s:�����#�#�|�|�3�3�D�4L�4L�4N�O�D�����r/c�p�|js|jj�|_|jSr=)r�r�rfr@s r-r�zDataSource.platform_type�s,���"�"�"&�+�+�"3�"3�"5�D���"�"�"r/c�\�|js|j�|_|jS)a�Return a string representing subplatform details for the datasource.

        This should be guidance for where the metadata is sourced.
        Examples of this on different clouds:
            ec2:       metadata (http://169.254.169.254)
            openstack: configdrive (/dev/path)
            openstack: metadata (http://169.254.169.254)
            nocloud:   seed-dir (/seed/dir/path)
            lxd:   nocloud (/seed/dir/path)
        )r��_get_subplatformr@s r-r�zDataSource.subplatform�s*��� � � $� 5� 5� 7�D��� � � r/c�F�|jrd|j�d�StS)z?Subclasses should implement to return a "slug (detail)" string.z
metadata (�))r��METADATA_UNKNOWNr@s r-rKzDataSource._get_subplatform�s'��� � ��� 5� 5�6�a�8�8��r/c�,�|jr|jS|jr�|jjt�r�|jjt�}t	|t
�r!|j
�|_|jS|j�j
�|_tjdtt|��|jS|j�j
�|_|jS)z�Return lowercase cloud name as determined by the datasource.

        Datasource can determine or define its own cloud product name in
        metadata.
        z5Ignoring metadata provided key %s: non-string type %s)r�rprz�METADATA_CLOUD_NAME_KEYrhrMrf�_get_cloud_namer�r��type)rAr�s  r-r�zDataSource.cloud_name�s�������#�#�#��=�=�T�]�]�.�.�/F�G����*�*�+B�C�J��*�c�*�#-�#3�#3�#5�� �����$(�#7�#7�#9�#?�#?�#A�� ��	�	�K�+���$������ $�3�3�5�;�;�=�D�����r/c��|jS)z�Return the datasource name as it frequently matches cloud name.

        Should be overridden in subclasses which can run on multiple
        cloud names, such as DatasourceEc2.
        )r�r@s r-rQzDataSource._get_cloud_name�s���{�{�r/c�X�|jsyd|jvr|jdSy)Nzlaunch-index)rpr@s r-rzDataSource.launch_index�s*���}�}���T�]�]�*��=�=��0�0�r/c��tjtj|j��g}|}|D]}|j	|�}�|Sr=)r�Filterr�safe_int�apply)rA�processed_ud�filters�new_ud�fs     r-r@zDataSource._filter_xdata�sN������
�
�d�.?�.?� @�A�
�����	%�A��W�W�V�_�F�	%��
r/c��|jSr=)r�r@s r-r?zDataSource.get_userdata_raw�s��� � � r/c��|jSr=)r�r@s r-rDzDataSource.get_vendordata_raw�s���"�"�"r/c��|jSr=)r�r@s r-rGzDataSource.get_vendordata2_raw�s���#�#�#r/c��iSr=r*r@s r-�get_config_objzDataSource.get_config_objs���	r/c�J�t|jjd��S)Nzpublic-keys)�normalize_pubkey_datarprzr@s r-r�zDataSource.get_public_ssh_keyss��$�T�]�]�%6�%6�}�%E�F�Fr/c��y)a5Publish the public SSH host keys (found in /etc/ssh/*.pub).

        @param hostkeys: List of host key tuples (key_type, key_value),
            where key_type is the first field in the public key file
            (e.g. 'ssh-rsa') and key_value is the key itself
            (e.g. 'AAAAB3NzaC1y...').
        Nr*)rA�hostkeyss  r-�publish_host_keyszDataSource.publish_host_keys	r�r/c���ddi}|j�D]V\}}|j|�s�|D]:}d|�|t|�d��}tjj|�s�6|ccS�Xy)N�sd)�vd�xvd�vtbz/dev/)reri�lenrrr!)rA�
short_name�mappings�nfrom�tlist�nto�cands       r-�
_remap_devicezDataSource._remap_devicesx���.�/��$�N�N�,�	 �L�E�5��(�(��/���
 ��&)�:�c�%�j�l�+C�D���7�7�>�>�$�'��K�
 �	 �r/c��yr=r*)rA�_names  r-�device_name_to_devicez DataSource.device_name_to_device"s��r/c�t�|j}	|jj�}|S#t$rY|SwxYw)z<Default locale is en_US.UTF-8, but allow distros to override)�default_localer��
get_localer/)rA�locales  r-ryzDataSource.get_locale*sD���$�$��	��[�[�+�+�-�F��
��#�	���
�	�s�*�	7�7c���|jjd|jjd��}|r|S|jjdi�jd�S)Nr�r��	placement�rprz)rA�top_level_azs  r-r�zDataSource.availability_zone3sY���}�}�(�(�����!2�!2�3F�!G�
������}�}� � ��b�1�5�5�6I�J�Jr/c�8�|jjd�S)Nr�r}r@s r-r�zDataSource.region<s���}�}� � ��*�*r/c�h�|jrd|jvryt|jd�S)Nr�ziid-datasource)rprMr@s r-r�zDataSource.get_instance_id@s+���}�}�
�T�]�]� B�#��4�=�=��/�0�0r/c�N�d}d}|}d}|jr|jjd�s�|rtd|�Sg}tj�}	|	dk(rd}tj
|	�}
|
r/|
j
d�dkDrt|
�jd�}n�|	r/|	j
d�dkDrt|	�jd�}n�|	r|	|g}n�||g}n�|jd}tj|�rMg}|rtj|�}|rt|�jd�}n(d	|jdd
�zg}n|jd�}t|�dkDr|d}	dj|dd�}n|d}	|r||k7r|	�d|��}	t|	|�S)a�Get hostname or fqdn from the datasource. Look it up if desired.

        @param fqdn: Boolean, set True to return hostname with domain.
        @param resolve_ip: Boolean, set True to attempt to resolve an ipv4
            address provided in local-hostname meta-data.
        @param metadata_only: Boolean, set True to avoid looking up hostname
            if meta-data doesn't have local-hostname present.

        @return: a DataSourceHostname namedtuple
            <hostname or qualified hostname>, <is_default> (str, bool).
            is_default is a bool and
            it's true only if hostname is localhost and was
            returned by util.get_hostname() as a default.
            This is used to differentiate with a user-defined
            localhost hostname.
            Optionally return (None, False) when
            metadata_only is True and local-hostname data is not available.
        �localdomain�	localhostFr�NT�.rzip-%srr�)rprzr�rr��get_fqdn_from_hosts�findrMr{r�is_ipv4_address�
gethostbyaddrrjrlr)rA�fqdn�
resolve_ip�
metadata_only�	defdomain�defhost�domainr��toksr��
hosts_fqdn�lhosts            r-r�zDataSource.get_hostnameFs���&"�	������
��}�}�D�M�M�$5�$5�6F�$G��)�$�
�;�;�
�D��(�(�*�H��;�&�!�
��1�1�(�;�J��j�o�o�c�2�Q�6��:��,�,�S�1���h�m�m�C�0�1�4��8�}�*�*�3�/��� �)�,����+���M�M�"2�3�E��"�"�5�)�����-�-�e�4�D���t�9�?�?�3�/�D�#�e�m�m�C��&=�=�>�D��{�{�3�'���t�9�q�=��A�w�H��X�X�d�1�2�h�'�F��A�w�H��F�i�'�"*�F�3�H�!�(�J�7�7r/c�:�|jj|��S)N)�data_source)r��get_package_mirror_infor@s r-r�z"DataSource.get_package_mirror_info�s���{�{�2�2�t�2�D�Dr/�source_event_typesc���i}|D][}|jj�D]<\}}||vs�|j|�s
t�||<||j	|��>�]|Sr=)�supported_update_eventsrerz�set�add)rAr��supported_events�event�update_scope�
update_eventss      r-�get_supported_eventszDataSource.get_supported_events�s~��24��'�	>�E��-�-�3�3�5�
>�����M�)�+�/�/��=�9<��(��6�$�\�2�6�6�u�=�
>�	>� �r/c��|j|�}|j�D]h\}}tjd|jdj|D�cgc]}|j��c}��|j
d|ztff��j|r#|j
�|j�}|rytjd|dj|D�cgc]}|j��c}��ycc}wcc}w)a�Refresh cached metadata if the datasource supports this event.

        The datasource has a list of supported_update_events which
        trigger refreshing all cached metadata as well as refreshing the
        network configuration.

        @param source_event_types: List of EventTypes which may trigger a
            metadata update.

        @return True if the datasource did successfully update cached metadata
            due to source_event_type.
        z:Update datasource metadata and %s config due to events: %s�, z
_%s_configTz(Datasource %s not updated for events: %sF)	r�rer�r�r?rr�r�r)rAr�r��scope�matched_eventsr��results       r-�update_metadata_if_supportedz'DataSource.update_metadata_if_supported�s��� �4�4�5G�H��%5�%;�%;�%=�		F�!�E�>��I�I�L�����	�	�N�C�5�5�;�;�C�D�
�
�#�#�l�U�&:�E�%B�$D�E�		F���#�#�%��]�]�_�F����	�	�6���I�I�0B�C�u�u�{�{�C�D�	
�
��!D��Ds�C1�C6c��yr�r*)rAr�s  r-�check_instance_idzDataSource.check_instance_id�s��r/c��y)acheck_if_fallback_is_allowed()
        Checks if a cached ds is allowed to be restored when no valid ds is
        found in local mode by checking instance-id and searching valid data
        through ds list.

        @return True if a ds allows fallback, False otherwise.
        Fr*r@s r-r�z'DataSource.check_if_fallback_is_allowed�s��r/c�~�|�t}|�t}|D]'}|��||vr|cStjd||�|cS|S)Nz%invalid dsmode '%s', using default=%s)�DSMODE_NETWORK�
VALID_DSMODESr�r)�
candidates�default�valid�	candidates    r-�_determine_dsmodezDataSource._determine_dsmode�s_���?�$�G��=�!�E�#�		�I�� ���E�!� � ����;�Y�����		��r/c��yr=r*r@s r-�network_configzDataSource.network_config�r�r/c��y)a(setup(is_new_instance)

        This is called before user-data and vendor-data have been processed.

        Unless the datasource has set mode to 'local', then networking
        per 'fallback' or per 'network_config' will have been written and
        brought up the OS at this point.
        Nr*)rA�is_new_instances  r-�setupzDataSource.setup�s��	r/c��y)a�activate(cfg, is_new_instance)

        This is called before the init_modules will be called but after
        the user-data and vendor-data have been fully processed.

        The cfg is fully up to date config, it contains a merged view of
           system config, datasource config, user config, vendor config.
        It should be used rather than the sys_cfg passed to __init__.

        is_new_instance is a boolean indicating if this is a new instance.
        Nr*)rA�cfgr�s   r-�activatezDataSource.activate�s��	r/r=)r*�T)F)FFF)NN)crDrErFr��dsmoderxr�r�r
rMr�r�r�r�rrr5rHrLrJrIr�rr4r7r9r;rr"r�BOOT_NEW_INSTANCE�BOOT�BOOT_LEGACY�HOTPLUGr��default_update_eventsr�r�rr�r�r�r�r�r��_ci_pkl_versionrrr�r�r�rBr�r�r�r�r�r�r�timedrrr�r<rBrErH�propertyr�r�rKr�rQrr@r?rDrGrar�rfrsrvryr�r�r�r�r�rr�r�r�r��staticmethodr�r�r�r�r*r/r-r#r#�sn��
�F�"�N��F�"&�K��#��%��N�
�L�48��x��d�C�i� 0�1�8�	�$�$��%�%��&�&����	?��E�"5�s�":�;���L��K��K���@	����'�'��N�N��!�!����	
���	����'�'�
���
���	���	�R����� ��!�
9��%��c�3�h��� 4�5�
��L�
0��U�3��8�_�
�" ��2�%��A�>��/3��h�s�m�2��O�#��#�u�#�4#?��#?��#?�J)��4���D��,�$
�L&�&�[���)�H�=��$��>��$J�X
�4�
�:J�x��
 �
�#��#�
�
!��
!� �� �� �.������!�#�$��G��� ���K��K��+��+�1�F8�PE� �t�I�� �$�"&�y�/�$�	
�$�L������*����	�r/)�	metaclassc�n�g}|s|St|t�r|j�St|ttf�rt|�St|t
�r]|j
�D]J\}}t|t�r|g}t|ttf�s�0|D]}|s�|j|���L|Sr=)rhrM�
splitlines�listr�rkrerg)�pubkey_data�keys�_keyname�klist�pkeys     r-rcrcs���
�D�����+�s�#��%�%�'�'��+��c�{�+��K� � ��+��&�*�0�0�2�	*�O�H�e��%��%�����%�$���-�!�*�D�����D�)�	*�	*��Kr/r;c	���t|||�}|D�cgc]}tj|���}	}t|vrdnd}
tjd|
|	�t
|	|�D]�\}}tjd|jdd�zd|
�d|��d	|
�d
|��|��}
	|
5tjd|�||||�}|jtjg�r/d
|
�d|��|
_
|tj|�fcddd�cS	ddd���ddj#|	�z}t%|��cc}w#1swY�/xYw#t$rtj td|�Y��wxYw)N�networkrz#Searching for %s data source in: %sz	search-%sr#r`zsearching for z data from zno z data found from )�name�description�message�parentz%Seeing if we can get any data from %szfound zGetting data from %s failedz4Did not find any data source, searched classes: (%s)r�)�list_sourcesrr��DEP_NETWORKr�r��zipr�ReportEventStackrjr�rr�r�r:rr6rrY)r�r�r��ds_deps�cfg_list�pkg_list�reporter�ds_listr\�ds_namesrr��cls�myrep�s�msgs                r-�find_sourcer�#sz���8�W�h�7�G�07�8�1�
�#�#�A�&�8�H�8�#�w�.�9�G�D��I�I�3�T�8�D���7�+�A�	��c��'�'��t�|�|�L�"�=�=�;?��F�26��=��	
��
	A��
9��	�	�A�3�G�����/���1�1��0�0�1��@D�T�$J�E�M��z�2�2�3�7�8�
9�
9��
9��A�&A�4�9�9��D��C�&�c�
*�*��59�
9�
9���	A��K�K��:�C�@�	A�s6�D:�E� A$D?�	E�E�?E	�E�$E3�2E3c�b�g}tjd|||�|D]�}tj|�}tj||dg�\}}|stjd|�|D]@}tj|�}	t|	d�}
|
|�}|s�/|j|�����|S)z�Return a list of classes that have the same depends as 'depends'
    iterate through cfg_list, loading "DataSource*" modules
    and calling their "get_datasource_list".
    Return an ordered list of classes that match (if any)
    zLLooking for data source in: %s, via packages %s that matches dependencies %s�get_datasource_listzDCould not import %s. Does the DataSource exist and is it importable?)	r�r�r�"match_case_insensitive_module_name�find_module�error�
import_module�getattrrm)r��dependsr��src_listr7�ds_name�m_locs�_looked_locs�m_loc�mod�lister�matchess            r-r�r�Ds����H��I�I�	8���������=�=�b�A��'�3�3��X� 5�6� 
������I�I�$��
�
�	�E��(�(��/�C��S�"7�8�F��W�o�G������(��
	��$�Or/�fieldc�z�|sytj|�}|sy|j�|j�k(Sr�)r�
read_dmi_datarf)r�r��	dmi_values   r-�instance_id_matches_system_uuidr�hs;��
���!�!�%�(�I�������)�/�/�"3�3�3r/c���|st}|st}|tk(r
|tk7r|S|Stj�D](\}}|\}}|j|�s�||�s�&|cS|tk7r|S|S)z@Lookup the canonical cloud-id for a given cloud_name and region.)rN�CLOUD_ID_REGION_PREFIX_MAPreri)r�r�r��prefix�
cloud_id_testr��valid_clouds       r-r�r�vs����%�
��!��
�!�!��)�)�����!;�!A�!A�!C����
�"/���;����V�$��Z�)@��O���%�%����Or/c��|syt|t�r|St|t�rtj|�St|t
�r+|durt
|jd�d��Std��tdt|�z��)aLdata: a loaded object (strings, arrays, dicts).
    return something suitable for cloudinit vendordata_raw.

    if data is:
       None: return None
       string: return string
       list: return data
             the list is then processed in UserDataProcessor
       dict: return convert_vendordata(data.get('cloud-init'))
    NTz
cloud-initF)�recursez'vendordata['cloud-init'] cannot be dictz$Unknown data type for vendordata: %s)
rhrMr�rcrdrk�convert_vendordatarzr5rR)�datar�s  r-r�r��sz�����$������$����}�}�T�"�"��$����d�?�%�d�h�h�|�&<�e�L�L��B�C�C�
�;�d�4�j�H�
I�Ir/c��eZdZy)�BrokenMetadataNrZr*r/r-rr�r[r/rc�t�g}t|�}|D]%\}}|t|�k(s�|j|��'|Sr=)r�rg)r�r��ret_list�depsetr��depss      r-�list_from_dependsr�sB���H�
��\�F��!�	��T��S��Y���O�O�C� �!��Or/r~�fnamec��	tj|�}	tj||dd��y#t$rtjt
d|�YywxYw#t$rtjt
d|�YywxYw)z[Use pickle to serialize Datasource to a file as a cache.

    :return: True on success
    zFailed pickling datasource %sF�wb�)�omoderz Failed pickling datasource to %sT)�pickle�dumpsr:rr6r�r )r~r�pk_contentss   r-rr�sz��
��l�l�3�'�������{�$�U�C��������C�8�#�>����
�����C�;�U�C���s �2�A�$A�A�$B�Bc�d�d}	tj|�}|sy	tj|�S#t$r@}tjj|�rtjd||�Yd}~�]d}~wwxYw#t$rYyt$rtjtd|�YywxYw)zBUse pickle to deserialize a instance Datasource from a cache file.Nzfailed loading pickle in %s: %sz#Failed loading pickled blob from %s)r�load_binary_filer:rr�isfiler�rrrrVr6)r�pickle_contentsr�s   r-�pkl_loadr�s����O�E��/�/��6������|�|�O�,�,���E�
�7�7�>�>�%� ��K�K�9�5�!�D���E��+��������C�>��F���s+�2�A>�	A;�6A6�6A;�>	B/�	#B/�.B/c�<�ttj��S)z�Check if command line argument for this datasource was passed
    Passing by command line overrides runtime datasource detection
    )�parse_cmdline_or_dmir�get_cmdliner*r/r-r�r��s�� �� 0� 0� 2�3�3r/�inputc�v�tjd|�}tjd|�}tjd|�}|xs|xs|}|xs|}|rA|jd�j�}t	j
d|�d|��dd|�d	��
�|r"|jd�r|jd�Sy)Nz(?:^|\s)ds=([^\s;]+)z(?:^|\s)ci\.ds=([^\s;]+)z (?:^|\s)ci\.datasource=([^\s;]+)r�z8Defining the datasource on the command line using ci.ds=z or ci.datasource=z23.2zUse ds=z instead)�
deprecated�deprecated_version�
extra_messager`)�re�search�group�stripr�	deprecate)r�
ds_parse_0�
ds_parse_1�
ds_parse_2r7rr�s       r-rr�s������2�E�:�J����6��>�J����>��F�J�	�	/�z�	/�Z�B��)�z�J���!�!�!�$�*�*�,��������!!�!'��*� &�#�F�8�8�4�	
�
�b�h�h�q�k��x�x��{��
r/)r`r*)zsystem-uuidr�)[�abcrcr�loggingrrr�collectionsr�enumrr�typingrrrr	r
rr�	cloudinitr
rrrrrrrr�r�cloudinit.atomic_helperr�cloudinit.distrosr�cloudinit.eventrr�cloudinit.filtersr�cloudinit.helpersr�cloudinit.persistencer�cloudinit.reportingr�DSMODE_DISABLED�DSMODE_LOCALr��DSMODE_PASSr��DEP_FILESYSTEMr��	DS_PREFIXr�REDACT_SENSITIVE_VALUErPr�rN�	getLoggerrDr�r�r5rOr:rVrYr]rlr�r�r�r��ABCMetar#rcrMr�r�r�r�r�r��IOErrorrrrrr�rr*r/r-�<module>r9s9������	�
�	�"��F�F�F����&��.�$�1�*�#�6�&��������� �,��?�
������	�<��6��'�������g����!��

�-�.��/�0��3�
4����
�$�
��
� �t��P�i�P�	�)�	�C�y�C� �F2H��:
���
�	� ��������:��w�%����w�t�:+�
�:�s�?��+�B!�J,�4��4�	�4��&J�0	�W�	���:��c��d��$�C��H�Z�0��*4�s�4�����r/