
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\me2              	   @   s.  d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZ d dlmZmZmZ d dlmZmZ d dlmZ d d	lmZmZmZ ejeZd
ddddddddddddgdZG dd de	Z G dd dej!Z"G dd de"Z#e#ej$ffe"ej$ej%ffgZ&eej! dddZ'dS )    N)	b64decode)suppress)Enum)AnyListTupleUnion)sources
url_helperutil)find_fallback_nicget_interfaces_by_mac)EphemeralIPNetwork)get_dmi_configget_local_instance_idis_on_akamaizhttp://169.254.169.254zhttp://[fd00:a9fe:a9fe::1])ipv4ipv6z	/v1/tokenz/v1/instancez/v1/user-data)tokenmetadatauserdataTzf2:3)	base_urlspathsallow_local_stageallow_init_stage
allow_dhcp
allow_ipv4
allow_ipv6preferred_mac_prefixesc               @   s   e Zd ZdZdZdZdZdS )MetadataAvailabilityResultzj
    Used to indicate how this instance should behave based on the availability
    of metadata to it
    r         N)__name__
__module____qualname____doc__NOT_AVAILABLE	AVAILABLEDEFER r)   r)   &/usr/lib/python3.6/DataSourceAkamai.pyr   -   s   r   c               @   s   e Zd ZdZdZdd ZdeeedddZe	dd	d
Z
e	dddZe	dddZeeeeef ef  dddZdeedddZedddZedddZdS )DataSourceAkamaiAkamaiFc             C   sJ   t jd tjj| ||| t | _tjt	 tj
|ddgi tg| _d S )NzSetting up Akamai DataSourceZ
datasourcer,   )LOGdebugr	   
DataSource__init__dictr   r   Zmergemanydictr   Zget_cfg_by_pathBUILTIN_DS_CONFIGds_cfg)selfsys_cfgdistror   r)   r)   r*   r0   <   s    
zDataSourceAkamai.__init__)	path_nameuse_v6returnc             C   s\   || j d krtdj|d}|s0| j d  r4d}| j d | }| j d | }dj||S )z
        Looks up the path for a given name and returns a full url for it.  If
        use_v6 is passed in, the IPv6 base url is used; otherwise the IPv4 url
        is used unless IPv4 is not allowed in ds_cfg
        r   zUnknown path name {}r   r   r   r   z{}{})r3   
