
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\me              	   @   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eZ ejeZG dd deZeee
edd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.
Z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descriptionZdistros	frequencyZexamplesZactivate_by_schema_keysc               @   s   e Zd ZdZdS )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__ r   r   %/usr/lib/python3.6/cc_set_hostname.pyr   K   s   r   )r   cfgcloudargsreturnc             C   s  t j|ddrtjd|  d S t j|dd }|d k	rB|jjd| t j|dd }|d k	rf|jjd| t j||\}}}tjj	|j
dd}	i }
tjj|	rtj|	jdkrt jt j|	}
||
jd	kp||
jd
k}|stjd d S |o|dkrtjd d S tjd|| y|jj|| W nN tk
rt } z0d||f }t jt| td||f |W Y d d }~X nX t|	||d d S )NZpreserve_hostnameFzVConfiguration option 'preserve_hostname' is set, not setting the hostname in module %sZprefer_fqdn_over_hostnamecreate_hostname_filedatazset-hostnamer   hostnamefqdnz*No hostname changes. Skipping set_hostnameZ	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   Zget_cfg_option_boolLOGdebugZdistroZ
set_optionZget_hostname_fqdnospathjoinZ	get_cpathexistsstatst_sizeZ	load_jsonZ	load_filegetZset_hostname	ExceptionZlogexcr   r   )r   r   r   r   Zhostname_fqdnr   r   r   Z
is_defaultZprev_fnZprev_hostnameZhostname_changedemsgr   r   r   handleS   sF    



$r+   )r   Zloggingr!   textwrapr   Z	cloudinitr   Zcloudinit.atomic_helperr   Zcloudinit.cloudr   Zcloudinit.configr   Zcloudinit.config.schemar   r   Zcloudinit.distrosr	   Zcloudinit.settingsr
   r   ZMODULE_DESCRIPTIONmetaZ	getLoggerr   r   r(   r   strlistr+   r   r   r   r   <module>   s4   
