HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/cc_ntp.cpython-310.pyc
o

�Ad�N�@s�UdZddlZddlZddlmZddlmZddlmZddlm	Z	m
Z
mZmZm
Z
ddlmZddlmZdd	lmZmZdd
lmZe�e�ZeZdZdZgd
�Zdddgdddd�dedgdddd�dedgdddd�ddgdddd�ddgdddd�d�Zddd�dgdd �d!�d"did"did#�dddd$�idd%diidddd&�d'dgddd(�d)d*d+gd+d,d-�d.�d"did/d0d1�d2�d+iid"diddd�d3did4�d"diddd�d3d/id4�d"diddd$�d/d0d1�d4�d"did"did#�d"diddd�d3d/id4�dd%diid5�
Zd6D]	Z ed7ee <�qd8D]	Z ed9ee <�qd:d;d<ed=�eed>�ed?�gedgd@�Z!ee"dA<ee!�Ze#gdB��Z$dCdD�Z%dEdF�Z&dYdGdH�Z'dZdIdJ�Z(dKdL�Z)						d[dMdN�Z*dOdP�Z+dQe,dRedSedTedUe-dVdfdWdX�Z.dS)\zNTP: enable and configure ntp�N)�Logger)�dedent)�log)�subp�
temp_utils�	templater�
type_utils�util)�Cloud)�Config)�
MetaSchema�get_meta_doc)�PER_INSTANCEz
/etc/ntp.conf�)�	almalinux�alpine�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�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-r5r6�openntpdr7z/etc/chrony/chrony.conf)r/r1)r/r0r1)r-r5r1)r5r-)r1r/r/)r/r1r2z/usr/local/etc/chrony.conf)r/r0r1r2z/usr/local/sbin/ntpdz/usr/local/etc/ntp.confr8zntpd.conf.openbsd)r.r/r0r1r2)r5r-r8z"/usr/lib/systemd/systemd-timesyncdz/etc/systemd/timesyncd.conf)r.r/)r-r7r.)r-r5r7)
rrrrrrrrrr#r$r(r*)r r!r"r)r&r'r(�cc_ntp�NTPzenable 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)�id�name�title�description�distros�examples�	frequency�activate_by_schema_keys�meta)r.r/r0r1cCs0t}t�t�}||vrtj|||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)�distro�dcfg�cfg�rM�9/usr/lib/python3/dist-packages/cloudinit/config/cc_ntp.py�distro_ntp_client_configs0s

rOcCs�t|j�}|r|dkrt�d|�|�|i�S|�dd�}i}|dkrT|jD]}|�|�}t�|�d��r?t�d|�|}nq&|sR|jd}t�d|�|�|�}|St�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)	rOr<�LOG�debug�get�
get_option�preferred_ntp_clientsr�which)rQrJ�
distro_cfg�distro_ntp_client�	clientcfg�clientrLrMrMrN�select_ntp_client>s@

�

��
�
��r\cCs(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.
    Nr5)rrW)�install_funcr0r.rMrMrN�install_ntp_clientps

	r^cCs$tj�|�rt�||d�dSdS)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/rMrMrN�rename_ntp_conf�s�rdcCsjg}|}|dkrd}n
|dks|dkrd}tdt�D]}|�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.
    r(rrr�r�.cSsg|]}|r|�qSrMrM)�.0�nrMrMrN�
<listcomp>�sz)generate_server_names.<locals>.<listcomp>zpool.ntp.org)�range�NR_POOL_SERVERS�append�join�str)rJ�names�pool_distro�xrMrMrN�generate_server_names�s��rrc	Cs|sg}|sg}t|�dkrt|�dkr|dkrdSt|�dkr6|dkr6|dkr6t|�}t�dd�|��nt|�dkrOt|�dkrOt|�}t�dd�|��|sUtd	��|s]|s]td
��||d�}|rvtjdd
d�}|d}tj	||d�t
�|||�|r�t�|�dSdS)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.
    rrNrr4z%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�poolsztemplate_name-z.tmpl)�prefix�suffix�)�content)
�lenrrrRrSrm�
ValueErrorr�mkstempr	�
write_filer�render_to_file�del_file)	�distro_namer1rtrur`�template_fnr3�params�tfilerMrMrN�write_ntp_config_template�s@ 
���
�
�r�cCsBg}t�t|����}|rd�t|��}|�dj|d��nt|�	d�|�	d�g�s/|�d�t|�
��D]\\}}d|}|dkrUt|t|t
�g�sT|�d	j||d
��q5|dkrit|t�sh|�dj||d
��q5|d
vr�|durrq5t|t
�s�|�dj||d
��q5t|t
�s�|�dj||d
��q5|r�tdjd�|�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})�keysr3r2zJEither ntp:config:template or ntp:config:template_name values are requiredzntp:config:r/z6Expected a config file path {keypath}. Found ({value}))�keypath�valuer0zHExpected a list of required package names for {keypath}. Found ({value}))r3r2Nz5Expected a string type for {keypath}. Found ({value})z$Invalid ntp configuration:\n{errors}�
)�errors)�REQUIRED_NTP_CONFIG_KEYS�
difference�setr�rm�sortedrl�format�anyrT�items�all�
isinstancern�listr{)�
ntp_configr��missingr��keyr�r�rMrMrN�supplemental_schema_validation�sn

������
���
���
������r�r<rL�cloudr�args�returnc
CsZd|vrt�d|�dS|d}|duri}t|t�s&tdjt�|�d���|�dd�}t	�
|�r9t�d|�dSt|�d	�|j�}t	j
||�d
i�gdd�}t|�t|�d�d
�d}|�d�s|�d��d|jj�}	|�|	�}|sd|�d�}
t|
��t|jj|�d�|�dg�|�dg�|�d�||�d�d�t|jj|d|dd�t	���r
|�d�dkr�z	|j�dd�Wntjy�t�d�Ynwz	|j�dd�Wntjy�t�d�Ynwz|j�d|�d��Wntj�y	}zt�d|��d}~wwz
|j�d |�d��WdStj�y,}zt�d!|��d}~ww)"zEnable and configure ntp.r5z8Skipping 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�configrDr/rcr3r2z{distro}z#No template found, not rendering %sr1rtru)r1rtrur`r�r3r0r.)r0r.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)rRrSr��dict�RuntimeErrorr�r�obj_namerTr	�is_falser\rJrIr�rd�replacer<�get_template_filenamer�r^�install_packages�is_BSD�manage_servicer�ProcessExecutionError�warning�	exception)r<rLr�rr��ntp_cfgr��ntp_client_configr�r2�msg�erMrMrN�handle's��

��
��

�
��

�
�
��
���

���r�)Nr4)N)NNNNNN)/�__doc__rGr_�loggingr�textwrapr�	cloudinitrrrrrr	�cloudinit.cloudr
�cloudinit.configr�cloudinit.config.schemarr
�cloudinit.settingsr�	getLogger�__name__rRrA�NTP_CONFrkr?rHrFrJrC�__annotations__�	frozensetr�rOr\r^rdrrr�r�rnr�r�rMrMrMrN�<module>s|
!�	�	�	�	��/���
���	�����������
���������������
����
���|	����(�;�
2


�B=������