3 \me @sUdZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z ddl mZmZdd lmZdd lmZeZd Zd d deegededggdZeeeZejeZGdddeZee e eddddZdS)z#Set Hostname: Set hostname and FQDNN)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_hostnamez Set HostnamezSet hostname and FQDNzpreserve_hostname: truezz hostname: myhost fqdn: myhost.example.com prefer_fqdn_over_hostname: true )idnametitle descriptionZdistros frequencyZexamplesZactivate_by_schema_keysc@seZdZdZdS)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__rr%/usr/lib/python3.6/cc_set_hostname.pyrKsr)r cfgcloudargsreturncCstj|ddrtjd|dStj|dd}|dk rB|jjd|tj|dd}|dk rf|jjd|tj||\}}}tjj |j dd} i} tjj | rtj | j dkrtjtj| } || jd kp|| jd k} | stjd dS|o|d krtjd dStjd||y|jj||WnNtk rt} z0d||f} tjt| td| | f| WYdd} ~ XnXt| ||ddS)NZpreserve_hostnameFzVConfiguration option 'preserve_hostname' is set, not setting the hostname in module %sZprefer_fqdn_over_hostnamecreate_hostname_filedataz set-hostnamerhostnamefqdnz*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)rr)rZget_cfg_option_boolLOGdebugZdistroZ set_optionZget_hostname_fqdnospathjoinZ get_cpathexistsstatst_sizeZ load_jsonZ load_filegetZ set_hostname ExceptionZlogexcrr)r rrrZ hostname_fqdnrrrZ is_defaultZprev_fnZ prev_hostnameZhostname_changedemsgrrrhandleSsF      $r+)rZloggingr!textwraprZ cloudinitrZcloudinit.atomic_helperrZcloudinit.cloudrZcloudinit.configrZcloudinit.config.schemarrZcloudinit.distrosr Zcloudinit.settingsr rZMODULE_DESCRIPTIONmetaZ getLoggerrrr(rstrlistr+rrrrs4