
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
cj]S                 @   s  U d Z ddlZddlZddlZddl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mZ ddlmZ ejeZeZd	Zd
Zdddddddddddddddddddddd d!d"d#d$d%d&g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,d0g d,d1dd+d2d3g d4d5dd+d6Zd7d'd8d	g d,d9d:d;d,id;d'id<d)d'd7d=id)d>d7iid	d,d.d?d@d)gd'd*dAdBdCdDgdDdEdFdGd;d'idHdIdJdKdDi id;d'id	d,d8dLd2idMd;d'id	d,d8dLdHidMd;d'id,d	d=dHdIdJdMd;d,id;d'id<d;d'id	d,d8dLdHidMd)d>d7iidNZxdhD ]Zed ee< q0W xdiD ]Zed# ee< qLW xdjD ]Zed ee< qhW dOdPdQedReedSedTged-gdUZeeeZ e dLd>dVd;gZ!dWdX Z"dYdZ Z#dkd[d\Z$dld]d^Z%d_d` Z&dmdadbZ'dcdd Z(e)eee*ddedfdgZ+dS )nzNTP: enable and configure ntp    N)dedent)subp
temp_utils	templater
type_utilsutil)Cloud)Config)
MetaSchemaget_meta_doc)PER_INSTANCEz/etc/ntp.conf   	almalinuxalpinecentos
cloudlinuxcosdebian	eurolinuxZfedorafreebsdmarinerZmiraclelinuxopenbsdZ	openeulerZOpenCloudOSopenmandrivaopensuseopensuse-microosopensuse-tumbleweedopensuse-leapphotonrhelrockysle_hpc	sle-microslesZ	TencentOSubuntuZ	virtuozzoZ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.confzsystemd-timesyncdztimesyncd.conf)r$   r,   r-   openntpdzsystemd-timesyncdz/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.   zntpd.conf.openbsd)r%   r&   r'   r(   r)   )r,   r$   r.   z"/usr/lib/systemd/systemd-timesyncdz/etc/systemd/timesyncd.conf)r%   r&   )r$   zsystemd-timesyncdr%   )r$   r,   zsystemd-timesyncd)r   r   r   r   r   r   r   r   r   r   r   r"   r#   Zcc_ntpZNTPzenable 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
        aY          # 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 %}
                 {% if peers -%}# peers{% endif %}
                 {% for peer in peers -%}
                 peer {{peer}}
                 {% endfor %}
                 {% if allow -%}# allow{% endif %}
                 {% for cidr in allow -%}
                 allow {{cidr}}
                 {% 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
          allow:
            - 192.168.23.0/32
          peers:
            - km001
            - km002)idnametitledescriptiondistrosZexamples	frequencyZactivate_by_schema_keysr'   c             C   s0   t }tjt}| |kr,tj|||  gdd}|S )zConstruct 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)distroZdcfgcfg r<   /usr/lib/python3.6/cc_ntp.pydistro_ntp_client_configs?  s
    
r>   c             C   s   t |j}| r.| dkr.tjd|  |j| i S |jdd}i }|dkrx:|jD ]0}|j|}tj|jdrNtjd| |}P qNW |s|jd }tjd| |j|}ntjd| |j|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)	r>   r0   LOGdebuggetZ
get_optionZpreferred_ntp_clientsr   which)r@   r:   Z
distro_cfgZdistro_ntp_clientZ	clientcfgZclientr;   r<   r<   r=   select_ntp_clientM  s4    



rE   c             C   s(   t j|rdS |dk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.
    Nr,   )r   rD   )Zinstall_funcr'   r%   r<   r<   r=   install_ntp_client  s
    	
