
    f                     ,   U d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ dd	lmZ dd
lmZ eZdZdddeeged ed       ed      gg dZeed<    ee      Z  ej0                  e      Z G d de      Zdedede
deddf
dZy)z#Set Hostname: Set hostname and FQDN    N)dedent)util)
write_json)Cloud)Config)
MetaSchemaget_meta_doc)ALL_DISTROS)PER_INSTANCEa'  This module handles setting the system hostname and fully qualified domain
name (FQDN). If ``preserve_hostname`` is set, then the hostname will not be
altered.

A hostname and FQDN can be provided by specifying a full domain name under the
``FQDN`` key. Alternatively, a hostname can be specified using the ``hostname``
key, and the FQDN of the cloud will be used. If a FQDN specified with the
``hostname`` key, it will be handled properly, although it is better to use
the ``fqdn`` config key. If both ``fqdn`` and ``hostname`` are set,
the ``prefer_fqdn_over_hostname`` will force the use of FQDN in all distros
when true, and when false it will force the short hostname. Otherwise, the
hostname to use is distro-dependent.

.. note::
    cloud-init performs no hostname input validation before sending the
    hostname to distro-specific tools, and most tools will not accept a
    trailing dot on the FQDN.

This module will run in the init-local stage before networking is configured
if the hostname is set by metadata or user data on the local system.

This will occur on datasources like nocloud and ovf where metadata and user
data are available locally. This ensures that the desired hostname is applied
before any DHCP requests are performed on these platforms where dynamic DNS is
based on initial hostname.
cc_set_hostnamezSet HostnamezSet hostname and FQDNzpreserve_hostname: truez            hostname: myhost
            create_hostname_file: true
            fqdn: myhost.example.com
            prefer_fqdn_over_hostname: true
            z            # On a machine without an ``/etc/hostname`` file, don't create it
            # In most clouds, this will result in a DHCP-configured hostname
            # provided by the cloud
            create_hostname_file: false
            )idnametitledescriptiondistros	frequencyexamplesactivate_by_schema_keysmetac                       e Zd ZdZy)SetHostnameErrorzRaised when the distro runs into an exception when setting hostname.

    This may happen if we attempt to set the hostname early in cloud-init's
    init-local timeframe as certain services may not be running yet.
    N)__name__
__module____qualname____doc__     B/usr/lib/python3/dist-packages/cloudinit/config/cc_set_hostname.pyr   r   T   s    r   r   r   cfgcloudargsreturnc                 j   t        j                  |dd      rt        j                  d|        y t        j                  |dd       }||j                  j                  d|       t        j                  |dd       }||j                  j                  d|       t        j                  ||      \  }}}t        j                  j                  |j                  d      d      }	i }
t        j                  j                  |	      rJt        j                  |	      j                  dkD  r(t        j                  t        j                  |	            }
||
j!                  d	      k7  xs ||
j!                  d
      k7  }|st        j                  d       y |r|dk(  rt        j                  d       y t        j                  d||       	 |j                  j#                  ||       t+        |	||d       y # t$        $ r9}d|d|d}t        j&                  t        |       t)        |d|      |d }~ww xY w)Npreserve_hostnameFzVConfiguration option 'preserve_hostname' is set, not setting the hostname in module %sprefer_fqdn_over_hostnamecreate_hostname_filedatazset-hostnamer   hostnamefqdnz*No hostname changes. Skipping set_hostname	localhostz6Hostname is localhost. Let other services handle this.zSetting the hostname to %s (%s)zFailed to set the hostname to z ()z: )r(   r)   )r   get_cfg_option_boolLOGdebugdistro
set_optionget_hostname_fqdnospathjoin	get_cpathexistsstatst_size	load_jsonload_text_filegetset_hostname	Exceptionlogexcr   r   )r   r   r    r!   hostname_fqdnr&   r(   r)   
is_defaultprev_fnprev_hostnamehostname_changedemsgs                 r   handlerF   \   s   %8%@		5	

 	 ,,($M   ;]K  33#T ' 68LM#'#9#9#u#E XtZ ggll5??62NCGM	ww~~g2777#3#;#;a#?t':':7'CD=#4#4$  +	""6*	*  		>?h+-		JKII/x@;!!(D1
 wXt<=	  ;9=xHC323:;s   G0 0	H294H--H2) r   loggingr2   textwrapr   	cloudinitr   cloudinit.atomic_helperr   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   r	   cloudinit.distrosr
   cloudinit.settingsr   r   MODULE_DESCRIPTIONr   __annotations__	getLoggerr   r-   r=   r   strlistrF   r   r   r   <module>rU      s    *  	   . ! # < ) +	 : $%}!	
 		
&  "5j : t
g!y 4> 4>6 4>% 4>t 4> 4>r   