ó oBú]c @sdZddlmZmZddlmZddlmZddlm Z ddlm Z ddlm Z ddlm Z dd l Z dd lZdd lZdd lmZejeƒZeZd Zd Zd ddddddgZiidd6dd6dgd6dd6dd6d d6d6idd6ed6dgd6dd6d d6d d6d6id!d6ed6d!gd6d!d6d d6d d6d!6id"d6d#d6gd6d$d6d%d6d d6d$6Ziiid&d6d6d6iidd6d6id d6dd6d6id'd6d$6d6iidd6d6id d6dd6d6id'd6d$6d6iid&d6d6d6Zi d(d)6d*d+6d,d-6ed.ƒd/6ed06ed1ƒed2ƒgd36ed46d5d66iid5d7gd66iid8d66id9d66d:d;6d<6ed=6ed>ƒd/6d?6id8d66id9d66d:d;6d<6ed=6ed@ƒd/6dA6id9d66dBdC6edDƒd/6dE6idFd66edC6edGƒd/6dH6iedIƒd/6d5gd66iid9d66edJƒd/6d6id9d66edKƒd/6d6id8d66id9d66d<6ed=6edLƒd/6d6id9d66edMƒd/6d6id9d66edNƒd/6d6dO6gdP6dQdR6edS6dT6dO6gdP6edS6d6dO6ZeddddgƒZ eeƒZdU„Z!dV„Z"d ddW„Z#d dX„Z$dY„Z%d d d d d dZ„Z&ed[„Z'd\„Z(d]„Z)d S(^sNTP: enable and configure ntpiÿÿÿÿ(tget_schema_doctvalidate_cloudconfig_schema(tlog(t PER_INSTANCE(t temp_utils(t templater(t type_utils(tutilN(tdedents /etc/ntp.confitcentostdebiantfedoratopensusetrheltslestubuntutchronydt check_exes/etc/chrony.conftconfpathtchronytpackagest service_nameschrony.conf.{distro}t template_namettemplatetntpdtntpsntp.conf.{distro}tntpdates/lib/systemd/systemd-timesyncds-/etc/systemd/timesyncd.conf.d/cloud-init.confssystemd-timesyncdstimesyncd.confs/etc/chrony/chrony.confs"/usr/lib/systemd/systemd-timesyncdtcc_ntptidtNTPtnamesenable and configure ntpttitles, 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 ``/etc/ntp.conf.dist`` 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``.t descriptiontdistrossµ # Override ntp with chrony configuration on Ubuntu ntp: enabled: true ntp_client: chrony # Uses cloud-init default chrony configuration s¼ # 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.2texamplest frequencytobjectttypetnulltarraytstringthostnametformattitemst uniqueItemssØ List of ntp pools. If both pools and servers are empty, 4 default pool servers will be provided of the format ``{0-3}.{distro}.pool.ntp.org``.tpoolssÜ List of ntp servers. If both pools and servers are empty, 4 default pool servers will be provided with the format ``{0-3}.{distro}.pool.ntp.org``.tserverstautotdefaults© Name of an NTP client to use to configure system NTP. When unprovided or 'auto' the default client preferred by the distribution will be used. The following built-in client names can be used to override existing configuration defaults: chrony, ntp, ntpdate, systemd-timesyncd.t ntp_clienttbooleans¸ Attempt to enable ntp clients if set to True. If set to False, ntp client will not be configured or installedtenabledsv Configuration settings or overrides for the ``ntp_client`` specified.s The path to where the ``ntp_client`` configuration is written.sß The executable name for the ``ntp_client``. For example, ntp service ``check_exe`` is 'ntpd' because it runs the ntpd binary.s‰ List of packages needed to be installed for the selected ``ntp_client``.s¹ The systemd or sysvinit service name used to start and stop the ``ntp_client`` service.sP Inline template allowing users to define their own ``ntp_client`` configuration template. The value must start with '## template:jinja' to enable use of templating support. t propertiestrequiredit minPropertiestadditionalPropertiestconfigcCsGt}tjtƒ}||krCtj|||gdtƒ}n|S(sò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. treverse(tDISTRO_CLIENT_CONFIGtcopytNTP_CLIENT_CONFIGRt mergemanydicttTrue(tdistrotdcfgtcfg((s;/usr/lib/python2.7/site-packages/cloudinit/config/cc_ntp.pytdistro_ntp_client_configss  "cCst|jƒ}|rA|dkrAtjd|ƒ|j|iƒS|jddƒ}i}|dkròxR|jD]G}|j|ƒ}tj|jdƒƒrotjd|ƒ|}PqoqoW|s|jd}tjd|ƒ|j|ƒ}qn"tjd|ƒ|j|iƒ}|S( s 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. R/s4Selected NTP client "%s" via user-data configurationR1Rs+Selected NTP client "%s", already installedis<Selected distro preferred NTP client "%s", not yet installeds1Selected NTP client "%s" via distro system config( RBRtLOGtdebugtgett get_optiontpreferred_ntp_clientsRtwhich(R1R?t distro_cfgtdistro_ntp_clientt clientcfgtclientRA((s;/usr/lib/python2.7/site-packages/cloudinit/config/cc_ntp.pytselect_ntp_clients2      cCs9tj|ƒrdS|dkr+dg}n||ƒdS(shInstall 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(RRHtNone(t install_funcRR((s;/usr/lib/python2.7/site-packages/cloudinit/config/cc_ntp.pytinstall_ntp_clientLs   cCs-tjj|ƒr)tj||dƒndS(sŽRename any existing ntp client config file @param confpath: string. Specify a path to an existing ntp client configuration file. s.distN(tostpathtexistsRtrename(R((s;/usr/lib/python2.7/site-packages/cloudinit/config/cc_ntp.pytrename_ntp_conf]scCs\g}|}|dkr!d}nx4tdtƒD]#}d||f}|j|ƒq1W|S(sÛ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. RR is%d.%s.pool.ntp.org(trangetNR_POOL_SERVERStappend(R?tnamest pool_distrotxR((s;/usr/lib/python2.7/site-packages/cloudinit/config/cc_ntp.pytgenerate_server_namesgs  cCs|sg}n|sg}nt|ƒdkrjt|ƒdkrjt|ƒ}tjddj|ƒƒn|stdƒ‚n| rœ| rœtdƒ‚ni|d6|d6}|rîtjdd d d ƒ}|d }tj |d |ƒnt j |||ƒ|rtj |ƒndS(sáRender a ntp client configuration for the specified client. @param distro_name: string. The distro class name. @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. is*Adding distro default ntp pool servers: %st,s Invalid value for path parameters$Not template_fn or template providedR.R-tprefixstemplate_name-tsuffixs.tmplitcontentN( tlenR\RCRDtjoint ValueErrorRtmkstempRt write_fileRtrender_to_filetdel_file(t distro_nameR.R-RRt template_fnRtparamsttfile((s;/usr/lib/python2.7/site-packages/cloudinit/config/cc_ntp.pytwrite_ntp_config_templatezs(  $  cCs>|rdd|g}nd|dg}tj|dtƒdS(s%Restart or reload an ntp system service. @param service: A string specifying the name of the service to be affected. @param systemd: A boolean indicating if the distro uses systemd, defaults to False. @returns: A tuple of stdout, stderr results from executing the action. t systemctlsreload-or-restarttservicetrestarttcaptureN(RtsubpR>(Rntsystemdtcmd((s;/usr/lib/python2.7/site-packages/cloudinit/config/cc_ntp.pyt reload_ntp¨scCsÿg}tjt|jƒƒƒ}|rXdjt|ƒƒ}|jdjd|ƒƒn4t|j dƒ|j dƒgƒsŒ|jdƒnxBt|j ƒƒD].\}}d|}|dkrt |t |t jƒgƒsÍ|jd jd |d |ƒƒqÍqŸ|d krDt |tƒsÍ|jd jd |d |ƒƒqÍqŸ|dkr™|dkrbqŸnt |t jƒsÍ|jdjd |d |ƒƒqÍqŸt |t jƒsŸ|jdjd |d |ƒƒqŸqŸW|rûtdjddj|ƒƒƒ‚ndS(sOValidate 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. s, s(Missing required ntp:config keys: {keys}tkeysRRsJEither ntp:config:template or ntp:config:template_name values are requireds ntp:config:Rs6Expected a config file path {keypath}. Found ({value})tkeypathtvalueRsHExpected a list of required package names for {keypath}. Found ({value})s5Expected a string type for {keypath}. Found ({value})s$Invalid ntp configuration:\n{errors}terrorss N(stemplates template_name(tREQUIRED_NTP_CONFIG_KEYSt differencetsetRuRbtsortedRXR*tanyRER+tallt isinstancetsixt string_typestlistRNRc(t ntp_configRxtmissingRutkeyRwRv((s;/usr/lib/python2.7/site-packages/cloudinit/config/cc_ntp.pytsupplemental_schema_validation·sH            c CsSd|kr tjd|ƒdS|d}|dkr?i}nt|tƒsrtdjdtj|ƒƒƒ‚nt |t ƒ|j dt ƒ}t j|ƒr´tjd|ƒdSt|j dƒ|jƒ}t j||j d iƒgd t ƒ}t|ƒtd |j d ƒƒd}|j d ƒs†|j d ƒjd|jjƒ} |j| ƒ}|s†d|j d ƒ} t| ƒ‚q†nt|jjd|j dgƒd|j dgƒd|j d ƒd|d |j d ƒƒt|jjd|dd|dƒy!t|dd|jjƒƒWn)t jk rN} tjd| ƒ‚nXdS(sEnable and configure ntp.Rs8Skipping module named %s, not present or disabled by cfgNsL'ntp' key existed in config, but not a dictionary type, is a {_type} insteadt_typeR3s)Skipping module named %s, disabled by cfgR1R8R9RRRs{distro}s#No template found, not rendering %sR.R-RRRiRRRRrs&Failed to reload/start ntp service: %s(RCRDRNRtdictt RuntimeErrorR*Rtobj_nameRtschemaRER>Rtis_falseRMR?R=R†RUtreplaceRtget_template_filenameRlRPtinstall_packagesRtt uses_systemdtProcessExecutionErrort exception( RRAtcloudRt_argstntp_cfgR3tntp_client_configRiRtmsgte((s;/usr/lib/python2.7/site-packages/cloudinit/config/cc_ntp.pythandleés\        !    (*t__doc__tcloudinit.config.schemaRRt cloudinitRtloggingtcloudinit.settingsRRRRRR;RQR€ttextwrapRt getLoggert__name__RCR#tNTP_CONFRWR!RNR<R:R>tFalseR‹t frozensetRyRBRMRPRUR\RlRtR†R™(((s;/usr/lib/python2.7/site-packages/cloudinit/config/cc_ntp.pyts:                         - -  2