File: //lib/python3/dist-packages/cloudinit/sources/__pycache__/DataSourceCloudStack.cpython-38.pyc
U
Ӈg�, � @ s� d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl m
Z
d dlmZm
Z
d dlmZ d dlmZ d dlmZ d d lmZ e �e�ZG d
d� d�ZG dd
� d
ej�Zdd� Zdd� Zdd� ZeejejffgZ dd� Z!dS )� N)�suppress)�gaierror�getaddrinfo� inet_ntoa)�pack)�sources�subp)�
url_helper)�util)�dhcp)�ec2c @ s( e Zd ZdZdd� 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
c C s
|| _ d S �N)�virtual_router_address)�selfr � r �H/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceCloudStack.py�__init__+ s z'CloudStackPasswordServerClient.__init__c C s: t � ddddddddd d
�|�d�| j�g�\}}|�� S )NZwgetz--quietz--tries�3z --timeoutZ20z--output-document�-z--headerzDomU_Request: {0}z{0}:8080)r �formatr �strip)r Zdomu_request�output�_r r r �_do_request. s
��z*CloudStackPasswordServerClient._do_requestc C s4 | � d�}|dkrd S |dkr&td��| � d� |S )NZsend_my_password)� �saved_passwordZbad_requestz-Error when attempting to fetch root password.r )r �RuntimeError)r �passwordr r r �get_passwordC s
z+CloudStackPasswordServerClient.get_passwordN)�__name__�
__module__�__qualname__�__doc__r r r r r r r r
s r
c sf e Zd ZdZdZdZdd� Zdd� Zd� fd d
� Zdd� Z d
d� Z
dd� Zdd� Ze
dd� �Z� ZS )�DataSourceCloudStackZ
CloudStack�x �2 c C sd t j�| |||� tj�|jd�| _d| _|| _t | j�| _
| j
sJtd��d| j
� d�| _i | _
d S )NZcsZlatestzNo virtual router found!zhttp://�/)r �
DataSourcer �os�path�join�seed_dir�api_ver�distro�get_vr_address�vr_addrr �metadata_address�cfg)r Zsys_cfgr. �pathsr r r r U s zDataSourceCloudStack.__init__c
C s� t �d� t�d�}|r|S t �d� ttj��, t�� �| jd�}|rX|W 5 Q R � S W 5 Q R X t �d| jj j
� tt��2 | jj �| jj
�}|�d�p�d}|W 5 Q R � S Q R X t �d� dS )z�
Try obtaining a "domain-name" DHCP lease parameter:
- From systemd-networkd lease
- From dhclient lease
z.Try obtaining domain name from networkd leasesZ
DOMAINNAMEzHCould not obtain FQDN from networkd leases. Falling back to ISC dhclientzdomain-namezBCould not obtain FQDN from ISC dhclient leases. Falling back to %sNzNo dhcp leases found)�LOG�debugr �networkd_get_option_from_leasesr �NoDHCPLeaseMissingDhclientError�IscDhclient�get_key_from_latest_leaser. �dhcp_client�client_name�FileNotFoundError�get_newest_leaseZfallback_interface�get)r �
domainnameZdomain_name�latest_leaser r r �_get_domainnamec s4
� ��
�
z$DataSourceCloudStack._get_domainnameFc sn t � �|||�}|rjd|jkrjt�d� | �� }|r^|j� d|� �}t�d|� t�||j�S t�d|� |S )z�
Returns instance's hostname / fqdn
First probes the parent class method.
If fqdn is requested, and the parent method didn't return it,
then attach the domain-name from DHCP response.
�.zFQDN requestedzObtained the following FQDN: %szNCould not determine domain name for FQDN. Fall back to hostname as an FQDN: %s) �super�get_hostname�hostnamer4 r5 rA r ZDataSourceHostnameZ
is_default)r ZfqdnZ
resolve_ipZ
metadata_onlyrE r? �� __class__r r rD � s
�z!DataSourceCloudStack.get_hostnamec C s~ | � � }|jdkrdS t�| jd�g}t�� }tj||j|jt j
d�\}}|r\t �d|� nt �d|t
t�� | �� t|�S )Nr Fzlatest/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_urlr1 �time� monotonicZwait_for_urlZtimeout_secondsr4 Zwarningr5 Zcritical�int�bool)r Z
url_paramsrH �
start_timeZurlZ _responser r r �wait_for_metadata_service� s.
���
�z.DataSourceCloudStack.wait_for_metadata_servicec C s | j S r )r2 �r r r r �get_config_obj� s z#DataSourceCloudStack.get_config_objc C s i }t j|| jd d�r>|d | _|d | _t�d| j� dS z�| �� sNW dS t� � }t
�| j| j
�| _t
�| j| j
�| _t�dtt� � | �� t| j�}z|�� }W n$ tk
r� t �td | j� Y nX |r�d|d
did�| _W dS tk
�r t �td| j
� Y dS X d S )
Nr'