name : cc_ntp.cpython-39.pyc
a

'�Dg�K�@s�UdZddlZddlZddlZddlmZmZddlmZm	Z	m
Z
mZmZddl
mZddlmZddlmZddlmZe�e�ZeZd	Zd
Zgd�Zdd
dgdddd�dedgdddd�dedgdddd�ddgdddd�ddgdddd�d�Zddd�d	gdd�d�dd d!d"�id#did d!d"�d$�d#did#did%�dddd&�idd'diid	ddd(�d)dgddd*�d+d,d-gd-d.d/�d0�d#did d!d"�d$�d-iid#did	dd�d1did2�d#did	dd�d1d id2�d#didd	d&�d d!d"�d2�d#did#did%�d#did	dd�d1d id2�dd'diid3�Zeeefed4<d5D]Z ed6ee <�q(d7D]Z ed8ee <�q@d9D]Z ed:ee <�qXd;eedgd<�Z!eed=<e"gd>��Z#d?d@�Z$edA�dBdC�Z%dQdDdE�Z&dRdFdG�Z'dHdI�Z(dSdJdK�Z)dLdM�Z*eeee+ddN�dOdP�Z,dS)TzNTP: 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�	eurolinuxZfedora�freebsd�marinerZmiraclelinux�openbsdZ	openeulerZOpenCloudOS�openmandriva�opensuse�opensuse-microos�opensuse-tumbleweed�
opensuse-leap�photon�rhel�rocky�sle_hpc�	sle-micro�slesZ	TencentOS�ubuntuZ	virtuozzoZchronydz/etc/chrony.conf�chronyzchrony.conf.{distro})�	check_exe�confpath�packages�service_name�
template_name�template�ntpd�ntpzntp.conf.{distro}�ntpdatez/etc/ntpd.confzntpd.conf.{distro}z/lib/systemd/systemd-timesyncdz-/etc/systemd/timesyncd.conf.d/cloud-init.conf�systemd-timesyncdztimesyncd.conf)r&r.r/�openntpdr0z/etc/chrony/chrony.conf)r(r*)r(r)r*)r&r.z"/usr/lib/systemd/systemd-timesyncdz/etc/systemd/timesyncd.conf)r'r(r*)r&r0)r.r&)r*r(r()r(r*r+z/usr/local/etc/chrony.conf)r(r)r*r+z/usr/local/sbin/ntpdz/usr/local/etc/ntp.confr1zntpd.conf.openbsd)r'r(r)r*r+)r.r&r1r')r&r.r0)rrrrrrrrrrrrr r$r%�DISTRO_CLIENT_CONFIG)rrrr)rrr!r )r"r#r$Zcc_ntp)�id�distros�	frequencyZactivate_by_schema_keys�meta)r'r(r)r*cCs0t}t�t�}||vr,tj|||gdd�}|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)r2�copy�NTP_CLIENT_CONFIGr�
mergemanydict)�distroZdcfg�cfg�r>�;/usr/lib/python3.9/site-packages/cloudinit/config/cc_ntp.py�distro_ntp_client_configs
s

r@)�returncCs�t|j�}|r.|dkr.t�d|�|�|i�S|�dd�}i}|dkr�|jD]2}|�|�}t�|�d��rLt�d|�|}q�qL|s�|jd}t�d|�|�|i�}nt�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 installedrz<Selected distro preferred NTP client "%s", not yet installedz1Selected NTP client "%s" via distro system config)	r@�name�LOG�debug�getZ
get_optionZpreferred_ntp_clientsr�which)rCr<Z
distro_cfgZdistro_ntp_clientZ	clientcfgZclientr=r>r>r?�select_ntp_clients<

�

�
��rIcCs(t�|�rdS|durdg}||�dS)ahInstall 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.
    Nr.)rrH)Zinstall_funcr)r'r>r>r?�install_ntp_clientMs
	
