o
    ¦®Øf©  ã                
   @   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 dd	lmZ d
egeg dœZeed< 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)Úutil)Ú
write_json)ÚCloud)ÚConfig)Ú
MetaSchema)ÚALL_DISTROS)ÚPER_INSTANCEÚcc_set_hostname)ÚidÚdistrosÚ	frequencyÚ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      s    r   ÚnameÚcfgÚcloudÚargsÚreturnc              
   C   s|  t  |dd¡rt d| ¡ d S t  |dd ¡}|d ur!|j d|¡ t  |dd ¡}|d ur3|j d|¡ t  ||¡\}}}tj 	| 
d¡d¡}	i }
tj |	¡r^t |	¡jdkr^t  t  |	¡¡}
||
 d	¡kpk||
 d
¡k}|sut d¡ d S |r‚|dkr‚t 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Úcreate_hostname_fileÚ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_text_fileÚgetÚset_hostnameÚ	ExceptionÚlogexcr   r   )r   r   r   r   Úhostname_fqdnr   r   r    Ú
is_defaultÚprev_fnÚprev_hostnameÚhostname_changedÚeÚmsgr   r   r   Úhandle'   sT   ýÿÿÿþ

€ýr<   )r   Úloggingr(   Ú	cloudinitr   Úcloudinit.atomic_helperr   Úcloudinit.cloudr   Úcloudinit.configr   Úcloudinit.config.schemar   Úcloudinit.distrosr   Úcloudinit.settingsr   r   Ú__annotations__Ú	getLoggerr   r#   r3   r   ÚstrÚlistr<   r   r   r   r   Ú<module>   s$   ü
"