403Webshell
Server IP : 3.96.16.70  /  Your IP : 216.73.216.15
Web Server : Apache
System : Linux ip-172-31-26-103.ca-central-1.compute.internal 6.1.163-186.299.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Feb 24 16:35:42 UTC 2026 x86_64
User : ec2-user ( 1000)
PHP Version : 8.4.18
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : OFF
Directory :  /usr/lib/python3.9/site-packages/cloudinit/sources/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /usr/lib/python3.9/site-packages/cloudinit/sources/__pycache__/DataSourceAzure.cpython-39.pyc
a

�½b�S�@s�UddlZddlZddlZddlZddlZddlZddlmmZ	ddl
mZddlm
Z
mZddlmZmZmZmZddlmZddlZddlmZddlmZddlmZmZmZmZmZdd	lm Z m!Z!dd
l"m#Z#ddl$m%Z%m&Z&m'Z'm(Z(ddl)m*Z*dd
l+m,Z,ddl-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;ddl<m=Z=m>Z>m?Z?e�@eA�ZBdZCddiZDdZEdZFdZGdZHdZIdZJdZKdZLdZMdZNdZOdZPejQe?ePejRfd�ZSGd d!�d!e�ZTGd"d#�d#e�ZUd$ZVeeWeXd%<gd&�ZYd'd(gZZd)d*�Z[d+d,�Z\eWeWeeWd-�d.d/�Z]eWeWd0�d1d2�Z^e1eeWd3�d4d5��Z_d�eWd3�d6d7�Z`d8d9�Zad:d;�Zbd<d=�ZceeWd3�d>d?�Zde�e��r�d@ZFeddA�Zfefdu�r�eB�gdB�dCefZEn
eB�gdD�dZVeIdEeEidFdG�ZhdEdHdIgdFdJ�ieFdKdL�gdM�ZidNeCgZjdOZkdPZldQZmGdRdS�dSejn�ZodTdU�ZpdVdW�ZqdXdY�ZrdZd[�Zsd\d]�Ztd�d_d`�Zue1dadb��Zve1dcdd��Zwe1eEdedefdfdg��Zxe1d�dhdi��Zydjdk�Zze1dldm��Z{e1dndo��Z|e1dpdq��Z}e1drds��Z~e1dtdu��Zd�dwdx�Z�e1dydz��Z�e1eVfd{d|��Z�e1d}d~��Z�e1dd���Z�e1e�d3�d�d���Z�e1e�d3�d�d���Z�e1e�d3�d�d���Z�e1eTj�eMeSdefd�d���Z�e1eTj�eMdefd�d���Z�e1d�d�d���Z�d�d��Z�Gd�d��d�e��Z�Gd�d��d�e��Z�eoZ�eoej�ffgZ�d�d��Z�dS)��N)�Enum)�sleep�time)�Any�Dict�List�Optional)�minidom)�dmi)�log)�net�sources�ssh_util�subp�util)�
EventScope�	EventType)�
device_driver)�EphemeralDHCPv4�NoDHCPLeaseError�NoDHCPLeaseInterfaceError�NoDHCPLeaseMissingDhclientError)�events)�netlink)�+DEFAULT_REPORT_FAILURE_USER_VISIBLE_MESSAGE�DEFAULT_WIRESERVER_ENDPOINT�azure_ds_reporter�azure_ds_telemetry_reporter�build_minimal_ovf�dhcp_log_cb�get_boot_telemetry�get_ip_from_lease_value�get_metadata_from_fabric�get_system_info�is_byte_swapped�push_log_to_kvp�report_diagnostic_event�report_failure_to_fabric)�UrlError�readurl�retry_on_url_exc�Azure�instance-idziid-AZURE-NODEz/dev/disk/cloud/azure_resourceZext4z 7783-7084-3265-9085-8269-3286-77z"/var/lib/cloud/data/reported_readyz/var/lib/waagentz/dev/sr0�zhttp://169.254.169.254/metadataz
2019-06-01z
2021-08-01z
2021-03-01)��i�i�)Zretry_codesZretry_instancesc@s*eZdZd�e�Zd�e�Zd�e�ZdS)�MetadataTypez{}/instancez{}/instance/networkz{}/reprovisiondataN)�__name__�
__module__�__qualname__�format�IMDS_URL�ALL�NETWORK�REPROVISION_DATA�r9r9�E/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceAzure.pyr0Ws

r0c@seZdZdZdZdZdZdS)�PPSType�NoneZRunningZSavableZUnknownN)r1r2r3�NONE�RUNNING�SAVABLE�UNKNOWNr9r9r9r:r;]sr;z/sys/firmware/acpi/tables/OEM0�PLATFORM_ENTROPY_SOURCE)z"/etc/netplan/90-hotplug-azure.yamlz /usr/local/sbin/ephemeral_eth.shz+/etc/udev/rules.d/10-net-device-added.rulesz#/run/network/interfaces.ephemeral.dZ	mlx4_coreZ	mlx5_corecCs�|��D]r}t�d|�r|��}t|�dkr|d�d�}t|�dkr|ddkr|d�|�r|d�d�}|dSqd	S)
z�
    dev.storvsc.1.%pnpinfo:
        classid=32412632-86cb-44a2-9b5c-50d1417354f5
        deviceid=00000000-0001-8899-0000-000000000000
    Zpnpinfo�r-�=r�deviceid��.N)�
splitlines�re�search�split�len�
startswith)�
sysctl_outrD�line�fields�columns�compsr9r9r:�"find_storvscid_from_sysctl_pnpinfo�s
�
��rRcCs2|��D]$}t�||�r|��}|dSqdS)z�
    scbus0 on ata0 bus 0
    scbus1 on ata1 bus 0
    scbus2 on blkvsc0 bus 0
    scbus3 on blkvsc1 bus 0
    scbus4 on storvsc2 bus 0
    scbus5 on storvsc3 bus 0
    scbus-1 on xpt0 bus 0
    rN)rGrHrIrJ)�camcontrol_outZdisk_drvrN�itemsr9r9r:�find_busdev_from_disk�s
rU)rS�busdev�returncCsN|��D]@}t�||�r|�d�}t|�dkr|d�d�}|dSqdS)z�
    <Msft Virtual CD/ROM 1.0>          at scbus1 target 0 lun 0 (cd0,pass0)
    <Msft Virtual Disk 1.0>            at scbus2 target 0 lun 0 (da0,pass1)
    <Msft Virtual Disk 1.0>            at scbus3 target 1 lun 0 (da1,pass2)
    �(r-rE�,rN)rGrHrIrJrK)rSrVrNrTZdev_passr9r9r:�find_dev_from_busdev�s
rZ)�macrWc
CsZt|�dkrRd�|dd�|dd�|dd�|dd�|dd�|dd�g�}|��S)	z1Normalize mac address with colons and lower-case.��:rr-����
)rK�join�lower)r[r9r9r:�normalize_mac_address�s
>�rd�rWcCsdd�t��D�S)z-Get Hyper-V NICs as normalized MAC addresses.cSs$g|]}|ddkrt|d��qS)r-�	hv_netvscrE�rd)�.0�nr9r9r:�
<listcomp>�s�z1get_hv_netvsc_macs_normalized.<locals>.<listcomp>)rZget_interfacesr9r9r9r:�get_hv_netvsc_macs_normalized�s�rkc	Cs>zt�|�jWStjy8t�dd�|��|YS0dS)NzFailed to execute: %s� )r�stdout�ProcessExecutionError�LOG�debugrb)�cmd�fail_retr9r9r:�execute_or_debug�s
rscCstddgdd�S)NZsysctlzdev.storvsc�)rr�rsr9r9r9r:�get_dev_storvsc_sysctl�srvcCstgd��S)N)�
camcontrol�devlistz-brur9r9r9r:�get_camcontrol_dev_bus�srycCstddg�S)Nrwrxrur9r9r9r:�get_camcontrol_dev�srzcCs�d}|dkrd}|d}dt|�}d�||�}t�}t||�}|sRt�d�dSt�}t�}d�|�}t||�}	|	r�t	||	�}
|
dur�t�d	�dS|
Sd
�|�}t||�}	|	r�t	||	�}
|
dur�t�d	�dS|
SdS)NZ00000000rEZ00000001r-Z000z{0}-{1}z#Fail to find storvsc id from sysctlz	blkvsc{0}zFail to find /dev/daXz
storvsc{0})
�strr4rvrRrorpryrzrUrZ)Zport_idZg0Zg1Zg0g1rMZ	storvscidZcamcontrol_b_outrSZblkvscZscbusxZdevnameZstorvscr9r9r:�get_resource_disk_on_freebsd�s<









r|zfreebsd-ufsrEzresource disk is not None�/dev/zresource disk is NoneZ
ephemeral0T)�data_dir�disk_aliases�apply_network_configZgpt�d)Z
table_typeZlayoutZ	overwritezephemeral0.1)Z
filesystemZdevice)�
disk_setupZfs_setup�
datasourceZnever_destroy_ntfszTemporary StorageZREDACTEDcs�eZdZdZejejejhiZ	dZ
ejZ
dZdd�Zedd��fdd	�Zd
d�Zdd
�Zedddd�eeeedd�dd��Zedd�dd��Zed�dd�Zedd��Zdd�Zdd�fdd�	Zedd ��Zeej e!dd!�eeee"d"�d#d$��Z#d%d&�Z$ee%ed�d'd(��Z&e"e%ed)�d*d+�Z'e%ed�d,d-�Z(d.d/�Z)d0d1�Z*ded2d3�Z+ed4d5��Z,edfeee-d8�d9d:��Z.ed;d<��Z/edd�d=d>��Z0ed?d@��Z1edAdB��Z2edCdD��Z3edEdF��Z4edgeeedG�dHdI��Z5eddJ�ee%eee%edK�dLdM��Z6e"eed)�dNdO�Z7e"e"e8dP�dQdR�Z9edSdT��Z:ee"e"ee%edU�dVdW��Z;dXdY�Z<edZd[��Z=e>d\d]��Z?e>d^d_��Z@e>d`da��ZAee"ed)�dbdc��ZB�ZCS)h�DataSourceAzurer+FrEcCsftj�||||�tj�|jd�|_i|_d|_t	�
t	�|ti�t
g�|_d|_d|_d|_t|_dS)NZazure)r
�
DataSource�__init__�os�pathrb�seed_dir�cfg�seedr�
mergemanydict�get_cfg_by_path�DS_CFG_PATH�BUILTIN_DS_CONFIG�ds_cfg�_iso_dev�_network_config�_ephemeral_dhcp_ctxr�_wireserver_endpoint)�self�sys_cfg�distro�pathsr9r9r:r�Cs�zDataSourceAzure.__init__N)�ci_pkl_versionrWcs"t��|�d|_d|_t|_dS�N)�super�	_unpickler�r�rr�)r�r���	__class__r9r:r�PszDataSourceAzure._unpicklecCstj�|�}d||jfS)Nz%s [seed=%s])r
r��__str__r�)r��rootr9r9r:r�WszDataSourceAzure.__str__cCsH|jdurd}n*|j�d�r"d}n|j��dkr6d}nd}d||jfS)z/Return the subplatform metadata source details.N�unknownz/devzconfig-disk�imdszseed-dirz%s (%s))r�rLrc)r�Zsubplatform_typer9r9r:�_get_subplatform[s
z DataSourceAzure._get_subplatform�)�iface�retry_sleep�timeout_minutes)r�r�r�rWcCs�|jdurtd��t�d|�t|td�|_d}|dt�}tjddt	d���@|du�rFz|j�
�}Wn�ty�td	tj
d
�Yn�ty�tdtjd
�d|_�Ynlty�td|tjd
�YnJtj�y}z.td
|j|j|j|jftjd
�WYd}~n
d}~00|du�rFt�||k�rFt|�qR�qFqR|du�r^d|_t��n$|d|j_d|v�r�t|d�|_Wd�n1�s�0YdS)a�Setup ephemeral networking.

        Keep retrying DHCP up to specified number of minutes.  This does
        not kill dhclient, so the timeout in practice may be up to
        timeout_minutes + the system-configured timeout for dhclient.

        :param timeout_minutes: Number of minutes to keep retrying for.

        :raises NoDHCPLeaseError: If unable to obtain DHCP lease.
        Nz/Bringing up networking when already configured.z)Requested ephemeral networking (iface=%s))r�Z
