
    Jh+.              
          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e      Z" G d dee      Z#de$fdZ%de$dee&ee&ee&   f   f   dee$eejN                     f   fdZ(dee j                      fdZ)de#fdZ*dede#fdZ+ edde+d      Z,ddde#edfgd d!d"d#d$	Z-y)%    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                   H    e Zd Z eded       eded      gZdedefdZy) UnattendedUpgradesDisabledReasonmsgzHuman readable reasondoccodezReason codec                      || _         || _        y Nr   r"   )selfr   r"   s      N/usr/lib/python3/dist-packages/uaclient/api/u/unattended_upgrades/status/v1.py__init__z)UnattendedUpgradesDisabledReason.__init__%   s    	    N)__name__
__module____qualname__r   r   fieldsstrr(    r)   r'   r   r      s5    e_*ABfo=9F
C s r)   r   c                      e Zd Z eded       eded       eded       eded	       ed
 ee      d       eded       ededd       ede	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Zy)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                t    || _         || _        || _        || _        || _        || _        || _        || _        y r$   )r2   r3   r4   r5   r6   r7   r8   r:   )	r&   r2   r3   r4   r5   r6   r7   r8   r:   s	            r'   r(   z'UnattendedUpgradesStatusResult.__init__k   s[     *C&(@%0 	1 / 	/ 0 	0 ,G(/ 	0 -I)r)   N)r*   r+   r,   r   r   r   r   r   r   r   r-   boolintr   r.   r   datetimer(   r/   r)   r'   r1   r1   *   s-   'F	

 	&		
 	2!		
 	0!		
 	1o&!		
 	)*		
 	1,		
 	*;		
q>F@I $(I #'	I
 /2I -0I .2#YI &*I .6,.
I '/x/@/@&AIr)   r1   returnc                      	 t        j                  d      } t        j                  d      }| xr |}|S # t        j                  $ r}t	        t        |            d }~ww xY 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_enabledr2   es       r'   _get_apt_daily_job_statusrG      sp    8 & = =!
 )/(E(E%)
% "C&C 	" %$ ++ 8%A778s   04 A!AA!r2   unattended_upgrades_cfgc                 &   | sdt         j                  fS t        D ]t  }|j                  |      }t	        |t
              r&|s$dt         j                  j                  |      fc S |dk(  s|rRdt         j                  j                  |      fc S  y)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)r2   rH   keyvalues       r'   _is_unattended_upgrades_runningrV      s     %xHHII. '++C0eT"5AAHH  I   C<uAAHH  I  " r)   c                      	 t         j                  j                  t              } t
        j
                  j                  |       S # t        $ r Y y w xY wr$   )ospathgetctimeUNATTENDED_UPGRADES_STAMP_PATHFileNotFoundErrorr>   fromtimestamp)creation_epochs    r'   !_get_unattended_upgrades_last_runr_      sI    ))*HI **>::  s   #A 	AAc                  (    t        t                     S r$   )_statusr   r/   r)   r'   statusrb      s    8:r)   cfgc                    t        j                  d      sNt        ddddg t        t        j
                  j                  t        j
                  j                        dd      S t               }t               }t        t        t        t        d      z               }|d   xs d	|d<   |d
   xs g |d
<   t        ||      \  }}|r"t        |j                  |j                        }nd}t        |t        |j!                  dd            d	k(  t#        |j!                  d      xs d      t#        |j!                  d      xs d      t%        |j!                  d
      xs g       |||      }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)r2   r3   r4   r5   r6   r8   r7   r:   zUnattended-Upgrader   1r    r   r   
raw_config)r   is_installedr1   r   r
   UNATTENDED_UPGRADES_UNINSTALLEDr   namerG   r_   r   setrM   r   rV   r.   rN   r=   rP   meta)rc   r2   r:   rH   r7   disabled_reasonr8   unattended_upgrades_results           r'   ra   ra      s    12-&+%*12/0020P<<@@==BB1 ).)-
 	
 !: ;#D#F 3+!"678	
 	  78?C 34 	  EFL" AB 	(!#:	#
 .N## %%/
+
 /3+!?";!$#''(?D"
 " .1#''5  	.
 ,/#''3  	,
 -1#''(MN -

 0$?%A5"8 (45L&M#%%r)   v1UnattendedUpgradesStatus)versionrj   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).r>   rX   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   rM   r[   r   r1   r<   rG   r.   NamedMessagerV   r_   rb   ra   endpoint_docr/   r)   r'   <module>r      sP    	 5 5 6 6 ( 2 ; C $  #  "G z ^IZ ^IB%4 %"#!#uS$s)^'<"<= 4(//0016;8H4E4E+F ;. _& _&< _&D 	#	 
 3 $g	
 =", o8r)   