rF   c             C   s    t jj| rtj| | d  dS )zRename 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    rK   c             C   sn   g }| }| dkrd}n| dks&| dkr*d}x>t dtD ]0}|jdjdd	 t|g|g d
g D  q6W |S )zGenerate 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    r   .c             S   s   g | ]}|r|qS r<   r<   ).0nr<   r<   r=   
<listcomp>  s    z)generate_server_names.<locals>.<listcomp>zpool.ntp.org)rangeNR_POOL_SERVERSappendjoinstr)r:   namesZpool_distroxr<   r<   r=   generate_server_names  s    (rX   c	             C   s(  |sg }|sg }|sg }|s g }t |dkrDt |dkrD| dkrDdS t |dkr|| dkr||dkr|t| }tjddj| n2t |dkrt |dkrt| }tjddj| |std	| r| rtd
||||d}	|rtjddd}
|
d }tj	||d t
j|||	 |r$tj| 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   r   Nr   r+   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   )Zcontent)lenrX   rA   rB   rT   
ValueErrorr   Zmkstempr   Z
write_filer   Zrender_to_fileZdel_file)Zdistro_namer(   rZ   r[   r\   r]   rH   template_fnr*   ZparamsZtfiler<   r<   r=   write_ntp_config_template  sD      rd   c             C   sL  g }t jt| j }|r<djt|}|jdj|d n"t| j	d| j	dgs^|jd xt| j
 D ]\}}d| }|dkrt|t|tgs|jd	j||d
 ql|dkrt|ts|jdj||d
 ql|dkr
|dkrqlt|ts(|jdj||d
 qlt|tsl|jdj||d
 qlW |rHtdjdj|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})keysr*   r)   zJEither ntp:config:template or ntp:config:template_name values are requiredzntp:config:r&   z6Expected a config file path {keypath}. Found ({value}))keypathvaluer'   zHExpected a list of required package names for {keypath}. Found ({value})Nz5Expected a string type for {keypath}. Found ({value})z$Invalid ntp configuration:\n{errors}
)errors)r*   r)   )REQUIRED_NTP_CONFIG_KEYS
differencesetre   rT   sortedrS   formatanyrC   itemsall
isinstancerU   listrb   )Z
ntp_configri   Zmissingre   keyrg   rf   r<   r<   r=   supplemental_schema_validation  sJ    



ru   )r0   r;   cloudargsreturnc          .   C   s  d|krt jd|  dS |d }|dkr,i }t|tsLtdjtj|d|jdd}t	j
|rrt jd|  dS t|jd	|j}t	j||jd
i gdd}t| t|jdd d}|jds|jdjd|jj}|j|}|sd|jd }	t|	t jd|jd t jd|jdg  t jd|jdg  t jd|jdg  t jd|jdg  t|jj|jd|jdg |jdg |jdg |jdg |jd||jdd	 t|jj|d |d d t	j r|jdd krTy|jjd!d  W n" tjk
r   t jd" Y nX y|jjd#d  W n" tjk
rR   t jd$ Y nX y|jjd%|jd W n4 tjk
r }
 zt jd&|
  W Y dd}
~
X nX y|jjd'|jd W n4 tjk
r }
 zt jd(|
  W Y dd}
~
X nX dS ))zEnable 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 cfgr@   config)r5   r&   )r&   r*   r)   z{distro}z#No template found, not rendering %szservice_name: %sr(   zservers: %srZ   z	pools: %sr[   z	allow: %sr\   z	peers: %sr]   )r(   rZ   r[   r\   r]   rH   rc   r*   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)rA   rB   rr   dictRuntimeErrorrn   r   Zobj_namerC   r   Zis_falserE   r:   r9   ru   rK   replacer0   Zget_template_filenamerd   rF   Zinstall_packagesZis_BSDZmanage_servicer   ZProcessExecutionErrorZwarningZ	exception)r0   r;   rv   rw   Zntp_cfgry   Zntp_client_configrc   r)   msger<   r<   r=   handleE  s    








r   )r   r   r   )r    r!   )r   r   r   )Nr+   )N)NNNNNNNN),__doc__r7   ZloggingrG   textwrapr   Z	cloudinitr   r   r   r   r   Zcloudinit.cloudr   Zcloudinit.configr	   Zcloudinit.config.schemar
   r   Zcloudinit.settingsr   Z	getLogger__name__rA   r4   ZNTP_CONFrR   r3   r8   r6   r:   meta	frozensetrj   r>   rE   rF   rK   rX   rd   ru   rU   rs   r   r<   r<   r<   r=   <module>   s6  












		)2


       
H=