Your IP : 216.73.217.13


Current Path : /lib/python3/dist-packages/cloudinit/config/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/cloudinit/config/__pycache__/cc_ntp.cpython-38.pyc

U

�Ad�N�@sUdZddlZddlZddlmZddlmZddlmZddlm	Z	m
Z
mZmZm
Z
ddlmZddlmZdd	lmZmZdd
lmZe�e�ZeZdZdZd
ddddddddddddddddddd d!d"d#d$d%d&d'd(gZd)d*d+gd+d,dd-�d.ed/gd/d0dd-�d1ed1gd1d0dd-�d.d2gd.d3dd-�d4d5gd6d7dd-�d8�Zd9d)d:�dgd.d;�d<�d=d.id=d)id>�d+d)d9d?�id+d@d9iidd.d0dA�dBd+gd)d,dC�dDdEdFgdFdGdH�dI�d=d)idJdKdL�dM�dFiid=d)idd.d:�dNd4idO�d=d)idd.d:�dNdJidO�d=d)id.dd?�dJdKdL�dO�d=d.id=d)id>�d=d)idd.d:�dNdJidO�d+d@d9iidP�
ZdQD]Z edee <�q>dRD]Z ed%ee <�qVdSdTdUedV�eedW�edX�ged/gdY�Z!ee"dZ<ee!�Ze#dNd@d[d=g�Z$d\d]�Z%d^d_�Z&dmd`da�Z'dndbdc�Z(ddde�Z)dodfdg�Z*dhdi�Z+e,eeee-ddj�dkdl�Z.dS)pzNTP: 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�Z	almalinux�alpine�centosZ
cloudlinux�cos�debian�	eurolinuxZfedora�freebsd�marinerZmiraclelinux�openbsdZ	openEulerZOpenCloudOS�openmandriva�opensuse�opensuse-microos�opensuse-tumbleweed�
opensuse-leap�photon�rhelZrocky�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,�openntpdr-z/etc/chrony/chrony.conf)r%r')r%r&r')r#r+r')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.confr.zntpd.conf.openbsd)r$r%r&r'r()r+r#r.z"/usr/lib/systemd/systemd-timesyncdz/etc/systemd/timesyncd.conf)r$r%)r#r-r$)r#r+r-)
rrrrrrrrrrrr!r")rrr)rr Zcc_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)�id�name�titleZdescription�distrosZexamples�	frequencyZactivate_by_schema_keys�metar&cCs0t}t�t�}||kr,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�r=�9/usr/lib/python3/dist-packages/cloudinit/config/cc_ntp.py�distro_ntp_client_configs0s

r?cCs�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.
    �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?r0�LOG�debug�getZ
get_optionZpreferred_ntp_clientsr�which)rAr;Z
distro_cfgZdistro_ntp_clientZ	clientcfgZclientr<r=r=r>�select_ntp_client>s@

�

�
��rFcCs(t�|�rdS|dkrdg}||�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+)rrE)Zinstall_funcr&r$r=r=r>�install_ntp_clientps
	
rGcCs 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rMcCsjg}|}|dkrd}n|dks&|dkr*d}tdt�D]0}|�d�dd	�t|�g|gd
gD���q4|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|�qSr=r=)�.0�nr=r=r>�
<listcomp>�sz)generate_server_names.<locals>.<listcomp>zpool.ntp.org)�range�NR_POOL_SERVERS�append�join�str)r;�namesZpool_distro�xr=r=r>�generate_server_names�s��rZc	Cs|sg}|sg}t|�dkr4t|�dkr4|dkr4dSt|�dkrl|dkrl|dkrlt|�}t�dd�|��n2t|�dkr�t|�dkr�t|�}t�dd�|��|s�td	��|s�|s�td
��||d�}|r�tjd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 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�poolsztemplate_name-z.tmpl)�prefix�suffix�)Zcontent)
�lenrZrBrCrV�
ValueErrorrZmkstempr	Z
write_filerZrender_to_fileZdel_file)	Zdistro_namer'r\r]rI�template_fnr)ZparamsZtfiler=r=r>�write_ntp_config_template�s@ 
����
rdcCsHg}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
k�r|dkr�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�setrerV�sortedrU�format�anyrD�items�all�
isinstancerW�listrb)Z
ntp_configriZmissingre�keyrgrfr=r=r>�supplemental_schema_validation�sl

�����
��
��
����ru)r0r<�cloudr�args�returnc
Cszd|krt�d|�dS|d}|dkr,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|jj|�d�|�dg�|�dg�|�d�||�d�d�t|jj|d|dd�t	���r(|�d�dk�r�z|j�dd�Wn"tjk
�r�t�d�YnXz|j�dd�Wn"tjk
�r�t�d�YnXz|j�d|�d��Wn4tjk
�r&}zt�d|��W5d}~XYnXz|j�d |�d��Wn4tjk
�rt}zt�d!|��W5d}~XYnXdS)"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 cfgrAZconfigr5r%rLr)r(z{distro}z#No template found, not rendering %sr'r\r])r'r\r]rIrcr)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)rBrCrr�dict�RuntimeErrorrnrZobj_namerDr	Zis_falserFr;r:rurM�replacer0Zget_template_filenamerdrGZinstall_packagesZis_BSDZmanage_servicerZProcessExecutionErrorZwarningZ	exception)r0r<rvrrwZntp_cfgryZntp_client_configrcr(�msg�er=r=r>�handle's��
��
��

�
��

�
�
��r�)Nr*)N)NNNNNN)/�__doc__r8rHZloggingr�textwraprZ	cloudinitrrrrrr	Zcloudinit.cloudr
Zcloudinit.configrZcloudinit.config.schemarr
Zcloudinit.settingsrZ	getLogger�__name__rBr3ZNTP_CONFrTr2r9r7r;r4�__annotations__�	frozensetrjr?rFrGrMrZrdrurWrsr�r=r=r=r>�<module>s�
�!�	�	�	�	��/���
���	�����������
���������������
����
���|	����(�;
�2


�
B>