U
    Ó‡g  ã                   @   sÀ   d dl Z d dlZd dlmZ d dlmZmZ d dlmZm	Z	 d dl
mZ d dl
mZmZ e e¡ZdZG dd	„ d	ejƒZd
d„ Zdd„ ZG dd„ deƒZeejffeejejffgZdd„ ZdS )é    N)ÚList)ÚdmiÚsources)Ú
EventScopeÚ	EventType)ÚDataSourceEc2)ÚDataSourceHostnameÚNicOrderzAlibaba Cloud ECSc                       sv   e Zd ZU dZdgZdZg Zee e	d< e
dd„ ƒZ‡ fdd„Zed	d
œ‡ fdd„Zddd„Zdd„ Zdd„ Z‡  ZS )ÚDataSourceAliYunZAliYunzhttp://100.100.100.200z
2016-01-01Úextended_metadata_versionsc                 C   s   dS )NzX-aliyun-ecs-metadata-token© ©Úselfr   r   úD/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceAliYun.pyÚimdsv2_token_put_header   s    z(DataSourceAliYun.imdsv2_token_put_headerc                    sB   t t| ƒ |||¡ t | j¡| _| jtj  t	j
¡ tj| _d S ©N)Úsuperr
   Ú__init__ÚcopyÚdeepcopyZdefault_update_eventsr   ZNETWORKÚaddr   ZBOOTr	   ÚNIC_NAMEÚ_fallback_nic_order)r   Zsys_cfgZdistroÚpaths©Ú	__class__r   r   r      s    zDataSourceAliYun.__init__N)Úci_pkl_versionÚreturnc                    s   t ƒ  |¡ tj| _d S r   )r   Ú	_unpickler	   r   r   )r   r   r   r   r   r   %   s    zDataSourceAliYun._unpickleFc                 C   s*   | j  d¡}d}|d kr d}d}t||ƒS )NÚhostnameFzlocalhost.localdomainT)ÚmetadataÚgetr   )r   ZfqdnZ
resolve_ipZmetadata_onlyr   Z
is_defaultr   r   r   Úget_hostname)   s    zDataSourceAliYun.get_hostnamec                 C   s   t | j di ¡ƒS )Nzpublic-keys)Úparse_public_keysr    r!   r   r   r   r   Úget_public_ssh_keys1   s    z$DataSourceAliYun.get_public_ssh_keysc                 C   s   t ƒ rtjjS tjjS d S r   )Ú
_is_aliyunÚEC2Z
CloudNamesZALIYUNZNO_EC2_METADATAr   r   r   r   Ú_get_cloud_name4   s    z DataSourceAliYun._get_cloud_name)FFF)Ú__name__Ú
__module__Ú__qualname__ZdsnameZmetadata_urlsZmin_metadata_versionr   r   ÚstrÚ__annotations__Úpropertyr   r   Úintr   r"   r$   r'   Ú__classcell__r   r   r   r   r
      s   


r
   c                   C   s   t  d¡tkS )Nzsystem-product-name)r   Zread_dmi_dataÚALIYUN_PRODUCTr   r   r   r   r%   ;   s    r%   c                 C   sŽ   g }|   ¡ D ]|\}}t|tƒr.| | ¡ ¡ qt|tƒrD| |¡ qt|tƒr| dg ¡}t|tƒrt| | ¡ ¡ qt|tƒr| |¡ q|S )Nzopenssh-key)	ÚitemsÚ
isinstancer+   ÚappendÚstripÚlistÚextendÚdictr!   )Zpublic_keysÚkeysZ_key_idZkey_bodyÚkeyr   r   r   r#   ?   s    




r#   c                   @   s   e Zd ZdZdZdS )ÚDataSourceAliYunLocalay  Datasource run at init-local which sets up network to query metadata.

    In init-local, no network is available. This subclass sets up minimal
    networking with dhclient on a viable nic so that it can talk to the
    metadata service. If the metadata service provides network configuration
    then render the network configuration for that instance based on metadata.
    TN)r(   r)   r*   Ú__doc__Zperform_dhcp_setupr   r   r   r   r:   O   s   r:   c                 C   s   t  | t¡S r   )r   Zlist_from_dependsÚdatasources)Zdependsr   r   r   Úget_datasource_listc   s    r=   )r   ZloggingÚtypingr   Z	cloudinitr   r   Zcloudinit.eventr   r   Zcloudinit.sourcesr   r&   r   r	   Z	getLoggerr(   ZLOGr0   r
   r%   r#   r:   ZDEP_FILESYSTEMZDEP_NETWORKr<   r=   r   r   r   r   Ú<module>   s    
*
þ