
    x[h              
          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
 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l0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7m8Z8m9Z9m:Z:m;Z; d dl<m=Z=  ej|                  e?      Z@dZAdZBdej                  deDde&de%deEf
dZF G d d      ZGdeHfdZIdddeHdeDfdZJy)     N)
namedtuple)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_IDzAggregated 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/   r0   default_eventsuser_eventsallowedhotplug_enabled_filer4   sscope_values	evt_scopes               2/usr/lib/python3/dist-packages/cloudinit/stages.pyupdate_event_enabledrL   B   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d7deee      fdZd Zed        Z	ed        Z
d Zed        Zd	 Zd8d
Zd Zedededefd       Zd Zd9dZd ZdefdZd Zd Zd Zd Zdej:                  fdZd Zd9dZ defdZ!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-d9dee.j^                     fd*Z0d+ Z1d, Z2	 d9d-Z3e4fd.Z5e4fd/Z6e4fd0Z7de8e9e:df   fd1Z;de<e8e9e:df   e8e=ef   f   fd2Z>d3 Z?d4 Z@deAfd5ZBd6 ZCy);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)rO   r   DEP_FILESYSTEMDEP_NETWORK_cfg_paths_distror-   ds_restored_previous_iidr%   ReportEventStackreporter)selfrO   r\   s      rK   __init__zInit.__init__   s{    "DL#22G4G4GHDL$&	/3158< ,0..$'"'H
 !    c                 .    i | _         d | _        d | _        y N)rV   rW   rX   r]   s    rK   _resetzInit._reset   s    	r_   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)rX   _extract_cfgpopr   fetchr@   rA   r>   r-   rf   r.   sys_cfg)r]   system_configdistro_name
distro_clss       rK   rf   zInit.distro   s    || --h7M'++Hh?K {3JII-z:%k=$**MDL *)-&*.(('||r_   c                 $    | j                  d      S )N
restricted)rh   rb   s    rK   r.   zInit.cfg   s      ..r_   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 )Nrp   system_infore   )rr   r>   )rr   r>   )read_cfgr8   r9   rV   ri   r   get_cfg_by_path)r]   restrictionocfgs      rK   rh   zInit._extract_cfg   s    }}TYY',&HH]D)
 	 H$''.>CD  G#''.FKDr_   c                     | j                   s6| j                  d      }t        j                  || j                        | _         | j                   S )Nr>   )rW   rh   r   Pathsr-   )r]   	path_infos     rK   r>   z
Init.paths   s9    {{))'2I!--	4??CDK{{r_   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)r>   	cloud_dirrun_dirospathjoin)r]   c_dirr   initial_dirss       rK   _initial_subdirszInit._initial_subdirs   s    

$$**$$GGLL	*GGLL	>:GGLL	:6GGLL	:6GGLL	84GGLL'GGLL,GGLL
+GGLL&GGLL'GGLL%(
 r_   c                     | j                   j                  g}|r%|j                  | j                   j                         |D ]  }t	        j
                  |        t        |      S ra   )r>   boot_finishedappendinstance_linkr   del_filelen)r]   rm_instance_lnkrm_listfs       rK   purge_cachezInit.purge_cache   sR    ::++,NN4::334 	AMM!	7|r_   c                 $    | j                          y ra   )_initialize_filesystemrb   s    rK   
initializezInit.initialize   s    ##%r_   mode_1mode_2r1   c                     | |z  S ra    )r   r   s     rK   _get_strictest_modezInit._get_strictest_mode   s    r_   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_filer6   
isinstancelistextract_usergroupchownbynamer@   warningr   )	r]   modelog_filepermserrorpermuges	            rK   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 ra   )rV   	_read_cfg)r]   	extra_fnss     rK   rs   zInit.read_cfg  s    yyy1DI r_   c                     | j                  |i       }|j                  di       j                  di       }|j                  d      t        dfv r|S | j                  ||      S )z read and merge our configurationrr   r>   r   N)_read_bootstrap_cfgr6   r'   )r]   r   initial_configr>   s       rK   r   zInit._read_cfg  sh     11)R@""="599'2F 99YOT#::!! ''	599r_   bootstrapped_configc           	          t        j                  || j                        }|j                  d      }t        j                  || j                  |t        |j                  |            }|j                  S )Ninstance_data_sensitiveinstance_data_file)r>   r-   additional_fnsbase_cfg)r   rx   r-   get_runpathConfigMergerfetch_base_configr   r.   )r]   r   r   no_cfg_pathsr   mergers         rK   r   zInit._read_bootstrap_cfg+  sj    }}%8$//J)55%
 %%$&$$9K	
 zzr_   c                 ^    t        j                  | j                  j                  d            S )Nobj_pkl)r   pkl_loadr>   get_ipath_currb   s    rK   _restore_from_cachezInit._restore_from_cache:  s$     

 8 8 CDDr_   c                 2   | j                   yt        j                  | j                  dd      r1t        j                  | j
                  j                  d      dd       t        j                  | j                   | j
                  j                  d            S )NFmanual_cache_cleanmanual_clean_markerw )omodecontentr   )	r-   r   get_cfg_option_boolr.   
