
    쑛h1L                     x   U d Z ddlZddlZddlZddlmZmZ ddlmZ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  ej*                  e      ZeZd	Zd
Zg dZdddgdddddedgdddddedgddddddg ddddddg dddddZi ddddd	g ddd d!dd"d#d$id%d&did"d#d$d'd(d&did&did)d*dddd+id,dd-diid.d	ddd/d0dgddd1d2d3d4gd4d5d6d7d8d&did"d#d$d'd9d4i id:d&did	ddd;did<d=d&did	ddd;d"id<d>d&didd	d+d"d#d$d<d?dd-diid@d&did&did)dAd&did	ddd;d"id<dBdd-diiZeeef   edC<   dDD ]
  Z ed=   ee <    dED ]
  Z ed@   ee <    dFD ]
  Z edA   ee <    dGeedgdHZ!eedI<    e"g dJ      Z#dK Z$dLefdMZ%dXdNZ&dYdOZ'dP Z(	 	 	 	 	 	 	 	 dZdQZ)dR Z*dSedTedUedVe+dLdf
dWZ,y)[zNTP: enable and configure ntp    N)DictMapping)subp
temp_utils	templater
type_utilsutil)Cloud)Config)
MetaSchema)PER_INSTANCEz/etc/ntp.conf   )	almalinuxalpineaosc
azurelinuxcentos
cloudlinuxcosdebian	eurolinuxfedorafreebsdmarinermiraclelinuxopenbsd	openeulerOpenCloudOSopenmandrivaopensuseopensuse-microosopensuse-tumbleweedopensuse-leapphotonraspberry-pi-osrhelrockysle_hpc	sle-microsles	TencentOSubuntu	virtuozzochronydz/etc/chrony.confchronyzchrony.conf.{distro})	check_execonfpathpackagesservice_nametemplate_nametemplatentpdntpzntp.conf.{distro}ntpdatez/etc/ntpd.confzntpd.conf.{distro}z/lib/systemd/systemd-timesyncdz-/etc/systemd/timesyncd.conf.d/cloud-init.confsystemd-timesyncdztimesyncd.conf)r/   r7   r8   openntpdr9   r   z/etc/chrony/chrony.conf)r1   r3   )r1   r2   r3   )r/   r7   r   z"/usr/lib/systemd/systemd-timesyncdz/etc/systemd/timesyncd.conf)r0   r1   r   r3   )r/   r9   r   )r7   r/   r   )r3   r1   r   r1   r   )r1   r3   r4   z/usr/local/etc/chrony.conf)r1   r2   r3   r4   z/usr/local/sbin/ntpdz/usr/local/etc/ntp.confr:   zntpd.conf.openbsd)r0   r1   r2   r3   r4   )r7   r/   r:   r   r   r   r0   )r/   r7   r9   r    r$   r%   r&   r*   r,   DISTRO_CLIENT_CONFIG)r!   r"   r#   )r   r   r'   )r(   r)   cc_ntp)iddistros	frequencyactivate_by_schema_keysmeta)r0   r1   r2   r3   c                     t         }t        j                  t              }| |v rt        j                  |||    gd      }|S )zConstruct a distro-specific ntp client config dictionary by merging
       distro specific changes into base config.

    @param distro: String providing the distro class name.
    @returns: Dict of distro configurations for ntp clients.
    Treverse)r;   copyNTP_CLIENT_CONFIGr	   mergemanydict)distrodcfgcfgs      9/usr/lib/python3/dist-packages/cloudinit/config/cc_ntp.pydistro_ntp_client_configsrL     s?      D
))%
&C~  #tF|!4dCJ    returnc                 F   t        |j                        }| r-| dk7  r(t        j                  d|        |j	                  | i       S |j                  dd      }i }|dk(  r|j                  D ]Q  }|j	                  |      }t        j                  |j	                  d            s9t        j                  d|       |} n |s7|j                  d   }t        j                  d|       |j	                  |i       }|S t        j                  d|       |j	                  |i       }|S )	a  Determine which ntp client is to be used, consulting the distro
       for its preference.

    @param ntp_client: String name of the ntp client to use.
    @param distro: Distro class instance.
    @returns: Dict of the selected ntp client or {} if none selected.
    autoz4Selected NTP client "%s" via user-data configuration
