3
\me @ s U d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl m
Z
ddlmZ ddl
mZmZ dd lmZ dd
lmZ eZdZdd
deegededgg dZeeeZ ejeZG dd deZeee
eddddZdS )z#Set Hostname: Set hostname and FQDN N)dedent)util)
write_json)Cloud)Config)
MetaSchemaget_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.
Zcc_set_hostnamezSet HostnamezSet hostname and FQDNzpreserve_hostname: truezz hostname: myhost
fqdn: myhost.example.com
prefer_fqdn_over_hostname: true
)idnametitledescriptionZdistros frequencyZexamplesZactivate_by_schema_keysc @ s e Zd ZdZdS )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 cfgcloudargsreturnc C s t j|ddrtjd| d S t j|dd }|d k rB|jjd| t j|dd }|d k rf|jjd| t j||\}}}tjj |j
dd} i }
tjj| rtj| j
dkrt jt j| }
||
jd kp||
jd
k}|stjd d S |o|dkrtjd
d S tjd|| y|jj|| W nN tk
rt } z0d||f }
t jt|
td|
|f |W Y d d }~X nX t| ||d d S )NZpreserve_hostnameFzVConfiguration option 'preserve_hostname' is set, not setting the hostname in module %sZprefer_fqdn_over_hostnamecreate_hostname_filedatazset-hostnamer hostnamefqdnz*No hostname changes. Skipping set_hostnameZ localhostz6Hostname is localhost. Let other services handle this.zSetting the hostname to %s (%s)z%Failed to set the hostname to %s (%s)z%s: %s)r r )r Zget_cfg_option_boolLOGdebugZdistroZ
set_optionZget_hostname_fqdnospathjoinZ get_cpathexistsstatst_sizeZ load_jsonZ load_filegetZset_hostname ExceptionZlogexcr r )r r r r Z
hostname_fqdnr r r Z
is_defaultZprev_fnZ
prev_hostnameZhostname_changedemsgr r r handleS sF
$r+ )r Zloggingr! textwrapr Z cloudinitr Zcloudinit.atomic_helperr Zcloudinit.cloudr Zcloudinit.configr Zcloudinit.config.schemar r Zcloudinit.distrosr Zcloudinit.settingsr
r ZMODULE_DESCRIPTIONmetaZ getLoggerr r r( r strlistr+ r r r r