509318396
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
\¼me©2  ã            	   @   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 rÂtƒ }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  yòt 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@   

  /
