
    ([Qhn3                     ^    d dl 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	  G d de      Z
y)    N)SoSComponent)_sos)__version__c                       e Zd ZdZdZi 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	Z	 	 d& fd	Zed        Zed        Z	d Z
d Zd Zd Zd  Zed!        Zed"        Zed#        Zd$ Zd% Z xZS )'	SoSUploadz
    This class is designed to upload files to a distribution
    defined location. These files can be either sos reports,
    sos collections, or other kind of files like: vmcores,
    application cores, logs, etc.

    z9Upload a file to a user or policy defined remote locationupload_file case_idlow_priorityF
upload_urlNupload_directoryupload_userupload_passupload_methodautoupload_no_ssl_verifyupload_protocolupload_s3_endpointzhttps://s3.amazonaws.comupload_s3_regionupload_s3_bucketupload_s3_access_keyupload_s3_secret_keyupload_s3_object_prefixupload_targetc                 8   |st         |   |||       d| _        nb|d   | _        |d   | _        |d   | _        || _        || _        || _        || _	        t        j                  d      | _        d| _        || _        | j                         | _        d | _        y )NToptionspolicymanifestsos_uiF)super__init__from_cmdlineoptsr   r   parsercmdlineargs_upload_filelogging	getLoggerui_logarchiveload_upload_targetsupload_targetsr   )selfr$   r&   r%   in_placehook_commonsr+   	__class__s          5/usr/lib/python3/dist-packages/sos/upload/__init__.pyr!   zSoSUpload.__init__4   s    GVT73 $D
 %Y/DI&x0DK(4DM DK"DLDI 'D!++H5DK %D"DL"668!    c                    d|_         |j                  dd      }|j                  ddd       |j                  dd	d
d       |j                  dd d       |j                  dd d       |j                  dd d       |j                  dd	dd       |j                  dd d       |j                  dd d       |j                  dd d       |j                  dd d       |j                  dd d        |j                  d!d d"       |j                  d#d$g d%d&'       |j                  d(d$g d)d*'       |j                  d+d,d-d./       |j                  d0d1g d2d3'       y )4Nzsos upload FILE [options]zUpload Optionsz.These options control how upload manages filesr   FILEzThe file or archive to upload)metavarhelpz	--case-idstorer
   zspecify case identifier)actiondestr7   z--upload-urlz&Upload the archive to specified server)defaultr7   z--upload-userzUsername to authenticate withz--upload-passzPassword to authenticate withz--upload-directoryr   z$Specify upload directory for archivez--upload-s3-endpointz#Endpoint to upload to for S3 bucketz--upload-s3-regionz!Region to upload to for S3 bucketz--upload-s3-bucketz"Name of the S3 bucket to upload toz--upload-s3-access-keyzAccess key for the S3 bucketz--upload-s3-secret-keyzSecret key for the S3 bucketz--upload-s3-object-prefixzPrefix for the S3 object/keyz--upload-methodr   )r   putpostz HTTP method to use for uploading)r;   choicesr7   z--upload-protocol)r   httpsftpsftps3z$Manually specify the upload protocolz--upload-no-ssl-verifyF
store_truez'Disable SSL verification for upload url)r;   r9   r7   z--upload-targetlocal)redhat	canonicalgenericrD   zmManually specify vendor-specific target for uploads. Supported options are:
redhat, canonical, generic, local)usageadd_argument_groupadd_argument)clsr$   
upload_grps      r2   add_parser_optionszSoSUpload.add_parser_optionsL   s   2..<
 	v%D 	  	FG)%> 	  	@%M 	  	O%D 	  	F%D 	  	F 4W%7%K 	  	M 	 6%J 	  	L 4d%H 	  	J 4d%I 	  	K 8$%C 	  	E 8$%C 	  	E ;T%C 	  	E 16(?%G 	  	I 	 3V(N%K 	  	M 	 8%'3%N 	  	P 	 17)-
'7 	  
	9r3   c                 H    |j                  d       |j                  d       y )NzSoS Upload Detailed HelpzThe upload command is designed to upload already existing sos reports, as well as other files like logs and vmcores to a distribution specific location.)	set_titleadd_text)rK   sections     r2   display_helpzSoSUpload.display_help   s$    453	
