
    }f3                        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
 d dlmZ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  ej4                  e      ZdZddgZddiZ G d de	      Z d Z!d Z"de de#de$fdZ% G d d      Z&y)    N)	signature)
ModuleType)DictList
NamedTupleOptional)configimporter
type_utilsutil)ALL_DISTROS)ConfigMerger)ReportEventStack)FREQUENCIES)Initcc_cc_migratorcc_rightscale_userdatacc_ubuntu_advantagecc_ubuntu_proc                   <    e Zd ZU eed<   eed<   eed<   ee   ed<   y)ModuleDetailsmodulename	frequencyrun_argsN)__name__
__module____qualname__r   __annotations__strr        :/usr/lib/python3/dist-packages/cloudinit/config/modules.pyr   r   +   s    
IN3ir#   r   c                     | j                  dd      }|j                         j                  d      r|dt        |      dz
   }|j	                         }|sy |j                  t              s
t        |}|S )N-_z.pyr      )replacelowerendswithlenstrip
startswith
MOD_PREFIX)r   
canon_names     r$   form_module_namer1   2   sp    c3'J""5)S_q%8:
!!#J  ,):6
r#   c           	      "   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
      y )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.)hasattrr3   
ValueErrorr   )modr   s     r$   validate_moduler:   >   s    C chh&CHH$se= /$ $
 	
 xxK/se=.Ixx$%Q(
 	
 sHse= /G G
 	
 r#   module_detailscfgreturnc                     t        | j                  j                  j                  di             }|sy|j	                  |j                               syy)Nactivate_by_schema_keysTF)	frozensetr   r3   getintersectionkeys)r;   r<   activate_by_schema_keys_keyss      r$   
_is_activerE   T   sL    #,""&&'@"E$  ('44SXXZ@r#   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y)ModulesNinitc                 \    || _         || _        d | _        |t        ddd      }|| _        y )Nzmodule-reporterzmodule-descF)r   descriptionreporting_enabled)rH   	cfg_files_cached_cfgr   reporter)selfrH   rL   rN   s       r$   __init__zModules.__init__`   s:    	"48'&)"'H
 !r#   r=   c                 $   | j                   ft        | j                  j                  | j                  j                  | j
                  | j                  j                        }|j                  | _         t        j                  | j                         S )N)paths
datasourceadditional_fnsbase_cfg)	rM   r   rH   rR   rS   rL   r<   copydeepcopy)rO   mergers     r$   r<   zModules.cfgm   sg     #!iioo99//#~~	F  &zzD}}T--..r#   c           	         g }|| j                   vr|S | j                   j                  |      }|s|S |D ]T  }|st        |t              r"|j	                  d|j                         i       9t        |t              rui }t        |      dk\  r|d   j                         |d<   t        |      dk\  r|d   j                         |d<   t        |      dk\  r|dd |d<   |s|j	                  |       t        |t              rei }d	}d
|v r|d
   j                         |d<   d}d|v r|d   j                         |d<   d|v r|d   xs g |d<   |s|s |j	                  |       3t        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.
        r9      r      freqr(   NargsFr   Tr   zFailed to read 'z' item in config, unknown type )r<   rA   
isinstancer!   appendr-   listr,   dict	TypeErrorr   obj_name)rO   r   module_listcfg_modsitemcontentsvalids          r$   _read_moduleszModules._read_modules{   s    #%txx88<<% $	D$$""tzz|
 D4)t9>&*1gmmoHUOt9>'+Aw}}HV$t9>'+ABxHV$&&x0D4)T>&*6l&8&8&:HUO E$&'+K'8'>'>'@HV$T>'+F|'9rHV$&&x0Z0068 C$	J r#   c           	         g }|D ]_  }|d   }|j                  d      }|j                  d      xs g }t        |      }|s=|r'|t        vrt        j                  d| d| d       d}|t
        v r.t        j                  d	| d
t
        |    dd       t
        |   }t        j                  |dt        j                  t              gdg      \  }}	|s;|t        v rt        j                  d|dd        nt        j                  d||	       t        j                  |d         }
t!        |
|       ||
j"                  d   }|j%                  t'        |
|||             b |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.
        r9   r\   r]   zConfig specified module z has an unknown frequency z22.1
