HEX
Server: Apache/2.4.65 (Debian)
System: Linux kubikelcreative 5.10.0-35-amd64 #1 SMP Debian 5.10.237-1 (2025-05-19) x86_64
User: www-data (33)
PHP: 8.4.13
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/cc_ntp.cpython-39.pyc
a

��`]V�@s�dZddlZddlZddlmZddlmZddlmZddlm	Z	ddlm
Z
ddlmZdd	lmZdd
l
mZmZddlmZe�e�ZeZdZd
Zgd�Zdddgdddd�dedgdddd�dedgdddd�ddgdddd�d�Zddd�dgdd�d �dd!diid"did"did#�d"diddd�d$d%id&�d"diddd�d$d%id&�dd!diid'�Zd(d)d*ed+�eed,�ed-�ged.dd.d/gd0d1d2d3�d4ed5�d6�d0d1d2d3�d4ed7�d6�d1d8ed9�d:�d;d4ed<�d:�ed=�d.gd1ed>�d?�d1ed@�d?�d0dAd1id4edB�d6�d1edC�d?�d1edD�d?�dE�gdFdGdH�dI�gdGdJ�idK�	ZegdL��Zee�ZdMdN�ZdOdP�Zd_dQdR�Z d`dSdT�Z!dUdV�Z"dadWdX�Z#dbdYdZ�Z$d[d\�Z%d]d^�Z&dS)czNTP: enable and configure ntp�N)�dedent)�log)�
temp_utils)�	templater)�
type_utils��subp)�util)�get_schema_doc�validate_cloudconfig_schema)�PER_INSTANCEz
/etc/ntp.conf�)�alpine�centos�debian�fedora�opensuse�rhel�sles�ubuntuZchronydz/etc/chrony.conf�chronyzchrony.conf.{distro})�	check_exe�confpath�packages�service_name�
template_name�template�ntpd�ntpzntp.conf.{distro}�ntpdatez/lib/systemd/systemd-timesyncdz-/etc/systemd/timesyncd.conf.d/cloud-init.conf�systemd-timesyncdztimesyncd.conf)rrrr z/etc/chrony/chrony.conf)rr)rrr)rrrr)rrrz"/usr/lib/systemd/systemd-timesyncd)rrr )rrrrrrZcc_ntpZNTPzenable and configure ntpaD        Handle ntp configuration. If ntp is not installed on the system and
        ntp configuration is specified, ntp will be installed. If there is a
        default ntp config file in the image or one is present in the
        distro's ntp package, it will be copied to a file with ``.dist``
        appended to the filename before any changes are made. A list of ntp
        pools and ntp servers can be provided under the ``ntp`` config key.
        If no ntp ``servers`` or ``pools`` are provided, 4 pools will be used
        in the format ``{0-3}.{distro}.pool.ntp.org``.z�        # Override ntp with chrony configuration on Ubuntu
        ntp:
          enabled: true
          ntp_client: chrony  # Uses cloud-init default chrony configuration
        a�        # Provide a custom ntp client configuration
        ntp:
          enabled: true
          ntp_client: myntpclient
          config:
             confpath: /etc/myntpclient/myntpclient.conf
             check_exe: myntpclientd
             packages:
               - myntpclient
             service_name: myntpclient
             template: |
                 ## template:jinja
                 # My NTP Client config
                 {% if pools -%}# pools{% endif %}
                 {% for pool in pools -%}
                 pool {{pool}} iburst
                 {% endfor %}
                 {%- if servers %}# servers
                 {% endif %}
                 {% for server in servers -%}
                 server {{server}} iburst
                 {% endfor %}
          pools: [0.int.pool.ntp.org, 1.int.pool.ntp.org, ntp.myorg.org]
          servers:
            - ntp.server.local
            - ntp.ubuntu.com
            - 192.168.23.2�object�null�array�string�hostname)�type�formatTa�                        List of ntp pools. If both pools and servers are
                        empty, 4 default pool servers will be provided of
                        the format ``{0-3}.{distro}.pool.ntp.org``. NOTE:
                        for Alpine Linux when using the Busybox NTP client
                        this setting will be ignored due to the limited
                        functionality of Busybox's ntpd.)r&�items�uniqueItems�descriptionz�                        List of ntp servers. If both pools and servers are
                        empty, 4 default pool servers will be provided with
                        the format ``{0-3}.{distro}.pool.ntp.org``.�autoa�                        Name of an NTP client to use to configure system NTP.
                        When unprovided or 'auto' the default client preferred
                        by the distribution will be used. The following
                        built-in client names can be used to override existing
                        configuration defaults: chrony, ntp, ntpdate,
                        systemd-timesyncd.)r&�defaultr*�booleanz�                        Attempt to enable ntp clients if set to True.  If set
                        to False, ntp client will not be configured or
                        installedzu                        Configuration settings or overrides for the
                        ``ntp_client`` specified.z~                                The path to where the ``ntp_client``
                                configuration is written.)r&r*z�                                The executable name for the ``ntp_client``.
                                For example, ntp service ``check_exe`` is
                                'ntpd' because it runs the ntpd binary.r&z�                                List of packages needed to be installed for the
                                selected ``ntp_client``.z�                                The systemd or sysvinit service name used to
                                start and stop the ``ntp_client``
                                service.aM                                Inline template allowing users to define their
                                own ``ntp_client`` configuration template.
                                The value must start with '## template:jinja'
                                to enable use of templating support.
                                )rrrrr�F)r*r&�
