3 \�me� � @ s� U d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl m Z ddlmZ ddl mZmZ dd lmZ dd lmZ eZdZdd deegeded�gg d�Zeee�Z eje�ZG dd� de�Zeee edd�dd�ZdS )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. Zcc_set_hostnamezSet HostnamezSet hostname and FQDNzpreserve_hostname: truezz hostname: myhost fqdn: myhost.example.com prefer_fqdn_over_hostname: true )�id�name�title�descriptionZdistros� frequencyZexamplesZactivate_by_schema_keysc @ s e Zd ZdZdS )�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 �%/usr/lib/python3.6/cc_set_hostname.pyr K s r )r �cfg�cloud�args�returnc C s� t j|dd�rtjd| � d S t j|dd �}|d k rB|jjd|� t j|dd �}|d k rf|jjd|� t j||�\}}}tjj |j d�d�} i } tjj| �r�tj| �j dkr�t jt j| ��} || jd �kp�|| jd �k}|s�tjd� d S |o�|dk�rtjd � 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_hostname�create_hostname_file�datazset-hostnamer �hostname�fqdnz*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_bool�LOG�debugZdistroZ set_optionZget_hostname_fqdn�os�path�joinZ get_cpath�exists�stat�st_sizeZ load_jsonZ load_file�getZset_hostname� ExceptionZlogexcr r )r r r r Z hostname_fqdnr r r Z is_defaultZprev_fnZ prev_hostnameZhostname_changed�e�msgr 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_DESCRIPTION�metaZ getLoggerr r r( r �str�listr+ r r r r �<module> s4