dhcp_log_func�<zobtain-dhcp-leasezobtain dhcp lease��name�description�parentzInterface not found for DHCP�Zlogger_funczdhclient executable not foundz&Failed to obtain DHCP lease (iface=%s)z7Command failed: cmd=%r stderr=%r stdout=%r exit_code=%s�	interfacezunknown-245)r��RuntimeErrorrorprrrr�ReportEventStackrZobtain_leaserr&�warningr�errorrrrnrq�stderrrmZ	exit_coderr�r!r�)r�r�r�r��lease�timeoutr�r9r9r:�_setup_ephemeral_networkinggsn
���

�
��
���



�z+DataSourceAzure._setup_ephemeral_networkingrecCs"|jdurdS|j��d|_dS)zTeardown ephemeral networking.N)r�Z
clean_network�r�r9r9r:�_teardown_ephemeral_networking�s

z.DataSourceAzure._teardown_ephemeral_networkingcCs|jdup|jjduS)z"Check if networking is configured.N)r�r�r�r9r9r:�_is_ephemeral_networking_up�s

�z+DataSourceAzure._is_ephemeral_networking_upcCs�i}|jd}d}d}i}d}i}i}t|j|�D�]$}	zl|	�d�r�t��rftj|	tdd�\}}}}nt�|	t�\}}}}|	|_nt|	�\}}}}d}|	}W�qZWq2t	y�t
d	|	tjd
�Yq2Yq2tj
�yt
d|	tjd
�ddi}d
dddiii}d}Yq2Yq2t�yV}
z,d|
}t
|tjd
�t�|��WYd}
~
q2d}
~
00q2t
d|tjd
�t|j��p||}|�r�dnd}
z|j|
d�Wnt�y�Yn0|���r�|jdd�}ni}|�s�|�s�d}t
|�t�|��|�||�}|tjk�r^t���r.d}t
|tjd
�t�|��|tjk�rB|��|��\}}}}|jdd�}|j|d�||_|�||t� |d|ig�|d��t!|�}t"|�}t#|�}|�r�t�d|�||d
dd<|�r�t�d|�||dd<|�rt�d |�||dd!<|dk�rt|d"�stzt$|||d#�}d$|i|d"<Wn8t%�yr}zt
d%|tjd
�WYd}~n
d}~00|�s�t&|�}|�r�t�d&�zt'�(d�)|�*���|d'<Wn"t%�y�t
d(tj+d
�Yn0|�s�d)}t
|tjd
�t�|��nt
d*|tjd
�||k�r$t
d+|tjd
�t,�}|�r<||dd,<|�-�|dd-<|j.du�r�|���r�|j/||d.�}z|j0|d/�}Wnt%�y�Yn.0t�d0|�|�r�||dd1<|�1�d|_.|S)2aWalk all instance metadata sources returning a dict on success.

        @return: A dictionary of any metadata content for this instance.
        @raise: InvalidMetaDataException when the expected metadata service is
            unavailable, broken or disabled.
        r~FNrtr}�udf)�mtypeTz$Did not find Azure data source in %sr�z%s was not mountable�local-hostname�system_info�default_userr�ZIMDSzBrokenAzureDataSource: %sz!Found provisioning metadata in %s�r��r�ra)�retrieszNo OVF or IMDS availablez%Free BSD is not supported for PPS VMs)�imds_mdr�)r��files�metadata�userdata_rawz Username retrieved from IMDS: %sz Hostname retrieved from IMDS: %sr�z(Disable password retrieved from IMDS: %sZdisable_passwordr�)�username�hostnameZ
