o
    x[h¨K  ã                   @   s  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 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dddœd	g ddœdœdd d!d"œid#did d!d"œd$œd#did#did%œdddd&œidd'diid	ddd(œd)dgddd*œd+d,d-gd-d.d/œd0œd#did d!d"œd$œd-i id#did	ddœd1did2œd#did	ddœd1d id2œd#didd	d&œd d!d"œd2œd#did#did%œd#did	ddœd1d id2œdd'diid3œZeeef ed4< d5D ]	Z ed6 ee < �qd7D ]	Z ed8 ee < �q d9D ]	Z ed: ee < �q,d;eedgd<œZ!eed=< e"g d>¢ƒZ#d?d@„ Z$dAefdBdC„Z%dTdDdE„Z&dUdFdG„Z'dHdI„ Z(								dVdJdK„Z)dLdM„ Z*dNedOedPedQe+dAdf
dRdS„Z,dS )Wz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Ú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-   r5   r6   Úopenntpdr7   z/etc/chrony/chrony.conf)r/   r1   )r/   r0   r1   )r-   r5   z"/usr/lib/systemd/systemd-timesyncdz/etc/systemd/timesyncd.conf)r.   r/   r1   )r-   r7   )r5   r-   )r1   r/   r/   )r/   r1   r2   z/usr/local/etc/chrony.conf)r/   r0   r1   r2   z/usr/local/sbin/ntpdz/usr/local/etc/ntp.confr8   zntpd.conf.openbsd)r.   r/   r0   r1   r2   )r5   r-   r8   r.   )r-   r5   r7   )r   r   r   r   r   r   r   r   r   r   r   r#   r$   r(   r*   ÚDISTRO_CLIENT_CONFIG)r    r!   r"   r   )r   r   r%   r$   )r&   r'   r(   Úcc_ntp)ÚidÚdistrosÚ	frequencyÚactivate_by_schema_keysÚmeta)r.   r/   r0   r1   c                 C   s0   t }t t¡}| |v rtj|||  gdd�}|S )zòConstruct 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)r9   ÚcopyÚNTP_CLIENT_CONFIGr   Úmergemanydict)ÚdistroÚdcfgÚcfg© rH   ú9/usr/lib/python3/dist-packages/cloudinit/config/cc_ntp.pyÚdistro_ntp_client_configs  s
   
rJ   Úreturnc                 C   sÆ   t |jƒ}| r| dkrt d| ¡ | | i ¡S | dd¡}i }|dkrU|jD ]}| |¡}t | d¡¡r?t d|¡ |} nq&|sS|jd }t d|¡ | |i ¡}|S t d|¡ | |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_clientr.   z+Selected NTP client "%s", already installedr   z<Selected distro preferred NTP client "%s", not yet installedz1Selected NTP client "%s" via distro system config)	rJ   ÚnameÚLOGÚdebugÚgetÚ
get_optionÚpreferred_ntp_clientsr   Úwhich)rM   rE   Ú
distro_cfgÚdistro_ntp_clientÚ	clientcfgÚclientrG   rH   rH   rI   Úselect_ntp_client  s@   

ÿ

ÿû
þúþrY   c                 C   s(   t  |¡rdS |du rdg}| |ƒ dS )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.
    Nr5   )r   rT   )Úinstall_funcr0   r.   rH   rH   rI   Úinstall_ntp_clientM  s
   
	r[   c                 C   s$   t j | ¡rt | | d ¡ dS dS )zŽRename 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©r/   rH   rH   rI   Úrename_ntp_conf^  s   ÿra   c                 C   sh   g }| }| dkrd}n
