U
    lHJeb                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlmZ d dlmZ d dlmZmZmZmZmZmZmZ d dlmZmZmZ dZdZdZd	Zd
ZddiZ e!e"e#Z$dZ%dZ&edde j'fde j'fgZ(edde)fde)fdee j  fdee) fdee* fdee* fdee* fdee) fdee) fg	Z+edde)fde)fde)fd e)fgZ,ed!d"e)fd#ee* fd$ee* fgZ-ed%d&eee)  fd'eee)  fgZ.d(Z/ej0ee j  d)d*d+Z1ej0ee j  d)d,d-Z2edd.e+d/d0d1Z3d2d3 Z4edd.e)d/d4d5Z5edd.e)d/d6d7Z6edd.e-d/d8d9Z7edd.e)d/d:d;Z8edd.e,d/d<d=Z9edd.e)e:d>d?d@Z;edd.e:d/dAdBZ<edd.e)e:d>dCdDZ=edd.e)e:d>dEdFZ>edd.e:d/dGdHZ?edd.d{e)e:dJdKdLZ@edd.e:d/dMdNZAedd.ee)e)f d/dOdPZBedd.e)e(d>dQdRZCe)ee) dSdTdUZDd|eee)  eee)  e:dVdWdXZEe)e:dYdZd[ZFd}e)e:e)d]d^d_ZGd~e)e*ddadbdcZHde)e)ee* ddddedfZIe)ddgdhdiZJdee) eee*  e:eeK eee)e)f  e:ee)e)f dkdldmZLdee) eee*  e:eeK eeeK  eee)e)f  e:ee)e)f dndodpZMe)ddqdrdsZNe)e:dtdudvZOe)d/dwdxZPee. d/dydzZQdS )    N)	lru_cache)rmtree)DictList
NamedTupleOptionalSequenceSetTuple)defaults
exceptionsutilz/var/run/reboot-requiredz/var/run/reboot-required.pkgsz/etc/machine-idz/var/lib/dbus/machine-idz!/usr/share/distro-info/ubuntu.csvZGenuineIntelZintelz5(?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_machine_archuname_release
build_dateproc_version_signature_versionmajorminorpatchabiflavorReleaseInfodistributionreleaseseriespretty_versionCpuInfo	vendor_idmodelsteppingRebootRequiredPkgsstandard_packageskernel_packagesz(Mon|Tue|Wed|Thu|Fri|Sat|Sun).*)unamereturnc                 C   sl   t  rtd d S td z(td| j}tj|j	tj
jW S  tk
rf   td Y d S X d S )NzPNot attempting to use timestamp of kernel changelog because we're in a containerz3Falling back to using timestamp of kernel changelogz1/usr/share/doc/linux-image-{}/changelog.Debian.gzzUnable to stat kernel changelog)is_containerLOGwarningosstatformatr   datetimeZfromtimestampst_mtimetimezoneutc	Exception)r'   stat_result r5   1/usr/lib/python3/dist-packages/uaclient/system.py_get_kernel_changelog_timestamp_   s&    
 
r7   c                 C   s   t t| j}|d kr(td t| S |d}ztj	|d}W n& t
k
rj   td t|  Y S X |jd kr|jtjjd}|S )Nz*Unable to find build date in uname versionr   z%a %b %d %H:%M:%S %Z %Yz-Unable to parse build date from uname version)tzinfo)researchRE_KERNEL_EXTRACT_BUILD_DATEversionr*   r+   r7   groupr/   strptime
ValueErrorr8   replacer1   r2   )r'   Z
date_matchZdate_strZdtr5   r5   r6   _get_kernel_build_datex   s    



rA   )maxsize)r(   c                  C   s   d } zt d}| d } W n tk
r:   td Y nX t }|j }t	|}|j
 }tt|}|d krtd| t|||| d d d d d d	S t|||| t|dt|dt|d|d	|d
d	S d S )Nz/proc/version_signature   z*failed to process /proc/version_signature.zFailed to parse kernel: %s)	r   r   r   r   r   r   r   r   r   r   r   r   r   r   )	load_filesplitr3   r*   r+   r,   r'   machinestriprA   r   r9   matchRE_KERNEL_UNAMEr   intr=   )r   Zproc_version_signature_fullr'   r   r   r   Zuname_matchr5   r5   r6   get_kernel_info   sF    