ValueErrorformat)r4   r7   r8   Zversion_keyZbase_urlpathr)   r)   r*   
_build_urlN   s    zDataSourceAkamai._build_url)r9   c             C   sZ   | j d  r| j d  s0| j d  r@| j d  r@tjd tjS | jrN| j S | j S dS )z
        Returns whether metadata should be retrieved at this stage, at the next
        stage, or never, in the form of a MetadataAvailabilityResult.
        r   r   r   r   z*Configuration prohibits fetching metadata.N)r3   r-   infor   r&   local_stage_should_fetch_data_local_should_fetch_data_network)r4   r)   r)   r*   _should_fetch_data`   s    
z#DataSourceAkamai._should_fetch_datac             C   sH   | j d stjd tjS | j d  rB| j d  rBtjd tjS tjS )z
        Returns whether metadata should be retrieved during the local stage, or
        if it should wait for the init stage.
        r   z)Configuration prohibits local stage setupr   r   z9Configuration does not allow for ephemeral network setup.)r3   r-   r>   r   r(   r'   )r4   r)   r)   r*   r@   t   s    

z)DataSourceAkamai._should_fetch_data_localc             C   s    | j d stjd tjS tjS )zS
        Returns whether metadata should be fetched during the init stage.
        r   z1Configuration does not allow for init stage setup)r3   r-   r>   r   r(   r'   )r4   r)   r)   r*   rA      s    

z+DataSourceAkamai._should_fetch_data_networkc                s   g }| j rt }d}| jd }x0|j D ]$\ }t fdd|D r(|}P q(W |dkrhtjd t }g }| jd r|jt	| j
|ddd	df | jd
 r| jd r|jt	| j
|dddf n4| jd r|jt df | jd
 r|jt df |S )z
        Returns a list of context managers which should be tried when setting
        up a network context.  If we're running in init mode, this return a
        noop since networking should already be configured.
        Nr   c                s   g | ]} j |qS r)   )
startswith).0prefix)macr)   r*   
<listcomp>   s    zBDataSourceAkamai._get_network_context_managers.<locals>.<listcomp>zGFailed to find default interface, attempting DHCP on fallback interfacer   FT)r   r   r   r   )r   )r?   r   r3   itemsanyr-   warningr   appendr   r6   noop)r4   network_context_managersZ
interfacesZ	interfaceZpreferred_prefixesinfr)   )rF   r*   _get_network_context_managers   sN    





z.DataSourceAkamai._get_network_context_managers)r8   r9   c             C   s4  yt j| jd|dddddddid	}|jd
krBtjd|j dS t|}t j| jd|ddddd|dd}tjt|| _	t j| jd|ddddd|id}t|| _
yt| j
j | _
W n0 tjk
r } ztjd| W Y dd}~X nX W n< t jk
r. } ztjd|rdnd| dS d}~X nX dS )z
        Runs through the sequence of requests necessary to retrieve our
        metadata and user data, creating a token for use in doing so, capturing
        the results.
        r   )r8   ZPUT   r!      zMetadata-Token-Expiry-SecondsZ300)Zrequest_methodtimeoutsec_betweenretriesheaders   z-Fetching token returned %s; not fetching dataTr   zapplication/json)ZAcceptzMetadata-Token)rR   rS   rT   rU   r   zMetadata-Tokenz*Failed to base64 decode userdata due to %sNz1Failed to retrieve metadata using IPv%s due to %s64F)r
   Zreadurlr=   coder-   r>   strjsonloadsr   Zuserdata_rawr   decodebinasciiErrorrJ   ZUrlError)r4   r8   Ztoken_responser   r   r   er)   r)   r*   _fetch_metadata   sP    

"z DataSourceAkamai._fetch_metadatac             C   s   t jd t st jd dS t }d|i| _| j }|tjkrf|tj	krXt jd dS t jd dS | j
 }xP|D ]>\}}|, | j|d}|r| jjd	|| jd< P W d
Q R X qtW t jd dS )zW
        Overrides _get_data in the DataSource class to actually retrieve data
        z#Getting data from Akamai DataSourcez#Not running on Akamai, not running.Fzinstance-idz5Metadata is not available, returning local data only.TzFConfigured not to fetch data at this stage; waiting for a later stage.)r8   idNzHFailed to contact metadata service, falling back to local metadata only.)r-   r.   r   r>   r   r   rB   r   r'   r&   rO   ra   getrJ   )r4   Zlocal_instance_idZavailabilityrM   Zmanagerr8   doner)   r)   r*   	_get_data'  s6    




zDataSourceAkamai._get_datac             C   s   t j| j dS )zj
        A local-only check to see if the instance id matches the id we see on
        the system
        zsystem-serial-number)r	   Zinstance_id_matches_system_uuidZget_instance_id)r4   r5   r)   r)   r*   check_instance_id[  s    z"DataSourceAkamai.check_instance_idN)F)F)r"   r#   r$   Zdsnamer?   r0   rZ   boolr=   r   rB   r@   rA   r   r   r   r   r   rO   ra   re   rf   r)   r)   r)   r*   r+   8   s   "MD4r+   c               @   s   e Zd ZdZdZdS )DataSourceAkamaiLocalz
    A subclass of DataSourceAkamai that runs the same functions, but during the
    init-local stage.  This allows configuring networking via cloud-init, as
    networking hasn't been configured yet.
    TN)r"   r#   r$   r%   r?   r)   r)   r)   r*   rh   e  s   rh   )r9   c             C   s   t j| tS )N)r	   Zlist_from_dependsdatasources)Zdependsr)   r)   r*   get_datasource_list  s    rj   )(r^   r[   Zloggingbase64r   
contextlibr   rL   enumr   Ztypingr   r   r   r   Z	cloudinitr	   r
   r   Zcloudinit.netr   r   Zcloudinit.net.ephemeralr   Z cloudinit.sources.helpers.akamair   r   r   Z	getLoggerr"   r-   r2   r   r/   r+   rh   ZDEP_FILESYSTEMZDEP_NETWORKri   rj   r)   r)   r)   r*   <module>   s@   

  /
