Your IP : 216.73.217.13


Current Path : /usr/lib/python3/dist-packages/cloudinit/sources/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/cloudinit/sources/__pycache__/DataSourceScaleway.cpython-38.pyc

U

�Ad�%�@s*ddlZddlZddlZddlZddlZddlmZddlmZddl	m
Z
ddl	mZddl	m
Z
mZmZmZddlmZmZddlmZdd	lmZdd
lmZe�e�ZdZeded
edd�ZdZdZ dd�Z!Gdd�dej"j#�Z$dd�Z%dd�Z&Gdd�dej'�Z(e(ej)ffgZ*dd�Z+dS)�N)�HTTPConnection)�PoolManager)�dmi)�log)�net�sources�
url_helper�util)�
EventScope�	EventType)�NoDHCPLeaseError)�EphemeralDHCPv4)�DataSourceHostnamezhttp://169.254.42.42z/conf?format=jsonz/user_data/cloud-initz/vendor_data/cloud-init)�metadata_url�userdata_url�vendordata_url��
cCs>t�d�}|dkrdStj�d�r&dSt��}d|kr:dSdS)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-manufacturer�ScalewayTz/var/run/scalewayZscalewayF)rZ
read_dmi_data�os�path�existsr	Zget_cmdline)Zvendor_nameZcmdline�r�F/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceScaleway.py�on_scaleway)s	
rcs*eZdZdZ�fdd�Zddd�Z�ZS)�SourceAddressAdapterzF
    Adapter for requests to choose the local address to bind to.
    cs||_tt|�jf|�dS�N)�source_address�superr�__init__)�selfr�kwargs��	__class__rrrEszSourceAddressAdapter.__init__FcCs0tjtjtjdfg}t||||j|d�|_dS)N�)Z	num_pools�maxsize�blockr�socket_options)rZdefault_socket_options�socketZ
SOL_SOCKETZSO_REUSEPORTrrZpoolmanager)r Zconnectionsr%r&r'rrr�init_poolmanagerIs��z%SourceAddressAdapter.init_poolmanager)F)�__name__�
__module__�__qualname__�__doc__rr)�
__classcell__rrr"rr@src
Cshz(tj|d|d|dd�d�}t�|j�WStjk
rb}z|jdkrPWY�
dS�W5d}~XYnXdS)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.
    NrcSs|jdkot|jtjj�S)N�)�code�
isinstance�cause�requests�
exceptions�ConnectionError)�_�excrrr�<lambda>qs
z%query_data_api_once.<locals>.<lambda>)�data�timeout�retriesZsessionZexception_cbr/)r�readurlr	�
decode_binary�contents�UrlErrorr0)�api_addressr:�requests_session�respr7rrr�query_data_api_onceVs�

rCc	Cs�tdt|d��D]�}zPt�d||�t��}|�dtd|fd��t|||d�}t�d|�|WSt	j
k
r�}z(t�d	||�t�
d
�|}WY�qW5d}~XYqXq|�dS)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$�z*Trying to get %s data (bind on port %d)...zhttp://z0.0.0.0)r)r:rAz%s-data downloadedz%Error while trying to get %s data: %srN)�range�max�LOG�debugr3ZSessionZmountrrCrr?�warning�time�sleep)	Zapi_typer@r;r:ZportrAr9r7�last_excrrr�query_data_api~s2���

