Your IP : 216.73.216.31


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

U

�Adj'�@s�ddlZddlZddlmZmZmZddlmZddlm	Z
ddlmZmZddlm
ZddlmZddlmZdd	lmZe
�e�ZGd
d�d�ZGdd
�d
ej�Zdd�Zdd�Zdd�Zddd�Zdd�Zeejej ffgZ!dd�Z"dS)�N)�gaierror�getaddrinfo�	inet_ntoa)�pack)�log)�sources�subp)�
url_helper)�util)�dhcp)�ec2c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�CloudStackPasswordServerClienta�
    Implements password fetching from the CloudStack password server.

    http://cloudstack-administration.readthedocs.org/
       en/latest/templates.html#adding-password-management-to-your-templates
    has documentation about the system.  This implementation is following that
    found at
    https://github.com/shankerbalan/cloudstack-scripts/
       blob/master/cloud-set-guest-password-debian
    cCs
||_dS�N)�virtual_router_address)�selfr�r�H/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceCloudStack.py�__init__*sz'CloudStackPasswordServerClient.__init__cCs:t�ddddddddd	d
�|�d�|j�g�\}}|��S)NZwgetz--quietz--tries�3z	--timeoutZ20z--output-document�-z--headerzDomU_Request: {0}z{0}:8080)r�formatr�strip)rZdomu_request�output�_rrr�_do_request-s
��z*CloudStackPasswordServerClient._do_requestcCs4|�d�}|dkrdS|dkr&td��|�d�|S)NZsend_my_password)��saved_passwordZbad_requestz-Error when attempting to fetch root password.r)r�RuntimeError)r�passwordrrr�get_passwordBs

z+CloudStackPasswordServerClient.get_passwordN)�__name__�
__module__�__qualname__�__doc__rrrrrrrr
sr
c@sLeZdZdZdZdZdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
edd��ZdS)�DataSourceCloudStackZ
CloudStack�x�2cCsXtj�||||�tj�|jd�|_d|_t�|_	|j	s@t
d��d|j	f|_i|_dS)NZcsZlatestzNo virtual router found!z
http://%s/)
r�
DataSourcer�os�path�join�seed_dir�api_ver�get_vr_address�vr_addrr�metadata_address�cfg)rZsys_cfgZdistro�pathsrrrrTszDataSourceCloudStack.__init__cCs~|��}|jdkrdSt�|jd�g}t��}tj||j|jtj	d�\}}|r\t�
d|�nt�d|tt��|��t
|�S)NrFzlatest/meta-data/instance-id)�urlsZmax_waitZtimeoutZ	status_cbzUsing metadata source: '%s'z>Giving up on waiting for the metadata from %s after %s seconds)Zget_url_paramsZmax_wait_seconds�uhelpZcombine_urlr/�timeZwait_for_urlZtimeout_seconds�LOGZwarning�debugZcritical�int�bool)rZ
url_paramsr2�
start_timeZurlZ	_responserrr�wait_for_metadata_service`s.
���
�z.DataSourceCloudStack.wait_for_metadata_servicecCs|jSr)r0�rrrr�get_config_objsz#DataSourceCloudStack.get_config_objcCsi}tj||jdd�r>|d|_|d|_t�d|j�dSz�|��sNWdSt��}t	�
|j|j�|_t	�
|j|j�|_t�dtt��|��t|j�}z|��}Wn$tk
r�t�td	|j�YnX|r�d|d
did�|_WdStk
�rt�td|j�YdSXdS)
N�/)�basez	user-dataz	meta-dataz%Using seeded cloudstack data from: %sTFz)Crawl of metadata service took %s secondsz/Failed to fetch password from virtual router %sZexpire)Z
ssh_pwauthrZchpasswdz(Failed fetching from metadata service %s)r
Zread_optional_seedr+Zuserdata_raw�metadatar5r6r:r4rZget_instance_userdatar,r/Zget_instance_metadatar7r
r.r�	ExceptionZlogexcr0)rZseed_retr9Zpassword_clientZset_passwordrrr�	_get_data�s\

���
�
���zDataSourceCloudStack._get_datacCs
|jdS)Nzinstance-id�r?r;rrr�get_instance_id�sz$DataSourceCloudStack.get_instance_idcCs
|jdS)Nzavailability-zonerBr;rrr�availability_zone�sz&DataSourceCloudStack.availability_zoneN)
r r!r"ZdsnameZurl_max_waitZurl_timeoutrr:r<rArC�propertyrDrrrrr$Ls0r$cCsDztdd�}Wn tk
r.t�d�YdSX|dddSdS)Nzdata-server�PzDNS Entry data-server not foundr�)rrr5r6)Zaddrinforrr�get_data_server�s
rHcCs^t�d���}|D]F}|�d�}|ddkrttdt|dd���}t�d|�|SqdS)	Nz/proc/net/route�	�Z00000000z<L��z"Found default route, gateway is %s)	r
Z	load_file�
splitlines�splitrrr7r5r6)�lines�line�itemsZgwrrr�get_default_gateway�s

rRcCsJdddg}|D]6}tj�|�rtt�|��dkrt�d|�|SqdS)Nz/var/lib/dhclientz
/var/lib/dhcpz/var/lib/NetworkManagerrzUsing %s lease directory)r(r)�exists�len�listdirr5r6)Zsupported_dirs�drrr�get_dhclient_d�s�
rWcCs�|dkrt�}|sdSt�|�}d}d}|D]P}|�d�r<q,|�d�sR|�d�sRq,tj�||�}tj�|�}||kr,|}|}q,|S)N���Z	dhclient6z.leasez.leases)rWr(rU�
startswith�endswithr)r*�getmtime)Zlease_dZlease_filesZlatest_mtimeZlatest_file�fnameZabs_path�mtimerrr�get_latest_lease�s$


r^c	Cs�t�}|rt�d|�|St�d�}|r8t�d|�|St�}|sRt�d�t�St|d��L}|D]@}d|krb|�d��	d�}t
|�d	krb|d	}t�d
|�|}qbW5QRX|s�t�d�t�S|S)Nz4Found metadata server '%s' via data-server DNS entryZSERVER_ADDRESSz-Found SERVER_ADDRESS '%s' via networkd_leasesz*No lease file found, using default gateway�rzdhcp-server-identifierz ;
� rKzFound DHCP identifier %sz$No DHCP found, using default gateway)rHr5r6rZnetworkd_get_option_from_leasesr^rR�openrrNrT)Zlatest_addressZ
lease_file�fdrPZwordsZdhcptokrrrr-s<�
�

r-cCst�|t�Sr)rZlist_from_depends�datasources)Zdependsrrr�get_datasource_list3srd)N)#r(r4ZsocketrrrZstructrZ	cloudinitrZloggingrrr	r3r
Z
cloudinit.netrZcloudinit.sources.helpersrZ	getLoggerr r5r
r'r$rHrRrWr^r-ZDEP_FILESYSTEMZDEP_NETWORKrcrdrrrr�<module>s(
.n

!-