name : alpine.cpython-39.pyc
a

'�Dgda�@s�ddlZddlZddlZddlZddlmZddlmZmZmZddl	m
Z
mZmZm
Z
mZddlmZddlmZmZe�e�ZdZGdd	�d	e
j�ZdS)
�N)�datetime)�Any�Dict�Optional)�distros�helpers�	lifecycle�subp�util)�HostnameConf)�
PER_ALWAYS�PER_INSTANCEa7# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}

csbeZdZdZdZdZdZdZdeed�iZ	dZ
d	Zd
d�Zdd
�Z
dDdd�Zejd�dd�Zdd�Zdd�Zdd�ZdEdd�Zdd�Zeeeed�dd �Zd!d"�ZdFd#d$�Zd%d&�d'd(�Zed)d*��Zed+��fd,d-�Z�fd.d/�Z ed0��fd1d2�Z!�fd3d4�Z"dG�fd5d6�	Z#dHd9d:�Z$e%d;d<��Z&e'dd=�eeed>�d?d@��Z(e%ee)edA�dBdC��Z*�Z+S)I�Distrozpy3-pipz/usr/share/bkeymaps/z&/etc/profile.d/50-cloud-init-locale.shz/etc/network/interfacesz/etc/shadowZeni)Zeni_pathZ
eni_headerz
/var/lib/dhcpzdhclient\.leasescCs6tj�||||�t�|�|_d|_d|_d|d<dS)NzC.UTF-8ZalpineZsshdZssh_svcname)rr�__init__rZRunners�_runner�default_localeZosfamily)�self�nameZcfg�paths�r�</usr/lib/python3.9/site-packages/cloudinit/distros/alpine.pyr.s
zDistro.__init__cCs|jS)zgThe default locale for Alpine Linux is different than
        cloud-init's DataSource default.
        )r�rrrr�
get_locale8szDistro.get_localeNcCsB|s
|j}|s|j}ddddd|ddg}t�|d�|�d�dS)	N�#z?# This file is created by cloud-init once per new instance bootzexport CHARSET=UTF-8zexport LANG=%szexport LC_COLLATE=C��
�)r�locale_conf_fnr
�
write_file�join)r�localeZout_fn�linesrrr�apply_locale>s�	zDistro.apply_locale)�pkglistcCs|��|jd|d�dS)N�add)�pkgs)�update_package_sources�package_command)rr#rrr�install_packagesQszDistro.install_packagescCsxd}z|�|�}Wn8tyJt�|jdd�}|r6nt�d�YdSYn0|sXtd�}|�|�t�	|t
|�d�dS)N�create_hostname_fileTz8create_hostname_file is False; hostname file not createdrr)�_read_hostname_conf�IOErrorr
Zget_cfg_option_boolZ_cfg�LOG�inforZset_hostnamer�str)r�hostname�filename�confr)rrr�_write_hostnameUs"��
zDistro._write_hostnamecCs|�|j�}|j|fS�N)�_read_hostnameZhostname_conf_fn)rZsys_hostnamerrr�_read_system_hostnamekszDistro._read_system_hostnamecCstt�|��}|��|Sr3)rr
�load_text_file�parse)rr0r1rrrr*oszDistro._read_hostname_confcCs8d}z|�|�}|j}Wnty*Yn0|s4|S|Sr3)r*r/r+)rr0�defaultr/r1rrrr4ts

zDistro._read_hostnamecCsdS)Nz	127.0.1.1rrrrr�_get_localhost_ipszDistro._get_localhost_ip)�layout�model�variant�optionscCs�|sd}t�|�t|��tj�|j|�}tj�|�sPd|}t�|�t|��|sjd}t�|�t|��tj�|d|�}tj�|�s�d|}t�|�t|��|r�t�	d�|r�t�	d�t
�
d||g�dS)	NzKeyboard layout not specified.z,Keyboard layout directory %s does not exist.zKeyboard variant not specified.z
%s.bmap.gzz(Keyboard variant file %s does not exist.z+Keyboard model is ignored for Alpine Linux.z.Keyboard options are ignored for Alpine Linux.zsetup-keymap)r,�error�RuntimeError�os�pathr�keymap_path�isdir�isfile�warningr	)rr:r;r<r=�msgZkeymap_layout_pathZkeymap_variant_pathrrr�
set_keymap�s>
��

���


zDistro.set_keymapcCstj||�|�d�dS)N)�tzZtz_file)rZset_etc_timezoneZ
_find_tz_file)rrHrrr�set_timezone�szDistro.set_timezonecCs�|durg}dg}|�d�|r6t|t�r6|�|�n|rNt|t�rN|�|�|r\|�|�|dkrr|�ddg�t�d|�}|�|�tj|dd�dS)	NZapkz--quietZupgradez--update-cachez--availablez%s-%sF)�capture)�append�
isinstancer.�list�extendr
Zexpand_package_listr	)r�command�argsr%�cmdr#rrrr'�s