disableSshPwd�ovf-env.xmlz)Failed to construct OVF from IMDS data %szRetrieved userdata from IMDSr�zBad userdata in IMDSzNo Azure metadata foundzfound datasource in %szusing files cached in %sZrandom_seedr,)r�r���pubkey_infoznegotiating returned %s�public-keys)2r��list_possible_azure_dsr�rLr�
is_FreeBSD�mount_cb�load_azure_ds_dirr��NonAzureDataSourcer&rorp�MountFailedError�BrokenAzureDataSourcer�r
�InvalidMetaDataException�boolr�rr��get_imds_data_with_api_fallback�_determine_pps_typer;r=r?�_wait_for_all_nics_ready�_reprovision�validate_imds_network_metadatar��updater��_username_from_imds�_hostname_from_imds�_disable_password_from_imdsr�	Exception�_userdata_from_imds�base64�	b64decoderbrJr��_get_random_seed�_iid�_negotiated�!_determine_wireserver_pubkey_info�
_report_ready�_cleanup_markers)r��crawled_data�ddirZovf_is_accessibleZmetadata_source�mdr�r�r��src�exc�msgZrequires_imds_metadatar�r��pps_typeZ
imds_usernameZ
imds_hostnameZimds_disable_password�contents�eZ
imds_userdatar�r��ssh_keysr9r9r:�crawl_metadata�s,

���
�"�




�������
���

��zDataSourceAzure.crawl_metadatacCs
t|j�S)�@Check platform environment to report if this datasource may run.)�_is_platform_viabler�r�r9r9r:r��sz#DataSourceAzure._is_platform_viabler9cstt|��|�tj|_dS)z.Reset any cached class attributes to defaults.N)r�r��clear_cached_attrsr
�UNSET�_metadata_imds)r�Z
attr_defaultsr�r9r:r��sz"DataSourceAzure.clear_cached_attrsc
Cs�|��sdSz
t�Wn0tyF}zt�d|�WYd}~n
d}~00z
t�Wn0ty�}zt�d|�WYd}~n
d}~00t|jj_	zvzt
jtjd|j
d�}WnPt�y�}z6td|tjd�|jtd	�WYd}~W|��dSd}~00W|��n
|��0|j�r:|jjd
k�r:|j�d��r:t�t}tj�|��rttd|tjd�t
�|d
tg�|_ntd|tjd�|d
|_|dd|_t
�|dtg�|_ |d|_!t
�"|jt#i�}t
�||jg�|_t$|jd|ddd�dS)z�Crawl and process datasource metadata caching metadata as attrs.

        @return: True on success, False on error, invalid or disabled
            datasource.
        Fz Failed to get boot telemetry: %sNz$Failed to get system information: %szCrawl of metadata service)�logfuncr��funcz"Could not crawl Azure metadata: %sr�)r�Zubuntur�zXEphemeral resource disk '%s' exists. Merging default Azure cloud ephemeral disk configs.r�zdEphemeral resource disk '%s' does not exist. Not merging default Azure cloud ephemeral disk configs.r�r�r�r~r�i�)�dirmodeT)%r�r r�ror�r#�BLACKLIST_DRIVERSr��
networking�blacklist_driversr�log_timerpr�r&r��_report_failurerr�r�r��get�*maybe_remove_ubuntu_network_config_scripts�RESOURCE_DISK_PATHr�r��existsr��#BUILTIN_CLOUD_EPHEMERAL_DISK_CONFIGr�r��DEFAULT_METADATAr�r�r�r��write_files)r�r�r��devpathZuser_ds_cfgr9r9r:�	_get_data�s�
"
"
�

����
�
���
���

�
�zDataSourceAzure._get_data)�md_type�exc_cb�infinite)r�r	rrWc
Cs�t�dt�zt||t||d�WStyj}z2t�dt�|jdkrViWYd}~SWYd}~n
d}~00d�t�}t|tjd�zt||t||d�WSty�}z"td|tj	d�iWYd}~Sd}~00dS)	aFetch metadata from IMDS using IMDS_VER_WANT API version.

        Falls back to IMDS_VER_MIN version if IMDS returns a 400 error code,
        indicating that IMDS_VER_WANT is unsupported.

        :return: Parsed metadata dictionary or empty dict on error.
        zAttempting IMDS api-version: %s)r�r	�api_versionr
rz"UrlError with IMDS api-version: %s�Nz!Fall back to IMDS api-version: {}r�z!Failed to fetch IMDS metadata: %s)
ro�info�
IMDS_VER_WANT�get_metadata_from_imdsr(�coder4�IMDS_VER_MINr&r�)r�r�r	r
rr��log_msgr9r9r:r��s:�
&
��z/DataSourceAzure.get_imds_data_with_api_fallbackcCs|jd�|�S)Nr)r�r)r�r�r9r9r:�device_name_to_device)sz%DataSourceAzure.device_name_to_devicec	Cs2z|�|jd�WSttfy(Yn0|��S)z+
        Retrieve public SSH keys.
        r�)�_get_public_keys_from_imdsr��KeyError�
ValueError�_get_public_keys_from_ovfr�r9r9r:�get_public_ssh_keys,s
z#DataSourceAzure.get_public_ssh_keys)r�rWcCs�zdd�|ddD�}Wn&ty@d}t|tjd��Yn0tdd�|D��rnd	}t|tjd�t|��d
�t|��}t|tjd�|S)z�Get SSH keys from IMDS metadata.

        :raises KeyError: if IMDS metadata is malformed/missing.
        :raises ValueError: if key format is not supported.

        :returns: List of keys.
        cSsg|]}|d�qS)ZkeyDatar9)rhZ