write_filer>   r   r   	pkl_storerb   s    rK   _write_to_cachezInit._write_to_cache@  sz    ??"##DHH.BEJ OO

(()>?
   OOTZZ55i@
 	
r_   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.   r6   r   obj_namer   r   )r]   pkg_listncfg_lists       rK   _get_datasourceszInit._get_datasourcesO  sr    88<< 56<"j))'23 	#A "	# 88<< 128b(##r_   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   r>   r   r   r   existsr   load_text_filestripget_instance_idhasattrr   r.   )r]   existingds
run_iid_fnrun_iids        rK   _restore_from_checked_cachez Init._restore_from_checked_cacheY  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Er_   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])rQ   rR   parentzLoaded datasource %s - %sr   z3Restored fallback datasource from checked cache: %s)r-   r%   r[   r\   r   rR   boolrY   r@   rA   r   r   find_sourcer.   rf   r>   r8   r9   rO   r   r   r   infoDataSourceNotFoundExceptionr   check_if_fallback_is_allowedrc   )	r]   r   myrepr   descr   r   dsnamer   s	            rK   _get_data_sourcezInit._get_data_sources  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   r   rb   s    rK   _get_instance_subdirszInit._get_instance_subdirs  s    --r_   c                 V    | j                   j                  |      }|st        d      |S )NzCNo instance directory is available. Has a datasource been fetched??)r>   	get_ipathRuntimeError)r]   subnameinstance_dirs      rK   
_get_ipathzInit._get_ipath  s5     zz++G43  r_   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   islinkr>   r   r   r   r   r   	load_jsonr   r   
write_jsonsym_link)r]   r   ncfg_instance_pathnetwork_links       rK   _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; ,r_   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   r>   r   resolveabsoluter@   r   r   r   r  r   r   r   r   r   r   rB   r   r   r-   r   r   	Exceptionr   r   previous_iidr   r   rc   )r]   idirdestinationalready_instancifieddir_listddpr   previous_dsds_fniidiid_fnr  s                rK   _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!!&) $,,T__=tOT<0	--e4::<K Kv{+GGLL23V{5K	

 oo--/b-0((*-

..}=v|LGGLL34f6M	
 	K2$5HH6 
 KKM
?  		s   #L 	L*)L*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)rZ   r>   rB   r   r   r   r   r   r   r  r,   r@   rA   )r]   r  r  s      rK   r  zInit.previous_iid	  s    )%%%ZZ!!&)b-0	9!%!4!4V!<!B!B!DD 			/1C1CD!!!	  	9!8D	9s   (B) )C ?C c                 v    | 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   )r]   previousrets      rK   is_new_instancezInit.is_new_instance  sA     $$&// =4??::<< 	 
r_   c                 &    | j                  |      S )zUoptionally load datasource from cache, otherwise discover
        datasource
        )r   )r   )r]   r   s     rK   rj   z