rK   c                     sn   ddl m}  t stddd |  D }dd tdD }dd |D  d	d |D } fd
d|D S )Nr   )get_installed_packages_namesz9get_installed_ubuntu_kernels needs to be executed as rootc                 S   s   g | ]}d |kr|qS )linux-image-r5   ).0packager5   r5   r6   
<listcomp>   s   z0get_installed_ubuntu_kernels.<locals>.<listcomp>c                 S   s$   g | ]}d t d|gd kr|qS )zLinux kernelfiler   subp)rN   rQ   r5   r5   r6   rP      s   z/boot/vmlinu[x|z]-*c                 S   s   g | ]}|t d d qS )rM   Nlen)rN   Zpackage_namer5   r5   r6   rP      s    c                 S   s   g | ]}|t d d qS )z/boot/vmlinu?-NrT   )rN   	file_namer5   r5   r6   rP      s   c                    s   g | ]}| kr|qS r5   r5   )rN   r<   Zlinux_image_versionsr5   r6   rP      s   )Zuaclient.aptrL   r   we_are_currently_rootRuntimeErrorglob)rL   Zlinux_imageZvmlinux_kernel_filesZvmlinuz_versionsr5   rW   r6   get_installed_ubuntu_kernels   s(    
r[   c                  C   s   t ddg\} }|  S )NZdpkgz--print-architecture)rS   rG   )out_errr5   r5   r6   get_dpkg_arch   s    r^   c                  C   s   zt dg\} }|  W S  tjk
r   z<td}d|ksDd|krLW Y dS d|kr\W Y dS W Y dS W n tk
r~   Y Y dS X Y nX d S )Nsystemd-detect-virtz/proc/1/cgroupZdockerZbuildkitZbuildahZpodman )rS   rG   r   ProcessExecutionErrorrD   r3   )r\   _Zproc_1_cgroupr5   r5   r6   get_virt_type   s    
rc   c                  C   s   t d} i }dD ]0}td|| tj}|r|d}|||< q|dd}|d}|d}tt|||rzt	|nd |rt	|nd dS )	Nz/proc/cpuinfo)r!   r"   r#   z^{}\s*:\s*(?P<info>\w*)infor!   r`   r"   r#   )
rD   r9   r:   r.   	MULTILINEr=   getr    CPU_VENDOR_MAPrJ   )Zcpu_info_contentZcpu_info_valuesZfieldZ	cpu_matchvalueZvendor_id_baser"   r#   r5   r5   r6   get_cpu_info   s&    




ri   c                 C   s~   | j r"| j di d}|r"|S | d}tt|fD ]*}tj|r6t|	d}|r6|  S q6t
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.
    ZmachineTokenInfoZ	machineIdz
machine-id
)Zmachine_tokenrf   Z	data_pathETC_MACHINE_IDDBUS_MACHINE_IDr,   pathexistsrD   rstripstruuidZuuid4Zwrite_cache)ZcfgZcfg_machine_idZfallback_machine_id_filerm   contentZ
machine_idr5   r5   r6   get_machine_id  s    