public_keyr9r9r:rjAs�z>DataSourceAzure._get_public_keys_from_imds.<locals>.<listcomp>�computeZ
publicKeysz"No SSH keys found in IMDS metadatar�css|]}t|d�VqdS))�keyN)�_key_is_openssh_formatted)rhrr9r9r:�	<genexpr>J�z=DataSourceAzure._get_public_keys_from_imds.<locals>.<genexpr>zKey(s) not in OpenSSH formatzRetrieved {} keys from IMDS)rr&rorp�anyrr4rK)r�r�r�rr9r9r:r8s
�
z*DataSourceAzure._get_public_keys_from_imdscCsXg}z*|jd}d�t|��}t|tjd�Wn$tyRd}t|tjd�Yn0|S)zYGet SSH keys that were fetched from wireserver.

        :returns: List of keys.
        r�zRetrieved {} keys from OVFr�zNo keys available from OVF)r�r4rKr&rorpr)r�r�rr9r9r:rSs
z)DataSourceAzure._get_public_keys_from_ovfcCs|jSr��r�r�r9r9r:�get_config_objcszDataSourceAzure.get_config_objcCst�|���Sr�)r
Zinstance_id_matches_system_uuidZget_instance_id)r�r�r9r9r:�check_instance_idfsz!DataSourceAzure.check_instance_idcCsxtj�|j�d�d�}t�d�}|dur0td��|��}tj�	|�rtt
�|���}|��|krb|St
|��|�rt|S|S)N�datar,zsystem-uuidzfailed to read system-uuid)r�r�rbr�Z	get_cpathr
�
read_dmi_datar�rcrr�	load_file�stripr$)r�ZpreviousZ
prev_iid_pathZsystem_uuidZiidr9r9r:r�js�
zDataSourceAzure._iidc
Cs�zpd}tjddtd��t�|�}Wd�n1s60Y|dur\d}t|tjd�ntd|tjd�Wn8ty�}z tt	|�tj
d��WYd}~n
d}~00dS)z�Use the netlink socket provided to wait for nic detach event.
        NOTE: The function doesn't close the socket. The caller owns closing
        the socket and disposing it safely.
        Nzwait-for-nic-detachzwait for nic detachr�zHPreprovisioned nic not detached as expected. Proceeding without failing.r�z%The preprovisioned nic %s is detached)rr�rrZwait_for_nic_detach_eventr&ror��AssertionErrorr{r�)r��nl_sock�ifnamer�r�r9r9r:�_wait_for_nic_detach�s&�(��
z$DataSourceAzure._wait_for_nic_detachr�皙�����?)r)r�r�cCsdt|�D]<}|jj�|�r0td|tjd�q`|d|krt|�qtd|||ftjd�dS)NzThe link %s is up.r�rEz;The link %s is not up after %f seconds, continuing anyways.)�ranger�r�Ztry_set_link_upr&rorr)r�r)r�r��ir9r9r:�wait_for_link_up�s
�

��z DataSourceAzure.wait_for_link_upcCs>t}t�d|�t�|djt��t�d��t	dtj
d�dS)Nz*Creating a marker file to report ready: %sz{pid}: {time}
)�pidrzRSuccessfully created reported ready marker file while in the preprovisioning pool.r�)�REPORTED_READY_MARKER_FILErorr�
write_filer4r��getpidrr&rp)r�r�r9r9r:�_create_report_ready_marker�s��z+DataSourceAzure._create_report_ready_markerc
Cs\z|��WnBtyN}z*d}t|tjd�t�|�|�WYd}~n
d}~00|��dS)z�Report ready for PPS, creating the marker file upon completion.

        :raises sources.InvalidMetaDataException: On error reporting ready.
        z9Failed reporting ready while in the preprovisioning pool.r�N)r�r�r&ror�r
r�r3)r�r�r�r9r9r:�_report_ready_for_pps�s"z%DataSourceAzure._report_ready_for_ppsc
s�d}d}d}d�d�d�|j|dd����fdd	�}z|jdtj|d
d�}Wn2ty�}zt�d||�WYd}~n
d}~00|r�t�d
|�d
}t|d�}t	d|tjd�n|�
�||fS)z�Check if a given interface is the primary nic or not. If it is the
        primary nic, then we also get the expected total nic count from IMDS.
        IMDS will process the request and send a response only for primary NIC.
        F���NrrEr�)r�r�cs��d���krT�d9�td|�ftjd�t|t�rTtd||j|jftjd�|jr~t|jtjtj	f�r~�d��dkSdS)NrEr-z>Ran into exception when attempting to reach %s after %d polls.r��4poll IMDS with %s failed. Exception: %s and code: %sraT)
r&ror��
isinstancer(�causer�requests�Timeout�ConnectionError)r�r��Zexpected_errors_countZmetadata_logging_thresholdZmetadata_poll_countr9r:�network_metadata_exc_cb�s,��
���zIDataSourceAzure._check_if_nic_is_primary.<locals>.network_metadata_exc_cbT)r�r	r
rz�Failed to get network metadata using nic %s. Attempt to contact IMDS failed with error %s. Assuming this is not the primary nic.z%s is the primary nicr�zExpected nic count: %dr�)r�r�r0r7r�ror�rrKr&r�)r�r)Z
is_primaryZexpected_nic_countr�r=r�r9r<r:�_check_if_nic_is_primary�s>#�
��z(DataSourceAzure._check_if_nic_is_primaryc
Cs�t�d�z�g}d}d}tjddt|�td��t�||�}Wd�n1sR0Y|�|�t	d|tjd�|�
|�|s�t�d	|�|�|�\}}|rt�d
�q�qWn6ty�}zt	t
|�tjd�WYd}~n
d}~00dS)z6Wait until the primary nic for the vm is hot-attached.z*Waiting for primary nic to be hot-attachedFNzwait-for-nic-attachz4wait for nic attach after %d nics have been attachedr�zDetected nic %s attached.r�z!Checking if %s is the primary niczFound primary nic for this VM.)rorrr�rKrrZwait_for_nic_attach_event�appendr&r.r>r'r{r�)r�r(Z
nics_foundZprimary_nic_foundr)�_r�r9r9r:�"_wait_for_hot_attached_primary_nic0s<
���"

�
�
z2DataSourceAzure._wait_for_hot_attached_primary_nicc
Cs�d}z|z0t��}|��|��|�|�|�|�Wn:tjyp}z tt|�t	j
d��WYd}~n
d}~00W|r�|��n|r�|��0dS)aWait for nic(s) to be hot-attached. There may be multiple nics
        depending on the customer request.
        But only primary nic would be able to communicate with wireserver
        and IMDS. So we detect and save the primary nic to be used later.
        Nr�)r�create_bound_netlink_socketr4r�r*rA�NetlinkCreateSocketErrorr&r{ror��close)r�r(r�r9r9r:r�es

�z(DataSourceAzure._wait_for_all_nics_readyc
s�d�tjjt�}ddi}d}ttj�t	��}d�_
d�_d}d}�fdd�}|�rވ��sh�j
d	d
��zZzˆjdus��jjdur�td���jj}t��}���t�d|�tjd
dtd��Zzt�||�Wn8t�y
}	ztd|	tjd�WYd}	~	n
d}	~	00Wd�n1�s"0YWn�tj�yt}	z*td|	tjd�t� d�|	�WYd}	~	nHd}	~	0t!�y�}	z&tdtjd�t� d�|	�WYd}	~	n
d}	~	00W|�r�|�"�n|�r�|�"�0��#�|�s�����s"|d7}z�j
dd
�Wnt!�y Y�q�Yn0tjddtd��Zzt$|t%||ddd�j&}Wn.t'�y|��#�YWd��q�Yn0Wd�n1�s�0Y�q�td|tjd�td�jtjd�|S)ztPoll IMDS for the new provisioning data until we get a valid
        response. Then return the returned JSON object.�{}?api-version={}�Metadata�trueNrErcs�t|t�r�|jdvrj�j�jkrX�jd9_t�d�j�td||j|jftjd��jd7_dStd||j|jftj	d�dStd	|tj	d�dS)
N)r.r/r-z:Backing off logging threshold for the same exception to %dr6r�rETFz1poll IMDS failed with an unexpected exception: %s)
r7r(r�imds_poll_counter�imds_logging_thresholdrorpr&r8r�)r��	exceptionr�r9r:r
�s<

�������z*DataSourceAzure._poll_imds.<locals>.exc_cbr�r�zMissing ephemeral contextz#Wait for vnetswitch to happen on %sz!wait-for-media-disconnect-connectzwait for vnet switchr�z'Error while waiting for vnet switch: %sr�z)Failed to create bound netlink socket: %sz2Failed to report ready while in provisioning pool.z&DHCP failed while in provisioning poolr�zget-reprovision-data-from-imdszget reprovision data from imdsTF)r��headers�exception_cbrZlog_req_respz#attempted dhcp %d times after reusez polled imds %d times after reuse)(r4r0r8�valuerr�r�r��isfiler0rIrHr�r�r�r�r�rrBr4rorprr�rZ!wait_for_media_disconnect_connectr'r&r�rCr�r
r�rrDr�r)�IMDS_TIMEOUT_IN_SECONDSr�r()
r��urlrKr(Zreport_readyZ
dhcp_attemptsZreprovision_datar
r�r�r9r�r:�
_poll_imds{s��$�
�����@������
�

��<��zDataSourceAzure._poll_imds)r�rWc
Cs�|��rbz"tdtjd�t|j|d�WdSty`}ztd|tjd�WYd}~n
d}~00zNtdtjd�|��z|j	dd	�Wnt
y�Yn0t|j|d�WdSty�}ztd
|tjd�WYd}~n
d}~00dS)z�Tells the Azure fabric that provisioning has failed.

        @param description: A description of the error encountered.
        @return: The success status of sending the failure signal.
        z>Using cached ephemeral dhcp context to report failure to Azurer�)�endpointr�Tz@Failed to report failure using cached ephemeral dhcp context: %sNz3Using new ephemeral dhcp to report failure to Azurer�r�z5Failed to report failure using new ephemeral dhcp: %sF)r�r&rorpr'r�r�r�r�r�r)r�r�r�r9r9r:r�sL�������zDataSourceAzure._report_failurer�)r�rWc
