o
    ÙKÕi+.  ã                
   @   s‚  d dl Z d dlZd dlmZmZmZmZmZ d dlm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZ d dlmZmZ d dlmZ d d	lmZmZmZmZmZmZmZ g d
¢Z dZ!G dd„ deƒZ"G dd„ deeƒZ#de$fdd„Z%de$dee&ee&ee& f f dee$eej' f fdd„Z(dee j  fdd„Z)de#fdd„Z*dede#fdd„Z+edde+dd �Z,d!d"d#e#ed$fgd%d&d'd(d)œ	Z-dS )*é    N)ÚDictÚListÚOptionalÚTupleÚUnion)ÚaptÚ
exceptionsÚmessagesÚsystem)ÚAPIEndpoint)ÚAdditionalInfo)ÚUnattendedUpgradesError)Úget_apt_config_keysÚget_apt_config_values)ÚUAConfig)ÚBoolDataValueÚ
DataObjectÚDatetimeDataValueÚFieldÚIntDataValueÚStringDataValueÚ	data_list)úAPT::Periodic::Enableú#APT::Periodic::Update-Package-Listsú!APT::Periodic::Unattended-Upgradeú#Unattended-Upgrade::Allowed-Originsz#/var/lib/apt/periodic/upgrade-stampc                   @   s:   e Zd Zededd�ededd�gZdedefdd„ZdS )	Ú UnattendedUpgradesDisabledReasonÚmsgzHuman readable reason©ÚdocÚcodezReason codec                 C   s   || _ || _d S ©N©r   r    )Úselfr   r    © r$   úN/usr/lib/python3/dist-packages/uaclient/api/u/unattended_upgrades/status/v1.pyÚ__init__%   s   
z)UnattendedUpgradesDisabledReason.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r   ÚfieldsÚstrr&   r$   r$   r$   r%   r      s
    þr   c                   @   s°   e Zd Zededd�ededd�ededd�eded	d�ed
eeƒdd�ededd�ededdd�ede	ddd�gZ
dedededed
ee dedee deej fdd„ZdS )ÚUnattendedUpgradesStatusResultÚsystemd_apt_timer_enabledz4Indicate if the ``apt-daily.timer`` jobs are enabledr   Úapt_periodic_job_enabledzFIndicate if the ``APT::Periodic::Enabled`` configuration is turned offÚ$package_lists_refresh_frequency_dayszFThe value of the ``APT::Periodic::Update-Package-Lists`` configurationÚ"unattended_upgrades_frequency_dayszDThe value of the ``APT::Periodic::Unattended-Upgrade`` configurationÚ#unattended_upgrades_allowed_originszFThe value of the ``Unattended-Upgrade::Allowed-Origins`` configurationÚunattended_upgrades_runningzRIndicate if the ``unattended-upgrade`` service is correctly configured and runningÚ#unattended_upgrades_disabled_reasonFzyObject that explains why ``unattended-upgrades`` is not running -- if the application is running, the object will be null)Úrequiredr   Úunattended_upgrades_last_runz)The last time ``unattended-upgrades`` ranc          	      C   s4   || _ || _|| _|| _|| _|| _|| _|| _d S r!   )r-   r.   r/   r0   r1   r2   r3   r5   )	r#   r-   r.   r/   r0   r1   r2   r3   r5   r$   r$   r%   r&   k   s   ÿÿÿÿ
z'UnattendedUpgradesStatusResult.__init__N)r'   r(   r)   r   r   r   r   r   r   r   r*   ÚboolÚintr   r+   r   Údatetimer&   r$   r$   r$   r%   r,   *   s~    ýüüüüüû
üÈ@ýüûúùø	ÿ÷ôr,   Úreturnc               
   C   sL   zt  d¡} t  d¡}| o|}W |S  tjy% } ztt|ƒd�‚d }~ww )Nzapt-daily.timerzapt-daily-upgrade.timer)Ú	error_msg)r
   Úis_systemd_unit_activer   ÚProcessExecutionErrorr   r+   )Úapt_daily_job_enabledÚapt_daily_upgrade_job_enabledr-   Úer$   r$   r%   Ú_get_apt_daily_job_status‹   s   ÿÿÿý€ÿr@   r-   Úunattended_upgrades_cfgc                 C   sl   | sdt jfS tD ]*}| |¡}t|tƒr"|s"dt jj|d�f  S |dks(|s3dt jj|d�f  S q	dS )NF)Úcfg_nameÚ0)TN)	r	   Ú(UNATTENDED_UPGRADES_SYSTEMD_JOB_DISABLEDÚUNATTENDED_UPGRADES_CONFIG_KEYSÚgetÚ
isinstanceÚlistÚ(UNATTENDED_UPGRADES_CFG_LIST_VALUE_EMPTYÚformatÚ(UNATTENDED_UPGRADES_CFG_VALUE_TURNED_OFF)r-   rA   ÚkeyÚvaluer$   r$   r%   Ú_is_unattended_upgrades_runningœ   s$   

ÿþÿþÿrN   c                  C   s2   zt j t¡} W n
 ty   Y d S w tj | ¡S r!   )ÚosÚpathÚgetctimeÚUNATTENDED_UPGRADES_STAMP_PATHÚFileNotFoundErrorr8   Úfromtimestamp)Úcreation_epochr$   r$   r%   Ú!_get_unattended_upgrades_last_run·   s   ÿrV   c                   C   s
   t tƒ ƒS r!   )Ú_statusr   r$   r$   r$   r%   ÚstatusÀ   s   