zDistro.package_commandF)�forcecCs$|jjd|jdg|rtntd�dS)Nzupdate-sources�update)Zfreq)r�runr'rr
)rrRrrrr&�s
�zDistro.update_package_sourcescCs|jsddg|_|jS)z7Allow distro to determine the preferred ntp client listZchronyZntp)Z_preferred_ntp_clientsrrrr�preferred_ntp_clients�s
zDistro.preferred_ntp_clients)�returncs�t�|�rt�d|�dSd|vr2t�d�|d=t�d�rPt�j|fi|��S|�	dd�}dd	g}d
ddd
dd�}ddi}|�
d�}|r�t|t�r�|�
d�}n"t|t�r�tjd|�d�ddd�dd�|D�}d�|�|d<|�
d�r�|�|d�|�r2|�r2|D]*}t�|��s|�|�t�d||��qd|v�rLt|d�|d<gdddd�}	t|���D]�\}
}|
|v�r�|�r�t|t��r�|�||
|g�n^|
|	v�r�|�r�t|t��r�|
dk�r�|�
d�|	|
<n||	|
<n|
|v�rf|�rf|�||
��qf|�
d ��s|�
d��r"|�d!�|�|�t�d"|�zt�|�Wn8tj�y~}zt�d#|�|�WYd}~n
d}~00|	dD]d}
t�d$|
�zt�d%||
g�Wn<tj�y�}z t�td&||
�|�WYd}~n
d}~00�q�|	d'�rt�j||	d'dd(�|	d)}|	d*}d}|j}zt�|�}Wn6t�yl}zt�d+|�|�WYd}~n
d}~00d}|��D](}|�
d,�}|d-|k�rz|}�q��qz|�r�d.}d.|d/<d.|d0<d.|d1<d2}|du�rt �!|�t �!d3�j"}t|�|d4<|d.k�r|d5}|d6}|du�r<||d7<|d.k�r4|d5}|d8}|�#|d,�|��}t�d9||�ztj$||d:dd;�Wn8t%�y�}zt�td<|�|�WYd}~n
d}~00nt�td=||�dS)>z�
        Add a user to the system using standard tools

        On Alpine this may use either 'useradd' or 'adduser' depending
        on whether the 'shadow' package is installed.

        Returns False if user already exists, otherwise True.
        z!User %s already exists, skipping.FZselinux_userz0Ignoring selinux_user parameter for Alpine LinuxZuseradd�
create_groupsTZadduserz-Dz-gz-hz-Gz-s�-u)Zgecos�homedir�
primary_group�shell�uid�systemz-S�groups�,z	The user z) has a 'groups' config value of type dictz22.3z=Use a comma-delimited string or array instead: group1,group2.)Z
deprecatedZdeprecated_versionZ
extra_messagecSsg|]}|���qSr)�strip)�.0�grrr�
<listcomp>�z#Distro.add_user.<locals>.<listcomp>rZz created group '%s' for user '%s'r\N)r^�
expiredate�inactive�passwdZno_create_homez-HzAdding user %szFailed to create user %szAdding user to group %s�addgroupz!Failed to add user %s to group %srg)Zhashedrerf�&Failed to read %s file, file not found�:rr���zpassword agingz
1970-01-01�z & zacct expiration date�zinactivity periodzUpdating %s for user %s�w�ZomodeZ
preserve_mode�Failed to update %s filezFailed to update %s for user %s)&r
�is_userr,r-rEr	�which�super�add_user�pop�getrLr.�split�dictrZ	deprecaterrK�is_group�create_group�debug�sorted�itemsrN�ProcessExecutionError�logexcZ
set_passwd�	shadow_fnr6�FileNotFoundError�
splitlinesrZ
fromisoformat�days�replacerr+)rr�kwargsrWZadduser_cmdZadduser_optsZ
adduser_flagsr^�groupZunsupported_busybox_values�key�val�eZ
addn_grouprerf�shadow_contents�shadow_file�
original_line�line�new_line_partsZupdate_typer���	__class__rrrv�s	


�



�



����



��

�


��



��
�zDistro.add_userc
s�tj�d�rdt�d�vr&t��|�Sdd|g}z,tj|ddgd�\}}t�d|�rZWd	SWn8tj	y�}zt
�td
|�|�WYd}~n
d}~00dS)zL
        Lock the password of a user, i.e., disable password logins
        �/usr/bin/passwd�bbsuidrgz-lr���rcszis already lockedTz&Failed to disable password for user %sN)
r@rA�islink�readlinkru�lock_passwdr	�re�searchr�r
r�r,)rrrQZ_out�errr�r�rrr��s��

zDistro.lock_passwd)rc
s�tj�d�rdt�d�vr&t��|�Sdd|g}z,tj|ddgd�\}}t�d|�rZWd	SWn8tj	y�}zt
�td
|�|�WYd}~n
d}~00dS)zM
        Unlock the password of a user, i.e., enable password logins
        r�r�rgrXrr�r�zis already unlockedTz%Failed to unlock password for user %sN)