CsZzt|j|j|d�}Wn8tyN}z td|tjd��WYd}~n
d}~00d|_|S)z�Tells the fabric provisioning has completed.

        :param pubkey_info: Fingerprints of keys to request from Wireserver.

        :raises Exception: if failed to report.

        :returns: List of SSH keys, if requested.
        )rRZiso_devr�zQError communicating with Azure fabric; You may experience connectivity issues: %sr�N)r"r�r�r�r&ror�)r�r�r#r�r9r9r:r�@s �
��zDataSourceAzure._report_readyc
CsPz|dddWStyJ}z td|tjd�WYd}~dSd}~00dS)NZextendedrZppsTypez2Could not retrieve pps configuration from IMDS: %sr�)r�r&rorp)r�r�r�r9r9r:�_ppstype_from_imds^s�z"DataSourceAzure._ppstype_from_imds)�ovf_cfgr�rWcCs�tj�t�rtj}np|�dd�tjjks:|�	|�tjjkrBtj}nB|�d�dusv|�dd�tj
jksv|�	|�tj
jkr~tj
}ntj}td|jt
jd�|S)z@Determine PPS type using OVF, IMDS data, and reprovision marker.�PreprovisionedVMTypeN�PreprovisionedVmTzPPS type: %sr�)r�r�rNr0r;r@rr?rMrSr>r=r&ror)r�rTr�r�r9r9r:r�hs*���
����z#DataSourceAzure._determine_pps_typecCsZ|��}tjddtd��.t|�\}}}|||d|ifWd�S1sL0YdS)zrInitiate the reprovisioning workflow.

        Ephemeral networking is up upon successful reprovisioning.
        zreprovisioning-read-azure-ovfz$read azure ovf during reprovisioningr�r�N)rQrr�r�read_azure_ovf)r�r�r��udr�r9r9r:r��s�zDataSourceAzure._reprovision)r�r�rWc	Csbd}z|�|�WnJttfy\|�dd�}d�|durDt|�nd�}t|tjd�Yn0|S)z�Determine the fingerprints we need to retrieve from Wireserver.

        :return: List of keys to request from Wireserver, if any, else None.
        N�_pubkeysz"Retrieved {} fingerprints from OVFrr�)	rrrrr4rKr&rorp)r�r�r�r�rr9r9r:r��s�z1DataSourceAzure._determine_wireserver_pubkey_infocCst�t�dS)zCleanup any marker files.N)r�del_filer0r�r9r9r:r��sz DataSourceAzure._cleanup_markersc	Cs>z(t||j�td�d�Wt|jd�nt|jd�0dS)NF)�is_new_instance�
preserve_ntfsZdef_log_file)�address_ephemeral_resizer�r�DS_CFG_KEY_PRESERVE_NTFSr%r�)r�r�r[r9r9r:�activate�s� zDataSourceAzure.activatecCs|j�di��di��d�S)Nr�rZplatformFaultDomain�r�rr�r9r9r:�availability_zone�s���z!DataSourceAzure.availability_zonecCs:|jr|jtjkr4|j�d�r&|j}nd}t|�|_|jS)asGenerate a network config like net.generate_fallback_network() with
        the following exceptions.

        1. Probe the drivers of the net-devices present and inject them in
           the network configuration under params: driver: <driver> value
        2. Generate a fallback network config that does not include any of
           the blacklisted devices.
        r�N)r�r
r�r�rr��parse_network_config)r�Znc_srcr9r9r:�network_config�s

