
    ܍~c-                        d dl Z d dlmZ d dlmZmZmZmZ d dlm	Z	m
Z
 d dlmZ d dl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  ej        e          ZdZ G d de          Zd Zd Zdede de!fdZ" G d d          Z#dS )    N)
ModuleType)DictList
NamedTupleOptional)configimporter)log)
type_utilsutil)ALL_DISTROS)ConfigMerger)ReportEventStack)FREQUENCIES)Initcc_c                   D    e Zd ZU eed<   eed<   eed<   ee         ed<   dS )ModuleDetailsmodulename	frequencyrun_argsN)__name__
__module____qualname__r   __annotations__strr        :/usr/lib/python3/dist-packages/cloudinit/config/modules.pyr   r      s@         
IIINNN3ir   r   c                 .   |                      dd          }|                                                    d          r|dt          |          dz
           }|                                }|sd S |                    t                    st          |}|S )N-_z.pyr      )replacelowerendswithlenstrip
startswith
MOD_PREFIX)r   
canon_names     r    form_module_namer-   &   s    c3''J""5)) ;S__q%8 9:
!!##J t  ,, 7)z::6
r   c           	      2   t          | d          rd| j        vs	d| j        vrt          d|  d| d          | j        d         t          vr$t          d|  d| d| j        d          d          t          | d	          rt          d|  d| d
          d S )Nmetar   distroszModule 'z' with name 'z4' MUST have a 'meta' attribute of type 'MetaSchema'.z' has an invalid frequency .schemazX' has a JSON 'schema' attribute defined. Please define schema in cloud-init-schema,json.)hasattrr/   
ValueErrorr   )modr   s     r    validate_moduler6   2   s   C  
ch&&CH$$$s $ $ $ $ $
 
 	
 xK//(s ( ( ( (x$( ( (
 
 	
 sH 
Gs G G G G G
 
 	

 
r   module_detailscfgreturnc                     t          | j        j                            di                     }|sdS |                    |                                          sdS dS )Nactivate_by_schema_keysTF)	frozensetr   r/   getintersectionkeys)r7   r8   activate_by_schema_keys_keyss      r    
_is_activerA   H   sc    #,"&&'@"EE$ $  ( t'44SXXZZ@@ u4r   c                       e Zd ZddefdZedej        fd            Zde	e
         fdZde	e         fdZde	e         fd	Zdd
Zd ZdS )ModulesNinitc                 f    || _         || _        d | _        |t          ddd          }|| _        d S )Nzmodule-reporterzmodule-descF)r   descriptionreporting_enabled)rD   	cfg_files_cached_cfgr   reporter)selfrD   rH   rJ   s       r    __init__zModules.__init__T   sH    	"48'&)"'  H
 !r   r9   c                     | j         Bt          | j        j        | j        j        | j        | j        j                  }|j        | _         t          j        | j                   S )N)paths
datasourceadditional_fnsbase_cfg)	rI   r   rD   rN   rO   rH   r8   copydeepcopy)rK   mergers     r    r8   zModules.cfga   s[     #!io9/#~	  F  &zD}T-...r   c           	         g }|| j         vr|S | j                             |          }|s|S |D ]}|st          |t                    r*|                    d|                                i           Et          |t                    ri }t          |          dk    r|d                                         |d<   t          |          dk    r|d                                         |d<   t          |          dk    r|dd         |d<   |r|                    |           t          |t                    rti }d	}d
|v r|d
                                         |d<   d}d|v r|d                                         |d<   d|v r|d         pg |d<   |r|r|                    |           }t          d|dt          j        |                    |S )aU  Read the modules from the config file given the specified name.

        Returns a list of module definitions. E.g.,
        [
            {
                "mod": "bootcmd",
                "freq": "always",
                "args": "some_arg",
            }
        ]

        Note that in the default case, only "mod" will be set.
        r5      r      freqr$   NargsFr   Tr   zFailed to read 'z' item in config, unknown type )r8   r=   
isinstancer   appendr)   listr(   dict	TypeErrorr   obj_name)rK   r   module_listcfg_modsitemcontentsvalids          r    _read_moduleszModules._read_moduleso   s    #%tx8<<%% 	 $	 $	D $$$ !""tzz||   
 D4)) t99>>&*1gmmooHUOt99>>'+Aw}}HV$t99>>'+ABBxHV$ 1&&x000D4)) T>>&*6l&8&8&:&:HUO E$&&'+K'8'>'>'@'@HV$T>>'+F|'9rHV$ 1 1&&x000ittZ06668   r   c           	      X   g }|D ]#}|d         }|                     d          }|                     d          pg }t          |          }|sI|r'|t          vrt                              d||           d}t          j        |dt          j        t                    gdg          \  }}	|st                              d||	           t          j
        |d	                   }
t          |
|           ||
j        d
         }|                    t          |