r@rAr�r�ru�
unlock_passwdr	r�r�r�r
r�r,)rrrQ�_r�r�r�rrr��s��
	
zDistro.unlock_passwdc
s^tj�d�rdt�d�vr&t��|�Sd}|j}zt�|�}Wn4t	yr}zt
�d|�|�WYd}~n
d}~00d}|��D].}|�
d�}|d|kr�t
�d|�|}q�q�|�rL|ddk�r>d|d<|�|d�|�d	�}t
�d
|�ztj||ddd
�Wn8t�y:}zt�t
d|�|�WYd}~n
d}~00nt
�d|�nt�t
d|�dS)Nr�r�rirjrz'Found /etc/shadow line matching user %s��0r�zExpiring password for user %srpTrqrrz'Password for user %s is already expiredzFailed to set 'expire' for %s)r@rAr�r�ru�
expire_passwdr�r
r6r�r,rEr�ryr}r�rrr+r�)r�userr�r�r�r�r�r�r�rrr��sN��
��
zDistro.expire_passwdcs�t�d�rt��||�Sd|g}|s(g}t�|�r@t�d|�n>zt�|�t�d|�Wn"tj	y|t�
td|�Yn0t|�dkr�|D]<}t�|�s�t�d||�q�t�d||g�t�d||�q�dS)	NZgroupaddrhz(Skipping creation of existing group '%s'zCreated new group %szFailed to create group %srzCUnable to add group member '%s' to group '%s'; user does not exist.zAdded user '%s' to group '%s')
r	rtrur|r
r{r,rEr-r�r��lenrs)rr�membersZ
group_add_cmd�memberr�rrr|s.



�zDistro.create_group�poweroff�nowc
Csp|dg}|dkr|dg7}nPz|�tt|�d��Wn4tyj}ztd|f�|�WYd}~n
d}~00|S)Nz-dr�r��<z?power_state[delay] must be 'now' or '+m' (minutes). found '%s'.)rKr.�int�
ValueError�	TypeError)r�modeZdelay�messagerOr�rrr�shutdown_command@s���zDistro.shutdown_commandcCsdS)z1
        Alpine uses OpenRC, not systemd
        Frrrrr�uses_systemdZszDistro.uses_systemdr�)�action�service�
extra_argsc	
Gs�ddg}ddg}t|�|dgt|�|dgt|�d|gt|�d|gt|�|dgt|�|dgt|�|dgt|�|d	gd
�}t||�}tj|d|d�S)
a<
        Perform the requested action on a service. This handles OpenRC
        specific implementation details.

        OpenRC has two distinct commands relating to services,
        'rc-service' and 'rc-update' and the order of their argument
        lists differ.
        May raise ProcessExecutionError
        z
rc-servicez	--nocolorz	rc-update�stop�start�delr$�restart�status)r�r��disable�enabler��reloadz
try-reloadr�T)rJr�)rMr	)	rr�r�r�r�Zinit_cmdZ
update_cmdZcmdsrQrrr�manage_serviceas
�
zDistro.manage_service)�blockdevrVcCs�tj�|�}|�d�r�t�t�|�j�}t�t�|�j�}t�d���}|D]j}|j	�d�rJt�
|j�}t�|j�rJt�t�|j�j�|krJt�t�|j�j�|krJtj�|j�}q�qJWd�n1s�0Y|�d�r�t
�d||�|SdS)aReturns underlying block device for a mapped device.

        If it is mapped, blockdev will usually take the form of
        /dev/mapper/some_name

        If blockdev is a symlink pointing to a /dev/dm-* device, return
        the device pointed to. Otherwise, return None.
        z/dev/mapperz/dev/zdm-Nz/dev/dm-z$%s is a mapped device pointing to %s)r@rA�realpath�
startswith�major�stat�st_rdev�minor�scandirr�lstat�S_ISBLK�st_moder,r})r�r�r�r��itZdeventry�resrrr�get_mapped_device}s2

����$
�zDistro.get_mapped_device)N)N)NN)N)r�r�N),�__name__�
__module__�__qualname__Zpip_package_namerBrZnetwork_conf_fnr��NETWORK_FILE_HEADERZrenderer_configsZdhclient_lease_directoryZdhclient_lease_file_regexrrr"rZPackageListr(r2r5r*r4r9r.rGrIr'r&�propertyrU�boolrvr�r�r�r|r��staticmethodr��classmethodr�rr��
__classcell__rrr�rr sP
�


"

[9#

��r)Zloggingr@r�r�r�typingrrrZ	cloudinitrrrr	r
Z"cloudinit.distros.parsers.hostnamerZcloudinit.settingsrr
Z	getLoggerr�r,r�rrrrr�<module>	s


© 2025 Cubjrnet7