zDataSourceAzure.network_configcCs|j�di��di��d�S)Nr�r�locationr`r�r9r9r:�region�szDataSourceAzure.regioncs�t�}z|d}dd�|dD��Wn,tyPtd|�d�tjd�YdS0�fdd�|D�}|sld	Std
||ftjd�|jr�|jjs�dSt�	|jj�}|r�t
|t�s�dSt|�}||vr�td||ftjd�dS)z=Validate IMDS network config and report telemetry for errors.�networkcSsg|]}t|d��qS)�
macAddressrg)rhr-r9r9r:rj�s�zBDataSourceAzure.validate_imds_network_metadata.<locals>.<listcomp>r�z6IMDS network metadata has incomplete configuration: %rr�Fcsg|]}|�vr|�qSr9r9)rh�m�Z	imds_macsr9r:rj�rTz>IMDS network metadata is missing configuration for NICs %r: %rz3IMDS network metadata is missing primary NIC %r: %r)
rkrr&rror�r�r�rZget_interface_macr7r{rd)r�r�Z
local_macsrcZmissing_macsZprimary_macr9rir:r��sJ�
������z.DataSourceAzure.validate_imds_network_metadata)r9)N)r�r+)N)Dr1r2r3Zdsnamerr7rZBOOT_NEW_INSTANCE�BOOTZdefault_update_eventsr�r
r�r�Z_ci_pkl_versionr��intr�r�r�rrr{r�r�r�r�r�r�r�rr0r6�imds_readurl_exception_callback�dictr�rrrrrr!r"r�r*�floatr.r3r4r>rAr�rQr�r�rSr;r�r�r�r�r_�propertyrarcrer��
__classcell__r9r9r�r:r�6s���
��X
K
T��/

��

^
4

0�

�


�




r�cCs,z|dddWSty&YdS0dS)Nr�	osProfileZ
adminUsername�r�Z	imds_datar9r9r:r�sr�cCs(z|ddWSty"YdS0dS)NrZuserDatarrrsr9r9r:r�sr�cCs,z|dddWSty&YdS0dS)NrrqZcomputerNamerrrsr9r9r:r�sr�cCs0z|ddddkWSty*YdS0dS)NrrqZdisablePasswordAuthenticationrGrrrsr9r9r:r�s��r�cCsFd|��vrdSt��}z|�|�}Wnty:YdS0|jduS)z?
    Validate whether or not the key is OpenSSH-formatted.
    z
FN)r&rZAuthKeyLineParser�parse�	TypeErrorZkeytype)r�parserZaklr9r9r:rsr�cCs`dD]V}g}td|�D]6}||t|�}tj�|�r|�|tj�|�f�q|r|SqgS)N)z-part�prtrE)r,r{r�r�rr?�realpath)rZmaxnumZsuff�foundZpnumZppathr9r9r:�_partitions_on_device0s
r{cCs*tjddd�}t�d|�tj�|�|vS)Nz	TYPE=ntfsT)Zno_cachezntfs_devices found = %s)r�find_devs_withrorpr�r�ry)rZntfs_devicesr9r9r:�_has_ntfs_filesystem=sr}cCs |rdd�t�tf}d|fStj�|�s6dd|fSt�d|tj�|��t	|�}t
|�dkrldd|fSt
|�dkr�d	|d
�dd�|D��f}d|fSt
|�dkr�|d
\}}n|d\}}t|�s�d|||f}d|fStdd��}d|||f}t
jddtd����}ztj||dddid�}	Wn�tj�y�}
zhd|_dt|
�v�rvd|dfWYd}
~
Wd�Sd|d||
ffWYd}
~
Wd�Sd}
~
00|	dk�r�d|	|_t�dd�t�t�d|d |	fWd�SWd�n1�s
0Yd|d!fS)"a�Determine if the ephemeral drive at devpath should be reformatted.

    A fresh ephemeral disk is formatted by Azure and will:
      a.) have a partition table (dos or gpt)
      b.) have 1 partition that is ntfs formatted, or
          have 2 partitions with the second partition ntfs formatted.
          (larger instances with >2TB ephemeral disk have gpt, and will
           have a microsoft reserved partition as part 1.  LP: #1686514)
      c.) the ntfs partition will have no files other than possibly
          'dataloss_warning_readme.txt'

    User can indicate that NTFS should never be destroyed by setting
    DS_CFG_KEY_PRESERVE_NTFS in dscfg.
    If data is found on NTFS, user is warned to set DS_CFG_KEY_PRESERVE_NTFS
    to make sure cloud-init does not accidentally wipe their data.
    If cloud-init cannot mount the disk to check for data, destruction
    will be allowed, unless the dscfg key is set.z:config says to never destroy NTFS (%s.%s), skipping checksrFFzdevice %s does not existzResolving realpath of %s -> %srzdevice %s was not partitionedr-z&device %s had 3 or more partitions: %srlcSsg|]}|d�qS)rEr9)rhrxr9r9r:rjmrz*can_dev_be_reformatted.<locals>.<listcomp>rEz5partition %s (%s) on device %s was not ntfs formattedcs&tdg��t�fdd�t�|�D��S)Nzdataloss_warning_readme.txtcsg|]}|���vr|�qSr9)rc)rh�f�Zignoredr9r:rj�rz?can_dev_be_reformatted.<locals>.count_files.<locals>.<listcomp>)�setrKr��listdir)Zmpr9rr:�count_files}s
z+can_dev_be_reformatted.<locals>.count_filesz1partition %s (%s) on device %s was ntfs formattedzmount-ntfs-and-countr�ZntfsZLANG�C)r�Zupdate_env_for_mountzcannot mount ntfszunknown filesystem type 'ntfs'Tz^ but this system cannot mount NTFS, assuming there are no important files. Formatting allowed.Nz but mount of %s failed: %szmounted and counted %d fileszxit looks like you're using NTFS on the ephemeral disk, to ensure that filesystem does not get wiped, set %s.%s in configz but had %d files on it.z3 and had no important files. Safe for reformatting.)rbr�r^r�r�rrorpryr{rKr}rrr�rrr�r�r�r{r�)rr\r�Z
partitionsZ	cand_partZ	cand_pathr��bmsg�evtZ
file_countr�r9r9r:�can_dev_be_reformattedDs�����
���
�8

�>r�Fc	Cs�tj�|�s"td|tjd�dStd|tjd�d}d}|rJd\}}nt||�\}}t�d||�|sndSdD]�}d|}d	||f}tj�|�r�zt�|�t�d
|�Wq�ty�}zt�	d||�WYd}~q�d}~00qrt�d|�qrdS)
Nz,Ephemeral resource disk '%s' does not exist.r�z$Ephemeral resource disk '%s' exists.F)TzFirst instance boot.zreformattable=%s: %s)r�Zmountsz#/var/lib/cloud/instance/sem/config_zMarker "%s" for module "%s"z%s removed.z%s: remove failed! (%s)z%s did not exist.)
r�r�rr&rorpr��unlinkr�r�)	rr[r\�resultr��modZsempathr�r�r9r9r:r]�s:��

&r]cCsjdd�}|sdS|si}t�||�|��D]8\}}tj�||�}d|vrT|||�}tj||dd�q,dS)NcSsfz>t�|�}|��D]}d|jvr|jtkrt|_qt�|�WSty`t�	d|�|YS0dS)z>Azure provides the UserPassword in plain text. So we redact itZUserPasswordz#failed to redact userpassword in %sN)
�ETZ
fromstring�iter�tag�text�DEF_PASSWD_REDACTIONZtostringr�roZcritical)Zcnt�fnamer��elemr9r9r:�_redact_password�s
��z%write_files.<locals>._redact_passwordr�i�)�filename�content�mode)rZ
ensure_dirrTr�r�rbr1)Zdatadirr�r�r�r�r�r�r9r9r:r�s
rcCs2g}|��s|S|jD]}||�r|�|�q|Sr�)�
hasChildNodes�
childNodesr?)ZnodeZfilter_func�ret�childr9r9r:�
find_child�s
r�c
Cst|dd��}t|�dkrgSt|�dkr:tdt|���|d}t|dd��}t|�dkr`gSg}tjj}|D]�}|��s~qpdddd�}|jD]b}|j|ks�|j	s�q�|j	�
�}	|	|��vr�q�t|j�dks�|jdj|kr�q�|jdj�
�||	<q�|�|�qp|S)	NcSs
|jdkS)NZ
PublicKeys��	localName�rir9r9r:�<lambda>
rz(load_azure_ovf_pubkeys.<locals>.<lambda>rrEz%Multiple 'PublicKeys'(%s) in SSH nodecSs
|jdkS)NZ	PublicKeyr�r�r9r9r:r�rrt)Zfingerprintr�rM)r�rKr�r	ZDocument�	TEXT_NODEr�r��nodeTyper�rc�keys�	wholeTextr&r?)
Zsshnode�resultsZpubkeys_nodeZpubkeysrzZ	text_nodeZpk_nodeZcurr�r�r9r9r:�load_azure_ovf_pubkeyss>

