Your IP : 216.73.216.31


Current Path : /usr/lib/python3/dist-packages/uaclient/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/uaclient/__pycache__/system.cpython-38.pyc

U

8�-d`D�@s�ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
ddlmZddl
mZmZmZmZmZmZmZddlmZmZmZdZdZdZd	Zd
ZdZdZed
dej fdej fg�Z!edde"fdee"fdee#fdee#fdee#fdee"fdee"fg�Z$e
dd�e$d�dd��Z%e
dd�e"d�dd��Z&e
dd�e"d�dd��Z'e
dd�e"d�d d!��Z(e
dd�ee"e"fd�d"d#��Z)e
dd�e"e*d$�d%d&��Z+e
dd�e*d�d'd(��Z,e
dd�e"e*d$�d)d*��Z-e
dd�e"e*d$�d+d,��Z.e
dd�e*d�d-d.��Z/e
dd�d\e"e*d0�d1d2��Z0e
dd�d]ee"ee"e"fd3�d4d5��Z1e
dd�e"e!d$�d6d7��Z2e"ee"d8�d9d:�Z3d^eee"eee"e*d;�d<d=�Z4e"e*d>�d?d@�Z5d_e"e*e"dB�dCdD�Z6d`e"e#ddF�dGdH�Z7dae"e"e#ddI�dJdK�Z8e"ddL�dMdN�Z9dbee"eee#e*ee:eee"e"fee"e"fdP�dQdR�Z;dcee"eee#e*ee:eee:eee"e"fee"e"fdS�dTdU�Z<e"ddV�dWdX�Z=e"e*dY�dZd[�Z>dS)d�N)�	lru_cache)�rmtree)�Dict�List�
NamedTuple�Optional�Sequence�Set�Tuple)�
exceptions�messages�utilz/var/run/reboot-requiredz/var/run/reboot-required.pkgsz/etc/machine-idz/var/lib/dbus/machine-idz!/usr/share/distro-info/ubuntu.csvz5(?P<release>\d+\.\d+) (LTS\s*)?(\((?P<series>\w+))?.*zd^(?P<major>[\d]+)[.-](?P<minor>[\d]+)[.-](?P<patch>[\d]+)-(?P<abi>[\d]+)-(?P<flavor>[A-Za-z0-9_-]+)$�
DistroInfo�eol�eol_esm�
KernelInfo�
uname_release�proc_version_signature_version�major�minor�patch�abi�flavor)�maxsize)�returnc
Cs�d}ztd�}|��d}Wntk
r:t�d�YnXt��j��}t	�
t|�}|dkr�t�tj
j|d��t||dddddd�St||t|�d��t|�d��t|�d��|�d	�|�d
�d�SdS)Nz/proc/version_signature�z*failed to process /proc/version_signature.)�kernel)rrrrrrrrrrrr)�	load_file�split�	Exception�logging�warning�os�uname�release�strip�re�match�RE_KERNEL_UNAMErZKERNEL_PARSE_ERROR�formatr�int�group)rZproc_version_signature_fullrZuname_match�r,�1/usr/lib/python3/dist-packages/uaclient/system.py�get_kernel_info;s<��
�r.cCstddg�\}}|��S)NZdpkgz--print-architecture)�subpr%)�out�_errr,r,r-�
get_dpkg_arch_sr2cCs6ztdg�\}}|��WStjk
r0YdSXdS)N�systemd-detect-virt�)r/r%r�ProcessExecutionError)r0�_r,r,r-�
get_virt_typees

r7cCs~|jr"|j�di��d�}|r"|S|�d�}tt|fD]*}tj�|�r6t|��	d�}|r6|Sq6t
t���}|�
d|�|S)z�
    Get system's unique machine-id or create our own in data_dir.
    We first check for the machine-id in machine-token.json before
    looking at the system file.
    ZmachineTokenInfoZ	machineIdz
machine-id�
)Z
machine_token�getZ	data_path�ETC_MACHINE_ID�DBUS_MACHINE_IDr"�path�existsr�rstrip�str�uuidZuuid4Zwrite_cache)ZcfgZcfg_machine_idZfallback_machine_id_filer<�contentZ
machine_idr,r,r-�get_machine_idns�

rBcCs�t�}|�dd�dd�}t�dd|�dd��}||d	<|�d
d�}|�dd�}|rV|s�t�t|�}|s|tj|�dd�|d��|��}|p�|�d
d�}|s�tj	|d��|p�|�dd�}|�
||��t�j
t�t�d��|S)z�
    Returns a dict of platform information.

    N.B. This dict is sent to the contract server, which requires the
    distribution, type and release keys.
    �NAMEZUNKNOWNZLinux)Zdistribution�typez\.\d LTSz LTSZVERSIONr4�versionZVERSION_CODENAMEZ
