
    iѥ              
          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 d dl	m
Z
mZmZmZmZmZmZmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZ d dlm Z m!Z! d dl"m#Z#m$Z$m%Z% d dl&m'Z' d d	l(m)Z) d d
l*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5m6Z6m7Z7m8Z8m9Z9 d dl:m;Z;  ejx                  e=      Z>dZ? G d d      Z@dZAdej                  deCde$de#deDf
dZE G d d      ZFdeGfdZHdddeGdeCfd ZIy)!    N)suppress)Path)AnyDictIterableListOptionalSetTupleUnion)atomic_helperclouddistrosfeatureshandlershelpersimporter	lifecyclenetsources
type_utilsutil)Netv1Netv2)
EventScope	EventTypeuserdata_to_events)BootHookPartHandler)CloudConfigPartHandler)JinjaTemplatePartHandler)ShellScriptPartHandler)ShellScriptByFreqPartHandler)cmdline)events)CLOUD_CONFIGDEFAULT_RUN_DIR
PER_ALWAYSPER_INSTANCEPER_ONCE)NetworkConfigSourceNO_PREVIOUS_INSTANCE_IDc                       e Zd Zd Zy)
_Semaphorec                      || _         || _        y N)	semaphoreargs)selfr0   r1   s      2/usr/lib/python3/dist-packages/cloudinit/stages.py__init__z_Semaphore.__init__:   s    "	    N)__name__
__module____qualname__r4    r5   r3   r-   r-   9   s    r5   r-   zAggregated cloud-config created by merging merged_system_cfg (/etc/cloud/cloud.cfg and /etc/cloud/cloud.cfg.d), metadata, vendordata and userdata. The combined_cloud_config represents the aggregated desired configuration acted upon by cloud-init.
datasourcecfgevent_source_typescopereturnc                    | j                   }t        |j                  di             }t        j                  t        j                  |      t        j                  |      g      }t        j                  | j                  j                  |t                     v rt        j                  | j                        }|j                  |d   v rt        j                  d|t        j                  | j                  j!                  d             |j                  |      st               ||<   ||   j#                  t        j                         t        j                  d|       |g}|D 	cg c]  }	|	j                   }
}	|D ]9  }||j                  |g       v st        j                  d|j                  |        y t        j                  d|
|       y	c c}	w )
aA  Determine if a particular EventType is enabled.

    For the `event_source_type` passed in, check whether this EventType
    is enabled in the `updates` section of the userdata. If `updates`
    is not enabled in userdata, check if defined as one of the
    `default_events` on the datasource. `scope` may be used to
    narrow the check to a particular `EventScope`.

    Note that on first boot, userdata may NOT be available yet. In this
    case, we only have the data source's `default_update_events`,
    so an event that should be enabled in userdata may be denied.
    updatesscopesz/Adding event: scope=%s EventType=%s found in %szhotplug.enabledzAllowed events: %sz$Event Allowed: scope=%s EventType=%sTz$Event Denied: scopes=%s EventType=%sF)default_update_eventsr   getr   mergemanydictcopydeepcopyr   HOTPLUGsupported_update_eventssetread_hotplug_enabled_filepathsvalueLOGdebug	get_cpathadd)r:   r;   r<   r=   default_eventsuser_eventsallowedhotplug_enabled_filerA   sscope_values	evt_scopes               r3   update_event_enabledrX   G   s   & 	((  5G	25K   MM+&MM.)	
G J>>BBsu   $==j>N>NO;;.x88IIA!!  **+<=	 ;;u%!$ENy001II"G,$)7F%+,AGG,L, 	Ir ::II6!
  II.>O  -s   %Gc                   >   e Zd Zd8deee      fdZd Zed        Z	ed        Z
d Zed        Zd	 Zd9d
Zd Zedededefd       Zd Zd:dZd ZdefdZd Zd Zd Zd Zdej:                  fdZd Zd:dZ defdZ!d Z"edej:                  fd       Z#d  Z$d! Z%d;d"Z&d# Z'd$ Z(d% Z)d& Z*d' Z+d( Z,d) Z-d* Z.d:dee/j`                     fd+Z1d, Z2d- Z3	 d:d.Z4e5fd/Z6e5fd0Z7e5fd1Z8de9e:e;df   fd2Z<de=e9e:e;df   e9e>ef   f   fd3Z?d4 Z@deAfd5ZBdeCfd6ZDd7 ZEy)<InitNds_depsc                     ||| _         n%t        j                  t        j                  g| _         i | _        d | _        d | _        d | _        d| _        d | _	        |t        j                  ddd      }|| _        y )NFzinit-reporterz	init-desc)namedescriptionreporting_enabled)r[   r   DEP_FILESYSTEMDEP_NETWORK_cfg_paths_distror:   ds_restored_previous_iidr$   ReportEventStackreporter)r2   r[   rh   s      r3   r4   zInit.__init__   s{    "DL#22G4G4GHDL$&	/3158< ,0..$'"'H
 !r5   c                 .    i | _         d | _        d | _        y r/   )rb   rc   rd   r2   s    r3   _resetzInit._reset   s    	r5   c                    | j                   s| j                  d      }|j                  dd      }t        j                  |      }t
        j                  d|        |||| j                        | _         | j                  6| j                   | j                  _	        | j                  | j                  _        | j                   S )NsystemdistroubuntuzUsing distro class %s)rd   _extract_cfgpopr   fetchrM   rN   rK   r:   rn   r;   sys_cfg)r2   system_configdistro_name
