U
    Ӈg!                  %   @   sl  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k
r|   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+d,d-g%ZdGd.d/ZdHd0d1ZdId2d3ZdJd4d5ZdKed6d7d8Zd9d: Zd;d< Zeeef d6d=d>Zeeef d6d?d@ZeedAZeddBdCdDZdd6dEdFZ dS )Lz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_outputTFZAliYunZAltCloudZAkamaizAmazon - Ec2ZAzureZBigstepZ	BrightboxZCloudCIXZ
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WSLZOtherc                 C   s@   t | ddi ddddg}|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.logz
cloud-initzcollect-logsz-tz/tmp/cloud-init-logs.tgzz--redactzlogs.tgzN)r   appendr	   r   )reportuiinclude_sensitiveZcommand r   2/usr/lib/python3/dist-packages/cloudinit/apport.pyattach_cloud_init_logsH   s    	
r   c                 C   s$   d}|r | |r t| ddi 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.txtZlshwN)yesnor   )r   r   promptr   r   r   attach_hwinfo^   s    r   c                 C   sT   t D ] }tj|j}t| ||j q|rPt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   Zlabelr   )r   r   r   Zapport_filer   r   r   r   attach_installer_filesi   s    r   c                 C   sZ   t jd}t| | t j|rV| dd | d rF| d  d7  < | d  d7  < 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)r   r   r   r   exists
setdefault)r   r   r   r   r   r   attach_ubuntu_pro_infoz   s    
r   )returnc                 C   s&   |sdS d}| |}|dk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)r   StopIteration)r   r   r   Zresponser   r   r   can_attach_sensitive   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-installerr   r   r   N)getr
   keysr   join)r   Znew_tagserrorsr   r   r   add_bug_tags   s     






r%   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RuntimeErrorr    r   r   r   r   r%   )r   r   r   r   r   r   add_info   s    


r(   c                 C   sX   |  di  di  d}|s"i S dddddd	}i }| D ]\}}|| ||< q>|S )
NZ	meta_dataZimdscomputeZImagePublisherZ
ImageOfferZImageSKUZImageVersionZVMSize)Z	publisherZofferZskuversionZvmSizer!   items)ds_datar)   Zname_to_report_mapZ
azure_dataZsrc_keyZreport_key_namer   r   r   _get_azure_data   s    r.   c                    sF   |  di  di  d}|s"i S dddddh  fd	d
| D S )NZdynamiczinstance-identitydocumentZarchitectureZbillingProductsZimageIdZinstanceTyperegionc                    s   i | ]\}}| kr||qS r   r   ).0keyvalueZwanted_keysr   r   
<dictcomp>   s      z!_get_ec2_data.<locals>.<dictcomp>r+   )r-   r/   r   r4   r   _get_ec2_data   s    
r6   )ZazureZec2)platformr   c                 C   sJ   t |}|sdS ||}| D ]"\}}|s0q"|| | |  < 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_INFOr!   r,   
capitalize)r   r7   r-   Zplatform_infoZretrieved_datar2   r3   r   r   r   add_datasource_specific_info   s    
r:   c              	   C   s   z0t  sW dS tdd}t|}W 5 Q R X W n$ tk
rT   t d Y dS X |d}|st dt	|
  dS ddd	d
ddd D ]\}}||}|r|| |< qt| |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: %sZCloudIDZ	CloudNameZCloudArchitectureZCloudPlatformZCloudRegionZCloudSubPlatform)Zcloud_idZ
cloud_namemachiner7   r0   Zsubplatformr7   Zds)r   openjsonloadFileNotFoundErrorloggingZ	getLoggerZwarningr!   sortedr"   r,   r:   )r   _ZfopenZinstance_datar<   r2   Z
report_keyr3   r   r   r   general_add_info   s@    



  rE   )NF)N)NF)N)N)!__doc__r?   rB   r   typingr   Zcloudinit.cmd.devel.logsr   r   Zcloudinit.cmd.statusr   Zapport.hookutilsr   r   r   r	   r&   ImportErrorZKNOWN_CLOUD_NAMESr   r   r   r   boolr    r%   r(   strr.   r6   r8   r:   rE   r   r   r   r   <module>   sz   
)