rJcCs tj�|�rt�||d�dS)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(r>r>r?�rename_ntp_conf^srPcCshg}|}|dkrd}n|dks&|dkr*d}tt�D]0}|�d�dd�t|�g|gd	gD���q2|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$rrr��.cSsg|]}|r|�qSr>r>)�.0�nr>r>r?�
<listcomp>~�z)generate_server_names.<locals>.<listcomp>zpool.ntp.org)�range�NR_POOL_SERVERS�append�join�str)r<�namesZpool_distro�xr>r>r?�generate_server_nameshs��r^c	Cs$|sg}|sg}|sg}|s g}t|�dkrDt|�dkrD|dkrDdSt|�dkr||dkr||dkr|t|�}t�dd�|��n2t|�dkr�t|�dkr�t|�}t�dd�|��|s�td	��|s�|s�td
��||||d�}	|�rtjdd
d�}
|
d}tj	||d�t
�|||	�|�r t�|�dS)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.
    rrNrr-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�peersztemplate_name-z.tmpl)�prefix�suffix�)Zcontent)
�lenr^rErFrZ�
ValueErrorrZmkstemprZ
write_filerZrender_to_fileZdel_file)Zdistro_namer*r`rarbrcrL�template_fnr,�paramsZtfiler>r>r?�write_ntp_config_template�sN  
���
��rkcCsHg}t�t|����}|r<d�t|��}|�dj|d��n"t|�	d�|�	d�g�s^|�d�t|�
��D]�\}}d|}|dkr�t|t|t
�g�s�|�d	j||d
��qj|dkr�t|t�s�|�dj||d
��qj|d
v�r|dur�qjt|t
��s&|�dj||d
��qjt|t
�sj|�dj||d
��qj|�rDtdjd�|�d���dS)aOValidate 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})�keysr,r+zJEither ntp:config:template or ntp:config:template_name values are requiredzntp:config:r(z6Expected a config file path {keypath}. Found ({value}))�keypath�valuer)zHExpected a list of required package names for {keypath}. Found ({value}))r,r+Nz5Expected a string type for {keypath}. Found ({value})z$Invalid ntp configuration:\n{errors}�
)�errors)�REQUIRED_NTP_CONFIG_KEYS�
difference�setrlrZ�sortedrY�format�anyrG�items�all�
isinstancer[�listrh)Z
ntp_configrp�missingrl�keyrnrmr>r>r?�supplemental_schema_validation�sd

�����
��
��
����r})rDr=�cloud�argsrAcCs�d|vrt�d|�dS|d}|dur,i}t|t�sLtdjt�|�d���|�dd�}t	�
|�rrt�d|�dSt|�d	�|j�}t	j
||�d
i�gdd�}t|�t|�d�d
�d}|�d�s�|d�d|jj�}|�|�}|s�d|�d�}	t|	��t�d|�d��t�d|�dg��t�d|�dg��t�d|�dg��t�d|�dg��t|jj|�d�|�dg�|�dg�|�dg�|�dg�|�d�||�d�d�	t|jj|d|dd�t	���r�|�d�d k�rJz|j�d!d �Wn tj�yt�d"�Yn0z|j�d#d �Wn tj�yHt�d$�Yn0z|j�d%|d�Wn6tj�y�}
zt�d&|
��WYd}
~
n
d}
~
00z|j�d'|d�Wn6tj�y�}
zt�d(|
��WYd}
~
n
d}
~
00dS))zEnable and configure ntp.r.z8Skipping module named %s, not present or disabled by cfgNzL'ntp' key existed in config, but not a dictionary type, is a {_type} instead)Z_type�enabledTz)Skipping module named %s, disabled by cfgrCZconfigr7r(rOr,r+z{distro}z#No template found, not rendering %szservice_name: %sr*zservers: %sr`z	pools: %sraz	allow: %srbz	peers: %src)r*r`rarbrcrLrir,r)r')r)r'r-�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)rErFry�dict�RuntimeErrorrurZobj_namerGrZis_falserIr<r;r}rP�replacerDZget_template_filenamerkrJZinstall_packagesZis_BSDZmanage_servicerZProcessExecutionErrorZwarning�	exception)rDr=r~rZntp_cfgr�Zntp_client_configrir+�msg�er>r>r?�handles��
��
��
�
��



��
��r�)Nr-)N)NNNNNNNN)-�__doc__r9ZloggingrK�typingrrZ	cloudinitrrrrrZcloudinit.cloudr	Zcloudinit.configr
Zcloudinit.config.schemarZcloudinit.settingsrZ	getLogger�__name__rEr5ZNTP_CONFrXr4r:r2r[�__annotations__r<r6�	frozensetrqr@rIrJrPr^rkr}rzr�r>r>r>r?�<module>sh
#�	�	�	�	��/�����������	�����������
���������������
����
����	��2


�
Q=

© 2025 Cubjrnet7