distro_clss       r3   rn   zInit.distro   s    || --h7M'++Hh?K {3JII-z:%k=$**MDL *)-&*.(('||r5   c                 $    | j                  d      S )N
restricted)rp   rj   s    r3   r;   zInit.cfg   s      ..r5   c                 
   | j                          t        j                  | j                        }|dk(  r|j	                  dd        |S |dk(  rt        j                  |di       }|S |dk(  rt        j                  |di       }|S )Nrx   system_inform   )rz   rK   )rz   rK   )read_cfgrE   rF   rb   rq   r   get_cfg_by_path)r2   restrictionocfgs      r3   rp   zInit._extract_cfg   s    }}TYY',&HH]D)
 	 H$''.>CD  G#''.FKDr5   c                     | j                   s6| j                  d      }t        j                  || j                        | _         | j                   S )NrK   )rc   rp   r   Pathsr:   )r2   	path_infos     r3   rK   z
Init.paths   s9    {{))'2I!--	4??CDK{{r5   c                    | j                   j                  }| j                   j                  }|t        j                  j                  |d      t        j                  j                  |dd      t        j                  j                  |dd      t        j                  j                  |dd      t        j                  j                  |dd      t        j                  j                  |d      t        j                  j                  |d      t        j                  j                  |d      t        j                  j                  |d	      t        j                  j                  |d
      t        j                  j                  |d	      g}|S )Nscriptszper-instancezper-oncezper-bootvendorseed	instancesr   semdata)rK   	cloud_dirrun_dirospathjoin)r2   c_dirr   initial_dirss       r3   _initial_subdirszInit._initial_subdirs   s    

$$**$$GGLL	*GGLL	>:GGLL	:6GGLL	:6GGLL	84GGLL'GGLL,GGLL
+GGLL&GGLL'GGLL%(
 r5   c                     | j                   j                  g}|r%|j                  | j                   j                         |D ]  }t	        j
                  |        t        |      S r/   )rK   boot_finishedappendinstance_linkr   del_filelen)r2   rm_instance_lnkrm_listfs       r3   purge_cachezInit.purge_cache   sR    ::++,NN4::334 	AMM!	7|r5   c                 $    | j                          y r/   )_initialize_filesystemrj   s    r3   
initializezInit.initialize   s    ##%r5   mode_1mode_2r>   c                     | |z  S r/   r9   )r   r   s     r3   _get_strictest_modezInit._get_strictest_mode   s    r5   c                    d}t        j                  | j                                t        j                  | j                  d      }|rt        t              5  | j                  dt        j                  |            }d d d        t        j                  ||d       | j                  j                  d      }|si }t        |t              s|g}d }|D ]3  }t        j                  |      \  }}	 t        j                  |||        y  t        j!                  d|dj#                  |      |       y y # 1 sw Y   xY w# t        $ r}|}Y d }~{d }~ww xY w)Ni  def_log_fileF)preserve_modesyslog_fix_permsz,Failed changing perms on '%s'. tried: %s. %s,)r   ensure_dirsr   get_cfg_option_strr;   r   OSErrorr   get_permissionsensure_filerC   
isinstancelistextract_usergroupchownbynamerM   warningr   )	r2   modelog_filepermserrorpermuges	            r3   r   zInit._initialize_filesystem   s5   ..01**488^D '" //4//9 Xt5AHHLL!34EeT*E --d31$$Xq!4	 KK>	3  &  Es$   &D2/D>2D;>	EEEc                 J    | j                   s| j                  |      | _         y y r/   )rb   	_read_cfg)r2   	extra_fnss     r3   r{   zInit.read_cfg  s    yyy1DI r5   c                     | j                  |i       }|j                  di       j                  di       }|j                  d      t        dfv r|S | j                  ||      S )z read and merge our configurationrz   rK   r   N)_read_bootstrap_cfgrC   r&   )r2   r   initial_configrK   s       r3   r   zInit._read_cfg  sh     11)R@""="599'2F 99YOT#::!! ''	599r5   bootstrapped_configc           	          t        j                  || j                        }|j                  d      }t        j                  || j                  |t        |j                  |            }|j                  S )Ninstance_data_sensitiveinstance_data_file)rK   r:   additional_fnsbase_cfg)r   r   r:   get_runpathConfigMergerfetch_base_configr   r;   )r2   r   r   no_cfg_pathsr   mergers         r3   r   zInit._read_bootstrap_cfg0  sj    }}%8$//J)55%
 %%$&$$9K	
 zzr5   c                 ^    t        j                  | j                  j                  d            S )Nobj_pkl)r   pkl_loadrK   get_ipath_currj   s    r3   _restore_from_cachezInit._restore_from_cache?  s$     

 8 8 CDDr5   c                    t        j                  | j                  dd      r1t        j                  | j                  j                  d      dd       t        j                  | j                  | j                  j                  d            S )Nmanual_cache_cleanFmanual_clean_markerw )omodecontentr   )	r   get_cfg_option_boolr;   