properties�requiredZ
minProperties�additionalProperties)�pools�servers�
ntp_client�enabled�config)r&r/r0r1)	�id�name�titler*�distros�examples�	frequencyr&r/)rrrrcCs0t}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)�DISTRO_CLIENT_CONFIG�copy�NTP_CLIENT_CONFIGr	�
mergemanydict)�distroZdcfg�cfg�rE�9/usr/lib/python3/dist-packages/cloudinit/config/cc_ntp.py�distro_ntp_client_configs's

rGcCs�t|j�}|r.|dkr.t�d|�|�|i�S|�dd�}i}|dkr�|jD]2}|�|�}t�|�d��rLt�d|�|}q�qL|s�|jd}t�d|�|�|�}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.
    r+z4Selected NTP client "%s" via user-data configurationr4rz+Selected NTP client "%s", already installedrz<Selected distro preferred NTP client "%s", not yet installedz1Selected NTP client "%s" via distro system config)	rGr8�LOG�debug�get�
get_option�preferred_ntp_clientsr�which)r4rCZ
distro_cfgZdistro_ntp_clientZ	clientcfg�clientrDrErErF�select_ntp_client5s:

�

�
��rOcCs(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)rrM)Zinstall_funcrrrErErF�install_ntp_clientbs
	
rPcCs 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�rrErErF�rename_ntp_confssrVcCsbg}|}|dkrd}n|dkr"d}tdt�D]0}|�d�dd�t|�g|gd	gD���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.
    rrr�r�.cSsg|]}|r|�qSrErE)�.0�nrErErF�
<listcomp>��z)generate_server_names.<locals>.<listcomp>zpool.ntp.org)�range�NR_POOL_SERVERS�append�join�str)rC�namesZpool_distro�xrErErF�generate_server_names}s�rdc	Cs�|sg}|sg}t|�dkrH|dkrH|dkrHt|�}t�dd�|��n2t|�dkrzt|�dkrzt|�}t�dd�|��|s�td��|s�|s�td��||d	�}|r�tjd
dd�}|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 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.
    rrrz%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)r3r2ztemplate_name-z.tmpl)�prefix�suffixr.)�contentN)
�lenrdrHrIr`�
ValueErrorr�mkstempr	�
write_filer�render_to_file�del_file)	�distro_namerr3r2rR�template_fnr�paramsZtfilerErErF�write_ntp_config_template�s8�
�
�
rrcCs,|rdd|g}n
d|dg}tj|dd�dS)a%Restart or reload an ntp system service.

    @param service: A string specifying the name of the service to be affected.
    @param systemd: A boolean indicating if the distro uses systemd, defaults
    to False.
    @returns: A tuple of stdout, stderr results from executing the action.
    �	systemctlzreload-or-restart�service�restartT)�captureNr)rt�systemd�cmdrErErF�
reload_ntp�s
rycCsHg}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})�keysrrzJEither ntp:config:template or ntp:config:template_name values are requiredzntp:config:rz6Expected a config file path {keypath}. Found ({value}))�keypath�valuerzHExpected a list of required package names for {keypath}. Found ({value}))rrNz5Expected a string type for {keypath}. Found ({value})z$Invalid ntp configuration:\n{errors}�
)�errors)�REQUIRED_NTP_CONFIG_KEYS�
difference�setrzr`�sortedr_r'�anyrJr(�all�
isinstancera�listrj)Z
ntp_configr~�missingrz�keyr|r{rErErF�supplemental_schema_validation�s`

�
����
��
��
���r�c
Cs�d|vrt�d|�dS|d}|dur,i}t|t�sLtdjt�|�d���t|t	�|�
dd�}t�|�r|t�d|�dSt
|�
d	�|j�}tj||�
d
i�gdd�}t|�t|�
d�d
�d}|�
d��s|�
d��d|jj�}	|�|	�}|�sd|�
d�}
t|
��t|jj|�
d�|�
dg�|�
dg�|�
d�||�
d�d�t|jj|d|dd�zt|d|j��d�Wn6tj�y�}zt�d|��WYd}~n
d}~00dS)zEnable and configure ntp.rz8Skipping module named %s, not present or disabled by cfgNzL'ntp' key existed in config, but not a dictionary type, is a {_type} instead)�_typer5Tz)Skipping module named %s, disabled by cfgr4r6r=rrUrrz{distro}z#No template found, not rendering %srr3r2)rr3r2rRrprrr)rr)rwz&Failed to reload/start ntp service: %s)rHrIr��dict�RuntimeErrorr'r�obj_namer�schemarJr	�is_falserOrCrBr�rV�replacer8�get_template_filenamerrrP�install_packagesry�uses_systemdr�ProcessExecutionError�	exception)r8rD�cloudr�_argsZntp_cfgr5Zntp_client_configrpr�msg�erErErF�handlesx�
��


����
��

�	��
r�)Nr)N)NNNNNN)F)'�__doc__r@rQ�textwrapr�	cloudinitr�loggingrrrrr	�cloudinit.config.schemar
r�cloudinit.settingsr�	getLogger�__name__rHr<ZNTP_CONFr^r:rAr?r��	frozensetrrGrOrPrVrdrrryr�r�rErErErF�<module>sF
�	�	�	��'��������
��������
���@	�#����
��	�������+��d��� -


�
7
2