deprecateddeprecated_versionNzModule has been renamed from z to z/. Update any references in /etc/cloud/cloud.cfgz24.1 handlez\Module `%s` has been removed from cloud-init. It may be removed from `/etc/cloud/cloud.cfg`.r(   z,Could not find module named %s (searched %s)r   r   )r   r   r   r   )rA   r1   r   r   	deprecateRENAMED_MODULESr
   find_moduler   rc   r	   REMOVED_MODULESLOGinfowarningimport_moduler:   r3   r_   r   )rO   raw_modsmostly_modsraw_modraw_namer\   r   mod_namemod_locslooked_locsr9   s              r$   _fixup_moduleszModules._fixup_modules   s     9	Gu~H;;v&D{{6*0bH'1HK/28* =&&*V- (. ?*7z*845 6>> (. +84$,$8$82z226:;hZ%!Hk .HHI  KKF #
 ((!5CC*|xx,!"%	e9	t r#   ry   c                    | j                   j                         }g }g }|D ]	  \  }}}}	 t        j                  d|||       |j	                  |       d| }	d|	d|}
t        |	|
| j                        }|| j                  ||d}|5  t        |j                        }|j                  }t        |      dk(  r.t        j                  dd	
       |j                  dt        i       |j                  |	|j                  ||      \  }}|rd|	z  |_        n
d|	z  |_        d d d         ||fS # 1 sw Y   xY w# t"        $ r:}t        j$                  t        d||       |j	                  ||f       Y d }~[d }~ww xY w)Nz(Running module %s (%s) with frequency %szconfig-zrunning z with frequency )r   rJ   parent)r   r<   cloudr]      z%Config modules with a `log` parameterz23.2rk   log)r\   z%s ran successfullyz%s previously ranzRunning module %s (%s) failed)rH   cloudifyrt   debugr_   r   rN   r<   r   ro   
parametersr,   r   rp   updaterunmessage	Exceptionlogexc)rO   ry   ccfailures	which_ranr9   r   r\   r]   run_namedescmyrep	func_argsfunc_signaturefunc_paramsran_res                     r$   _run_moduleszModules._run_modules   s   YY! 	%0 )	+!CtT(+		>c4
   &$TF+9A4H(!tDMM !88 		  G%.szz%:N"0";";K;'1,'N/5 "((%6 ff #**id % GC (=(H(;h(FG-)	+T 8$$)G G"  +C!@$Lq	**+s1   A!D>BD2#D>2D;	7D>>	F/E<<Fc                 X    |||d}|g}| j                  |      }| j                  |      S )N)r9   r]   r\   )r   r   )rO   r|   r]   r\   	mod_to_berx   ry   s          r$   
run_singlezModules.run_single+  s>     
	 ;))(3  --r#   c                    | j                  |      }| j                  |      }| j                  j                  j                  }g }g }| j
                  j                  dg       }g }g }	|D ]  }
|
\  }}}}||j                  d   }t        |
| j
                        s|j                  |       D|r5|t        gk7  r+||vr'||vr|j                  |       j|j                  |       |	j                  ||||g        |r%t        j                  ddj                  |             |r&t        j                  ddj                  |      |       |r%t        j                  ddj                  |             | j                  |	      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_modulesr4   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, )ri   r   rH   distror   r<   rA   r3   rE   r_   r   rt   ru   joinr   )rO   section_namerx   ry   distro_nameskippedforced
overriddeninapplicable_modsactive_modsr;   r9   r   _freq_argsworked_distross                   r$   run_sectionzModules.run_section7  s}    %%l3))(3ii&&++XX\\"6;
) 	:N(6%S$u{ XXi0Nndhh7!((. .[M"An4:-t, MM$'T5%89'	:* HH*+
 HH2 ! HH769JK  --r#   )NN)r   r   r   r   rP   propertyr	   Configr<   r   r   ri   r   r   r   r   r   r"   r#   r$   rG   rG   _   sn    !T ! /V]] / /9T$Z 9vA$}*= AF0%](; 0%d
.8.r#   rG   )'rV   logginginspectr   typesr   typingr   r   r   r   	cloudinitr	   r
   r   r   cloudinit.distrosr   cloudinit.helpersr   cloudinit.reporting.eventsr   cloudinit.settingsr   cloudinit.stagesr   	getLoggerr   rt   r/   rs   rq   r   r1   r:   ra   boolrE   rG   r"   r#   r$   <module>r      s        3 3 8 8 ) * 7 * !g! 
  ?
J 	
,} 4 D P. P.r#   