o
    x[h0!  ã                   @   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ÚCloudCIXÚ
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Samsung Cloud Platformz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_logsI   s   þþ
r0   c                 C   s,   d}|r|  |¡rt| ddiƒ dS dS dS )z*Optionally attach hardware info from lshw.z¡Your 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
   ÿÿr4   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_filer7   r.   r.   r/   Úattach_installer_filesj   s   ýr:   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)r5   r6   r7   r   ÚexistsÚ
setdefault)r*   r+   r7   r.   r.   r/   Úattach_ubuntu_pro_info{   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)r2   ÚStopIteration)r*   r+   r3   Úresponser.   r.   r/   Úcan_attach_sensitive†   s   ÿ
rD   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"   






ürP   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ÚRuntimeErrorrD   r0   r4   r:   r@   rP   )r*   r+   r,   r.   r.   r/   Úadd_info¬   s   ÿ


rS   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©rK   Úitems)Úds_datarV   Úname_to_report_mapÚ
azure_dataÚsrc_keyÚreport_key_namer.   r.   r/   Ú_get_azure_dataÀ   s   ûrh   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>ra   )rc   rj   r.   rs   r/   Ú_get_ec2_dataÑ   s   ÿ
ÿrv   )ÚazureÚec2Úplatformc                 C   sJ   t  |¡}|s	dS ||ƒ}| ¡ D ]\}}|sq|| | ¡ | ¡  < qdS )z–Add datasoure specific information from the ds dictionary.

    ds_data contains the "ds" entry from data from
    /run/cloud/instance-data.json.
    N)ÚPLATFORM_SPECIFIC_INFOrK   rb   Ú
capitalize)r*   ry   rc   Úplatform_infoÚretrieved_datarq   rr   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 )zÆEntry 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Úmachinery   rk   Úsubplatformry   Úds)r   ÚopenÚjsonÚloadÚFileNotFoundErrorÚloggingÚ	getLoggerÚwarningrK   ÚsortedrL   rb   r~   )r*   Ú_ÚfopenÚinstance_datar€   rq   Ú
report_keyrr   r.   r.   r/   Úgeneral_add_infoö   sH   ÿ€ÿû

þú
ù
€ÿr˜   )NF)N)rA   N)!Ú__doc__r�   r�   r5   Útypingr   Úcloudinit.cmd.devel.logsr   r   Úcloudinit.cmd.statusr   Úapport.hookutilsr   r   r   r	   rQ   ÚImportErrorÚKNOWN_CLOUD_NAMESr0   r4   r:   r@   ÚboolrD   rP   rS   Ústrrh   rv   rz   r~   r˜   r.   r.   r.   r/   Ú<module>   s4   ÿ
*