|||                     %|S )zConvert list of returned from _read_modules() into new format.

        Invalid modules and arguments are ignored.
        Also ensures that the module has the required meta fields.
        r5   rX   rY   z6Config specified module %s has an unknown frequency %sN handlez,Could not find module named %s (searched %s)r   r   )r   r   r   r   )r=   r-   r   LOGwarningr	   find_moduler   r_   r   import_moduler6   r/   r[   r   )rK   raw_modsmostly_modsraw_modraw_namerX   r   mod_namemod_locslooked_locsr5   s              r    _fixup_moduleszModules._fixup_modules   sn     &	 &	Gu~H;;v&&D{{6**0bH'11H  K//L   $,$82z26::;hZ% %!Hk  B  
 (!55CC***|x,!"%	      r   rn   c                 d   | j                                         }g }g }|D ]\  }}}}	 t                              d|||           || j        |t          |g}	|                    |           d| }
d|
d|}t          |
|| j                  }|5  |                    |
|j	        |	|          \  }}|rd|
z  |_
        n
d|
z  |_
        d d d            n# 1 swxY w Y   # t          $ r>}t          j        t          d	||           |                    ||f           Y d }~d }~ww xY w||fS )
Nz(Running module %s (%s) with frequency %szconfig-zrunning z with frequency )r   rF   parent)rX   z%s ran successfullyz%s previously ranzRunning module %s (%s) failed)rD   cloudifyri   debugr8   r[   r   rJ   runrh   message	Exceptionr   logexc)rK   rn   ccfailures	which_ranr5   r   rX   rY   	func_argsrun_namedescmyrepran_res                   r    _run_moduleszModules._run_modules   s   Y!! 	'2  	+  	+#S$d+		>c4   "48Rd;	  &&&+T++9A44H(!tDM    G G ff #*id %  GC  G(=(H(;h(FG G G G G G G G G G G G G G G  + + +C!@$LLLq	********+ 8$$s<   A)C#9CC#C	C#C	C##
D+-3D&&D+c                 h    |||d}|g}|                      |          }|                     |          S )N)r5   rY   rX   )rt   r   )rK   rq   rY   rX   	mod_to_berm   rn   s          r    
run_singlezModules.run_single  sI     
 
	 ;))(33  ---r   c                 L   |                      |          }|                     |          }| j        j        j        }g }g }| j                            dg           }g }g }	|D ]}
|
\  }}}}||j        d         }t          |
| j                  s|	                    |           D|r?|t          gk    r3||vr/||vr|	                    |           p|	                    |           |		                    ||||g           |r.t                              dd                    |                     |r/t                              dd                    |          |           |r.t                              dd                    |                     |                     |	          S )	zRuns all modules in the given section.

        section_name - One of the modules lists as defined in
          /etc/cloud/cloud.cfg. One of:
         - cloud_init_modules
         - cloud_config_modules
         - cloud_final_modules
        unverified_modulesNr0   z?Skipping modules '%s' because no applicable config is provided.,zSkipping modules '%s' because they are not verified on distro '%s'.  To run anyway, add them to 'unverified_modules' in config.z running unverified_modules: '%s'z, )re   rt   rD   distror   r8   r=   r/   rA   r[   r   ri   infojoinr   )rK   section_namerm   rn   distro_nameskippedforced
overriddeninapplicable_modsactive_modsr7   r5   r   _freq_argsworked_distross                   r    run_sectionzModules.run_section  s    %%l33))(33i&+X\\"6;;
) 	: 	:N(6%S$u{ Xi0Nndh77 !((...  (.[M"A"An44:--t,,, MM$'''T5%89999 	HH*++  
  	HH2 !!    	LHH769J9JKKK  ---r   )NN)r   r   r   r   rL   propertyr   Configr8   r   r   re   r   rt   r   r   r   r   r   r    rC   rC   S   s        ! !T ! ! ! ! /V] / / / X/9T$Z 9 9 9 9v.$}*= . . . .`'%](; '% '% '% '%R
. 
. 
. 
.8. 8. 8. 8. 8.r   rC   )$rR   typesr   typingr   r   r   r   	cloudinitr   r	   r
   loggingr   r   cloudinit.distrosr   cloudinit.helpersr   cloudinit.reporting.eventsr   cloudinit.settingsr   cloudinit.stagesr   	getLoggerr   ri   r+   r   r-   r6   r]   boolrA   rC   r   r   r    <module>r      s          3 3 3 3 3 3 3 3 3 3 3 3 & & & & & & & & $ $ $ $ $ $ & & & & & & & & ) ) ) ) ) ) * * * * * * 7 7 7 7 7 7 * * * * * * ! ! ! ! ! !g!! 
    J   	 	 	
 
 
,} 4 D    t. t. t. t. t. t. t. t. t. t.r   