VERSION_ID)Zorig_verZmod_ver�series)rEr$)r$rFrZarchZvirt)�parse_os_releaser9r&�subr'�REGEX_OS_RELEASE_VERSIONrZParsingErrorOnOSReleaseFile�	groupdictZMissingSeriesOnOSReleaseFile�update�lowerr.rr2r7)Z
os_releaseZ
platform_inforErFr$r'Z
match_dictr,r,r-�get_platform_info�s:
�
���
rM)rFrcCstddg�\}}||kS)N�/usr/bin/ubuntu-distro-infoz--supported-esm�r/�rFr0r1r,r,r-�is_lts�srQcCstt�d�S�NrF)rQrMr,r,r,r-�is_current_series_lts�srScCstddg�\}}||kS)NrNz--supportedrOrPr,r,r-�is_supported�srTcCs,t|�sdStdd|dg�\}}t|�dkS)zCReturn True when Ubuntu series supports ESM and is actively in ESM.FrNz--seriesz-yeolr)rQr/r*rPr,r,r-�
is_active_esm�s
�rUcCstt�d�SrR)rUrMr,r,r,r-�is_current_series_active_esm�srV�/run)�run_pathrc	Cs�ztdg�WdStjk
r&YnXztdddg�WdSttfk
rTYnXdD]$}tj�||�}tj�|�rZdSqZdS)z>Checks to see if this code running in a container of some sortZischrootFr3z--quietz--containerT)Zcontainer_typezsystemd/container)	r/rr5�IOError�OSErrorr"r<�joinr=)rX�filenamer<r,r,r-�is_container�s
r])�release_filercCsH|sd}i}t|���D]*}|�dd�\}}|r|���d�||<q|S)Nz/etc/os-release�=r�")r�
splitlinesrr%)r^�data�line�key�valuer,r,r-rG�srGcCs�ztt���}Wn tk
r0t�tj��YnX|D]t}|�d�}|d|kr6|dkr^d}nd|dkrr|dn|d}t	t
j
�|dd	���t
j
�|d	���d
�Sq6t�tj
�|���dS)N�,�Zxenialz
2026-04-23ZLTSr��z%Y-%m-%d)rr)r�DISTRO_INFO_CSVra�FileNotFoundErrorrZUserFacingErrorrZMISSING_DISTRO_INFO_FILErr�datetime�strptime�dateZ"MISSING_SERIES_IN_DISTRO_INFO_FILEr))rF�linesrc�valuesrr,r,r-�get_distro_info�s"
�
�rq)�programrcCsrtjj|krt|�r|Sdd�tj�dd��tj�D�}dd�|D�}|D]"}tj�||�}t|�rJ|SqJdS)z;Find whether the provided program is executable in our PATHcSsg|]}|�d��qS)r`)r%��.0�pr,r,r-�
<listcomp>szwhich.<locals>.<listcomp>�PATHr4cSsg|]}tj�|��qSr,)r"r<�abspathrsr,r,r-rvsN)	r"r<�sep�is_exe�environr9r�pathsepr[)rr�pathsZnormalized_pathsr<Zprogram_pathr,r,r-�whichs�
r~)�installed_pkgs�installed_pkgs_regexrcCs�tj�t�sdS|dkr$|dkr$dSzttt��d��}Wntk
rPYdSX|dk	rpt	|�
|��dkrpdS|dk	r�|D]"}|D]}t�||�r�dSq�q|dS)a�Check if the system needs to be rebooted.

    :param installed_pkgs: If provided, verify if the any packages in
        the list are present on /var/run/reboot-required.pkgs. If that
        param is provided, we will only return true if we have the
        reboot-required marker file and any package in reboot-required.pkgs
        file. When both installed_pkgs and installed_pkgs_regex are
        provided, they act as an OR, so only one of the two lists must have
        a match to return True.
    :param installed_pkgs_regex: If provided, verify if the any regex in
        the list matches any line in /var/run/reboot-required.pkgs. If that
        param is provided, we will only return true if we have the
        reboot-required marker file and any match in reboot-required.pkgs
        file. When both installed_pkgs and installed_pkgs_regex are
        provided, they act as an OR, so only one of the two lists must have
        a match to return True.
    FNTr8r)
r"r<r=�REBOOT_FILE_CHECK_PATH�setr�REBOOT_PKGS_FILE_PATHrrk�len�intersectionr&�search)rr�Zreboot_required_pkgs�pkg_nameZ	pkg_regexr,r,r-�
should_reboot's&�r�)r<rcCstj�|�ot�|tj�S)N)r"r<�isfile�access�X_OK)r<r,r,r-rz]srzT)r\�decoderc	Cs4t�d|�t|d��}|��}W5QRX|�d�S)z!Read filename and decode content.zReading file: %s�rb�utf-8)r �debug�open�readr�)r\r��streamrAr,r,r-rbsr�)r\�modercCs@t�d|�tjtj�|�dd�t�|���t�	||�dS)NzCreating file: %sT��exist_ok)
r r�r"�makedirsr<�dirname�pathlib�PathZtouch�chmod)r\r�r,r,r-�create_filejsr�)r\rAr�rc
Cs�d}z~tjtj�|�dd�tjddtj�|�d�}t�d||j�|�	|�
d��|��|��t�
|j|�t�|j|�Wn:tk
r�}z|dk	r�t�|j�|�W5d}~XYnXdS)	z�Write content to the provided filename encoding it if necessary.

    @param filename: The full path of the file to write.
    @param content: The content to write to the file.
    @param mode: The filesystem mode to set on the file.
    NTr��wbF)r��delete�dirz*Writing file %s atomically via tempfile %sr�)r"r�r<r��tempfileZNamedTemporaryFiler r��name�write�encode�flush�closer��renamer�unlink)r\rAr�Ztmpf�er,r,r-�
