o
    wÚ7e—  ã                
   @   sî   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ƒgg dœZeed< eeƒZ e e¡ZG dd„ deƒZdedede
deddf
dd„ZdS )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: truezz            hostname: myhost
            fqdn: myhost.example.com
            prefer_fqdn_over_hostname: true
            )ÚidÚnameÚtitleÚdescriptionÚdistrosÚ	frequencyÚexamplesÚactivate_by_schema_keysÚmetac                   @   s   e Zd ZdZdS )ÚSetHostnameErrorzÛRaised 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__© r   r   úB/usr/lib/python3/dist-packages/cloudinit/config/cc_set_hostname.pyr   K   s    r   r   ÚcfgÚcloudÚargsÚreturnc              
   C   sX  t  |dd¡rt d| ¡ d S t  |dd ¡}|d ur!|j d|¡ t  ||¡\}}}tj 	| 
d¡d¡}i }	tj |¡rLt |¡jdkrLt  t  |¡¡}	||	 d¡kpY||	 d	¡k}
|
sct d
¡ d S |rp|dkrpt d¡ d S t d||¡ z	|j ||¡ W n! ty¡ } zd||f }t  t|¡ td||f ƒ|‚d }~ww t|||dœƒ d S )NÚpreserve_hostnameFzVConfiguration option 'preserve_hostname' is set, not setting the hostname in module %sÚprefer_fqdn_over_hostnameÚ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 %s (%s)z%s: %s)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_fileÚgetÚset_hostnameÚ	ExceptionÚlogexcr   r   )r   r   r   r   Úhostname_fqdnr#   r$   Ú
is_defaultÚprev_fnÚprev_hostnameÚhostname_changedÚeÚmsgr   r   r   ÚhandleS   sJ   ýÿÿþ

€ýr@   ) r   Úloggingr,   Útextwrapr   Ú	cloudinitr   Úcloudinit.atomic_helperr   Úcloudinit.cloudr   Úcloudinit.configr   Úcloudinit.config.schemar   r   Úcloudinit.distrosr	   Úcloudinit.settingsr
   r   ÚMODULE_DESCRIPTIONr   Ú__annotations__Ú	getLoggerr   r'   r7   r   ÚstrÚlistr@   r   r   r   r   Ú<module>   s<   ÿþ
ï
"