write_filerK   r   r   	pkl_storedsrj   s    r3   _write_to_cachezInit._write_to_cacheE  sh    ##DHH.BEJ OO

(()>?
   $***B*B9*MNNr5   c                     | j                   j                  d      xs g }dt        j                  t              fD ]  }||vs|j                  |        | j                   j                  d      xs g }||fS )Ndatasource_pkg_listr   datasource_list)r;   rC   r   obj_namer   r   )r2   pkg_listncfg_lists       r3   _get_datasourceszInit._get_datasourcesP  sr    88<< 56<"j))'23 	#A "	# 88<< 128b(##r5   c                    |dvrt        d|z        | j                         }|sy| j                  j                  d      }t        j
                  j                  |      r$t        j                  |      j                         }nd }||j                         k(  r|d|z  fS |dk(  r|d|z  fS t        |d      r"|j                  | j                        r|d	|z  fS d d
|z  fS )N)checktrustz!Unexpected value for existing: %s)Nzno cache foundinstance_idz&restored from cache with run check: %sr   zrestored from cache: %scheck_instance_idzrestored from checked cache: %szcache invalid in datasource: %s)
ValueErrorr   rK   r   r   r   existsr   load_text_filestripget_instance_idhasattrr   r;   )r2   existingr   
run_iid_fnrun_iids        r3   _restore_from_checked_cachez Init._restore_from_checked_cacheZ  s    --@8KLL%%'+ZZ++M:
77>>*%))*5;;=GGb((**@2EFF 1B677r./B4H4H5 =BCC?"DEEr5   c           	         | j                   | j                   S t        j                  dd|z  | j                        5 }| j	                  |      \  }}||_        t        |      | _        t        j                  |j
                         d d d        s	 | j                         \  }}t        j                  | j                  | j                  | j                  t!        j"                  | j$                        ||| j                        \  }}t'        j(                  | j                  j*                         t        j-                  d||       || _         | j5                          |S # 1 sw Y   xY w# t        j.                  $ ru}|dk7  r|| j1                         }|r'|j3                         rt        j-                  d|       n+t'        j(                  | j                  j*                         |Y d }~d }~ww xY w)Nzcheck-cachez"attempting to read from cache [%s])r]   r^   parentzLoaded datasource %s - %sr   z3Restored fallback datasource from checked cache: %s)r:   r$   rg   rh   r   r^   boolre   rM   rN   r   r   find_sourcer;   rn   rK   rE   rF   r[   r   r   r   infoDataSourceNotFoundExceptionr   check_if_fallback_is_allowedrk   )	r2   r   myrepr   descr   r   dsnamer   s	            r3   _get_data_sourcezInit._get_data_sourcet  s   ??&??"$$<xG==
 	) 77AHB $E#BxDIIe''(	) %)%:%:%<"( %00HHKKJJMM$,,/MM
F djj6674fbA  		U	) 	)4 66 w&G--/"99;HHM
 MM$**":":;Gs&   AE#B6E/ #E,/G7A+G22G7c                 
    g dS )N)r   r   r   r9   rj   s    r3   _get_instance_subdirszInit._get_instance_subdirs  s    --r5   c                 V    | j                   j                  |      }|st        d      |S )NzCNo instance directory is available. Has a datasource been fetched??)rK   	get_ipathRuntimeError)r2   subnameinstance_dirs      r3   
