3 ܓi3@s`ddlZddlZddlZddlZddlZddlmZddlmZ ddlm Z GdddeZ dS)N) SoSComponent)_sos) __version__cseZdZdZdZddddddddddddddddddZd#fd d Zed d Zed dZ ddZ ddZ ddZ ddZ ddZeddZeddZeddZdd Zd!d"ZZS)$ 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 locationFNautozhttps://s3.amazonaws.com) upload_filecase_id low_priorityZ upload_urlupload_directoryZ upload_userZ upload_passZ upload_methodZupload_no_ssl_verifyZupload_protocolZupload_s3_endpointZupload_s3_regionZupload_s3_bucketZupload_s3_access_keyZupload_s3_secret_keyZupload_s3_object_prefix upload_targetcs~|stj|||d|_nN|d|_|d|_|d|_||_||_||_||_ t j d|_ d|_||_ |j|_d|_dS)NTZoptionspolicymanifestZsos_uiF)super__init__ from_cmdlineoptsr rparsercmdlineargsZ _upload_fileloggingZ getLoggerui_logarchiveload_upload_targetsupload_targetsr )selfrrrZin_placeZ hook_commonsr) __class__/usr/lib/python3.6/__init__.pyr5s     zSoSUpload.__init__cCs:d|_|jdd}|jdddd|jdd d d d |jd ddd|jdddd|jdddd|jdd ddd |jdddd|jdddd|jdddd|jdddd|jddd d|jd!dd"d|jd#d$d$d%d&gd'd(|jd)d$d$d*d+d,d-gd.d(|jd/d0d1d2d3|jd4d5d6d7d8d5gd9d(dS):Nzsos upload FILE [options]zUpload Optionsz.These options control how upload manages filesrFILEzThe file or archive to upload)metavarhelpz --case-idZstorer zspecify case identifier)actiondestr!z --upload-urlz&Upload the archive to specified server)defaultr!z --upload-userzUsername to authenticate withz --upload-passzPassword 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-keyzAccess key for the S3 bucketz--upload-s3-secret-keyzSecret key for the S3 bucketz--upload-s3-object-prefixzPrefix for the S3 object/keyz--upload-methodrZputZpostz HTTP method to use for uploading)r$choicesr!z--upload-protocolZhttpsZftpZsftpZs3z$Manually specify the upload protocolz--upload-no-ssl-verifyF store_truez'Disable SSL verification for upload url)r$r"r!z--upload-targetlocalZredhatZ canonicalgenericzmManually specify vendor-specific target for uploads. Supported options are: redhat, canonical, generic, local)ZusageZadd_argument_group add_argument)clsrZ upload_grprrradd_parser_optionsMsX  zSoSUpload.add_parser_optionscCs|jd|jddS)NzSoS 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.)Z set_titleZadd_text)r*Zsectionrrr display_helps zSoSUpload.display_helpcCsd}|jjdtd|j|}|jj|d}|jjsyt||jjdWnLtk rr|jddYn.t k r}z|j|dWYd d }~XnXd S) 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 rzExiting on user cancelN) rinforZ_fmt_msgrZbatchinputKeyboardInterrupt_exit Exception)rZ disclaimerZ intro_msgprompterrrintros  zSoSUpload.introcCs|j|j|jj|jjdS)N) cmdlineoptsr r r )rr r r )rrrr get_commonsszSoSUpload.get_commonscCs ||_dS)zMSet common host data for the Upload targets to reference N)commons)rr:rrr set_commonsszSoSUpload.set_commonscCst|jj}xP|jjD]B}|j||jr|jj}|jjd|d||_ |j |_ qW|j s~|jd|_ |j j |_ |jj d|j dS)aThis 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. zInstallation matches z, checking for upload targetsr(zUpload target set to N) listrvaluesremoveZcheck_distributionr__name__rdebugr nameZ upload_namer0)rZchecksr Zcnamerrrdetermine_upload_targets    z!SoSUpload.determine_upload_targetcCs\ddl}i}xJ|j|jjdD]6}|d|j|j|jd}|j|j|||j <qW|S)zELoads all upload targets supported by the local installation rNtargetsr/)rrr) Zsos.upload.targets _load_modulesZuploadrCrrrr;r9Z get_target_id)rsosZsupported_upload_tgtsr Z target_classrrrrs zSoSUpload.load_upload_targetscCs6g}x,|jD]"}tjj|r |j|j||q W|S)zHelper to import upload targets)__path__ospathisdirextend_find_modules_in_path)r*packageZsubmodmodulesrHrrrrDs   zSoSUpload._load_modulescCsrg}tjj|rnx\ttj|D]J}|jds0q d|kr:q tjj|\}}d|d|}|j|j|q W|S)aUGiven 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..) rGrHexistssortedlistdirendswithsplitextrJ_import_modules)r*rHZ modulenamerMZpyfileZfname_modnamerrrrKs   zSoSUpload._find_modules_in_pathcCs&tj|}ddtj|tjD}|S)z/Import and return all found classes in a modulecSs$g|]\}}|jjdr||fqS)zsos.upload.targets) __module__ startswith).0rAr*rrr sz-SoSUpload._import_modules..) importlib import_moduleinspectZ getmembersZisclass)r*rWmodulerMrrrrUs zSoSUpload._import_modulescCs(|j|_|jd}|jr$|jjdS)Nr8)r9r:r r Z_configure_low_priority)r cmdline_optsrrrpre_work s  zSoSUpload.pre_workcCsy|j|jr6|j|jj|_|jj|jd|_|jj }|rX|dkrX|j ||_ n|j |j s||j j dtjd|j j|jytj|jjdkr@tjj|jr&y,|j j|j|j jtd|jdWnDtk r"}z&|j j td|tjdWYdd}~XnXn|j j t|jd n|j j td|jd WnDtk r}z&|j j td |jd |d WYdd}~XnXWn"tk r|jddYnXdS)N)r`r'z|No upload target set via command line options or autodetected. Please specify one using the option --upload-target. Exiting.r/rzFile z uploaded successfullyzUpload attempt failed: z is not a file.z is empty.zCannot upload z:  zExiting on user cancelr.)rarr7rrrr Zprompt_for_case_idZcaseidr rrBrerrorsysexitr9rGstatst_sizerHisfileZupload_archiver0rVr4r2r3)rZcmdline_targeterrr6rrrexecutesB    6zSoSUpload.execute)NNNFNN)r?rX __qualname____doc__ZdescZ arg_defaultsr classmethodr+r,r7r9r;rBrrDrKrUrarj __classcell__rr)rrrsB 5   r) rGrdrr^r\Z sos.componentrrErrVrrrrrr s