rMcs�eZdZdZejejejej	hiZ
�fdd�Zdd�Zdd�Z
edd	��Zed
d��Zdd
�Zdd�Zddd�Zedd��Zedd��Z�ZS)�DataSourceScalewayrcs�tt|��|||�t�t�|ddgi�tg�|_|jd|_|jd|_	|jd|_
t|j�dt
��|_t|j�dt��|_d|_tj|_dS)NZ
datasourcerrrrr;r:)rrNrr	Z
mergemanydictZget_cfg_by_path�BUILTIN_DS_CONFIGZds_cfg�metadata_address�userdata_address�vendordata_address�int�get�DEF_MD_RETRIESr;�DEF_MD_TIMEOUTr:�_fallback_interfacer�UNSET�_network_config)r Zsys_cfg�distro�pathsr"rrr�s��zDataSourceScaleway.__init__cCsZtj|j|j|jd�}t�t�|j	��|_
td|j|j|j�|_
td|j|j|j�|_dS)N)r:r;z	user-datazvendor-data)rr<rPr:r;�json�loadsr	r=r>�metadatarMrQZuserdata_rawrRZvendordata_raw)r rBrrr�_crawl_metadata�s$���z"DataSourceScaleway._crawl_metadatac
Cs�t�s
dS|jdkrt��|_z8t|j|j��d��tjt	j
d|jd�W5QRXWn8tk
r�}zt�
t	t|��WY�dSd}~XYnXdS)NF)Ztmp_dirzCrawl of metadata service)Zlogfunc�msg�funcT)rrWr�find_fallback_nicr
rZZget_tmp_exec_pathr	Zlog_timerGrHr_rZlogexc�str)r �errr�	_get_data�s$

��zDataSourceScaleway._get_datac	Cs�|jdkr t�dtj�tj|_|jtjkr2|jS|jdkrFt��|_dd|jd�}ddig}|jdr�|d	d|jdd
d|jdddd
d|jddd�gd�g7}||d<d|gd�|_|jS)z`
        Configure networking according to data received from the
        metadata API.
        Nz5Found None as cached _network_config. Resetting to %sZphysicalz%s)�type�namerfZdhcp4Zipv6Zstatic�address�netmaskz::�0�gateway)Znetwork�prefixrk)rfrhriZroutes�subnetsr$)�versionZconfig)	rYrGrIrrXrWrrbr^)r Znetcfgrmrrr�network_config�s6
�



����z!DataSourceScaleway.network_configcCsdSrr�r rrr�launch_indexszDataSourceScaleway.launch_indexcCs
|jdS)N�id)r^rprrr�get_instance_idsz"DataSourceScaleway.get_instance_idcCs^dd�|jdD�}d}t|�}|j�dg�D]*}|�|�s>q.|�|d|��dd��q.|S)NcSsg|]}|d�qS)�keyr)�.0rtrrr�
<listcomp>sz:DataSourceScaleway.get_public_ssh_keys.<locals>.<listcomp>Zssh_public_keyszAUTHORIZED_KEY=Ztagsr6� )r^�lenrT�
startswith�append�replace)r Zssh_keysZakeypre�plen�tagrrr�get_public_ssh_keyss
z&DataSourceScaleway.get_public_ssh_keysFcCst|jdd�S)NZhostnameF)rr^)r ZfqdnZ
resolve_ipZ
metadata_onlyrrr�get_hostnameszDataSourceScaleway.get_hostnamecCsdSrrrprrr�availability_zone!sz$DataSourceScaleway.availability_zonecCsdSrrrprrr�region%szDataSourceScaleway.region)FFF)r*r+r,Zdsnamer
ZNETWORKrZBOOT_NEW_INSTANCEZBOOTZBOOT_LEGACYZdefault_update_eventsrr_re�propertyrorqrsr~rr�r�r.rrr"rrN�s*��

'


rNcCst�|t�Sr)rZlist_from_depends�datasources)Zdependsrrr�get_datasource_list/sr�),r\rr(rJr3Zurllib3.connectionrZurllib3.poolmanagerrZ	cloudinitrrZloggingrrrr	Zcloudinit.eventr
rZcloudinit.net.dhcprZcloudinit.net.ephemeralr
Zcloudinit.sourcesrZ	getLoggerr*rGZDS_BASE_URLrOrUrVrZadaptersZHTTPAdapterrrCrMZ
DataSourcerNZDEP_FILESYSTEMr�r�rrrr�<module>s<
�(&