�

��r�c
Cs:zt�|�}WnDtyR}z,d|}t|tjd�t|�|�WYd}~n
d}~00t|jdd��}t	|�dkrxt
d��t	|�dkr�tdt	|���|d}t|d	d��}t	|�dkr�t
d
��t	|�dkr�tdt	|�df��|d}|��s�td
��d}dii}	i}
d}d}d}
|jD�]�}|j
|jk�s|j�s8�q|j��}d}d}t	|j�dk�r~|jdj
|jk�r~d}|jdj}tdd�|j��D��}|dk�s�|dk�r�|�d�dv�r�t�d�|����}n|}n�|dk�r�|}
n�|dk�r�|}n�|dk�r
||	d<n�|dk�rV|�d�dv�r:t�d�|����}n|}ttj|id�i|
d<nX|d k�rnt|�|
d!<n@|d"k�r�t�|�|
d#<n&|�r||v�r�||	|<n||	d|<�qi}|
�r�|
|d$<|�r�d|d%<t|k�r�t |�|d&<|
d'<|�rd(|i|
d)<d#|
v�r|�rd|
d#<t!|�}t�"|
|g�}
|	||
fS)*NzInvalid ovf-env.xml: %sr�cSs
|jdkS)NZProvisioningSectionr�r�r9r9r:r�Brz read_azure_ovf.<locals>.<lambda>rzNo ProvisioningSectionrEz$found '%d' ProvisioningSection itemscSs
|jdkS)N�!LinuxProvisioningConfigurationSetr�r�r9r9r:r�Orz$No LinuxProvisioningConfigurationSetzfound '%d' %ssr�z#no child nodes of configuration setZseedfromZ
azure_datartFTcSsg|]\}}||f�qSr9r9)rh�k�vr9r9r:rjtrz"read_azure_ovf.<locals>.<listcomp>ZuserdataZ
customdata�encoding)Nr�r�Zuserpasswordr�r��dscfg)�defaultr�ZsshrYZ disablesshpasswordauthenticationZ
ssh_pwauthr�Zlock_passwd�passwd�passwordr�r�)#r	ZparseStringr�r&ror�r�r��documentElementrKr�r�r�r�r�r�rcr�rmZ
attributesrTrr�r�rbrJ�DS_NAMErZ	load_yamlr�Zis_falser��encrypt_pass�_get_preprovisioning_cfgsr�)r��domr�Z	error_strr�ZprovSectionZ
lpcs_nodesZlpcsZmd_propsr�r�rXr�r�r�r��simplerM�attrsr�ZdefuserZpreprovisioning_cfgr9r9r:rW8s� 
�
��
��
��









rWcCs�ddd�}t|jdd��}|r*t|�dkr8t�d�|St|ddd��}|rZt|�dkrht�d	�|St|�|d
<t|�|d<|S)a�Read the preprovisioning related flags from ovf and populates a dict
    with the info.

    Two flags are in use today: PreprovisionedVm bool and
    PreprovisionedVMType enum. In the long term, the PreprovisionedVm bool
    will be deprecated in favor of PreprovisionedVMType string/enum.

    Only these combinations of values are possible today:
        - PreprovisionedVm=True and PreprovisionedVMType=Running
        - PreprovisionedVm=False and PreprovisionedVMType=Savable
        - PreprovisionedVm is missing and PreprovisionedVMType=Running/Savable
        - PreprovisionedVm=False and PreprovisionedVMType is missing

    More specifically, this will never happen:
        - PreprovisionedVm=True and PreprovisionedVMType=Savable
    FN)rVrUcSs
|jdkS)NZPlatformSettingsSectionr�r�r9r9r:r��rz+_get_preprovisioning_cfgs.<locals>.<lambda>rz!PlatformSettingsSection not foundcSs
|jdkS)NZPlatformSettingsr�r�r9r9r:r��rzPlatformSettings not foundrVrU)r�r�rKrorp�_get_preprovisionedvm_cfg_value�#_get_preprovisionedvmtype_cfg_value)r�r�Zplatform_settings_section�platform_settingsr9r9r:r��s*

�
�
��r�cCs\d}t|ddd��}|r&t|�dkr4t�d�|St�|djj�}td|tj	d�|S)NFrcSs
|jdkS)NrVr�r�r9r9r:r��rz1_get_preprovisionedvm_cfg_value.<locals>.<lambda>zPreprovisionedVm not foundzPreprovisionedVm: %sr�)
r�rKrorprZtranslate_bool�
firstChild�	nodeValuer&r)r�ZpreprovisionedVmZpreprovisionedVmValr9r9r:r��s�

�
�r�cCsdd}t|ddd��}|r4t|�dks4|djdurBt�d�|S|djj}td|tjd�|S)NrcSs
|jdkS)NrUr�r�r9r9r:r��rz5_get_preprovisionedvmtype_cfg_value.<locals>.<lambda>zPreprovisionedVMType not foundzPreprovisionedVMType: %sr�)r�rKr�rorpr�r&r)r�ZpreprovisionedVMTypeZpreprovisionedVMTypeValr9r9r:r��s"��
��

�r��$6$cCst�||tjdd��S)Nrw)Zstrlen)�cryptrZrand_str)r�Zsalt_idr9r9r:r�sr�cCs^z:t|��}|�d�Wd�WdS1s.0YWntyXt�d|�Yn0dS)z;Return boolean indicating path to cdrom device has content.iNTzcdrom (%s) is not configuredF)�open�read�IOErrorrorp)�	cdrom_dev�fpr9r9r:�_check_freebsd_cdroms

&r�cCs*|durdStj|ddd�}t�|���S)zIReturn content random seed file if available, otherwise,
    return None.NTF)�quiet�decode)rr%r�Z	b64encoder�)�sourcer�r9r9r:r�sr�ccsT|VtVt��r(d}t|�rF|VndD]}t�d|�EdHq,|rP|VdS)Nz/dev/cd0)Ziso9660r�zTYPE=%s)�DEFAULT_PROVISIONING_ISO_DEVrr�r�r|)r�Z	cache_dirr�Zfstyper9r9r:r�5sr�cCsrtj�|d�}tj�|�s"td��t|d��}|��}Wd�n1sJ0Yt|�\}}}|||d|ifS)Nr�zNo ovf-env file found�rb)r�r�rbrNr�r�r�rW)Z