_get_ipathzInit._get_ipath  s5     zz++G43  r5   netcfgc                 :   t         j                  j                  | j                  j                        sy| j                  j                  d      }| j                  j                  d      }t         j                  j                  |      rD|t        j                  t        j                  |            k7  r1t        j                  ||d       nt        j                  ||d       t         j                  j                  |      st        j                  ||       yy)zCreate /var/lib/cloud/instance/network-config.json

        Only attempt once /var/lib/cloud/instance exists which is created
        by Init.instancify once a datasource is detected.
        Nnetwork_config  r   )r   r   islinkrK   r   r   r   r   r   	load_jsonr   r   
write_jsonsym_link)r2   r  ncfg_instance_pathnetwork_links       r3   _write_network_config_jsonzInit._write_network_config_json  s     ww~~djj667 !ZZ556FGzz--.>?77>>,-##$67  ((&U $$%7eLww~~l+MM,l; ,r5   c                 Z   | j                         }t        | j                  j                        j	                         j                         }|t        |      j                         k(  }|rt        j                  d       nSt        j                  | j                  j                         t        j                  || j                  j                         g }| j                         D ]1  }|j                  t        j                  j                  ||             3 t        j                   |       | j                  j#                  d      }t%        j&                  | j(                        d| j(                  }d }t        j                  j                  |d      }		 t        j*                  |	      j-                         }|s|}t        j0                  |	d|z         t        j0                  t        j                  j                  |d      d|z         | j(                  j3                         }
t        j                  j                  |d      }| j5                         }t        j0                  |d|
z         t        j0                  | j                  j7                  d      d|
z         t        j0                  t        j                  j                  |d	      d|z         | j9                          |r||k(  rt        j                  d
       |
S | j;                          |
S # t.        $ r Y lw xY w)Nz0Instance link already exists, not recreating it.r   z: r:   z%s
zprevious-datasourceinstance-idr   zprevious-instance-idzJNot re-loading configuration, instance id and datasource have not changed.)r  r   rK   r   resolveabsoluterM   r   r   r   r  r   r   r   r   r   r   rO   r   r   r   r   r   	Exceptionr   r   previous_iidr   r   rk   )r2   idirdestinationalready_instancifieddir_listddpr   previous_dsds_fniidiid_fnr  s                r3   _reflect_cur_instancezInit._reflect_cur_instance  se     4::334<<>GGI*d4j.A.A.CCHHGHMM$**223MM$

 8 89 ++- 	3AOOBGGLLq12	3" ZZ!!&) $,,TWW5tww?T<0	--e4::<K Kv{+GGLL23V{5K	

 gg%%'b-0((*-

..}=v|LGGLL34f6M	
 	K2$5HH6 
 KKM
?  		s   #L 	L*)L*c                 H    | j                   st        d      | j                   S )a*  ds has a reference to the DataSource object

        self.datasource can contain a DataSource or None
        self.ds is preferred for any code path that is known to require an
        initialized DataSource, basically any code besides the initial
        bootstrapping of various objects.
        zDatasource is not initialized.)r:   r  rj   s    r3   r   zInit.ds
  s!     ?@@r5   c                    | j                   | j                   S | j                  j                  d      }t        j                  j                  |d      }	 t        j                  |      j                         | _         t        j                  d| j                          | j                   S # t        $ r t        | _         Y Bw xY w)Nr   r  zprevious iid found to be %s)rf   rK   rO   r   r   r   r   r   r   r  r+   rM   rN   )r2   r  r!  s      r3   r  zInit.previous_iid  s    )%%%ZZ!!&)b-0	9!%!4!4V!<!B!B!DD 			/1C1CD!!!	  	9!8D	9s   (B) )C ?C c                 r    | j                         }|t        k(  xs || j                  j                         k7  S )zReturn true if this is a new instance.

        If datasource has already been initialized, this will return False,
        even on first boot.
        )r  r+   r   r   )r2   previouss     r3   is_new_instancezInit.is_new_instance%  s:     $$&// 54772244	
r5   c                 &    | j                  |      S )zUoptionally load datasource from cache, otherwise discover
        datasource
        )r   )r   )r2   r   s     r3   rr   z