r3   c                    d}| j                   j                  dt         d       | j                  |      }| j                   j                  |       d}| j                  j
                  s(	 t        |       | j                   j                  d       y	y	# t        $ r | j                  dd       Y y	t        $ r}| j                  |d       Y d	}~y	d	}~ww xY w)
zmPrint the intro message and prompts for a case ID if one is not
        provided on the command line
        a}  This utility is used to upload files to a target location based either on a command line option or detecting the local distribution.

The archive to be uploaded may contain data considered sensitive and its content should be reviewed by the originating organization before being passed to any third party.

No configuration changes will be made to the system running this utility.
z
sos upload (version )z,
Press ENTER to continue, or CTRL-C to quit
r	   zExiting on user cancel      N)
r*   infor   _fmt_msgr#   batchinputKeyboardInterrupt_exit	Exception)r.   
disclaimer	intro_msgpromptes        r2   introzSoSUpload.intro   s    
 	1+a@AMM*-	#Ayy!f  $  % :

3S9 !

1a  !s   +&B C0C8CCc                     | j                   | j                  | j                   j                  | j                   j                  dS )N)cmdlineoptsr   r
   r   )r#   r   r
   r   )r.   s    r2   get_commonszSoSUpload.get_commons   s4    99kkyy(( $		 : :	
 	
r3   c                     || _         y)zMSet common host data for the Upload targets
            to reference
        N)commons)r.   rg   s     r2   set_commonszSoSUpload.set_commons   s     r3   c                 <   t        | j                  j                               }| j                  j                         D ]u  }|j                  |       |j	                         s%|j
                  j                  }| j                  j                  d| d       || _	        |j                         | _        w | j                  s3| j                  d   | _	        | j                  j                         | _        | j                  j                  d| j                          y)a  This sets the upload target and loads that target's options.

        If no upload target is matched and no target is provided by
        the user, then we abort.

        If an upload target is provided in the command line,
        this will not run.
        zInstallation matches z, checking for upload targetsrG   zUpload target set to N)listr-   valuesremovecheck_distributionr1   __name__r*   debugr   nameupload_namerW   )r.   checksr   cnames       r2   determine_upload_targetz!SoSUpload.determine_upload_target   s     d))0023!00779 	8MMM-(//1%//88!!$9% A4 #4 5%2"#0#5#5#7 	8 !!!%!4!4Y!?D#11668D+D,<,<+=>	@r3   c                 $   ddl }i }| j                  |j                  j                  d      D ]`  } |d   | j                  | j
                  | j                        }|j                  | j                                |||j                         <   b |S )zELoads all upload targets supported by the local installation
        r   NtargetsrV   )r$   r&   r%   )
sos.upload.targets_load_modulesuploadrv   r$   r&   r%   rh   re   get_target_id)r.   sossupported_upload_tgtsr   target_classs        r2   r,   zSoSUpload.load_upload_targets   s     	" "!//

0B0BIN 	OM+=+#';;!%$(LLL
 $$T%5%5%78BN!,"<"<">?	O %$r3   c                     g }|j                   D ]C  }t        j                  j                  |      s#|j	                  | j                  ||             E |S )zHelper to import upload targets)__path__ospathisdirextend_find_modules_in_path)rK   packagesubmodmodulesr   s        r2   rx   zSoSUpload._load_modules   sP     $$ 	HDww}}T"s88vFG	H r3   c                 P   g }t         j                  j                  |      rt        t        j                  |            D ]c  }|j                  d      sd|v rt         j                  j                  |      \  }}d| d| }|j                  | j                  |             e |S )aU  Given a path and a module name, find everything that can be imported
        and then import it

            path - the filesystem path of the package
            modulename - the name of the module in the package

        E.G. a path of 'targets', and a modulename of 'redhat' equates to
        importing sos.upload.targets.redhat
        z.py__zsos.upload..)	r   r   existssortedlistdirendswithsplitextr   _import_modules)rK   r   