rs   c                  C   s   t  } | dd}tdd| dd}| dd}| dd}|rH|stt|}|sntj| dd|d	| }|p|d
d}|stj	|d|p|dd}t
||| |dS )NNAMEZUNKNOWNz\.\d LTSz LTSZVERSIONr`   ZVERSION_CODENAMEZ
VERSION_ID)Zorig_verZmod_verr   )r<   r   )r   r   r   r   )_parse_os_releaserf   r9   subrH   REGEX_OS_RELEASE_VERSIONr   ZParsingErrorOnOSReleaseFile	groupdictZMissingSeriesOnOSReleaseFiler   lower)Z
os_releaser   r   r   r   rH   Z
match_dictr5   r5   r6   get_release_info)  s2    
 rz   )r   r(   c                 C   s   t ddg\}}| |kS )N/usr/bin/ubuntu-distro-infoz--supported-esmrR   r   r\   r]   r5   r5   r6   is_ltsI  s    r}   c                   C   s   t t jS N)r}   rz   r   r5   r5   r5   r6   is_current_series_ltsO  s    r   c                 C   s   t ddg\}}| |kS )Nr{   z--supportedrR   r|   r5   r5   r6   is_supportedT  s    r   c                 C   s,   t | sdS tdd| dg\}}t|dkS )zCReturn True when Ubuntu series supports ESM and is actively in ESM.Fr{   z--seriesz-yeolr   )r}   rS   rJ   r|   r5   r5   r6   is_active_esmZ  s    
r   c                   C   s   t t jS r~   )r   rz   r   r5   r5   r5   r6   is_current_series_active_esme  s    r   /run)run_pathr(   c              	   C   s   zt dg W dS  tjk
r&   Y nX zt dddg W dS  ttfk
rT   Y nX dD ]$}tj| |}tj|rZ dS qZdS )z>Checks to see if this code running in a container of some sortZischrootFr_   --quietz--containerT)Zcontainer_typezsystemd/container)	rS   r   ra   IOErrorOSErrorr,   rm   joinrn   )r   filenamerm   r5   r5   r6   r)   j  s    
r)   c                  C   s.   ddl m}  |  D ]}d|jkr dS qdS )zReturns True if any package installed has "ubuntu-desktop" in the name.

    This includes ubuntu-desktop, ubuntu-desktop-minimal, kubuntu-desktop, etc.
    r   )aptzubuntu-desktopTF)uaclientr   Zget_installed_packagesname)r   rO   r5   r5   r6   
is_desktop  s
    
r   c                  C   sf   zt d} W n tk
r(   t d} Y nX i }|  D ]*}|dd\}}|r6| d||< q6|S )Nz/etc/os-releasez/usr/lib/os-release=rC   ")rD   FileNotFoundError
splitlinesrE   rG   )Zfile_contentsdatalinekeyrh   r5   r5   r6   ru     s    ru   c                 C   s   zt t }W n tk
r,   t Y nX |D ]t}|d}|d | kr2| dkrZd}nd|d krn|d n|d }ttj	|d d	
 tj	|d	
 d
  S q2tj| dd S )N,   Zxenialz
2026-04-23ZLTSr         z%Y-%m-%d)r   r   )r   )rD   DISTRO_INFO_CSVr   r   r   ZMissingDistroInfoFilerE   r   r/   r>   dateZMissingSeriesInDistroInfoFile)r   linesr   valuesr   r5   r5   r6   get_distro_info  s    
r   )programr(   c                 C   sr   t jj| krt| r| S dd t jddt jD }dd |D }|D ]"}t j|| }t|rJ|  S qJdS )z;Find whether the provided program is executable in our PATHc                 S   s   g | ]}| d qS )r   )rG   rN   pr5   r5   r6   rP     s    zwhich.<locals>.<listcomp>PATHr`   c                 S   s   g | ]}t j|qS r5   )r,   rm   abspathr   r5   r5   r6   rP     s     N)	r,   rm   sepis_exeenvironrf   rE   pathsepr   )r   pathsZnormalized_pathsrm   Zprogram_pathr5   r5   r6   which  s    
r   )installed_pkgsinstalled_pkgs_regexr(   c                 C   s   t jtsdS | dkr$|dkr$dS ztttd}W n tk
rP   Y dS X | dk	rpt	| 
|dkrpdS |dk	r|D ]"}|D ]}t||r  dS qq|dS )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.
    FNTrj   r   )r,   rm   rn   REBOOT_FILE_CHECK_PATHsetrD   REBOOT_PKGS_FILE_PATHrE   r   rU   intersectionr9   r:   )r   r   Zreboot_required_pkgspkg_nameZ	pkg_regexr5   r5   r6   should_reboot  s&    r   )rm   r(   c                 C   s   t j| ot | t jS r~   )r,   rm   isfileaccessX_OK)rm   r5   r5   r6   r     s    r   T)r   decoder(   c              	   C   s4   t | d}td|  | }W 5 Q R X |dS )z!Read filename and decode content.rbzReading file: %sutf-8)openr*   debugreadr   )r   r   streamrr   r5   r5   r6   rD     s    rD     )r   moder(   c                 C   s@   t d|  tjtj| dd t|   t	| | d S )NzCreating file: %sTexist_ok)
r*   r   r,   makedirsrm   dirnamepathlibPathZtouchchmod)r   r   r5   r5   r6   create_file
  s    r   )r   rr   r   r(   c              
   C   s"  d}t j| }|r<t|  }t|j}|dkrH|}n|dkrHd}zt jt j	| dd t
jddt j	| d}td| |j ||d	 |  |  t |j| |rt |j|j|j t |j|  W n> tk
r } z|dk	rt |j |W 5 d}~X Y nX dS )
a_  Write content to the provided filename encoding it if necessary.

    We preserve the file ownership and permissions if the file is present
    and no mode argument is provided.

    @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.
    Nr   Tr   wbF)r   deletedirz*Writing file %s atomically via tempfile %sr   )r,   rm   r   r   r   r-   S_IMODEst_moder   r   tempfileZNamedTemporaryFiler*   r   r   writeencodeflushcloser   chownst_uidst_gidrenamer3   unlink)r   rr   r   ZtmpfZis_file_presentZ	file_statZf_modeer5   r5   r6   
write_file  s@      
  