Init.fetch1  s     $$h$77r5   c                 "    | j                         S r/   )r"  rj   s    r3   
instancifyzInit.instancify7  s    ))++r5   c           	          t        j                  | j                  | j                  | j                  | j
                  t        j                  | j                        | j                        S )N)rh   )	r   Cloudr   rK   r;   rn   r   Runnersrh   rj   s    r3   cloudifyzInit.cloudify:  sF    {{GGJJHHKKOODJJ']]
 	
r5   c                    | j                  | j                  j                         d       | j                  | j                  j	                         d       | j                  | j                  j                         d       | j                  | j                  j                         d       | j                  | j                  j                         d       | j                  | j                  j                         d       y )Nuserdata
vendordatavendordata2)
_store_rawdatar   get_userdata_raw_store_processeddataget_userdata_store_raw_vendordataget_vendordata_rawget_vendordataget_vendordata2_rawget_vendordata2rj   s    r3   updatezInit.updateE  s    DGG446
C!!$''"6"6"8*E""477#=#=#?N!!$''"8"8":LI""GG'')=	
 	!!$''"9"9";]Kr5   c                     t        j                  dd| j                        5  | j                  j	                  | j                                d d d        y # 1 sw Y   y xY w)Nzsetup-datasourcezsetting up datasourcer   )r'  )r$   rg   rh   r   setupr'  rj   s    r3   setup_datasourcezInit.setup_datasourceO  sR    $$ 7
 	B GGMM$*>*>*@MA	B 	B 	Bs   +AA c                     t        j                  dd| j                        5  | j                  j	                  | j
                  | j                                | j                          d d d        y # 1 sw Y   y xY w)Nzactivate-datasourcezactivating datasourcer>  )r;   r'  )r$   rg   rh   r   activater;   r'  r   rj   s    r3   activate_datasourcezInit.activate_datasourceU  sk    $$!#==
 	#
 GGHHd.B.B.D     "	# 	# 	#s   AA22A;c                 ^    |d}t        j                  | j                  d|z        |d       y )Nr5   z%s_rawr	  )r   r   r  r2   r   r:   s      r3   r3  zInit._store_rawdata`  s*    <D:(=>eLr5   c                     |<t        |      t        t        t        fvr t	        dt        t        |            z        t        |t              rt        j                  |      }| j                  ||       y )Nz'vendordata_raw is unsupported type '%s')	typebytesstrr   	TypeErrorr   r   
json_dumpsr3  rE  s      r3   r7  zInit._store_raw_vendordataf  sc    T
5#t2D D9CT
OK  dD! ++D1DD*-r5   c                 j    |d}t        j                  | j                  |      t        |      d       y )Nr   r	  )r   r   r  rI  )r2   processed_datar:   s      r3   r5  zInit._store_processeddataq  s.    !NOOJ'^)<e	
r5   c                 &   |i }|j                  | j                  | j                  d       t        di |}t	        di |}t        di |}||t        t        fi |t        t        fi |t        t        fi ||t        di |d|||gig}|S )N)rK   r:   sub_handlersr9   )r<  rK   r   r   r!   r   r"   r'   r(   r)   r    )r2   optscloudconfig_handlershellscript_handlerboothook_handlerdef_handlerss         r3   _default_handlerszInit._default_handlersy  s    <D"gg	
 5<t<4<t<.66(<t<(>>(:T:$  ('$
  r5   c                 ,    | j                  ddd      S )Nvendor_scriptsvendor_cloud_configscript_pathcloud_config_pathrP  rU  rj   s    r3   _default_vendordata_handlersz!Init._default_vendordata_handlers  s%    %%/%: & 
 	
r5   c                 ,    | j                  ddd      S )NrW  vendor2_cloud_configrY  r\  r]  rj   s    r3   _default_vendordata2_handlersz"Init._default_vendordata2_handlers  s%    %%/%; & 
 	
r5   c                 *   |g }| j                   j                  d      }| j                  d      |fD ]8  }|s|t        j                  vst        j                  j                  d|       : fd}t        j                          ||       |D ]/  }j                  |d      }	|	st        j                  d|	|       1 | j                         fd}
fd	}fd
}	  |
         ||        |        y#  |        w xY w)zb
        Generalized handlers suitable for use with either vendordata
        or userdata
        Nr   r   c                    | rt         j                  j                  |       sy t        j                  |       }|j                         D ]  \  }}	 t        j                  |dgddg      \  }}|st        j                  d|||       ?t        j                  |d         }t        j                  |      }j                  |      }|rt        j                  d|||        y # t        $ r t        j                   t        d|       Y w xY w)Nr   
list_typeshandle_partzJCould not find a valid user-data handler named %s in file %s (searched %s)r   z(Added custom handler for %s [%s] from %sz"Failed to register handler from %s)r   r   isdirr   get_modules_from_diritemsr   find_modulerM   r   import_moduler   fixup_handlerregisterrN   r  logexc)	r   potential_handlersfnamemod_namemod_locslooked_locsmodtypes
c_handlerss	           r3   register_handlers_in_dirz3Init._do_handlers.<locals>.register_handlers_in_dir  s   rww}}T2!%!:!:4!@#5#;#;#= x,4,@,@ 2$}(E-)Hk $A$!' !"00!=C"005C&//4E		F!!	%0 ! KKA5s   7C#AC##$D
	D
