o
    w7e                     @   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y3   dZY nw g dZd	Zd
ZdefddZdddZdddZdddZdddZdddZdd Zdd ZdS )z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_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Otherz/var/log/cloud-init.logz/var/log/cloud-init-output.logreturnc                  C   s   t  } | dS )Nuserdata_raw)r   get_ipath_cur)paths r)   2/usr/lib/python3/dist-packages/cloudinit/apport.py_get_user_data_fileH   s   
r+   c                 C   s,   t | ddd tg d 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.txt)z
cloud-initzcollect-logsz-t/tmp/cloud-init-logs.tgzr,   zlogs.tgzN)r   r   r   reportuir)   r)   r*   attach_cloud_init_logsM   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.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
   r4   c              
   C   s   |rit  }z,t|d}t|}|di dsJ 	 W d   W dS 1 s+w   Y  W n ttjjt	fy?   Y nw d}|
|}|du rMt|rkd}||t}|rct|d  | d< dS d	| d< dS dS 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_datav1
cloud_nameNz/Is this machine running in a cloud environment?zOPlease select the cloud vendor or environment in which this instance is runningr   	CloudNameNone)r   openget_runpathjsonloadgetIOErrordecoderJSONDecodeErrorAssertionErrorr2   StopIterationchoiceKNOWN_CLOUD_NAMES)r.   r/   r(   filer5   r3   responser)   r)   r*   attach_cloud_infoi   s0   
&
rH   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_filerK   r)   r)   r*   attach_installer_files   s   rN   c                 C   sx   |r6t  }d|}||}|du rt|r8tj|}t| |d tD ]}tj|j}t	| ||j
 q%dS dS dS )z(Optionally provide user-data if desired.zYour 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+   formatr2   rC   rI   rJ   rK   r   r   r   rL   )r.   r/   user_data_filer3   rG   rK   rM   r)   r)   r*   attach_user_data   s"   
rQ   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-installerTags  N)r>   appendkeys
setdefaultjoin)r.   new_tagserrorsr)   r)   r*   add_bug_tags   s"   






ra   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RuntimeErrorr0   r4   rH   rQ   rN   ra   r-   r)   r)   r*   add_info   s   




rd   )N)__doc__r<   rI   cloudinit.cmd.develr   cloudinit.cmd.devel.logsr   r   apport.hookutilsr   r   r   r   rb   ImportErrorrE   CLOUDINIT_LOGCLOUDINIT_OUTPUT_LOGstrr+   r0   r4   rH   rN   rQ   ra   rd   r)   r)   r)   r*   <module>   s,   '



(
