
    }fg                     2   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 Zded	efd
Zd	ej.                  e   fdZd Zd Zd Zd Zd Z G d de	j<                        Zee	j@                  ffgZ!d Z"y)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                      t        j                  t        d      } | rt        j	                  d       yt        j	                  d       y)NT)quietz5/etc/hosts exists - setting manage_etc_hosts to FalseFz=/etc/hosts does not exists - setting manage_etc_hosts to True)r   load_text_file	ETC_HOSTSLOGdebug)hostss    F/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceRbxCloud.pyget_manage_etc_hostsr      s7    	6E		IJIIMN    increturnc           	      T    t        t        t        t        |             |z               S N)strr   int)addrr   s     r   increment_ipr   #   s!    {3{401C7899r   c                 4    | t        | d      t        | d      gS )zReturn 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   s    r   get_three_ipsr   '   s%     	T1T1 r   c                 4    t        j                   dg| z         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
    arping)r   )cmds    r   _sub_arpr"   4   s     99hZ#%&&r   c           	          d}|j                   dv rd}| D ]  }	 t        dd||d   |d   g        y # t        j                  $ r(}t        j                  d|d   |d   |       Y d }~Sd }~ww xY w)	Nz-S)fedoracentosrhelz-sz-c2sourcedestinationz&Failed to arping from "%s" to "%s": %s)namer"   r   ProcessExecutionErrorr   warning)itemsdistrosource_paramitemerrors        r   gratuitous_arpr2   =   s    L{{22 	sL$x.$}:MN
 )) 		 KK8X]#	 			s   2A-A((A-c                     t        t        j                  d      t        j                  d      z         } | sy| D ]'  }	 t        j                  |t        g d      }|r|c S ) t        j                  dd	j                  |              y# t
        $ r(}|j                  t        j                  k7  r Y d}~|d}~wt        j                  $ r t        j                  t        d|       Y w xY w)
zAReturns False (not found or error) or a dictionary with metadata.zLABEL=CLOUDMDzLABEL=cloudmdF)vfatfatmsdosfs)devicecallbackmtypeNz-Failed to mount %s when looking for user dataz0Did not find RbxCloud data, searched devices: %s,)setr   find_devs_withmount_cbread_user_data_callbackOSErrorerrnoENOENTMountFailedErrorlogexcr   r   join)devicesr7   rbx_dataerrs       r   get_mdrH   R   s    O,


o
.	/G  	}}00H
  " II:CHHW<M   	yyELL( )$$ 	KKDf	s   !B

	C*B661C*)C*c                    dt        |       D cg c]e  \  }}ddj                  t        |            |d   j                         |d   D cg c]#  }d|d   |d   d	   d
|d   d   |d   d   d   d% c}dg c}}}dS c c}w c c}}}w )zGenerate network configuration

    @param netadps: A list of network adapter settings

    @returns: A dict containing network config
       physicalzeth{}
macaddressipstaticaddressnetworknetmaskautogatewaydnsnameservers)typerO   rQ   controlrS   dns_nameservers)rV   r*   mac_addresssubnets)versionconfig)	enumerateformatr   lower)netadpsinetadprM   s       r   generate_network_configrc   q   s     ( 'w/'
 
& 6# #s1v.%l399; %Tl  !)#%i=#))#4Y#?#)#))#4Y#?+1)+<U+C),		
 
s   ;B
(B4B
B
c                 z   t        j                  t        j                  t        j                  j                  | d                  }t        j                  t        j                  j                  | d      d      }d|vsd|vrt        j                  t        d	       y
|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 	cg c]*  }|d   D ]   }t        |d   d         D ]
  }	|d   |	d " , c}	}}ddd||dg|d|di|t               dd}
t        j                  d       t        j                  |
       |
S c c}	}}w )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.json)fname)textz	user.dataT)re   r
   vmrb   z(Failed to load metadata. Invalid format.NadditionalMetadatausernamesshKeyspasswordsha512_idr*   )zinstance-idzlocal-hostnamepublic-keysrM   rP   rS   rO   )r(   r)   default_userzALL=(ALL) NOPASSWD:ALLF)r*   gecossudopasswdlock_passwdssh_authorized_keys)
ssh_pwauthdisable_rootsystem_infonetwork_configmanage_etc_hosts)userdatametadatar2   cfgzreturning DATA object:)r   	load_jsonload_binary_fileospathrD   r   rC   r   getrc   r   r   r   )	mount_dir	meta_data	user_datari   ssh_keyshashrP   rb   rM   targetdatas              r   r>   r>      s    ""i)NOI ##ggll9k2$I 9	 9CCD}}126:::FH}}126::9bIHD}})2.22:>-.z:8D%i&9:G $T?51'of5
 $H-
 
Tl
 'y(9)(DE	
  )}V<
<
<
  $%56"#(+3!	 & 4 6
D> II&'IIdOK7
s   ;/F6c                       e Zd ZdZej
                  ej                  ej                  ej                  hiZ
d Zd Zd Zed        Zd Zd Zd Zd	 Zy
)DataSourceRbxCloudRbxCloudc                 r    t         j                  j                  | |||       d | _        d | _        d | _        y r   )r   
DataSource__init__seedr2   r|   )selfsys_cfgr.   pathss       r   r   zDataSourceRbxCloud.__init__   s2    ##D'65A	"r   c                 d    t         j                  j                  |       }|d| j                  dS )Nz [seed=])r   r   __str__r   )r   roots     r   r   zDataSourceRbxCloud.__str__   s'    !!))$/!%tyy11r   c                 r    t               }|du ry|d   | _        |d   | _        |d   | _        |d   | _        y)zw
        Metadata is passed to the launching instance which
        is used to perform instance configuration.
        Frz   r{   r2   r|   T)rH   userdata_rawr{   r2   r|   )r   rF   s     r   	_get_datazDataSourceRbxCloud._get_data   sK    
 8u$Z0 ,&'78E?r   c                      | j                   d   S )Nrx   r|   r   s    r   rx   z!DataSourceRbxCloud.network_config   s    xx())r   c                      | j                   d   S )Nrn   )r{   r   s    r   get_public_ssh_keysz&DataSourceRbxCloud.get_public_ssh_keys   s    }}]++r   c                     | j                   S r   )r   r   s    r   get_userdata_rawz#DataSourceRbxCloud.get_userdata_raw   s       r   c                     | j                   S r   r   r   s    r   get_config_objz!DataSourceRbxCloud.get_config_obj   s    xxr   c                 D    t        | j                   | j                         y r   )r2   r.   )r   r|   is_new_instances      r   activatezDataSourceRbxCloud.activate  s    t**DKK8r   N)__name__
__module____qualname__dsnamer   NETWORKr   BOOT_NEW_INSTANCEBOOTBOOT_LEGACYdefault_update_eventsr   r   r   propertyrx   r   r   r   r    r   r   r   r      sm    F''NN!!
2 * *,!9r   r   c                 6    t        j                  | t              S r   )r   list_from_dependsdatasources)dependss    r   get_datasource_listr     s    $$Wk::r   )#__doc__r@   loggingr   os.pathtyping	ipaddressr   	cloudinitr   r   r   cloudinit.eventr   r   	getLoggerr   r   r   r   r   r   r   Listr   r"   r2   rH   rc   r>   r   r   DEP_FILESYSTEMr   r   r   r   r   <module>r      s      	   ! ) ) 1g!	:C :C :
6;;s+ 
'*>B=@09++ 09j '0023;r   