F)	overwritez$Added default handler for %s from %sc                      j                         D ]F  \  } }|j                  v rt        j                  |       j                  j	                  |       H y r/   )rh  initializedr   
call_beginr   _ctypers  ru  r   	frequencys     r3   init_handlersz(Init._do_handlers.<locals>.init_handlers  sX    )//1 3*000 ##Cy9&&--c23r5   c                 d    d| d}t        j                  t         j                  |       y )Nr   )r   
handlerdirr   r}  handlercountexcluded)r   )r   walkwalker_callback)r  	part_dataru  r   data_msgr}  r  s     r3   walk_handlersz(Init._do_handlers.<locals>.walk_handlers  s8     '"& !"$I MM(H$<$<9Mr5   c                     j                         D ]G  \  } }|j                  vrj                  j                  |       	 t        j                  |       I y # t
        $ r t        j                  t        d|       Y qw xY w)NzFailed to finalize handler: %s)	rh  ry  remover   call_endr  r   rm  rM   r{  s     r3   finalize_handlersz,Init._do_handlers.<locals>.finalize_handlers  s    )//1 	Lj444 &&--c2L%%c4;	L ! LKK%EsKLs   A$BB)rK   rO   r  sysr   insertr   ContentHandlersrl  rM   rN   r.  )r2   r  c_handlers_listr}  r  cdirr  rv  rs  rt  r~  r  r  ru  r   r  s    ` `         @@@r3   _do_handlerszInit._do_handlers  s    Hzz##J/z*  	&AQchh&1%	& 	F ,,.
 	!&
 # 	NC''u'=E		@%M	N }}	3	N 	N"	L	 O(#s   2D	 		Dc                    t        j                  dd| j                        5  t        j                  | j
                  dd      r| j                  |       nt        j                  d       d d d        t        j                  dd| j                        5  | j                  d	|       d d d        t        j                  d
d| j                        5  | j                  d|       d d d        | j                          t        j                  | j
                        }t        |d<   | j                  d      |d<   t        j                          |d<   t#        j$                  | j&                  j)                  d      |d       | j&                  j)                  d      }	 t        j*                  t        j,                  |            }|d   |d<   |d   |d<   t#        j$                  ||d       y # 1 sw Y   xY w# 1 sw Y   UxY w# 1 sw Y   %xY w# t.        t0        f$ r!}t        j3                  d||       Y d }~y d }~wt4        j6                  t8        f$ r!}t        j3                  d||       Y d }~y d }~ww xY w)Nzconsume-user-datazreading and applying user-datar>  allow_userdataTz,allow_userdata = False: discarding user-datazconsume-vendor-dataz reading and applying vendor-datar1  zconsume-vendor-data2z!reading and applying vendor-data2r2  _docrm   rz   r   combined_cloud_configr	  r
  r   zESkipping write of system_info/features to %s. Unable to read file: %szDSkipping write of system_info/features to %s. Invalid JSON found: %s)r$   rg   rh   r   r   r;   _consume_userdatarM   rN   _consume_vendordatark   rE   rF   COMBINED_CLOUD_CONFIG_DOCrp   r   get_featuresr   r  rK   r   r  r   r   IOErrorr   jsonJSONDecodeErrorrJ  )r2   r}  combined_cloud_cfgjson_sensitive_fileinstance_jsonr   s         r3   consume_datazInit.consume_data  sI    $$,==
 	J
 ''2BDI&&y1		HI	J $$!.==
 	>
 $$\9=	> $$"/==
 	?
 $$]I>	? 	
 "]]4884%>6" -1,=,=h,G=) *2)>)>)@:&  JJ""#:;	

 #jj445NO	 NN##$78M& (:-'Hm$$6z$Bj!  	
O	J 	J	> 	>	? 	?H ! 	KK+#	 $$i0 	KK*#	 	sH   A	G/G<H	(H /G9<H	HI>%II>I99I>c                    |dk(  r3| j                   j                         st        j                  d       yd}nC|dk(  r3| j                   j	                         st        j                  d       yd}nt        d      t        j                  | j                  | j                   g | j                  d	
      }|j                  j                  |i       }t        |t              sdd	i}t        j                  d||       |j                  d      }|j                  dd      }t        j                  |      st        j                  d|       yt        |t               rt#        j$                  d| ddd       t        j                  d||       |dk(  r+| j                   j'                         }| j)                         }	n*| j                   j+                         }| j-                         }	| j/                  ||	||       y)zH
        Consume the vendordata and run the part handlers on it
        r1  zno vendordata from datasourceNvendor_datar2  zno vendordata2 from datasourcevendor_data2z>vendor_source arg must be either 'vendordata' or 'vendordata2'F)rK   r:   r   r   include_vendorenabledz$invalid %s setting. resetting to: %sdisabled_handlersz%s consumption is disabled.zUse of string 'z!' for 'vendor_data:enabled' fieldz23.1zUse boolean value instead.)
