o
    w7e                      @   s   d dl Z d dlZd dlmZmZmZ d dlmZ d dlm	Z
 d dlmZmZ d dlmZ d dlmZ d dlmZ d d	lmZ e
eZG d
d dZG dd dejZdd Zdd Zdd ZeejejffgZdd Z dS )    N)gaierrorgetaddrinfo	inet_ntoa)pack)log)sourcessubp)
url_helper)util)dhcp)ec2c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	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__*      
z'CloudStackPasswordServerClient.__init__c                 C   s:   t  ddddddddd	d
|d| jg\}}| S )Nwgetz--quietz--tries3z	--timeout20z--output-document-z--headerzDomU_Request: {0}z{0}:8080)r   formatr   strip)r   domu_requestoutput_r   r   r   _do_request-   s   
z*CloudStackPasswordServerClient._do_requestc                 C   s4   |  d}|dv rd S |dkrtd|  d |S )Nsend_my_password) saved_passwordbad_requestz-Error when attempting to fetch root password.r!   )r   RuntimeError)r   passwordr   r   r   get_passwordB   s   

z+CloudStackPasswordServerClient.get_passwordN)__name__
__module____qualname____doc__r   r   r%   r   r   r   r   r      s
    r   c                   @   sL   e Zd ZdZdZdZdd Zdd Zdd	 Zd
d Z	dd Z
edd ZdS )DataSourceCloudStack
CloudStackx   2   c                 C   sX   t j| ||| tj|jd| _d| _t | _	| j	s t
dd| j	f | _i | _d S )NcslatestzNo virtual router found!z
http://%s/)r   
DataSourcer   ospathjoinseed_dirapi_verget_vr_addressvr_addrr#   metadata_addresscfg)r   sys_cfgdistropathsr   r   r   r   T   s   
zDataSourceCloudStack.__init__c                 C   s   |   }|jdkrdS t| jdg}t }tj||j|jtj	d\}}|r1t
d| t|S td|tt |  t|S )Nr   Fzlatest/meta-data/instance-id)urlsmax_waittimeout	status_cbzUsing metadata source: '%s'z>Giving up on waiting for the metadata from %s after %s seconds)get_url_paramsmax_wait_secondsuhelpcombine_urlr8   timewait_for_urltimeout_secondsLOGwarningdebugcriticalintbool)r   
url_paramsr=   
start_timeurl	_responser   r   r   wait_for_metadata_service`   s.   

	z.DataSourceCloudStack.wait_for_metadata_servicec                 C   s   | j S r   )r9   r   r   r   r   get_config_obj   s   z#DataSourceCloudStack.get_config_objc                 C   s  i }t j|| jd dr|d | _|d | _td| j dS zV|  s'W dS t }t	
| j| j| _t	| j| j| _tdtt |  t| j}z| }W n tyg   t td	| j Y W dS w |rsd|d
did| _W dS  ty   t td| j Y dS w )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 %sexpire)
ssh_pwauthr$   chpasswdz(Failed fetching from metadata service %s)r
   read_optional_seedr4   userdata_rawmetadatarH   rJ   rR   rE   r   get_instance_userdatar5   r8   get_instance_metadatarL   r   r7   r%   	Exceptionlogexcr9   )r   seed_retrO   password_clientset_passwordr   r   r   	_get_data   s\   


zDataSourceCloudStack._get_datac                 C   
   | j d S )Nzinstance-idr\   rS   r   r   r   get_instance_id   r   z$DataSourceCloudStack.get_instance_idc                 C   re   )Nzavailability-zonerf   rS   r   r   r   availability_zone   s   
z&DataSourceCloudStack.availability_zoneN)r&   r'   r(   dsnameurl_max_waiturl_timeoutr   rR   rT   rd   rg   propertyrh   r   r   r   r   r*   L   s    0r*   c                  C   s>   zt dd} W n ty   td Y d S w | d d d S )Nzdata-serverP   zDNS Entry data-server not foundr      )r   r   rH   rJ   )addrinfor   r   r   get_data_server   s   
rp   c                  C   s^   t d } | D ]#}|d}|d dkr,ttdt|d d}td| |  S q	d S )	Nz/proc/net/route	   00000000z<L      z"Found default route, gateway is %s)	r
   	load_file
splitlinessplitr   r   rL   rH   rJ   )lineslineitemsgwr   r   r   get_default_gateway   s   
r}   c                  C   sz   t  } | rtd|  | S td} | rtd|  | S tj }|s+td t S tj|}| s;td t S | S )Nz4Found metadata server '%s' via data-server DNS entrySERVER_ADDRESSz-Found SERVER_ADDRESS '%s' via networkd_leasesz*No lease file found, using default gatewayz$No DHCP found, using default gateway)	rp   rH   rJ   r   networkd_get_option_from_leasesIscDhclientget_latest_leaser}   !parse_dhcp_server_from_lease_file)latest_address
lease_filer   r   r   r6      s,   



r6   c                 C   s   t | tS r   )r   list_from_dependsdatasources)dependsr   r   r   get_datasource_list   s   r   )!r1   rE   socketr   r   r   structr   	cloudinitr   loggingr   r   r	   rC   r
   cloudinit.netr   cloudinit.sources.helpersr   	getLoggerr&   rH   r   r0   r*   rp   r}   r6   DEP_FILESYSTEMDEP_NETWORKr   r   r   r   r   r   <module>   s&   
.n&