o x[hda@sddlZddlZddlZddlZddlmZddlmZmZmZddl m Z m Z m Z m Z mZddlmZddlmZmZeeZdZGdd d e jZdS) N)datetime)AnyDictOptional)distroshelpers lifecyclesubputil) 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} csneZdZdZdZdZdZdZdeediZ dZ d Z d d Z d d Z dIddZdejfddZddZddZddZdIddZddZdeded ed!efd"d#Zd$d%ZdJd&d'Zd(d)d*d+Zed,d-Zd.effd/d0 Zfd1d2Z d3effd4d5 Z!fd6d7Z"dIfd8d9 Z#dKdd?Z&e'dd@dAedBedCefdDdEZ(e%dFed.e)efdGdHZ*Z+S)LDistrozpy3-pipz/usr/share/bkeymaps/z&/etc/profile.d/50-cloud-init-locale.shz/etc/network/interfacesz /etc/shadoweni)eni_path eni_headerz /var/lib/dhcpzdhclient\.leasescCs6tj||||t||_d|_d|_d|d<dS)NzC.UTF-8alpinesshd ssh_svcname)rr__init__rRunners_runnerdefault_localeosfamily)selfnamecfgpathsr:/usr/lib/python3/dist-packages/cloudinit/distros/alpine.pyr.s   zDistro.__init__cCs|jS)zgThe default locale for Alpine Linux is different than cloud-init's DataSource default. )rrrrr get_locale8szDistro.get_localeNcCsB|s|j}|s |j}ddddd|ddg}t|d|ddS) N#z?# This file is created by cloud-init once per new instance bootzexport CHARSET=UTF-8zexport LANG=%szexport LC_COLLATE=C )rlocale_conf_fnr write_filejoin)rlocaleout_fnlinesrrr apply_locale>s zDistro.apply_localepkglistcCs||jd|ddS)Nadd)pkgs)update_package_sourcespackage_command)rr-rrrinstall_packagesQszDistro.install_packagescCsxd}z||}Wnty%t|jdd}|rntdYdSYnw|s,td}||t |t |ddS)Ncreate_hostname_fileTz8create_hostname_file is False; hostname file not createdr#r%) _read_hostname_confIOErrorr get_cfg_option_bool_cfgLOGinfor set_hostnamer'str)rhostnamefilenameconfr3rrr_write_hostnameUs&   zDistro._write_hostnamecCs||j}|j|fSN)_read_hostnamehostname_conf_fn)r sys_hostnamerrr_read_system_hostnameks  zDistro._read_system_hostnamecCstt|}||Sr@)r r load_text_fileparse)rr=r>rrrr4oszDistro._read_hostname_confcCs8d}z ||}|j}Wn tyYnw|s|S|Sr@)r4r<r5)rr=defaultr<r>rrrrAts   zDistro._read_hostnamecCdS)Nz 127.0.1.1rr rrr_get_localhost_ipszDistro._get_localhost_iplayoutmodelvariantoptionscCs|s d}t|t|tj|j|}tj|s(d|}t|t||s5d}t|t|tj|d|}tj|sQd|}t|t||rXt d|r_t dt d||gdS) 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) r8error RuntimeErrorospathr( keymap_pathisdirisfilewarningr )rrJrKrLrMmsgkeymap_layout_pathkeymap_variant_pathrrr set_keymaps>        zDistro.set_keymapcCstj|||ddS)N)tztz_file)rset_etc_timezone _find_tz_file)rrZrrr set_timezoneszDistro.set_timezonecCs|durg}dg}|d|rt|tr||n |r't|tr'|||r.|||dkr9|ddgtd|}||tj|dddS) Napkz--quietupgradez--update-cachez --availablez%s-%sF)capture)append isinstancer;listextendr expand_package_listr )rcommandargsr/cmdr-rrrr1s      zDistro.package_commandF)forcecCs$|jjd|jdg|r tntddS)Nzupdate-sourcesupdate)freq)rrunr1r r )rrjrrrr0s   zDistro.update_package_sourcescCs|jsddg|_|jS)z7Allow distro to determine the preferred ntp client listchronyntp)_preferred_ntp_clientsr rrrpreferred_ntp_clientss zDistro.preferred_ntp_clientsreturnc snt|r td|dSd|vrtd|d=tdr(tj|fi|S| dd}dd g}d d d d dd}ddi}| d}|r{t |t rP| d}nt |tratjd|dddddd|D}d||d<| dr{||d|r|r|D]}t|s||td||qd|vrt |d|d<gdddd} t|D]B\} } | |vr| rt | t r||| | gq| | vr| rt | t r| dkr| d| | <q| | | <q| |vr| r||| q| d s| dr|d!||td"|zt|Wntjy)} ztd#|| d} ~ ww| dD]+} td$| z td%|| gWq.tjyY} z ttd&|| | d} ~ ww| d'rjtj|| d'dd(| d)}| d*}d}|j}zt|}Wnty} ztd+|| d} ~ wwd}|D]}| d,}|d-|kr|}nq|r-d.}d.|d/<d.|d0<d.|d1<d2}|durt !|t !d3j"}t ||d4<|d.kr|d5}|d6}|dur||d7<|d.kr|d5}|d8}|#|d,|}td9||z tj$||d:dd;WdSt%y,} z ttd<|| d} ~ wwttd=||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.F selinux_userz0Ignoring selinux_user parameter for Alpine Linuxuseradd create_groupsTadduserz-Dz-gz-hz-Gz-s-u)gecoshomedir primary_groupshelluidsystemz-Sgroups,z The user z) has a 'groups' config value of type dictz22.3z=Use a comma-delimited string or array instead: group1,group2.) deprecateddeprecated_version extra_messagecSsg|]}|qSr)strip).0grrr sz#Distro.add_user..rzz created group '%s' for user '%s'r|N)r~ expiredateinactivepasswdno_create_homez-HzAdding user %szFailed to create user %szAdding user to group %saddgroupz!Failed to add user %s to group %sr)hashedrr&Failed to read %s file, file not found:rr#zpassword agingz 1970-01-01z & zacct expiration datezinactivity periodzUpdating %s for user %swomode preserve_modeFailed to update %s filezFailed to update %s for user %s)&r is_userr8r9rUr whichsuperadd_userpopgetrcr;splitdictr deprecater(rbis_group create_groupdebugsorteditemsreProcessExecutionErrorlogexc set_passwd shadow_fnrEFileNotFoundError splitlinesr fromisoformatdaysreplacer'r5)rrkwargsru adduser_cmd adduser_opts adduser_flagsr~groupunsupported_busybox_valueskeyvale addn_grouprrshadow_contents shadow_file original_linelinenew_line_parts update_typer __class__rrrs                                zDistro.add_userc tjdr dtdvrt|Sdd|g}ztj|ddgd\}}td|r-Wd SWd Stj yE}z t t d ||d }~ww) zL Lock the password of a user, i.e., disable password logins /usr/bin/passwdbbsuidrz-lrrcszis already lockedTz&Failed to disable password for user %sN) rPrQislinkreadlinkr lock_passwdr researchrr rr8)rrri_outerrrrrrrs    zDistro.lock_passwdrc r) zM Unlock the password of a user, i.e., enable password logins rrrrwrrrzis already unlockedTz%Failed to unlock password for user %sN) rPrQrrr unlock_passwdr rrrr rr8)rrri_rrrrrrs     zDistro.unlock_passwdc sBtjdr dtdvrt|Sd}|j}zt|}Wnt y3}zt d||d}~wwd}| D]}| d}|d|krQt d||}nq:|r|ddkrd|d<||d|d }t d |z tj||d d d WdSty}z tt d||d}~wwt d|dStt d|dS)Nrrrrrz'Found /etc/shadow line matching user %s0rzExpiring password for user %srTrrz'Password for user %s is already expiredzFailed to set 'expire' for %s)rPrQrrr expire_passwdrr rErr8rUrrrrr(r'r5r)ruserrrrrrrrrrrsV          zDistro.expire_passwdcstdr t||Sd|g}|sg}t|r td|nz t|td|Wntj y>t td|Ynwt |dkrf|D] }t |sVtd||qGtd||gtd||qGdSdS) Ngroupaddrz(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 rrrr rr8rUr9rrlenr)rrmembers group_add_cmdmemberrrrrs4     zDistro.create_grouppoweroffnowc Csd|dg}|dkr|dg7}|Sz|tt|dW|Sty1}ztd|f|d}~ww)Nz-drr<z?power_state[delay] must be 'now' or '+m' (minutes). found '%s'.)rbr;int ValueError TypeError)rmodedelaymessagergrrrrshutdown_command@s"  zDistro.shutdown_commandcCrH)z1 Alpine uses OpenRC, not systemd Frrrrr uses_systemdZszDistro.uses_systemdractionservice extra_argsc Gsddg}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-updatestopstartdelr.restartstatus)rrdisableenablerreloadz try-reloadrT)rar)rdr ) rrrrrinit_cmd update_cmdcmdsrirrrmanage_serviceas  zDistro.manage_serviceblockdevcCstj|}|drjtt|j}tt|j}td@}|D]5}|j drZt |j}t |j rZtt|jj|krZtt|jj|krZtj|j}nq%Wdn1sewY|drxt 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)rPrQrealpath startswithmajorstatst_rdevminorscandirrlstatS_ISBLKst_moder8r)rrrritdeventryresrrrget_mapped_device}s2      zDistro.get_mapped_devicer@)NN)rrN),__name__ __module__ __qualname__pip_package_namerRr&network_conf_fnrNETWORK_FILE_HEADERrenderer_configsdhclient_lease_directorydhclient_lease_file_regexrr!r,r PackageListr2r?rDr4rArIr;rYr^r1r0propertyrqboolrrrrrr staticmethodr classmethodrrr __classcell__rrrrr sZ    "   [ 9 #   r)loggingrPrrrtypingrrr cloudinitrrrr r "cloudinit.distros.parsers.hostnamer cloudinit.settingsr r getLoggerrr8rrrrrrs