deprecateddeprecated_versionextra_messagez)%s will be consumed. disabled_handlers=%s)r  )r   r8  rM   rN   r:  r  r   r   rc   r;   rC   r   dictr   r   is_truerI  r   	deprecater9  r^  r;  ra  r  )
r2   vendor_sourcer}  cfg_name
_cc_mergervdcfgr  no_handlersvendor_data_msgr  s
             r3   r  zInit._consume_vendordatao  s    L(77--/		9:$Hm+77..0		:;%H$ 
 ))++wwXX 

 ""8R0%&&EKK6% ))I&ii 3T:||G$II3]Cgs#,WI 6. .#):	 			7	
 L("gg446O"??AO"gg557O"@@BO 	_i+ 	 	
r5   c                     | j                   j                  d      }| j                         }| j                  |||       y)z@
        Consume the userdata and run the part handlers
        TN)r   r6  rU  r  )r2   r}  user_data_msgr  s       r3   r  zInit._consume_userdata  s<     ,,T2 002 	-)Dr5   c                     |r	d|v r|d   S |S )z
        Network configuration can be passed as a dict under a "network" key, or
        optionally at the top level. In both cases, return the config.
        networkr9   )r2   r;   s     r3   _get_network_key_contentszInit._get_network_key_contents  s    
 9#y>!
r5   c           
         t         j                  j                  | j                  j	                  d      d      }t         j                  j                  |      rd |fS t        j                  t        j                         t        j                  t        j                         t        j                  d t        j                  | j                  j                  d      i}| j                   rT| j"                  j$                  }t'        | j"                  d      rB| j"                  j(                  |t        j                  <   nt*        j,                  j$                  }|D ]  }t/        |t              st0        j3                  d|       *||vrt0        j3                  d|       E| j5                  ||         }t7        j8                  |      rt0        j;                  d|       d |fc S |s||fc S  | j                  j                  dd      st0        j3                  d	       | j<                  j?                         t        j@                  fS )
Nr   zupgraded-networkr  r  z7data source specifies an invalid network cfg_source: %sz;data source specifies an unavailable network cfg_source: %sznetwork config disabled by %sTzEmpty network config found)!r   r   r   rK   rO   r   r*   CMD_LINEr#   read_kernel_cmdline_config	INITRAMFSread_initramfs_configDS
SYSTEM_CFGr;   rC   r:   r   network_config_sourcesr   r  r   
DataSourcer   rM   r   r  r   is_disabled_cfgrN   rn   generate_fallback_configFALLBACK)r2   disable_fileavailable_cfgsorder
cfg_sourcencfgs         r3   _find_networking_configzInit._find_networking_config  s    ww||JJ  (*<
 77>>,',''  (('*L*L*N))7+H+H+J""D**DHHLL,C	
 ??GG22Etww 019=9O9O2556&&==E 	*Jj*=> M /&
 11.2LMD""4(		9:Fj))j))+	*, xx||It,KK45KK002((
 	
r5   c                     	 t         j                  d|       | j                  j                  j	                  |       y # t
        $ r }t         j                  d|       Y d }~y d }~ww xY w)Nz applying net config names for %szFailed to rename devices: %s)rM   rN   rn   
networkingapply_network_config_namesr  r   )r2   r  r   s      r3   _apply_netcfg_nameszInit._apply_netcfg_names  sP    	;II8&AKK""==fE 	;KK6::	;s   ;> 	A'A""A'c                 ~    t        t        j                  | j                  j	                  d            dt
        f      S )Nr   apply_network_config)r-   r   FileSemaphoresrK   r   r)   rj   s    r3   _get_per_boot_network_semaphorez$Init._get_per_boot_network_semaphore  s4    ""4::#9#9%#@A#X.
 	
