
    7j                        d Z ddlmZ ddlZddl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mZmZ dd	lmZ  ed
      ZdZdZddZddZddZdddZy)u  Full discovery: the complete read-only inventory.

Orchestrates the individual discovery modules, writes the snapshot to
/var/lib/wolfpanel/discovery/last.json, and optionally uploads it to the
Central API. Strictly read-only — no server state is changed.
    )annotationsN)datetimetimezone)Path)Any)Config)gitprojectsservicessystem)
get_loggerzdiscovery.full)z/etc/nginx/sites-enabledz/etc/nginx/conf.dz/etc/apache2/sites-enabled)z/etc/letsencrypt/livez/etc/ssl/certsc                     g } t         D ]E  }t        |      }|j                         s| j                  d |j	                  d      D               G | S )zHList vhost config file paths (read-only; contents are not parsed in v1).c              3  T   K   | ]   }|j                         st        |       " y w)N)is_filestr).0ps     H/var/www/downloads.wolfpanel.net/public_html/agent/src/discovery/full.py	<genexpr>z_vhost_files.<locals>.<genexpr>    s     GA199;QGs   ((*)_VHOST_DIRSr   is_dirextendglob)found	directorybases      r   _vhost_filesr      sK    E  H	I;;=LLG3GGH L    c                    g } t         D ]  }t        |      }|j                         st        |j	                  d            dd t        |j	                  d            z   D ]  }d|j
                  v sd|j
                  j                         v r.t        j                  ddd	d
dt        |      g      }| j                  t        |      |r#|j                         j                  dd      d   ndd         | S )u   Locate certificate files and read their expiry via openssl if present.

    SECURITY: only certificates (public) are inspected — never private keys.
    z**/*.pemN   z**/fullchain*privkeykeyopensslx509z-enddatez-nooutz-in=   )path	not_after)
_CERT_DIRSr   r   listr   namelowerr   run_readonlyr   appendstripsplit)certsr   r   certenddates        r   _ssl_certificatesr6   $   s    
 #%E 	I{{}:./5TYY=W8XX 	DDII%$))//2C)C))FJ%TKG LLIFM!6!6sA!>r!BSW		  Lr   c                 J   t        j                         } i dt        j                         dt        j                         dt        j
                         dt        j                         dt        j                         dt        j                         dt        j                         dt        j                         d	t        j                         d
t        j                         dt        j                         dt        j                         d| dt        j                         dt        j                          dt        j"                         dt        j$                         t'               t)               t+        j,                         t/        j0                         dS )z$Build the full inventory dictionary.oskernel	cpu_countmemorydiskload_averageuptimeprivate_ips	public_ipnetwork_interfaces
open_portssystemd_servicesweb_serverslanguage_runtimes
containers	databases	cron_jobs)vhostsssl_certificatesgit_repositoriesr
   )r   rD   r   os_infor9   r:   memory_usage
disk_usager=   uptime_secondsr?   r@   rA   rB   rC   rE   rF   rG   rH   r   r6   r	   find_repositoriesr
   find_projects)webs    r   collectrS   =   s   



 Cfnn&--/ 	V%%' 	&%%'	
 	!!# 	++- 	&'') 	v))+ 	V%%' 	f779 	f'') 	H557 	s 	X779 	h))+  	X'')!" 	X'')#$ .-/113**,+ r   c                   t               }| j                  | j                  t        j                  t
        j                        j                         |d}| j                  j                  dd       | j                  dz  }|j                  t        j                  |d      d       t        j                  d	|       |rd
dlm}m} 	  || |d ||             |S |S # t$        $ r!}t        j'                  d|       Y d}~|S d}~ww xY w)zCollect, persist the snapshot, and optionally upload it.

    Returns the snapshot dict. Upload failures are logged but never fatal.
    )	server_idagent_versioncollected_at	discoveryT)parentsexist_okz	last.json   )indentzutf-8)encodingz discovery snapshot written to %sr   )categories_from_inventoryupload_inventoryfullz9discovery upload failed (will rely on local snapshot): %sN)rS   rU   rV   r   nowr   utc	isoformatdiscovery_dirmkdir
write_textjsondumpsloginfodiscovery.uploadr^   r_   	Exceptionwarning)	config
api_clientupload	inventorysnapshotsnapshot_pathr^   r_   excs	            r   runru   Y   s    
 	I%%-- X\\2<<>	H td;((;6MTZZ;gNHH/?P	Z))4	 O8O  	ZKKSUXYYO	Zs   
C 	D	(DD	)returnz	list[str])rv   zlist[dict[str, Any]])rv   dict[str, Any])T)rn   r   rp   boolrv   rw   )__doc__
__future__r   rg   r   r   pathlibr   typingr   rn   r   rX   r	   r
   r   r   loggerr   ri   r   r+   r   r6   rS   ru    r   r   <module>r      sS    #  '    5 5 !" ^8
28 r   