509318396
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
ŠcjZ  ã               @   sê   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
mZ ddlmZmZ ejeƒZdZdd„ Zeed	œd
d„Zeje dœdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZG dd„ de	jƒZee	j ffgZ!dd„ Z"dS )zr
This file contains code used to gather the user data passed to an
instance on rootbox / hyperone cloud platforms
é    N)ÚIPv4Address)ÚsourcesÚsubpÚutil)Ú
EventScopeÚ	EventTypez
/etc/hostsc              C   s.   t jtdd�} | r tjdƒ dS tjdƒ dS )NT)Úquietz5/etc/hosts exists - setting manage_etc_hosts to FalseFz=/etc/hosts does not exists - setting manage_etc_hosts to True)r   Ú	load_fileÚ	ETC_HOSTSÚLOGÚdebug)Zhosts© r   ú(/usr/lib/python3.6/DataSourceRbxCloud.pyÚget_manage_etc_hosts   s    

r   )ÚincÚreturnc             C   s   t ttt| ƒƒ| ƒƒS )N)Ústrr   Úint)Úaddrr   r   r   r   Úincrement_ip#   s    r   )r   c             C   s   | t | dƒt | dƒgS )z–Return a list of 3 IP addresses: [addr, addr + 2, addr + 3]

    @param addr: an object that is passed to IPvAddress
    @return: list of strings
    é   é   )r   )r   r   r   r   Úget_three_ips'   s    r   c             C   s   t j dg|  ƒS )z©
    Uses the preferred cloud-init subprocess def of subp.subp
    and runs arping.  Breaking this to a separate function
    for later use in mocking and unittests
    Zarping)r   )Úcmdr   r   r   Ú_sub_arp4   s    r   c             C   s€   d}|j dkrd}xh| D ]`}ytdd	||d
 |d gƒ W q tjk
rv } ztjd|d
 |d |ƒ W Y d d }~X qX qW d S )Nz-SÚ	almalinuxÚfedoraÚcentosÚ
cloudlinuxÚrhelz-sz-cÚ2ÚsourceÚdestinationz&Failed to arping from "%s" to "%s": %s)r   r   r   r   r   )Únamer   r   ZProcessExecutionErrorr   Zwarning)ÚitemsÚdistroZsource_paramÚitemÚerrorr   r   r   Úgratuitous_arp=   s    

r(   c              C   sº   t tjdƒtjdƒ ƒ} | s dS x‚| D ]z}y"tj|tdddgd�}|rJ|S W q& tk
r| } z|jtjkrl‚ W Y dd}~X q& tjk
rž   tj	t
d	|ƒ Y q&X q&W t
jd
dj| ƒƒ dS )zAReturns False (not found or error) or a dictionary with metadata.zLABEL=CLOUDMDzLABEL=cloudmdFZvfatZfatZmsdosfs)ÚdeviceÚcallbackZmtypeNz-Failed to mount %s when looking for user dataz0Did not find RbxCloud data, searched devices: %sú,)Úsetr   Zfind_devs_withZmount_cbÚread_user_data_callbackÚOSErrorÚerrnoÚENOENTZMountFailedErrorÚlogexcr   r   Újoin)Zdevicesr)   Úrbx_dataÚerrr   r   r   Úget_mdR   s,    
r5   c             C   s   ddd„ t | ƒD ƒdœS )z‹Generate network configuration

    @param netadps: A list of network adapter settings

    @returns: A dict containing network config
    é   c                sB   g | ]:\}‰ d dj t|ƒƒˆ d jƒ ‡ fdd„ˆ d D ƒdœ‘qS )Zphysicalzeth{}Z
macaddressc          	      s>   g | ]6}d |d ˆ d d dˆ d d ˆ d d d dœ‘qS )	ZstaticÚaddressÚnetworkÚnetmaskÚautoÚgatewayZdnsZnameservers)Útyper7   r9   Zcontrolr;   Zdns_nameserversr   )Ú.0Úip)Únetadpr   r   ú
<listcomp>€   s   
z6generate_network_config.<locals>.<listcomp>.<listcomp>r>   )r<   r#   Zmac_addressZsubnets)Úformatr   Úlower)r=   Úir   )r?   r   r@   {   s   z+generate_network_config.<locals>.<listcomp>)ÚversionÚconfig)Ú	enumerate)Znetadpsr   r   r   Úgenerate_network_configq   s    rG   c             C   s  t jt jtjj| dƒdd�d�}t jtjj| dƒdd�}d|ksHd	|krXt jtd
ƒ dS |jdi ƒjdƒ}|jdi ƒjdg ƒ}d}|jdi ƒjdƒr¤|d d d }t	|d	 ƒ}||d d |d d g dœdd„ |d	 D ƒddd||dg|d|dœi|t
ƒ dœdœ}tjdƒ tj|ƒ |S )a   This callback will be applied by util.mount_cb() on the mounted
    drive.

    @param mount_dir: String representing path of directory where mounted drive
    is available

    @returns: A dict containing userdata, metadata and cfg based on metadata.
    z
cloud.jsonF)ÚfnameÚdecode)Útextz	user.dataT)rH   r   Zvmr?   z(Failed to load metadata. Invalid format.NZadditionalMetadataÚusernameZsshKeysZpasswordZsha512Z_idr#   )zinstance-idzlocal-hostnamezpublic-keysc             S   s>   g | ]6}|d  D ](}t |d d ƒD ]}|d |dœ‘q$qqS )r>   r8   r;   r7   )r!   r"   )r   )r=   r?   r>   Útargetr   r   r   r@   ·   s   z+read_user_data_callback.<locals>.<listcomp>Zdefault_userzALL=(ALL) NOPASSWD:ALL)r#   ZgecosZsudoZpasswdZlock_passwdZssh_authorized_keys)Z
ssh_pwauthZdisable_rootZsystem_infoÚnetwork_configZmanage_etc_hosts)ÚuserdataÚmetadatar(   Úcfgzreturning DATA object:)r   Z	load_jsonr	   ÚosÚpathr2   r1   r   ÚgetrG   r   r   )Z	mount_dirZ	meta_dataZ	user_datarK   Zssh_keysÚhashr8   Údatar   r   r   r-   ’   sD    	




r-   c               @   sj   e Zd ZdZejejejej	hiZ
dd„ Zdd„ Zdd„ Zedd	„ ƒZd
d„ Zdd„ Zdd„ Zdd„ ZdS )ÚDataSourceRbxCloudZRbxCloudc             C   s   t jj| |||ƒ d | _d S )N)r   Ú
DataSourceÚ__init__Úseed)ÚselfZsys_cfgr%   Úpathsr   r   r   rX   Þ   s    zDataSourceRbxCloud.__init__c             C   s   t jj| ƒ}d|| jf S )Nz%s [seed=%s])r   rW   Ú__str__rY   )rZ   Úrootr   r   r   r\   â   s    zDataSourceRbxCloud.__str__c             C   s>   t ƒ }|dkrdS |d | _|d | _|d | _|d | _dS )zw
        Metadata is passed to the launching instance which
        is used to perform instance configuration.
        FrN   rO   r(   rP   T)r5   Úuserdata_rawrO   r(   rP   )rZ   r3   r   r   r   Ú	_get_dataæ   s    



zDataSourceRbxCloud._get_datac             C   s
   | j d S )NrM   )rP   )rZ   r   r   r   rM   ô   s    z!DataSourceRbxCloud.network_configc             C   s
   | j d S )Nzpublic-keys)rO   )rZ   r   r   r   Úget_public_ssh_keysø   s    z&DataSourceRbxCloud.get_public_ssh_keysc             C   s   | j S )N)r^   )rZ   r   r   r   Úget_userdata_rawû   s    z#DataSourceRbxCloud.get_userdata_rawc             C   s   | j S )N)rP   )rZ   r   r   r   Úget_config_objþ   s    z!DataSourceRbxCloud.get_config_objc             C   s   t | j | jƒ d S )N)r(   r%   )rZ   rP   Zis_new_instancer   r   r   Úactivate  s    zDataSourceRbxCloud.activateN)Ú__name__Ú
__module__Ú__qualname__Zdsnamer   ZNETWORKr   ZBOOT_NEW_INSTANCEZBOOTZBOOT_LEGACYZdefault_update_eventsrX   r\   r_   ÚpropertyrM   r`   ra   rb   rc   r   r   r   r   rV   Ô   s   
rV   c             C   s   t j| tƒS )N)r   Zlist_from_dependsÚdatasources)Zdependsr   r   r   Úget_datasource_list  s    ri   )#Ú__doc__r/   ZloggingrQ   Zos.pathZtypingZ	ipaddressr   Z	cloudinitr   r   r   Zcloudinit.eventr   r   Z	getLoggerrd   r   r
   r   r   r   r   ZListr   r   r(   r5   rG   r-   rW   rV   ZDEP_FILESYSTEMrh   ri   r   r   r   r   Ú<module>   s*   
		!B3