write_fileqs,
��r�)�	file_pathrcCs&tj�|�r"t�d|�t�|�dS)z<Remove a file if it exists, logging a message about removal.zRemoving file: %sN)r"r<r=r r�r�)r�r,r,r-�ensure_file_absent�sr�F)�args�rcs�capture�timeout�envrc
Csdd�|D�}|r|�tj�|dkr,dg}t�d�|��}z*tj|tjtj|d�}|j	|d�\}}	WnZt
k
r�z$tj||j
|�d�|	�d�d	��Wn tk
r�tj|d
��YnXYnX|j
|kr�tj||j
|�d�|	�d�d	��|�rt�d||j
|	�|�d�|	�d�fS)a�Run a command and return a tuple of decoded stdout, stderr.

    @param args: A list of arguments to feed to subprocess.Popen
    @param rcs: A list of allowed return_codes. If returncode not in rcs
        raise a ProcessExecutionError.
    @param capture: Boolean set True to log the command and response.
    @param timeout: Optional float indicating number of seconds to wait for
        subp to return.
    @param env: Optional dictionary of environment variable to pass to Popen.

    @return: Tuple of utf-8 decoded stdout, stderr
    @raises ProcessExecutionError on invalid command or returncode not in rcs.
    @raises subprocess.TimeoutError when timeout specified and the command
        exceeds that number of seconds.
    cSs$g|]}t|t�r|n|�d��qS)r�)�
isinstance�bytesr�)rt�xr,r,r-rv�sz_subp.<locals>.<listcomp>Nr� )�stdout�stderrr�)r�r�)�cmdZ	exit_coder�r�)r�zRan cmd: %s, rc: %s stderr: %s)rKr"r{r
Zredact_sensitive_logsr[�
subprocess�Popen�PIPEZcommunicaterZrr5�
returncoder��UnboundLocalErrorr r�)
r�r�r�r�r�Z
bytes_argsZredacted_cmd�procr0�errr,r,r-�_subp�sR���

��r�)r�r�r�r��retry_sleepsr�rc
Cs�|dk	r|��nd}zt|||||d�\}}Wq�Wqtjk
r�}zd|rrt�t|��d�|j|j	�}	t�
|	�|sx�dt|�}
t�t|�|
�t�
|�d��W5d}~XYqXq||fS)a�Run a command and return a tuple of decoded stdout, stderr.

     @param subp: A list of arguments to feed to subprocess.Popen
     @param rcs: A list of allowed return_codes. If returncode not in rcs
         raise a ProcessExecutionError.
     @param capture: Boolean set True to log the command and response.
     @param timeout: Optional float indicating number of seconds to wait for a
         subp call to return.
     @param retry_sleeps: Optional list of sleep lengths to apply between
        retries. Specifying a list of [0.5, 1] instructs subp to retry twice
        on failure; sleeping half a second before the first retry and 1 second
        before the next retry.
     @param env: Optional dictionary of environment variables to provide to
        subp.

    @return: Tuple of utf-8 decoded stdout, stderr
    @raises ProcessExecutionError on invalid command or returncode not in rcs.
    @raises subprocess.TimeoutError when timeout specified and the command
        exceeds that number of seconds.
    N)r�zStderr: {}
Stdout: {}z Retrying %d more times.r)�copyr�rr5r r�r?r)r�r�r!r��time�sleep�pop)r�r�r�r�r�r�r0r�r��msgZ	retry_msgr,r,r-r/�s
$r/)�folder_pathrcCs$tj�|�r t�d|�t|�dS)NzRemoving folder: %s)r"r<r=r r�r)r�r,r,r-�ensure_folder_absent�sr�)�job_namerc
CsVztdd|g�\}}Wn*tjk
r@}z
|j}W5d}~XYnX|sJdS|��dkS)aZ
    Get if the systemd job is active in the system. Note that any status
    different from "active" will make this function return False.
    Additionally, if the system doesn't exist we will also return False
    here.

    @param job_name: Name of the systemd job to look at

    @return: A Boolean specifying if the job is active or not
    Z	systemctlz	is-activeNFZactive)r/rr5r�r%)r�r0r6r�r,r,r-�get_systemd_job_statesr�)rW)N)NN)T)r�)r�)NFNN)NFNNN)?rlr r"r�r&r�r�r�r@�	functoolsrZshutilr�typingrrrrrr	r
Zuaclientrrr
r�r�r:r;rjrIr(rnrr?r*rr.r2r7rBrM�boolrQrSrTrUrVr]rGrqr~r�rzrr�r�r��floatr�r/r�r�r,r,r,r-�<module>s�$���





��#-
 �

�6	�

�@�


�.