modulenamer   pyfilefname_modnames           r2   r   zSoSUpload._find_modules_in_path   s     77>>$ D!12 =u-6>77++F3q'
|1UG<s227;<= r3   c                 8   |j                  d      d   }	 t        |t               t               |g      }t        j                  |t
        j                        }|j                         D ]  }|d   dv s|j                  |        |S # t        $ r}|d}~ww xY w)z/Import and return all found classes in a moduler      Nr   )DeviceAuthorizationClassUpload
RHELPolicyUbuntuPolicy)
split
__import__globalslocalsImportErrorinspect
getmembersisclasscopyrl   )rK   r   mod_short_namemodulera   r   mods          r2   r   zSoSUpload._import_modules   s     !s+A.	FH~>NOF $$VW__=<<> 	$C1v $ $
 s#	$   	G	s   B	 		BBBc                     | j                         | _        | j                  d   }|j                  r| j                  j	                          y y )Nrd   )re   rg   r   r   _configure_low_priority)r.   cmdline_optss     r2   pre_workzSoSUpload.pre_work  s>    '')||M2$$KK//1 %r3   c           
         | j                          | j                  rV| j                          | j                  j                  | _        | j                  j                  | j                        | _        | j                  j                  }|r|dk7  r| j                  |   | _	        n| j                          | j                  s0| j                  j                  d       t        j                  d       | j                  j                  | j!                                	 t#        j$                  | j
                        j&                  dkD  rt"        j(                  j+                  | j
                        rY	 | j                  j-                  | j
                         | j                  j/                  t1        d| j
                   d             y | j                  j                  t1        | j
                   d	             y | j                  j                  t1        d| j
                   d
             y # t2        $ rF}| j                  j                  t1        d|              t        j                  d       Y d }~y d }~ww xY w# t2        $ r?}| j                  j                  t1        d| j
                   d| d             Y d }~y d }~ww xY w)N)r   rD   z|No upload target set via command line options or autodetected.
Please specify one using the option --upload-target.
Exiting.rV   r   zFile z uploaded successfullyzUpload attempt failed: z is not a file.z
 is empty.zCannot upload z:  )r   r"   rb   r#   r   r+   r   prompt_for_case_idcaseidr   r-   rt   r*   errorsysexitre   r   statst_sizer   isfileupload_archiverW   r   r]   )r.   cmdline_targeterrra   s       r2   executezSoSUpload.execute  s   JJL9900DL++88!YY 9 DK 00n7!%!4!4^!DD((*!! KK HHQK##D$4$4$67	Hwwt||$,,q077>>$,,/$**99$,,G((dll^3IJK KK%%a4<<.(H&IJ!!!eDLL>$D"EF % $))!.EcU,K*LM$  	HKKa.b1 EFGG	HsJ   AJ  1AH. 	1J  ;2J  .	I=7<I83J  8I==J   	K	5KK)NNNFNN)rn   
__module____qualname____doc__descarg_defaultsr!   classmethodrM   rR   rb   re   rh   rt   r,   rx   r   r   r   r   __classcell__)r1   s   @r2   r   r      sy    KDr2 	 	d	
 	D 	t 	t 	 	 	6 	8 	D 	D 	 	  	"4!" 	#L( GL,0"0 29 29h 
 
!<
@0%    ,  "2(Hr3   r   )r   r   r(   r   sos.componentr   r{   r   r   r   r    r3   r2   <module>r      s,    
 
   &  kH kHr3   