
    }f'                     *   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ 	 ddlmZmZmZmZ dZg d
ZdefdZddZddZddZddZddZddZd Zd Zdeeef   fdZdeeef   fdZ ee dZ!deddfdZ"ddZ#y# e$ r d	ZY Yw xY w)zCloud-init apport interface    N)Dict)read_cfg_paths)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Otherreturnc                  8    t               } | j                  d      S )Nuserdata_raw)r   get_ipath_cur)pathss    2/usr/lib/python3/dist-packages/cloudinit/apport.py_get_user_data_filer/   H   s    E~..    c                 V    t        | ddd       t        g d       t        | dd       y)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.tgzr2   zlogs.tgzN)r
   r   r   reportuis     r.   attach_cloud_init_logsr6   M   s:     @)M		
 H *Jr0   c                 N    d}|r!|j                  |      rt        | ddi       yyy)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
   )r4   r5   prompts      r.   attach_hwinfor;   `   s2    	0 
 
bhhv#FZ,@A rr0   c                    |rjt               }	 t        |j                  d            5 }t        j                  |      }|j                  di       j                  d      sJ 	 ddd       yyyy# 1 sw Y   nxY wn.# t        t        j                  j                  t        f$ r Y nw xY wd}|j                  |      }|t        |s[d}|j                  |t              }|rt        |d      | d<   yd	| d<   )
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AssertionErrorr9   StopIterationchoiceKNOWN_CLOUD_NAMES)r4   r5   r-   filer=   r:   responses          r.   attach_cloud_inforP   k   s    
 	e''89 T $		$$((r266|DDD   
   55~F 		 C88F#,  yy):;H&7&D{#&,{#s(   A; :A."A; .A73A; ;(B&%B&c                     t         D ]B  }t        j                  j                  |j                        }t	        | ||j
                         D y)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)r4   r5   apport_filerT   s       r.   attach_installer_filesrW      sA     . C77##K$4$45fh0A0ABCr0   c                     t         j                  j                  d      }t        | |       t         j                  j	                  |      r2| j                  dd       | d   r| dxx   dz  cc<   | dxx   dz  cc<   yy)z<Attach ubuntu pro logs and tag if keys present in user-data.z/var/log/ubuntu-advantage.logTags  z
ubuntu-proN)rR   rS   rT   r	   exists
setdefault)r4   r5   rT   s      r.   attach_ubuntu_pro_infor^      si    ww ?@H&(+	ww~~h&"%&>6Nc!Nv,&	  r0   c                 f   |rt               }dj                  |      }|j                  |      }|t        |rxt        j
                  j                  |      }t        | |d       t        D ]B  }t        j
                  j                  |j
                        }t        | ||j                         D yyy)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/   formatr9   rK   rR   rS   rT   r   r   r	   rU   )r4   r5   user_data_filer:   rO   rT   rV   s          r.   attach_user_datarb      s    	,. F>*	 	 88F#ww''7H/:? 77++K,<,<=%Hk&7&7  
r0   c                    g }| j                  d      r|j                  d       | j                  d      r|j                  d       d| j                         v r| d   }d|v r|j                  d       | j                  d      r|j                  d	       |rA| j                  d
d       | d
   r| d
xx   dz  cc<   | d
xx   dj	                  |      z  cc<   yy)z$Add any appropriate tags to the bug.CurtinErrorcurtinSubiquityLog	subiquityJournalErrorszBreaking ordering cyclezsystemd-orderingUdiLogzubuntu-desktop-installerrY   rZ   r[   N)rF   appendkeysr]   join)r4   new_tagserrorss      r.   add_bug_tagsro      s    Hzz- !zz.!$&++-'($.OO./zz(23&"%&>6Nc!Nv#((8,,	 r0   c                     t         st        d      t        | |       t        | |       t	        | |       t        | |       t        | |       t        | |       t        |        y)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RuntimeErrorr6   r;   rP   rb   rW   r^   ro   r3   s     r.   add_infors      s`     I
 	
 62&&"fb!VR 62&62&r0   c                     | j                  di       j                  di       j                  d      }|si S dddddd	}i }|j                         D ]  \  }}||   ||<    |S )
N	meta_dataimdscomputeImagePublisher
ImageOfferImageSKUImageVersionVMSize)	publisherofferskuversionvmSizerF   items)ds_datarw   name_to_report_map
azure_datasrc_keyreport_key_names         r.   _get_azure_datar      s    kk+r*..vr:>>yIG	%! J$6$<$<$> 7 &-g&6
?#7r0   c                     | j                  di       j                  di       j                  d      }|si S h d}|j                         D ci c]  \  }}||v s|| c}}S c c}}w )Ndynamiczinstance-identitydocument>   regionimageIdarchitectureinstanceTypebillingProductsr   )r   r   wanted_keyskeyvalues        r.   _get_ec2_datar      st    Ir"&&':B?CCJO  	K &.^^%5!sE9KU
  s   A#A#)azureec2platformc                     t         j                  |      }|sy ||      }|j                         D ],  \  }}|s	|| |j                         |j                         z   <   . y)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_INFOrF   r   
capitalize)r4   r   r   platform_inforetrieved_datar   r   s          r.   add_datasource_specific_infor     sg     +..x8M"7+N$**, A
U;@x""$s~~'778Ar0   c                 8   	 t               syt        dd      5 }t        j                  |      }ddd       j                  d      }|s<t        j                         j                  dt        |j                                      yddd	d
dddj                         D ]  \  }}|j                  |      }|s|| |<     t        | |d   |j                  d             y# 1 sw Y   xY w# t        $ r& t        j                         j                  d       Y yw xY w)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.r>   z8instance-data.json lacks 'v1' metadata. Present keys: %sCloudIDr@   CloudArchitectureCloudPlatformCloudRegionCloudSubPlatform)cloud_idr?   machiner   r   subplatformr   ds)r   rB   rD   rE   FileNotFoundErrorlogging	getLoggerwarningrF   sortedrk   r   r   )r4   _fopenr=   r>   r   
report_keyr   s           r.   general_add_infor   !  s"   
$&6< 	- IIe,M	- 
		4	 B##F=%%'(	
 	 !&#) eg
'Z s!&F:
' !: 1 1$ 7;	- 	- ##1	
 	s,   
C* C* CC* C'#C* *,DD)N)r)   N)$__doc__rD   r   rR   typingr   cloudinit.cmd.develr   cloudinit.cmd.devel.logsr   r   cloudinit.cmd.statusr   apport.hookutilsr   r	   r
   r   rq   ImportErrorrM   strr/   r6   r;   rP   rW   r^   rb   ro   rs   r   r   r   r   r    r0   r.   <module>r      s   
 "   	  . 7
  J
% P/S /
&B%-P
C'.-(*S#X "d38n $ $3=I A3 AD A *K  Js   B BB