U
    L¬÷dš  ã                #   @   s  d Z ddlZddlZddlmZ ddlmZmZ z ddlm	Z	m
Z
mZmZ dZW n ek
rh   dZY nX dd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*g#Zd+Zd,Zed-œd.d/„Zd>d0d1„Zd?d2d3„Zd@d4d5„ZdAd6d7„ZdBd8d9„Zd:d;„ Zd<d=„ ZdS )CzCloud-init apport interfaceé    N)Úread_cfg_paths)ÚINSTALLER_APPORT_FILESÚ INSTALLER_APPORT_SENSITIVE_FILES)Úattach_fileÚattach_file_if_existsÚattach_root_command_outputsÚroot_command_outputTFZAliYunZAltCloudZAkamaizAmazon - Ec2ZAzureZBigstepZ	BrightboxZ
CloudSigmaZ
CloudStackZDigitalOceanZE24CloudzGCE - Google Compute EnginezHuawei CloudZExoscalezHetzner CloudZNWCSz IBM - (aka SoftLayer or BlueMix)ZLXDZMAASZNoCloudZ
OpenNebulaZ	OpenStackZOracleZOVFz'RbxCloud - (HyperOne, Rootbox, Rubikon)ZOpenTelekomCloudzSAP Converged CloudZScalewayZSmartOSZUpCloudZVMwareZVultrZZStackZOutscaleZOtherz/var/log/cloud-init.logz/var/log/cloud-init-output.log)Úreturnc                  C   s   t ƒ } |  d¡S )NZuserdata_raw)r   Zget_ipath_cur)Úpaths© r   ú2/usr/lib/python3/dist-packages/cloudinit/apport.pyÚ_get_user_data_fileH   s    r   c                 C   s0   t | dddœƒ tddddgƒ t| ddƒ d	S )
zBAttach cloud-init logs and tarfile from 'cloud-init collect-logs'.z-egrep -i "warn|error" /var/log/cloud-init.logz"cat /var/log/cloud-init-output.log)zcloud-init-log-warningszcloud-init-output.log.txtz
cloud-initzcollect-logsz-tz/tmp/cloud-init-logs.tgzzlogs.tgzN)r   r   r   ©ÚreportÚuir   r   r   Úattach_cloud_init_logsM   s    üþ	
ÿr   c                 C   s$   d}|r |  |¡r t| ddiƒ 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.txtZlshwN)Úyesnor   )r   r   Úpromptr   r   r   Úattach_hwinfo^   s    ÿr   c              
   C   sÄ   |rÀt ƒ }zJt| d¡ƒ2}t |¡}| di ¡ d¡s<t‚W 5 Q R £ W dS Q R X W n ttjj	tfk
rr   Y nX d}| 
|¡}|dkrŽt‚|rÀd}| |t¡}|r¸t|d  | d< nd	| d< dS )
aÐ  Prompt for cloud details if instance-data unavailable.

    When we have valid _get_instance_data, apport/generic-hooks/cloud_init.py
    provides CloudName, CloudID, CloudPlatform and CloudSubPlatform.

    Apport/generic-hooks are delivered by cloud-init's downstream branches
    ubuntu/(devel|kinetic|jammy|focal|bionic) so they will not be represented
    in upstream main.

    In absence of viable instance-data.json format, prompt for the cloud below.
    Úinstance_dataZv1Z
cloud_nameNz/Is this machine running in a cloud environment?zOPlease select the cloud vendor or environment in which this instance is runningr   Z	CloudNameÚNone)r   ÚopenZget_runpathÚjsonÚloadÚgetÚAssertionErrorÚIOErrorÚdecoderZJSONDecodeErrorr   ÚStopIterationZchoiceÚKNOWN_CLOUD_NAMES)r   r   r
   Úfiler   r   Úresponser   r   r   Úattach_cloud_infoi   s(    

ÿr"   c                 C   s*   t D ] }tj |j¡}t| ||jƒ q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   Úlabel)r   r   Úapport_filer%   r   r   r   Úattach_installer_files‘   s    r(   c                 C   sp   |rlt ƒ }d |¡}| |¡}|dkr*t‚|rltj |¡}t| |dƒ tD ] }tj |j¡}t	| ||j
ƒ qJdS )z(Optionally provide user-data if desired.z¿Your user-data, cloud-config or autoinstall files can optionally  be provided from {0} and could be useful to developers when addressing this bug. Do you wish to attach user-data to this bug?Nzuser_data.txt)r   Úformatr   r   r#   r$   r%   r   r   r   r&   )r   r   Zuser_data_filer   r!   r%   r'   r   r   r   Úattach_user_dataž   s    ýÿ
r*   c                 C   s¨   g }|   d¡r| d¡ |   d¡r,| d¡ d|  ¡ krR| d }d|krR| d¡ |   d¡rf| d	¡ |r¤|  d
d¡ | d
 rŽ| d
  d7  < | d
  d |¡7  < dS )z$Add any appropriate tags to the bug.ZCurtinErrorZcurtinZSubiquityLogZ	subiquityZJournalErrorszBreaking ordering cyclezsystemd-orderingZUdiLogzubuntu-desktop-installerZTagsÚ ú N)r   ÚappendÚkeysÚ
setdefaultÚjoin)r   Znew_tagsÚerrorsr   r   r   Úadd_bug_tags³   s     






r2   c                 C   sJ   t stdƒ‚t| |ƒ t| |ƒ t| |ƒ t| |ƒ t| |ƒ t| ƒ dS )a-  This is an entry point to run cloud-init's apport functionality.

    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ÚRuntimeErrorr   r   r"   r*   r(   r2   r   r   r   r   Úadd_infoÇ   s    ÿ




r5   )N)N)N)N)N)Ú__doc__r   r#   Zcloudinit.cmd.develr   Zcloudinit.cmd.devel.logsr   r   Zapport.hookutilsr   r   r   r   r3   ÚImportErrorr   ZCLOUDINIT_LOGZCLOUDINIT_OUTPUT_LOGÚstrr   r   r   r"   r(   r*   r2   r5   r   r   r   r   Ú<module>   sn   
Ý'


(

