܍~cGM UdZddlZddlZddlmZddlmZddlmZddlm Z m Z m Z m Z m Z ddlmZddlmZdd lmZmZdd lmZejeZeZd Zd Zgd ZdddgdddddedgdddddedgddddddgddddddgdddddZdddd gdd d!d"did"did#dddd$idd%diid ddd&d'dgddd(d)d*d+gd+d,d-d.d"did/d0d1d2d+iid"did ddd3did4d"did ddd3d/id4d"didd d$d/d0d1d4d"did"did#d"did ddd3d/id4dd%diid5 Zd6d7d8ed9eed:ed;gedgd<Z ee!d=<ee Ze"gd>Z#d?Z$d@Z%dMdAZ&dNdBZ'dCZ( dOdDZ)dEZ*dFe+dGedHedIedJe,dKdf dLZ-dS)PzNTP: enable and configure ntpN)Logger)dedent)log)subp temp_utils templater type_utilsutil)Cloud)Config) MetaSchema get_meta_doc) PER_INSTANCEz /etc/ntp.conf) almalinuxalpinecentos cloudlinuxcosdebian eurolinuxfedorafreebsdmariner miraclelinuxopenbsd openEuler openmandrivaopensusephotonrhelrockyslesubuntu virtuozzochronydz/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/r0openntpdr1z/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.confr2zntpd.conf.openbsd)r(r)r*r+r,)r/r'r2z"/usr/lib/systemd/systemd-timesyncdz/etc/systemd/timesyncd.conf)r(r))r'r1r()r'r/r1) rrrrrrrrrr r!r#r$cc_ntpNTPzenable 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)idnametitle descriptiondistrosexamples frequencyactivate_by_schema_keysmeta)r(r)r*r+ct}tjt}||vrtj|||gd}|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)DISTRO_CLIENT_CONFIGcopyNTP_CLIENT_CONFIGr mergemanydict)distrodcfgcfgs 9/usr/lib/python3/dist-packages/cloudinit/config/cc_ntp.pydistro_ntp_client_configsrI$sE D )% & &C ~~ #tF|!4dCCC Jct|j}|r7|dkr1td|||iS|dd}i}|dkr|jD]]}||}tj|drtd||}n^|s=|jd}td|||}n1td|||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 z)7LrJcxtj|rtj||dzdSdS)zRename any existing ntp client config file @param confpath: string. Specify a path to an existing ntp client configuration file. z.distN)ospathexistsr renamer)s rHrename_ntp_confrbusA  w~~h2 Hh01111122rJc g}|}|dkrd}n|dks|dkrd}tdtD]J}|ddt |g|gzd gzDK|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.cg|]}||Srg).0ns rH z)generate_server_names..sMMMq1MMMMrJz pool.ntp.org)rangeNR_POOL_SERVERSappendjoinstr)rEnames pool_distroxs rHgenerate_server_namesrss EK ! 8  v44 1o & &   HHMMSVVH }47GGMMM      LrJc |sg}|sg}t|dkrt|dkr|dkrdSt|dkrJ|dkrD|dkr>t|}tdd|nct|dkrPt|dkr=t|}tdd||st d |s|st d ||d }|r4t jd d }|d}tj ||tj ||||rtj |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. 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)serverspoolsztemplate_name-z.tmpl)prefixsuffix)content) lenrsrNrOrn ValueErrorrmkstempr write_filerrender_to_filedel_file) distro_namer+rvrwr^ template_fnr-paramstfiles rHwrite_ntp_config_templaters4   7||qSZZ1__1E1E G  8 # # F " "( 44 9388G;L;LMMMM W  s5zzQ%k22 8#((5//    =;<<< AxA?@@@ 5 1 1F7"*:7KKKAh   X6666 [$777# k"""""##rJcg}tt|}|rLdt |}|d|nLt| d| dgs|dt | D],\}}d|z}|dkrOt|t|tgs*|d || `|d kr@t|ts*|d || |d vrC|t|ts*|d|| t|ts*|d|| .|r6tdd|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 requiredz ntp:config:r)z6Expected a config file path {keypath}. Found ({value}))keypathvaluer*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 differencesetrrnsortedrmformatanyrPitemsall isinstancerolistr}) ntp_configrmissingrkeyrrs rHsupplemental_schema_validationrsF&11#joo6G6G2H2HIIG  yy)) 6 = =4 = H H      # #Z^^O%D%DE       Z--//00 U#% *  z%55677  ''-vgUv'K'KJ  eT**  ''-vgUv'K'K1 1 1}eS))  ''-vgUv'K'KE3''  MMGNN#5O      3 : :yy(( ;       rJr6rGcloudrargsreturnc d|vrtd|dS|d}|i}t|ts5t dt j||dd}tj |rtd|dSt|d |j }tj ||d igd }t|t|d  d}|dsq|dd|j j} || }|s'd|dz} t | t'|j j|d|dg|dg|d ||dt)|j j|d|dtjr|ddkr |j ddn/#t0j$rtdYnwxYw |j ddn/#t0j$rtdYnwxYw |j d|dn3#t0j$r!} td| d} ~ wwxYw |j d |ddS#t0j$r!} td!| d} ~ wwxYw)"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)_typeenabledTz)Skipping module named %s, disabled by cfgrMconfigr?r)rar-r,z{distro}z#No template found, not rendering %sr+rvrw)r+rvrwr^rr-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)rNrOrdict RuntimeErrorrr obj_namerPr is_falserXrErDrrbreplacer6get_template_filenamerr[install_packagesis_BSDmanage_servicerProcessExecutionErrorwarning exception) r6rGrrrntp_cfgrntp_client_configrr,msges rHhandlers[ C F    %jG g & &  $$*F1DW1M1MF$N$N   kk)T**G }W =tDDD* L!!5<* GKK"556##4555.22:>>????K   , , $)--o>>FF  )  11-@@  $5#''889 s## # &**>:: Ir**kk'2&&  " ": . ."&&z22 %":.#K0  {}}   0 0F : : @ ++FF;;;;- @ @ @ >????? @ C ++Iv>>>>- C C C ABBBBB C  L ' '+//??    )    MMBBB sT$K)K,+K,0L )L87L8<.M++N:NN.OO?O::O?)Nr.)N)NNNNNN).__doc__rBr]loggingrtextwrapr cloudinitrrrrr r cloudinit.cloudr cloudinit.configr cloudinit.config.schemar rcloudinit.settingsr getLogger__name__rNr;NTP_CONFrlr9rCrAr=__annotations__ frozensetrrIrXr[rbrsrrrorrrgrJrHrsg$## $$$$$$CCCCCCCCCCCCCC!!!!!!######<<<<<<<<++++++g!!     4&J / G,   K!, $- 6C+) C))^2%  ("    F  I  %1     1 ("0  5! %3   01# &0   , I >5   B I ("  9    I ("  =    I !'ODD=5    F  I    I ("  =     1 kzzH   '6 :         'P %ws::j:::v ,t  %9999   ///d"2222>    ?#?#?#?#D: : : zZ ZZ#(Z/5Z=AZ ZZZZZZrJ