r5   c                 h    | j                         } |j                  j                  |j                   S r/   )r  r0   has_runr1   )r2   r   s     r3   _network_already_configuredz Init._network_already_configured  s+    224$s}}$$chh//r5   c                    	 ddl m}m}  j                         \  }}|t        j                  d|       y fd		 fd} j                  T j                         sD |       s= 	t        j                        s't        j                  d        j                  |       y j                         \  }}|r( j                  |        |||j                  ddd	
        j                  j                  j!                  |        j                  |       t        j                  d|||        j#                         }	  |j$                  j&                  |j(                   5   j                  j+                  ||      cddd       S # 1 sw Y   yxY w# t,        j.                  $ r }t        j1                  d|       Y d}~yd}~wt2        $ r# t        j5                  d j                         Y yw xY w)zApply the network config.

        Find the config, determine whether to apply it, apply it via
        the distro, and optionally bring it up
        r   )
SchemaTypevalidate_cloudconfig_schemaNz network config is disabled by %sc                     t        j                  j                  | t        j                        xr j                  j                  | g      S )N)r:   r;   r<   r=   )rX   r   r;   r   NETWORKupdate_metadata_if_supported)
event_typer2   s    r3   "event_enabled_and_metadata_updatedzEInit.apply_network_config.<locals>.event_enabled_and_metadata_updated)  sG    '77HH", ((	 E
 ''66
|DEr5   c                  V    j                          xr   t        j                        S r/   )r  r   BOOT)r  r2   s   r3   should_run_on_boot_eventz;Init.apply_network_config.<locals>.should_run_on_boot_event1  s)    4466 G6y~~Fr5   zWNo network config applied. Neither a new instance nor datasource network update allowedFT)configschema_typestrictlog_detailslog_deprecationsz5Applying network configuration from %s bringup=%s: %s)bring_upz@Unable to render networking. Network config is likely broken: %sz_distro '%s' does not implement apply_network_config. networking may not be configured properly.)cloudinit.config.schemar  r  r  rM   r   r:   r'  r   BOOT_LEGACYrN   r  r  NETWORK_CONFIGrn   r  wait_for_physdevsr  r0   lockr1   r  r   RendererNotFoundErrorr   NotImplementedErrorr   )
r2   r  r  r  r  srcr  r   r   r  s
   `        @r3   r  zInit.apply_network_config  s   	

 224>HH7=	E	 OO'((*,.6y7L7LMII9
 $$V, 224++F3'&55!!% 	008 	  ( 	C		
 224	###SXX. {{77X 8    (( 	II$
 " 	KK=
 	s<   >#F !F>	F FF F G1'G,G10G1)NN)Fr/   )r   )Fr6   r7   r8   r	   r   rI  r4   rk   propertyrn   r;   rp   rK   r   r   r   staticmethodintr   r   r{   r   r  r   r   r   r   r   r   r  r   r   r  r  r"  r   r  r'  rr   r*  r.  r<  r@  rC  r3  r7  r5  r   HandlerrU  r^  ra  r  r(   r  r  r  r   r   r   r  r   r*   r  r  r-   r  r   r  r  r9   r5   r3   rZ   rZ      s   !c 3 !,    / /  && C    #J2:&$ E	O$F4.G,>,> .`.
< <4;z 
G&& 
 
"

8,	
LB	#M	.
d83C3C.D @

 >Bt l &2 N
` <H L
\ +7 EeUD6H0I 2
	uUE4'(%0CS0H*II	J2
h;
 
0T 0Xr5   rZ   r   c                 h    t        j                  t        j                  j	                  | d            S )Nz	cloud.cfg)r   	read_confr   r   r   )r   s    r3   read_runtime_configr  t  s     >>"'',,w<==r5   r   c                    t        j                  t        j                         t        j                  t        |      t        |       t        j                         gd      S )Nr   T)reverse)r   rD   get_builtin_cfgread_conf_with_confdr%   r  read_conf_from_cmdline)r   r   s     r3   r   r   x  sW       "%%1C  ('')	
  r5   )JrE   r  loggingr   r  
contextlibr   pathlibr   typingr   r   r   r   r	   r
   r   r   	cloudinitr   r   r   r   r   r   r   r   r   r   r   r   cloudinit.configr   r   cloudinit.eventr   r   r   cloudinit.handlers.boot_hookr   cloudinit.handlers.cloud_configr   !cloudinit.handlers.jinja_templater    cloudinit.handlers.shell_scriptr!   ,cloudinit.handlers.shell_script_by_frequencyr"   cloudinit.netr#   cloudinit.reportingr$   cloudinit.settingsr%   r&   r'   r(   r)   cloudinit.sourcesr*   	getLoggerr6   rM   r+   r-   r  r  r  r   rX   rZ   rI  r  r   r9   r5   r3   <module>r     s      	 
   I I I    * E E = B F B " &  2g!3  F B""B	B !B 	B
 
BJe eP> > ;? s 4 r5   