r   )	file_pathr(   c                 C   s@   zt |  td|  W n  tk
r:   td|  Y nX dS )z<Remove a file if it exists, logging a message about removal.zRemoved file: %sz*Tried to remove %s but file does not existN)r,   r   r*   r   r   )r   r5   r5   r6   ensure_file_absent<  s
    
r   F)argsrcscapturetimeoutoverride_env_varspipe_stdouterrr(   c                 C   sV  dd | D }d}d}|r&t j}t j}d}	|r8tj|}	|dkrFdg}td| }
z&t j||||	d}|j|d\}}W nr t	k
r   z<|r|
dnd	}|r|
dnd	}tj|
|j||d
W n  tk
r   tj|
dY nX Y nX |r|
dnd	}|r|
dnd	}|j|kr6tj|
|j||d
|rNtd|
|j| ||fS )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 override_env_vars: Optional dictionary of environment variables.
        If None, the current os.environ is used for the subprocess.
        If defined, these env vars get merged with the current process'
        os.environ for the subprocess, overriding any values that already
        existed in os.environ.

    @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.
    c                 S   s$   g | ]}t |tr|n|d qS )r   )
isinstancebytesr   )rN   xr5   r5   r6   rP   `  s    z_subp.<locals>.<listcomp>Nr    )stdoutstderrenv)r   r   r`   )cmdZ	exit_coder   r   )r   zRan cmd: %s, rc: %s stderr: %s)
subprocessPIPEr,   r   r   Zredact_sensitive_logsr   PopenZcommunicater   r   r   ra   
returncodeUnboundLocalErrorr*   r   )r   r   r   r   r   r   Z
bytes_argsr   r   Z
merged_envZredacted_cmdprocr\   errZ
out_resultZ
err_resultr5   r5   r6   _subpE  sf    

r   )r   r   r   r   retry_sleepsr   r   r(   c           
   
   C   s   |dk	r|  nd}z t| |||||d\}}W qW q tjk
r }	 z\|rltt|	 td|	j|	j	 |sr tt|	 tdt
| t|d W 5 d}	~	X Y qX q||fS )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 override_env_vars: Optional dictionary of environment variables.
        If None, the current os.environ is used for the subprocess.
        If defined, these env vars get merged with the current process'
        os.environ for the subprocess, overriding any values that already
        existed in os.environ.

    @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   r   zStderr: %s
Stdout: %szRetrying %d more times.r   )copyr   r   ra   r*   r   rp   r+   r   r   rU   timesleeppop)
r   r   r   r   r   r   r   r\   r   r   r5   r5   r6   rS     s*     
$rS   )folder_pathr(   c                 C   s>   zt |  td|  W n  tk
r8   td|  Y nX d S )NzRemoved folder: %sz,Tried to remove %s but folder does not exist)r   r*   r   r   )r   r5   r5   r6   ensure_folder_absent  s
    r   )service_namer(   c                 C   s2   zt ddd| g W n tjk
r,   Y dS X dS )a^  
    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 service_name: Name of the systemd job to look at

    @return: A Boolean specifying if the job is active or not
    Z	systemctlz	is-activer   FT)rS   r   ra   )r   r5   r5   r6   is_systemd_unit_active  s
    r   c                  C   sB   t  rtjS tjd} | r,| d tj S tj	dd tj S )NZXDG_CACHE_HOME/~z/.cache/)
r   rX   r   ZUAC_RUN_PATHr,   r   rf   ZUSER_CACHE_SUBDIRrm   
expanduser)Zxdg_cache_homer5   r5   r6   get_user_cache_dir  s    r   c                  C   st   zt t} W n tk
r"   Y d S X g }g }d}|  D ]&}t||rT|| q8|| q8tt|t|dS )Nz^(linux-image|linux-base).*)r%   r&   )	rD   r   r   rE   r9   rH   appendr$   sorted)Zpkg_list_strr%   r&   Zkernel_regexpkgr5   r5   r6   get_reboot_required_pkgs  s    r   )r   )NN)T)r   )N)NFNNT)NFNNNT)Rr/   rZ   Zloggingr,   r   r9   r-   r   r   r   rq   	functoolsr   Zshutilr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   r   rk   rl   r   rg   Z	getLoggerZreplace_top_level_logger_name__name__r*   rw   rI   r   r   rp   rJ   r   r   r    r$   r;   uname_resultr7   rA   rK   r[   r^   rc   ri   rs   rz   boolr}   r   r   r   r   r)   r   ru   r   r   r   r   rD   r   r   r   floatr   rS   r   r   r   r   r5   r5   r5   r6   <module>   s4  $ 





	

		+#
  

6   +     

Y      


8