Init.fetch$  s     $$h$77r_   c                 "    | j                         S ra   )r  rb   s    rK   
instancifyzInit.instancify*  s    ))++r_   c           	          t        j                  | j                  | j                  | j                  | j
                  t        j                  | j                        | j                        S )N)r\   )	r   Cloudr-   r>   r.   rf   r   Runnersr\   rb   s    rK   cloudifyzInit.cloudify-  sF    {{OOJJHHKKOODJJ']]
 	
r_   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_vendordata2rb   s    rK   updatezInit.update8  s    DOO<<>
K!!$//">">"@*M""OO..0,	
 	!!OO**,l	
 	""OO//1=	
 	!!OO++-}	
r_   c                     t        j                  dd| j                        5  | j                  t	        d      | j                  j                  | j                                d d d        y # 1 sw Y   y xY w)Nzsetup-datasourcezsetting up datasourcer   z!Datasource is None, cannot setup.)r   )r%   r[   r\   r-   r   setupr   rb   s    rK   setup_datasourcezInit.setup_datasourceH  sk    $$ 7
 	J &"#FGGOO!!$2F2F2H!I	J 	J 	Js   AA..A7c                 *   t        j                  dd| j                        5  | j                  t	        d      | j                  j                  | j                  | j                                | j                          d d d        y # 1 sw Y   y xY w)Nzactivate-datasourcezactivating datasourcer7  z$Datasource is None, cannot activate.)r.   r   )	r%   r[   r\   r-   r   activater.   r   r   rb   s    rK   activate_datasourcezInit.activate_datasourceP  s    $$!#==
 
	#
 &"#IJJOO$$HHd.B.B.D %    "
	# 
	# 
	#s   AB		Bc                 ^    |d}t        j                  | j                  d|z        |d       y )Nr_   z%s_rawr  )r   r   r   r]   r   r-   s      rK   r,  zInit._store_rawdata]  s*    <D:(=>eLr_   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_dumpsr,  r>  s      rK   r0  zInit._store_raw_vendordatac  sc    T
5#t2D D9CT
OK  dD! ++D1DD*-r_   c                 j    |d}t        j                  | j                  |      t        |      d       y )Nr   r  )r   r   r   rB  )r]   processed_datar-   s      rK   r.  zInit._store_processeddatan  s.    !NOOJ'^)<e	
r_   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)r>   r-   sub_handlersr   )r5  r>   r-   r    r"   r   r#   r(   r)   r*   r!   )r]   optscloudconfig_handlershellscript_handlerboothook_handlerdef_handlerss         rK   _default_handlerszInit._default_handlersv  s    <D"oo	
 5<t<4<t<.66(<t<(>>(:T:$  ('$
  r_   c                 ,    | j                  ddd      S )Nvendor_scriptsvendor_cloud_configscript_pathcloud_config_pathrI  rN  rb   s    rK   _default_vendordata_handlersz!Init._default_vendordata_handlers  s%    %%/%: & 
 	
r_   c                 ,    | j                  ddd      S )NrP  vendor2_cloud_configrR  rU  rV  rb   s    rK   _default_vendordata2_handlersz"Init._default_vendordata2_handlers  s%    %%/%; & 
 	
r_   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_moduler@   r   import_moduler   fixup_handlerregisterrA   r  logexc)	r   potential_handlersfnamemod_namemod_locslooked_locsmodtypes
c_handlerss	           rK   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 ra   )ra  initializedr   
call_beginr   _ctyperl  rn  r   	frequencys     rK   init_handlersz(Init._do_handlers.<locals>.init_handlers  sX    )//1 3*000 ##Cy9&&--c23r_   c                 d    d| d}t        j                  t         j                  |       y )Nr   )r   
handlerdirr   rv  handlercountexcluded)r   )r   walkwalker_callback)r{  	part_datarn  r   data_msgrv  r  s     rK   walk_handlersz(Init._do_handlers.<locals>.walk_handlers  s8     '"& !"$I MM(H$<$<9Mr_   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)	ra  rr  remover   call_endr  r   rf  r@   rt  s     rK   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)r>   rB   r   sysr   insertr   ContentHandlersre  r@   rA   r'  )r]   r  c_handlers_listrv  r{  cdirr  ro  rl  rm  rw  r  r  rn  r   r  s    ` `         @@@rK   _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-datar7  allow_userdataTz,allow_userdata = False: discarding user-datazconsume-vendor-dataz reading and applying vendor-datar*  zconsume-vendor-data2z!reading and applying vendor-data2r+  _docre   rr   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%   r[   r\   r   r   r.   _consume_userdatar@   rA   _consume_vendordatarc   r8   r9   COMBINED_CLOUD_CONFIG_DOCrh   r   get_featuresr   r  r>   r   r  r   r   IOErrorr   jsonJSONDecodeErrorrC  )r]   rv  combined_cloud_cfgjson_sensitive_fileinstance_jsonr   s         rK   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
        r*  zno vendordata from datasourceNvendor_datar+  zno vendordata2 from datasourcevendor_data2z>vendor_source arg must be either 'vendordata' or 'vendordata2'F)r>   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-   r1  r@   rA   r3  r   r   r   rW   r.   r6   r   dictr   r   is_truerB  r   	deprecater2  rW  r4  rZ  r  )
r]   vendor_sourcerv  cfg_name
_cc_mergervdcfgr  no_handlersvendor_data_msgr  s
             rK   r  zInit._consume_vendordatal  s    L(??557		9:$Hm+??668		:;%H$ 
 ))++XX 

 ""8R0%&&EKK6% ))I&ii 3T:||G$II3]Cgs#,WI 6. .#):	 			7	
 L("oo<<>O"??AO"oo==?O"@@BO 	_i+ 	 	
r_   c                     | j                   j                  d      }| j                         }| j                  |||       y)z@
        Consume the userdata and run the part handlers
        TN)r-   r/  rN  r  )r]   rv  user_data_msgr  s       rK   r  zInit._consume_userdata  s<     44T: 002 	-)Dr_   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.
        networkr   )r]   r.   s     rK   _get_network_key_contentszInit._get_network_key_contents  s    
 9#y>!