source_dirZovf_filer�r�r�rXr�r9r9r:r�Ds&r�c
Cs�|tjkrN|rNz
t|�WStyL}zt�dt|��WYd}~n
d}~00zt�WSty�}zt�dt|��WYd}~n
d}~00iS)a_Convert imds_metadata dictionary to network v2 configuration.
    Parses network configuration from imds metadata if present or generate
    fallback network config excluding mlx4_core devices.

    @param: imds_metadata: Dict of content read from IMDS network service.
    @return: Dictionary containing network version 2 standard configuration.
    z?Failed generating network config from IMDS network metadata: %sNz-Failed generating fallback network config: %s)r
r��+_generate_network_config_from_imds_metadatar�ror�r{�-_generate_network_config_from_fallback_config)�
imds_metadatar�r9r9r:rbRs	
�&rbc	Cs�did�}|d}t|d�D�]^\}}d}dj|d�}d|d	d
i}d|dd�}d
D]�}	|�|	i��dg�}
|
s�t�d|	|�qXd}|	dkr�d}nd}|
r�d|d<||d<|
d	d�D]N}||	dd�d|�}
|d}|�d�s�g|d<|d�dj||
d��q�qX|�rn|�rnt|d�}|�d|��i|d��t	|�}|�r`|d k�r`||d!d"<||d#|<qt�d$|||�q|S)%aConvert imds_metadata dictionary to network v2 configuration.
    Parses network configuration from imds metadata.

    @param: imds_metadata: Dict of content read from IMDS network service.
    @return: Dictionary containing network version 2 standard configuration.
    r-)�version�	ethernetsrfr�Fzeth{idx})�idxzroute-metricrEr�T)Zdhcp4zdhcp4-overrides�dhcp6)�ipv4Zipv6Z	ipAddresszNo %s addresses found for: %rr�Z24Z128r�zdhcp6-overridesNZsubnetr�prefixZprivateIpAddress�	addressesz
{ip}/{prefix})�ipr�rgZ
macaddress)�matchzset-namerfr��driverr�z<No configuration for: %s (dev_config=%r) (has_ip_address=%r))
�	enumerater4rrorpr?rdr�rcr)r�Z	netconfigZnetwork_metadatar�ZintfZhas_ip_addressZnicnameZ
dhcp_overrideZ
dev_configZ	addr_typer�Zdefault_prefix�addrZ	netPrefixZ	privateIpr[r�r9r9r:r�ksb
��
���r�cCstjtdd�}|duriS|S)z�Generate fallback network config excluding blacklisted devices.

    @return: Dictionary containing network version 2 standard configuration.
    T)r�Z
config_driverN)rZgenerate_fallback_configr�r r9r9r:r��s�r�c
Csjtjdt|||||fd�}ztjfi|��WStyd}z td|tjd��WYd}~n
d}~00dS)a�Query Azure's instance metadata service, returning a dictionary.

    For more info on IMDS:
        https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service

    @param retries: The number of retries of the IMDS_URL.
    @param md_type: Metadata type for IMDS request.
    @param api_version: IMDS api-version to use in the request.

    @return: A dict of instance metadata containing compute and network
        info.
    z/Crawl of Azure Instance Metadata Service (IMDS))r�r�r��argsz$exception while getting metadata: %sr�N)rorp�_get_metadata_from_imdsrr�r�r&r�)r�r	rr
r�kwargsr�r9r9r:r�s��rc
Cs&d�|j|�}ddi}|tkr0|tjkr0|d}zt|t||||d�}Wn^ty�}zFt|t	�rp|j
dkrp�n"td|tj
d�iWYd}~SWYd}~n
d}~00zd	d
lm}	|	}
Wnty�t}
Yn0zt�|j�WS|
�y }z$td|j|ftj
d�WYd}~n
d}~00iS)NrErFrGz&extended=true)r�rKr�rLrr
zBIgnoring IMDS instance metadata. Get metadata from IMDS failed: %sr�r)�JSONDecodeErrorz`Ignoring non-json IMDS instance metadata response: %s. Loading non-json IMDS response failed: %s)r4rM�IMDS_EXTENDED_VER_MINr0r6r)rOr�r7r(rr&ror�Zjson.decoderr��ImportErrorrrZ	load_jsonr�)r�r
r	rrrPrKZresponser�r�Zjson_decode_errorr9r9r:r��sL�
��&
��r�cCsh|st}d}|D]R}tj�|�r|s@t�dtjjtj	jg�d}tj�
|�rXt�|�qt�
|�qdS)a(Remove Azure-specific ubuntu network config for non-primary nics.

    @param paths: List of networking scripts or directories to remove when
        present.

    In certain supported ubuntu images, static udev rules or netplan yaml
    config is delivered in the base ubuntu image to support dhcp on any
    additional interfaces which get attached by a customer at some point
    after initial boot. Since the Azure datasource can now regenerate
    network configuration as metadata reports these new devices, we no longer
    want the udev rules or netplan's 90-hotplug-azure.yaml to configure
    networking on eth1 or greater as it might collide with cloud-init's
    configuration.

    Remove the any existing extended network scripts if the datasource is
    enabled to write network per-boot.
    Fz|Removing Ubuntu extended network scripts because cloud-init updates Azure network configuration on the following events: %s.TN)�UBUNTU_EXTENDED_NETWORK_SCRIPTSr�r�rrorrrjrMZBOOT_LEGACY�isdirrZdel_dirrZ)r�Zloggedr�r9r9r:r	s�rcCs�tjddtd��v}t�d�}|tkr4Wd�dSd|}||_t|tj	d�t
j�t
j�
|d	��rvWd�dSWd�d
S1s�0YdS)r�zcheck-platform-viabilityzfound azure asset tagr�zchassis-asset-tagNTz(Non-Azure DMI asset tag '%s' discovered.r�r�F)rr�rr
r$�AZURE_CHASSIS_ASSET_TAGr�r&rorpr�r�rrb)r�r�Z	asset_tagr�r9r9r:r�C	s�
r�c@seZdZdS)r�N�r1r2r3r9r9r9r:r�U	sr�c@seZdZdS)r�Nr�r9r9r9r:r�Y	sr�cCst�|t�Sr�)r
Zlist_from_depends�datasources)Zdependsr9r9r:�get_datasource_listg	sr�)N)rw)N)r�)N)�r�r��	functoolsr�Zos.pathrHZxml.etree.ElementTreeZetreeZElementTreer��enumrrr�typingrrrrZxml.domr	r9Z	cloudinitr
rZloggingrr
rrrZcloudinit.eventrrZ
cloudinit.netrZcloudinit.net.dhcprrrrZcloudinit.reportingrZcloudinit.sources.helpersrZcloudinit.sources.helpers.azurerrrrrrr r!r"r#r$r%r&r'Zcloudinit.url_helperr(r)r*Z	getLoggerr1ror�rrZ
DEFAULT_FSr�r0ZAGENT_SEED_DIRr�rOr5rrr�ZIMDS_RETRY_CODES�partialr:rlr0r;rAr{�__annotations__r�r�rRrUrZrdrkrsrvryrzr|r�Zres_diskrpr�rr�r^ZDEF_EPHEMERAL_LABELr�r�r�r�r�r�r�rr{r}r�r]rr�r�rWr�r�r�r�r�r�r�r�rmrbr�r�r6rr�rr�r�r�r�ZDataSourceAzureNetZDEP_FILESYSTEMr�r�r9r9r9r:�<module>s0
@
�
	+




�	��
�S




j�)

7
n
.

 




I
�#�5%
�

Youez - 2016 - github.com/yon3zu
LinuXploit