rX   Úcfgc              
   C   sú   t  d¡stddddg ttjjtjjd�ddd�S tƒ }t	ƒ }t
tttdƒ ƒƒ}|d p.d	|d< |d
 p6g |d
< t||ƒ\}}|rKt|j|jd�}nd}t|t| dd¡ƒd	kt| d¡p_dƒt| d¡pgdƒt| d
¡pog ƒ|||d�}d|i|_|S )aù  
    This endpoint returns the status around ``unattended-upgrades``. The focus
    of the endpoint is to verify if the application is running and how it is
    configured on the machine.

    .. important::

        For this endpoint, we deliver a unique key under ``meta`` called
        ``raw_config``. This field contains all related ``unattended-upgrades``
        configurations, unparsed. This means that this field will maintain both
        original name and values for those configurations.
    zunattended-upgradesFr   r"   N)r-   r.   r/   r0   r1   r3   r2   r5   zUnattended-Upgrader   Ú1r   Ú r   r   Ú
raw_config)r   Úis_installedr,   r   r	   ÚUNATTENDED_UPGRADES_UNINSTALLEDr   Únamer@   rV   r   ÚsetrE   r   rN   r+   rF   r7   rH   Úmeta)rY   r-   r5   rA   r2   Údisabled_reasonr3   Úunattended_upgrades_resultr$   r$   r%   rW   Ä   sˆ   
þõÿÿÿ
ÿ
ÿÿýþ
ÿýÿüÿü
þæ
rW   Úv1ÚUnattendedUpgradesStatus)Úversionr_   ÚfnÚoptions_clsz27.14FzT
from uaclient.api.u.unattended_upgrades.status.v1 import status

result = status()
zYRaised if we cannot run a necessary command to show the status of ``unattended-upgrades``z'pro api u.unattended_upgrades.status.v1a@  
{
    "apt_periodic_job_enabled": true,
    "package_lists_refresh_frequency_days": 1,
    "systemd_apt_timer_enabled": true,
    "unattended_upgrades_allowed_origins": [
        "${distro_id}:${distro_codename}",
        "${distro_id}:${distro_codename}-security",
        "${distro_id}ESMApps:${distro_codename}-apps-security",
        "${distro_id}ESM:${distro_codename}-infra-security"
    ],
    "unattended_upgrades_disabled_reason": null,
    "unattended_upgrades_frequency_days": 1,
    "unattended_upgrades_last_run": null,
    "unattended_upgrades_running": true
}
aÈ  
- Possible attributes in JSON ``meta`` field:

  .. code-block:: json

     {
         "meta": {
             "environment_vars": [],
             "raw_config": {
                 "APT::Periodic::Enable": "1",
                 "APT::Periodic::Unattended-Upgrade": "1",
                 "APT::Periodic::Update-Package-Lists": "1",
                 "Unattended-Upgrade::Allowed-Origins": [
                     "${distro_id}:${distro_codename}",
                     "${distro_id}:${distro_codename}-security",
                     "${distro_id}ESMApps:${distro_codename}-apps-security",
                     "${distro_id}ESM:${distro_codename}-infra-security"
                 ]
             }
         }
     }
é   )	Úintroduced_inÚrequires_networkÚexample_pythonÚresult_classr   Úexample_cliÚexample_jsonÚextraÚextra_indent).r8   rO   Útypingr   r   r   r   r   Úuaclientr   r   r	   r
   Úuaclient.api.apir   Úuaclient.api.data_typesr   Úuaclient.api.exceptionsr   Úuaclient.aptr   r   Úuaclient.configr   Úuaclient.data_typesr   r   r   r   r   r   r   rE   rR   r   r,   r6   r@   r+   ÚNamedMessagerN   rV   rX   rW   ÚendpointÚ_docr$   r$   r$   r%   Ú<module>   sX    $
aÿþ
ý	büþÿ
É