
    x[hg                     6   S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSKJ	r	J
r
Jr  SSKJrJr  \R                  " \5      rSrS rS\S	\4S
 jrS	\R.                  \   4S jrS rS rS rS rS r " S S\	R<                  5      r\\	R@                  44/r!S r"g)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                      [         R                  " [        SS9n U (       a  [        R	                  S5        g[        R	                  S5        g)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    [        [        [        [        U 5      5      U-   5      5      $ N)strr   int)addrr   s     r   increment_ipr   #   s!    {3{401C7899r   c                 4    U [        U S5      [        U S5      /$ )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                 6    [         R                   " S/U -   5      $ )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           	          SnUR                   S;   a  SnU  H  n [        SSX#S   US   /5        M     g ! [        R                   a)  n[        R                  SUS   US   U5         S nAMV  S nAff = f)	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	sLx.$}:MN 
 )) 		 KK8X]#	 			s   5A2	A--A2c                     [        [        R                  " S5      [        R                  " S5      -   5      n U (       d  gU  H+  n [        R                  " U[        / SQS9nU(       a  Us  $ M-     [        R                  SS	R                  U 5      5        g! [
         a*  nUR                  [        R                  :w  a  e  SnAM  SnAf[        R                   a     [        R                  " [        SU5         M  f = f)
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< C3C<;C<c                 &   S[        U 5       VVVs/ s Hh  u  pSSR                  [        U5      5      US   R                  5       US    Vs/ s H$  nSUS   US   S	   S
US   S   US   S   S   S.PM&     snS.PMj     snnnS.$ s  snf s  snnnf )zGenerate 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/'
& 0	# #s1v.%l399; %Tl + !)#%i=#))#4Y#?#)#))#4Y#?+1)+<U+C),	 +	$ 0'
 
s   ;B
+B7	B
B
c                    [         R                  " [         R                  " [        R                  R                  U S5      S9S9n[         R                  " [        R                  R                  U S5      SS9nSU;  d  SU;  a  [         R                  " [        S	5        g
UR                  S0 5      R                  S5      nUR                  S0 5      R                  S/ 5      nS
nUR                  S0 5      R                  S5      (       a  US   S   S   n[        US   5      nUUS   S   US   S   / S.US    VVV	s/ s H1  nUS     H$  n[        US   S   5        H  n	US   U	S.PM     M&     M3     sn	nnSSSUUS/USUS.0U[        5       S.S.n
[        R                  S5        [        R                  U
5        U
$ s  sn	nnf )zThis 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< F =" =-
  $%56"#(+3!	 & 4 6
D> II&'IIdOK7
s   ?8Gc                       \ rS rSrSr\R                  \R                  \R                  \R                  10rS rS rS r\S 5       rS rS rS	 rS
 rSrg)DataSourceRbxCloud   RbxCloudc                 n    [         R                  R                  XX#5        S U l        S U l        S U l        g r   )r   
DataSource__init__seedr2   r|   )selfsys_cfgr.   pathss       r   r   DataSourceRbxCloud.__init__   s.    ##D6A	"r   c                 h    [         R                  R                  U 5      nU< SU R                  < S3$ )Nz [seed=])r   r   __str__r   )r   roots     r   r   DataSourceRbxCloud.__str__   s'    !!))$/!%tyy11r   c                 t    [        5       nUSL a  gUS   U l        US   U l        US   U l        US   U l        g)z_
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_dataDataSourceRbxCloud._get_data   sK    
 8u$Z0 ,&'78E?r   c                      U R                   S   $ )Nrx   r|   r   s    r   rx   !DataSourceRbxCloud.network_config   s    xx())r   c                      U R                   S   $ )Nrn   )r{   r   s    r   get_public_ssh_keys&DataSourceRbxCloud.get_public_ssh_keys   s    }}]++r   c                     U R                   $ r   )r   r   s    r   get_userdata_raw#DataSourceRbxCloud.get_userdata_raw   s       r   c                     U R                   $ r   r   r   s    r   get_config_obj!DataSourceRbxCloud.get_config_obj   s    xxr   c                 D    [        U R                   U R                  5        g r   )r2   r.   )r   r|   is_new_instances      r   activateDataSourceRbxCloud.activate  s    t**DKK8r   )r|   r2   r{   r   r   N)__name__
__module____qualname____firstlineno__dsnamer   NETWORKr   BOOT_NEW_INSTANCEBOOTBOOT_LEGACYdefault_update_eventsr   r   r   propertyrx   r   r   r   r   __static_attributes__ r   r   r   r      sm    F''NN!!
2 * *,!9r   r   c                 8    [         R                  " U [        5      $ 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!	:C :C :
6;;s+ 
'*>B=@09++ 09j '0023;r   