r_   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                   r=t#        | j                   d      r'| j                   j$                  |t        j                  <   | j                   r| j                   j&                  }nt(        j*                  j&                  }|D ]  }t-        |t              st.        j1                  d|       *||vrt.        j1                  d|       E| j3                  ||         }t5        j6                  |      rt.        j9                  d|       d |fc S |s||fc S  | j                  j                  dd      st.        j1                  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   r>   rB   r   r+   CMD_LINEr$   read_kernel_cmdline_config	INITRAMFSread_initramfs_configDS
SYSTEM_CFGr.   r6   r-   r   r  network_config_sourcesr   
DataSourcer   r@   r   r  r   is_disabled_cfgrA   rf   generate_fallback_configFALLBACK)r]   disable_fileavailable_cfgsorder
cfg_sourcencfgs         rK   _find_networking_configzInit._find_networking_config  s    ww||JJ  (*<
 77>>,',''  (('*L*L*N))7+H+H+J""D**DHHLL,C	
 ??wt8HI.. .112 ??OO::E&&==E 	*Jj*=> M /&
 11.2LMD""4(		9:Fj))j))+	*, xx||It,KK45KK002((
 	
r_   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)r@   rA   rf   
networkingapply_network_config_namesr  r   )r]   r   r   s      rK   _apply_netcfg_nameszInit._apply_netcfg_names  sP    	;II8&AKK""==fE 	;KK6::	;s   ;> 	A'A""A'c                      t        dd      t        j                  | j                  j	                  d            dt
        f      S )N	Semaphorezsemaphore argsr   apply_network_config)r   r   FileSemaphoresr>   r   r*   rb   s    rK   _get_per_boot_network_semaphorez$Init._get_per_boot_network_semaphore  s>    8z+'78""4::#9#9%#@A#X.
 	
r_   c                 h    | j                         } |j                  j                  |j                   S ra   )r  	semaphorehas_runargs)r]   r   s     rK   _network_already_configuredz Init._network_already_configured  s+    224$s}}$$chh//r_   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                         \  }} j                  |       |r |||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/   r0   )rL   r-   r.   r   NETWORKupdate_metadata_if_supported)
event_typer]   s    rK   "event_enabled_and_metadata_updatedzEInit.apply_network_config.<locals>.event_enabled_and_metadata_updated(  sG    '??HH", ((	 M
 //>>
|LMr_   c                  V    j                          xr   t        j                        S ra   )r  r   BOOT)r  r]   s   rK   should_run_on_boot_eventz;Init.apply_network_config.<locals>.should_run_on_boot_event0  s)    4466 G6y~~Fr_   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  r@   r   r-   r   r   BOOT_LEGACYrA   r  r
  NETWORK_CONFIGrf   r  wait_for_physdevsr  r  lockr  r  r   RendererNotFoundErrorr   NotImplementedErrorr   )
r]   r  r  r  r   srcr  r   r   r  s
   `        @rK   r  zInit.apply_network_config  s   	

 224>HH7=	M	 OO'((*,.6y7L7LMII9
 $$V, 224''/'&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)Fra   )r   )D__name__
__module____qualname__r
   r	   rB  r^   rc   propertyrf   r.   rh   r>   r   r   r   staticmethodintr   r   rs   r   r  r   r   r   r   r   r   r  r   r   r   r
  r  r  r   rj   r#  r'  r5  r9  r<  r,  r0  r.  r   HandlerrN  rW  rZ  r  r)   r  r  r  r   r   r   r  r   r+   r  r  r  r   r  r  r   r_   rK   rN   rN      s   !c 3 !,    / /  && C    #J2:&$ E
$F4.G,>,> .`.
< <4;z"8,	

 J#M	.
d83C3C.D @

 >Bt l &2 N
` <H L
\ +7 EeUD6H0I 4
	uUE4'(%0CS0H*II	J4
l;
0T 0Yr_   rN   r   c                 h    t        j                  t        j                  j	                  | d            S )Nz	cloud.cfg)r   	read_confr   r   r   )r   s    rK   read_runtime_configr  t  s     >>"'',,w<==r_   r   c                    t        j                  t        j                         t        j                  t        |      t        |       t        j                         gd      S )Nr   T)reverse)r   r7   get_builtin_cfgread_conf_with_confdr&   r  read_conf_from_cmdline)r   r   s     rK   r   r   x  sW       "%%1C  ('')	
  r_   )Kr8   r  loggingr   r  collectionsr   
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+   	getLoggerr  r@   r,   r  r  r  r   rL   rN   rB  r  r   r   r_   rK   <module>r     s      	 
 "   I I I    * E E = B F B " &  2g!3 F B""B	B !B 	B
 
BJj jZ> > ;? s 4 r_   