3 cj]S@sUdZddlZddlZddlZddlmZddlmZmZm Z m Z m Z ddl m Z ddlmZddlmZmZddlmZejeZeZd Zd Zd d d ddddddddddddddddddd d!d"d#d$d%d&gZd'd(d)gd)d*dd+d,ed-gd-d.dd+d/ed/gd/d.dd+d,d0gd,d1dd+d2d3gd4d5dd+d6Zd7d'd8d gd,d9d:d;d,id;d'id<d)d'd7d=id)d>d7iid d,d.d?d@d)gd'd*dAdBdCdDgdDdEdFdGd;d'idHdIdJdKdDiid;d'id d,d8dLd2idMd;d'id d,d8dLdHidMd;d'id,d d=dHdIdJdMd;d,id;d'id<d;d'id d,d8dLdHidMd)d>d7iidN ZxdhD]Zedee<q0WxdiD]Zed#ee<qLWxdjD]Zedee<qhWdOdPdQedReedSedTged-gdUZeeeZe dLd>dVd;gZ!dWdXZ"dYdZZ#dkd[d\Z$dld]d^Z%d_d`Z&dmdadbZ'dcddZ(e)ee e*ddedfdgZ+dS)nzNTP: enable and configure ntpN)dedent)subp temp_utils templater type_utilsutil)Cloud)Config) MetaSchema get_meta_doc) PER_INSTANCEz /etc/ntp.conf almalinuxalpinecentos cloudlinuxcosdebian eurolinuxZfedorafreebsdmarinerZ miraclelinuxopenbsdZ openeulerZ OpenCloudOS openmandrivaopensuseopensuse-microosopensuse-tumbleweed opensuse-leapphotonrhelrockysle_hpc sle-microslesZ TencentOSubuntuZ virtuozzoZchronydz/etc/chrony.confchronyzchrony.conf.{distro}) check_execonfpathpackages service_name template_nametemplatentpdntpzntp.conf.{distro}ntpdatez/etc/ntpd.confzntpd.conf.{distro}z/lib/systemd/systemd-timesyncdz-/etc/systemd/timesyncd.conf.d/cloud-init.confzsystemd-timesyncdztimesyncd.conf)r$r,r-openntpdzsystemd-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.zntpd.conf.openbsd)r%r&r'r(r))r,r$r.z"/usr/lib/systemd/systemd-timesyncdz/etc/systemd/timesyncd.conf)r%r&)r$zsystemd-timesyncdr%)r$r,zsystemd-timesyncd) rrrrrrrrrrrr"r#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 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)idnametitle descriptiondistrosZexamples frequencyZactivate_by_schema_keysr'cCs0t}tjt}||kr,tj|||gdd}|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_CONFIGcopyNTP_CLIENT_CONFIGr mergemanydict)distroZdcfgcfgr</usr/lib/python3.6/cc_ntp.pydistro_ntp_client_configs?s  r>cCst|j}|r.|dkr.tjd||j|iS|jdd}i}|dkrx:|jD]0}|j|}tj|jdrNtjd||}PqNW|s|jd}tjd||j|}ntjd||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 installedrzr0LOGdebuggetZ get_optionZpreferred_ntp_clientsrwhich)r@r:Z distro_cfgZdistro_ntp_clientZ clientcfgZclientr;r<r<r=select_ntp_clientMs4         rEcCs(tj|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,)rrD)Z install_funcr'r%r<r<r=install_ntp_clients rFcCs tjj|rtj||ddS)zRename any existing ntp client config file @param confpath: string. Specify a path to an existing ntp client configuration file. z.distN)ospathexistsrrename)r&r<r<r=rename_ntp_confs rKcCsng}|}|dkrd}n|dks&|dkr*d}x>tdtD]0}|jdjdd t|g|gd gDq6W|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"rrrr.cSsg|] }|r|qSr<r<).0nr<r<r= sz)generate_server_names..z pool.ntp.org)rangeNR_POOL_SERVERSappendjoinstr)r:namesZ pool_distroxr<r<r=generate_server_namess(rXc Cs(|sg}|sg}|sg}|s g}t|dkrDt|dkrD|dkrDdSt|dkr||dkr||dkr|t|}tjddj|n2t|dkrt|dkrt|}tjddj||std | r| rtd ||||d } |rtjd d d} | d}tj ||dt j ||| |r$tj |dS)aRender 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)serverspoolsallowpeersztemplate_name-z.tmpl)prefixsuffix)Zcontent) lenrXrArBrT ValueErrorrZmkstemprZ write_filerZrender_to_fileZdel_file) Z distro_namer(rZr[r\r]rH template_fnr*ZparamsZtfiler<r<r=write_ntp_config_templatesD    rdcCsLg}tjt|j}|rrErFrKrXrdrurUrsrr<r<r<r=s6                    )2   H=