ntp_clientr0   z+Selected NTP client "%s", already installedr   z<Selected distro preferred NTP client "%s", not yet installedz1Selected NTP client "%s" via distro system config)	rL   nameLOGdebugget
get_optionpreferred_ntp_clientsr   which)rQ   rH   
distro_cfgdistro_ntp_client	clientcfgclientrJ   s          rK   select_ntp_clientr]   !  s%    +6;;7J jF*		BJ	
 ~~j"-- )),?IF"22 	F..(Czz#''+./		A6  		 11!4FIIN #vr2I  			?	
 NN#4b9	rM   c                 J    t        j                  |      ry|dg} | |       y)ah  Install ntp client package if not already installed.

    @param install_func: function.  This parameter is invoked with the contents
    of the packages parameter.
    @param packages: list.  This parameter defaults to ['ntp'].
    @param check_exe: string.  The name of a binary that indicates the package
    the specified package is already installed.
    Nr7   )r   rX   )install_funcr2   r0   s      rK   install_ntp_clientr`   S  s(     zz)7rM   c                 v    t         j                  j                  |       rt        j                  | | dz          yy)zRename any existing ntp client config file

    @param confpath: string. Specify a path to an existing ntp client
    configuration file.
    z.distN)ospathexistsr	   renamer1   s    rK   rename_ntp_confrg   d  s,     
ww~~hHh01  rM   c           	          g }| }| dk(  rd}n| dk(  s| dk(  rd}t        t              D ]E  }|j                  dj                  t	        |      g|gz   dgz   D cg c]  }|s|	 c}             G |S c c}w )zGenerate a list of server names to populate an ntp client configuration
    file.

    @param distro: string.  Specify the distro name
    @returns: list: A list of strings representing ntp servers for this distro.
    r*   r    r   r    .zpool.ntp.org)rangeNR_POOL_SERVERSappendjoinstr)rH   namespool_distroxns        rK   generate_server_namesrt   n  s     EK !	8	v4 ?# 
HH VH}47GGMq1M	

 L	 Ns   A2A2c	                 (   |sg }|sg }|sg }|sg }|s|s| dk(  ry|s;| dk(  r6|dk(  r1t        |       }t        j                  ddj                  |             n4|s2|s0t        |       }t        j                  ddj                  |             |st	        d      |s|st	        d	      ||||d
}	|r3t        j                  dd      }
|
d   }t        j                  ||       t        j                  |||	       |rt        j                  |       yy)a  Render a ntp client configuration for the specified client.

    @param distro_name: string.  The distro class name.
    @param service_name: string. The name of the NTP client service.
    @param servers: A list of strings specifying ntp servers. Defaults to empty
    list.
    @param pools: A list of strings specifying ntp pools. Defaults to empty
    list.
    @param allow: A list of strings specifying a network/CIDR. Defaults to
    empty list.
    @param peers: A list nodes that should peer with each other. Defaults to
    empty list.
    @param path: A string to specify where to write the rendered template.
    @param template_fn: A string to specify the template source file.
    @param template: A string specifying the contents of the template. This
    content will be written to a temporary file before being used to render
    the configuration file.

    @raises: ValueError when path is None.
    @raises: ValueError when template_fn is None and template is None.
    r   Nr   r6   z%Adding distro default ntp servers: %s,z*Adding distro default ntp pool servers: %sz Invalid value for path parameterz$Not template_fn or template provided)serverspoolsallowpeersztemplate_name-z.tmpl)prefixsuffix   )content)rt   rS   rT   rn   
ValueErrorr   mkstempr	   
write_filer   render_to_filedel_file)distro_namer3   rw   rx   ry   rz   rc   template_fnr5   paramstfiles              rK   write_ntp_config_templater     s   @ 5[E%9{h.<63I (4		9388G;LM%k2		8#((5/	
 ;<<x?@@ 	F ""*:7KAhX6[$7k" rM   c                    g }t         j                  t        | j                                     }|r<dj	                  t        |            }|j                  dj                  |             n<t        | j                  d      | j                  d      g      s|j                  d       t        | j                               D ]  \  }}d|z   }|dk(  r?t        |t        |t              g      r,|j                  d	j                  ||
             O|dk(  r4t        |t              re|j                  dj                  ||
             |dv r7|t        |t              r|j                  dj                  ||
             t        |t              r|j                  dj                  ||
              |r*t        dj                  dj	                  |                  y)aO  Validate user-provided ntp:config option values.

    This function supplements flexible jsonschema validation with specific
    value checks to aid in triage of invalid user-provided configuration.

    @param ntp_config: Dictionary of configuration value under 'ntp'.

    @raises: ValueError describing invalid values provided.
    z, z(Missing required ntp:config keys: {keys})keysr5   r4   zJEither ntp:config:template or ntp:config:template_name values are requiredzntp:config:r1   z6Expected a config file path {keypath}. Found ({value}))keypathvaluer2   zHExpected a list of required package names for {keypath}. Found ({value}))r5   r4   Nz5Expected a string type for {keypath}. Found ({value})z$Invalid ntp configuration:\n{errors}
)errors)REQUIRED_NTP_CONFIG_KEYS
differencesetr   rn   sortedrm   formatanyrU   itemsall
isinstancero   listr   )
ntp_configr   missingr   keyr   r   s          rK   supplemental_schema_validationr     s    F&11#joo6G2HIGyy)6==4=H	
 	
	#Z^^O%DE 		
 Z--/0 
U#%*z%567''-vgUv'K JeT*''-vgUv'K 11}eS)''-vgUv'K E3'MMGNN#5 O /: 3::yy( ; 
 	
 rM   rR   rJ   cloudargsc                    d|vrt         j                  d|        y|d   }|i }t        |t              s.t	        dj                  t        j                  |                  |j                  dd      }t        j                  |      rt         j                  d|        yt        |j                  d	      |j                        }t        j                  ||j                  d
i       gd      }t        |       t        |j                  d             d}|j                  d      s[|d   j!                  d|j                  j"                        }|j%                  |      }|sd|j                  d      z  }	t	        |	      t         j                  d|j                  d             t         j                  d|j                  dg              t         j                  d|j                  dg              t         j                  d|j                  dg              t         j                  d|j                  dg              t'        |j                  j"                  |j                  d      |j                  dg       |j                  dg       |j                  dg       |j                  dg       |j                  d      ||j                  d      	       t)        |j                  j*                  |d   |d          t        j,                         rn|j                  d      d k7  r:	 |j                  j/                  d!d        	 |j                  j/                  d#d        	 |j                  j/                  d%|d          	 |j                  j/                  d'|d          y# t0        j2                  $ r t         j5                  d"       Y w xY w# t0        j2                  $ r t         j5                  d$       Y w xY w# t0        j2                  $ r}
t         j7                  d&|
        d}
~
ww xY w# t0        j2                  $ r}
t         j7                  d(|
        d}
~
ww xY w))zEnable and configure ntp.r7   z8Skipping module named %s, not present or disabled by cfgNzL'ntp' key existed in config, but not a dictionary type, is a {_type} instead)_typeenabledTz)Skipping module named %s, disabled by cfgrQ   configrC   r1   rf   r5   r4   z{distro}z#No template found, not rendering %szservice_name: %sr3   zservers: %srw   z	pools: %srx   z	allow: %sry   z	peers: %srz   )r3   rw   rx   ry   rz   rc   r   r5   r2   r0   )r2   r0   r6   stopz Failed to stop base ntpd servicedisablez#Failed to disable base ntpd serviceenablez Failed to enable ntp service: %sreloadz&Failed to reload/start ntp service: %s)rS   rT   r   dictRuntimeErrorr   r   obj_namerU   r	   is_falser]   rH   rG   r   rg   replacerR   get_template_filenamer   r`   install_packagesis_BSDmanage_servicer   ProcessExecutionErrorwarning	exception)rR   rJ   r   r   ntp_cfgr   ntp_client_configr   r4   msges              rK   handler     s   C		F	
 	%jG g&$$*F1D1DW1MF$N
 	
 kk)T*G}}W		=tD *L!5<< **	GKK"56 ##45.22:>?K  ,)/:BB))
 11-@5#''89  s##II "3"7"7"GHIImW[[B78IIk7;;w34IIk7;;w34IIk7;;w34&**>:Ir*kk'2&kk'2&kk'2&"":."&&z2
 %%":.#K0
 {{}  0F:@++FF;C++Iv>	LL''+N;##'7	
 -- @>?@ -- CABC )) 	MM<a@	 %% >BsT   &N  O  O<  P.  (O
O(O98O9<P+P&&P+.QQQ)Nr6   )N)NNNNNNNN)-__doc__rE   loggingrb   typingr   r   	cloudinitr   r   r   r   r	   cloudinit.cloudr
   cloudinit.configr   cloudinit.config.schemar   cloudinit.settingsr   	getLogger__name__rS   r?   NTP_CONFrl   r>   rF   r;   ro   __annotations__rH   rA   	frozensetr   rL   r]   r`   rg   rt   r   r   r   r    rM   rK   <module>r      s   $   	   C C ! # . +g!	 H &J / G, K!, $- 6C+)C) XN)1%

 ("

N) =5
N)$ I
 >5
	%N)6 F
 I
	7N)F 
%1
GN)R 1
SN)\ '"0
 5!
%3	
 01#&0
]N)D I
 >5
	EN)V BWN)\ I
 ("

 9
]N)t I
 ("

 =
uN)L I
 !'OD=5
	MN)` 1
aN)j F
 I
	kN)z I
 ("

 =
{N)R 1
SN) d39o N` K DF#7
#C D 3 @F#7#? @ ' @F#7#? @  %w	j  %9 
/W /d"2> 


	J#Z:
z_ _6 _% _t _ _rM   