a 'DgK@sUdZddlZddlZddlZddlmZmZddlmZm Z m Z m Z m Z ddl mZddlmZddlmZddlmZeeZeZd Zd Zgd Zd d dgdddddedgdddddedgddddddgddddddgdddddZdd dd gddddd d!d"id#d id d!d"d$d#did#d id%dd dd&idd'diid ddd(d)dgd dd*d+d,d-gd-d.d/d0d#d id d!d"d$d-iid#d id ddd1did2d#d id ddd1d id2d#d idd d&d d!d"d2d#did#d id%d#d id ddd1d id2dd'diid3Zeeefed4<d5D]Z ed6ee <q(d7D]Z ed8ee <q@d9D]Z ed:ee <qXd;eedgd<Z!eed=<e"gd>Z#d?d@Z$edAdBdCZ%dQdDdEZ&dRdFdGZ'dHdIZ(dSdJdKZ)dLdMZ*eeee+ddNdOdPZ,dS)TzNTP: enable and configure ntpN)DictMapping)subp temp_utils templater type_utilsutil)Cloud)Config) MetaSchema) PER_INSTANCEz /etc/ntp.conf) almalinuxalpineaosc azurelinuxcentos 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.confsystemd-timesyncdztimesyncd.conf)r&r.r/openntpdr0z/etc/chrony/chrony.conf)r(r*)r(r)r*)r&r.z"/usr/lib/systemd/systemd-timesyncdz/etc/systemd/timesyncd.conf)r'r(r*)r&r0)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.confr1zntpd.conf.openbsd)r'r(r)r*r+)r.r&r1r')r&r.r0)rrrrrrrrrrrrr r$r%DISTRO_CLIENT_CONFIG)rrrr)rrr!r )r"r#r$Zcc_ntp)iddistros frequencyZactivate_by_schema_keysmeta)r'r(r)r*cCs0t}tt}||vr,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. Treverse)r2copyNTP_CLIENT_CONFIGr mergemanydict)distroZdcfgcfgr>;/usr/lib/python3.9/site-packages/cloudinit/config/cc_ntp.pydistro_ntp_client_configs s  r@)returncCst|j}|r.|dkr.td|||iS|dd}i}|dkr|jD]2}||}t|drLtd||}qqL|s|jd}td|||i}ntd|||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 installedrzr>r?select_ntp_clients<        rIcCs(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.)rrH)Z install_funcr)r'r>r>r?install_ntp_clientMs rJcCs tj|rt||ddS)zRename any existing ntp client config file @param confpath: string. Specify a path to an existing ntp client configuration file. z.distN)ospathexistsrrenamer(r>r>r?rename_ntp_conf^s rPcCshg}|}|dkrd}n|dks&|dkr*d}ttD]0}|dddt|g|gd gDq2|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$rrr.cSsg|] }|r|qSr>r>).0nr>r>r? ~z)generate_server_names..z pool.ntp.org)rangeNR_POOL_SERVERSappendjoinstr)r<namesZ pool_distroxr>r>r?generate_server_nameshs r^c Cs$|sg}|sg}|sg}|s g}t|dkrDt|dkrD|dkrDdSt|dkr||dkr||dkr|t|}tdd|n2t|dkrt|dkrt|}tdd||std |s|std ||||d } |rtjd d d} | d}tj ||dt ||| |r t |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) lenr^rErFrZ ValueErrorrZmkstemprZ write_filerZrender_to_fileZdel_file) Z distro_namer*r`rarbrcrL template_fnr,paramsZtfiler>r>r?write_ntp_config_templatesN    rkcCsHg}tt|}|rr>r?supplemental_schema_validationsd      r})rDr=cloudargsrAc Csd|vrtd|dS|d}|dur,i}t|tsLtdjt|d|dd}t |rrtd|dSt |d |j }t j ||d igdd }t|t|d d d}|ds|dd|j j}||}|sd|d} t| td|dtd|dgtd|dgtd|dgtd|dgt|j j|d|dg|dg|dg|dg|d ||dd t|j j|d|ddt r|dd krJz|j d!d Wn tjytd"Yn0z|j d#d Wn tjyHtd$Yn0z|j d%|dWn6tjy} ztd&| WYd} ~ n d} ~ 00z|j d'|dWn6tjy} ztd(| WYd} ~ n d} ~ 00dS))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_typeenabledTz)Skipping module named %s, disabled by cfgrCZconfigr7r(rOr,r+z{distro}z#No template found, not rendering %szservice_name: %sr*z servers: %sr`z pools: %sraz allow: %srbz peers: %src)r*r`rarbrcrLrir,r)r')r)r'r-stopz Failed to stop base ntpd servicedisablez#Failed to disable base ntpd serviceenablez Failed to enable ntp service: %sreloadz&Failed to reload/start ntp service: %s)rErFrydict RuntimeErrorrurZobj_namerGrZis_falserIr<r;r}rPreplacerDZget_template_filenamerkrJZinstall_packagesZis_BSDZmanage_servicerZProcessExecutionErrorZwarning exception) rDr=r~rZntp_cfgrZntp_client_configrir+msger>r>r?handles               r)Nr-)N)NNNNNNNN)-__doc__r9ZloggingrKtypingrrZ cloudinitrrrrrZcloudinit.cloudr Zcloudinit.configr Zcloudinit.config.schemar Zcloudinit.settingsr Z getLogger__name__rEr5ZNTP_CONFrXr4r:r2r[__annotations__r<r6 frozensetrqr@rIrJrPr^rkr}rzrr>r>r>r?sh     #    /         2   Q=