o
    f!                     @   s"  d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
Z
 zddlmZmZmZmZ dZW n ey=   dZY nw g d	Zd#d
dZd$ddZd#ddZd$ddZd$defddZdd Zdd Zdeeef fddZdeeef fddZeedZdeddfdd Zd%d!d"Z dS )&zCloud-init apport interface    N)Dict)INSTALLER_APPORT_FILES INSTALLER_APPORT_SENSITIVE_FILES)is_cloud_init_enabled)attach_fileattach_file_if_existsattach_root_command_outputsroot_command_outputTF)$AliYunAltCloudAkamaizAmazon - Ec2AzureBigstep	Brightbox
CloudSigma
CloudStackDigitalOceanE24CloudzGCE - Google Compute EnginezHuawei CloudExoscalezHetzner CloudNWCSz IBM - (aka SoftLayer or BlueMix)LXDMAASNoCloud
OpenNebula	OpenStackOracleOVFz'RbxCloud - (HyperOne, Rootbox, Rubikon)OpenTelekomCloudzSAP Converged CloudScalewaySmartOSUpCloudVMwareVultrZStackOutscaleWSLOtherc                 C   s<   t | ddi g d}|s|d t| t| dd dS )zBAttach cloud-init logs and tarfile from 'cloud-init collect-logs'.zcloud-init-log-warningsz-egrep -i "warn|error" /var/log/cloud-init.log)z
cloud-initzcollect-logsz-t/tmp/cloud-init-logs.tgzz--redactr'   zlogs.tgzN)r   appendr	   r   )reportuiinclude_sensitivecommand r-   2/usr/lib/python3/dist-packages/cloudinit/apport.pyattach_cloud_init_logsG   s   
r/   c                 C   s,   d}|r| |rt| ddi dS dS dS )z*Optionally attach hardware info from lshw.zYour device details (lshw) may be useful to developers when addressing this bug, but gathering it requires admin privileges. Would you like to include this info?zlshw.txtlshwN)yesnor   )r)   r*   promptr-   r-   r.   attach_hwinfo]   s
   r3   c                 C   sX   t D ]}tj|j}t| ||j q|r(tD ]}tj|j}t| ||j qdS dS )a   Attach any subiquity installer logs config.

    To support decoupling apport integration from installer config/logs,
    we eventually want to either source this function or APPORT_FILES
    attribute from subiquity  and/or ubuntu-desktop-installer package-hooks
    python modules.
    N)r   ospathrealpathr   labelr   )r)   r*   r+   apport_filer6   r-   r-   r.   attach_installer_filesh   s   r9   c                 C   s^   t jd}t| | t j|r-| dd | d r#| d  d7  < | d  d7  < dS dS )z<Attach ubuntu pro logs and tag if keys present in user-data.z/var/log/ubuntu-advantage.logTags  z
ubuntu-proN)r4   r5   r6   r   exists
setdefault)r)   r*   r6   r-   r-   r.   attach_ubuntu_pro_infoy   s   
r?   returnc                 C   s&   |sdS d}| |}|du rt|S )z(Optionally provide user-data if desired.FaQ  Your user data, cloud-config, network config, or autoinstall files can optionally be provided and could be useful to developers when addressing this bug. However, this data should not be included if it contains any sensitive data such as passwords and secrets. Gathering it requires admin privileges. Would you like to include this info?N)r1   StopIteration)r)   r*   r2   responser-   r-   r.   can_attach_sensitive   s   
rC   c                 C   s   g }|  dr|d |  dr|d d|  v r)| d }d|v r)|d |  dr3|d	 |rT| d
d | d
 rG| d
  d7  < | d
  d|7  < dS dS )z$Add any appropriate tags to the bug.CurtinErrorcurtinSubiquityLog	subiquityJournalErrorszBreaking ordering cyclezsystemd-orderingUdiLogzubuntu-desktop-installerr:   r;   r<   N)getr(   keysr>   join)r)   new_tagserrorsr-   r-   r.   add_bug_tags   s"   






rO   c                 C   sN   t stdt| |}t| || t| | t| || t| | t|  dS )a-  This is an entry point to run cloud-init's package-specific hook

    Distros which want apport support will have a cloud-init package-hook at
    /usr/share/apport/package-hooks/cloud-init.py which defines an add_info
    function and returns the result of cloudinit.apport.add_info(report, ui).
    z;No apport imports discovered. Apport functionality disabledT)
has_apportRuntimeErrorrC   r/   r3   r9   r?   rO   )r)   r*   r+   r-   r-   r.   add_info   s   


rR   c                 C   sX   |  di  di  d}|si S dddddd	}i }| D ]
\}}|| ||< q|S )
N	meta_dataimdscomputeImagePublisher
ImageOfferImageSKUImageVersionVMSize)	publisherofferskuversionvmSizerJ   items)ds_datarU   name_to_report_map
azure_datasrc_keyreport_key_namer-   r-   r.   _get_azure_data   s   rg   c                    s@   |  di  di  d}|si S h d  fdd| D S )Ndynamiczinstance-identitydocument>   regionimageIdarchitectureinstanceTypebillingProductsc                    s   i | ]\}}| v r||qS r-   r-   ).0keyvaluewanted_keysr-   r.   
<dictcomp>   s    z!_get_ec2_data.<locals>.<dictcomp>r`   )rb   ri   r-   rr   r.   _get_ec2_data   s   
ru   )azureec2platformc                 C   sJ   t |}|s	dS ||}| D ]\}}|sq|| | |  < qdS )zAdd datasoure specific information from the ds dictionary.

    ds_data contains the "ds" entry from data from
    /run/cloud/instance-data.json.
    N)PLATFORM_SPECIFIC_INFOrJ   ra   
capitalize)r)   rx   rb   platform_inforetrieved_datarp   rq   r-   r-   r.   add_datasource_specific_info   s   
r}   c                 C   s   z"t  sW dS tdd}t|}W d   n1 sw   Y  W n ty3   t d Y dS w |d}|sIt dt	|
  dS ddd	d
ddd D ]\}}||}|rc|| |< qTt| |d |d dS )zEntry point for Apport.

    This hook runs for every apport report

    Add a subset of non-sensitive cloud-init data from
    /run/cloud/instance-data.json that will be helpful for debugging.
    Nz"/run/cloud-init/instance-data.jsonrzKcloud-init run data not found on system. Unable to add cloud-specific data.v1z8instance-data.json lacks 'v1' metadata. Present keys: %sCloudID	CloudNameCloudArchitectureCloudPlatformCloudRegionCloudSubPlatform)cloud_id
cloud_namemachinerx   rj   subplatformrx   ds)r   openjsonloadFileNotFoundErrorlogging	getLoggerwarningrJ   sortedrK   ra   r}   )r)   _fopeninstance_datar   rp   
report_keyrq   r-   r-   r.   general_add_info   sH   



r   )NF)N)r@   N)!__doc__r   r   r4   typingr   cloudinit.cmd.devel.logsr   r   cloudinit.cmd.statusr   apport.hookutilsr   r   r   r	   rP   ImportErrorKNOWN_CLOUD_NAMESr/   r3   r9   r?   boolrC   rO   rR   strrg   ru   ry   r}   r   r-   r-   r-   r.   <module>   s4   
(