| dks| dkrd}t tƒD ]}| d dd„ t|ƒg|g d	g D ƒ¡¡ q|S )
zÛGenerate 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   Ú Ú.c                 S   s   g | ]}|r|‘qS rH   rH   )Ú.0ÚnrH   rH   rI   Ú
<listcomp>~  s    z)generate_server_names.<locals>.<listcomp>zpool.ntp.org)ÚrangeÚNR_POOL_SERVERSÚappendÚjoinÚstr)rE   ÚnamesÚpool_distroÚxrH   rH   rI   Úgenerate_server_namesh  s   ÿÿro   c	                 C   sü   |sg }|sg }|sg }|sg }|s|s| dkrdS |s2| dkr2|dkr2t | ƒ}t dd |¡¡ n|sC|sCt | ƒ}t dd |¡¡ |sItdƒ‚|sQ|sQtd	ƒ‚||||d
œ}	|rltjddd�}
|
d }tj||d� t	 
|||	¡ |r|t |¡ dS dS )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   r4   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)ro   rO   rP   rj   Ú
ValueErrorr   Úmkstempr   Ú
write_filer   Úrender_to_fileÚdel_file)Údistro_namer1   rq   rr   rs   rt   r]   Útemplate_fnr3   ÚparamsÚtfilerH   rH   rI   Úwrite_ntp_config_template…  sF    
ÿüÿr‚   c                 C   sB  g }t  t|  ¡ ƒ¡}|rd t|ƒ¡}| dj|d�¡ nt|  	d¡|  	d¡gƒs/| d¡ t|  
¡ ƒD ]\\}}d| }|dkrUt|t|tƒgƒsT| d	j||d
�¡ q5|dkrit|tƒsh| dj||d
�¡ q5|dv r‚|du rrq5t|tƒs�| dj||d
�¡ q5t|tƒs‘| dj||d
�¡ q5|rŸtdjd |¡d�ƒ‚dS )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})Úkeysr3   r2   zJEither ntp:config:template or ntp:config:template_name values are requiredzntp:config:r/   z6Expected a config file path {keypath}. Found ({value}))ÚkeypathÚvaluer0   zHExpected a list of required package names for {keypath}. Found ({value}))r3   r2   Nz5Expected a string type for {keypath}. Found ({value})z$Invalid ntp configuration:\n{errors}Ú
)Úerrors)ÚREQUIRED_NTP_CONFIG_KEYSÚ
differenceÚsetrƒ   rj   Úsortedri   ÚformatÚanyrQ   ÚitemsÚallÚ
isinstancerk   Úlistry   )Ú
ntp_configr‡   Úmissingrƒ   Úkeyr…   r„   rH   rH   rI   Úsupplemental_schema_validationÒ  sn   

ÿÿÿÿÿ€
ÿÿ€
ÿÿ€
ÿÿ€ÿÿÿr•   rN   rG   ÚcloudÚargsc                 C   sÐ  d|vrt  d| ¡ dS |d }|du ri }t|tƒs&tdjt |¡d�ƒ‚| dd¡}t	 
|¡r9t  d| ¡ dS t| d	¡|jƒ}t	j|| d
i ¡gdd�}t|ƒ t| d¡d� d}| d¡s~|d  d|jj¡}| |¡}|s~d| d¡ }	t|	ƒ‚t  d| d¡¡ t  d| dg ¡¡ t  d| dg ¡¡ t  d| dg ¡¡ t  d| dg ¡¡ t|jj| d¡| dg ¡| dg ¡| dg ¡| dg ¡| d¡|| d¡d�	 t|jj|d |d d� t	 ¡ �rF| d¡d k�r%z	|j d!d ¡ W n tj�y
   t  d"¡ Y nw z	|j d#d ¡ W n tj�y$   t  d$¡ Y nw z|j d%|d ¡ W n tj�yE }
 zt  d&|
¡ ‚ d}
~
ww z|j d'|d ¡ W dS  tj�yg }
 zt  d(|
¡ ‚ d}
~
ww ))zEnable and configure ntp.r5   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 cfgrM   Úconfigr@   r/   r`   r3   r2   z{distro}z#No template found, not rendering %szservice_name: %sr1   zservers: %srq   z	pools: %srr   z	allow: %srs   z	peers: %srt   )r1   rq   rr   rs   rt   r]   r   r3   r0   r.   )r0   r.   r4   Ú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)rO   rP   r�   ÚdictÚRuntimeErrorrŒ   r   Úobj_namerQ   r   Úis_falserY   rE   rD   r•   ra   ÚreplacerN   Úget_template_filenamer‚   r[   Úinstall_packagesÚis_BSDÚmanage_servicer   ÚProcessExecutionErrorÚwarningÚ	exception)rN   rG   r–   r—   Úntp_cfgr™   Úntp_client_configr   r2   ÚmsgÚerH   rH   rI   Úhandle  s²   ÿ

ÿÿ
ÿÿ
ÿ
ÿÿ



÷ý
ÿÿÿ€þ
ÿ€þr¯   )Nr4   )N)NNNNNNNN)-Ú__doc__rB   Úloggingr\   Útypingr   r   Ú	cloudinitr   r   r   r   r   Úcloudinit.cloudr	   Úcloudinit.configr
   Úcloudinit.config.schemar   Úcloudinit.settingsr   Ú	getLoggerÚ__name__rO   r=   ÚNTP_CONFrh   r<   rC   r9   rk   Ú__annotations__rE   r?   Ú	frozensetrˆ   rJ   rY   r[   ra   ro   r‚   r•   r‘   r¯   rH   rH   rH   rI   Ú<module>   sj  
#ú	ú	ú	ú	úß/þýûþÿÿþüÿÿü	þÿÿÿýüûôÿþü
ÿÿþÿøÿþÿøÿþûÿÿü
ÿþÿøÿÿ �û 	üÿ
2


÷M"=