dbfdg 3 ([Qh(S@sddlZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z ddl m Z ddlmZddlmZdd lmZdd lmZdd lmZmZGd d d ZGdddZdS)N)fill)SUPPRESS)datetime)getpass)rmtree)Path) __version__)TarFileArchive) SoSOptions) TempFileUtil shell_outc @seZdZdZdZiZdZdZdZdZ dddddddddd dd d Z d d dddgZ ddZ ddZ ddZddZd9ddZddZddZedd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd:d+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Z dS); SoSComponentaAny sub-command that sos supports needs to subclass SoSComponent in order to be properly supported by the sos binary. This class contains the standardized entrypoint for subcommands, as well as building out supported options from both globally shared option lists, and options supported by that specific subcommand. When sos initializes, it will load an unintialized instance of each class found within one recursion of the module root directory that subclasses SoSComponent. If sos is able to match the user-specified subcommand to one that exists locally, then that SoSComponent is initialized, logging is setup, and a policy is loaded. From there, the component's execute() method takes over. Added in 4.0 ZunsetTFautoz/etc/sos/sos.confNr) batchcompression_type config_filedebugencrypt encrypt_key encrypt_passquietthreadstmp_dirsysroot verbosityzsos_logs/sos.logzsos_reports/manifest.jsonzsos_commands/process/ps_*zsos_commands/selinux/ps_*z+sos_commands/systemd/systemctl_status_--allcCs||_||_||_d|_d|_d|_d|_d|_yddl}|j|j |j Wn6t k r}zt j jd|dWYdd}~XnXt|jd|_|jr|j|jj|j|j|_|jrH|j}t|_tjj| stj|tj rd|d}|d7}t jj||j d ||_!t"j#d |j!d |_t$|j|_|j%|jdk r|jj&d t'|jj&d dj(|j|jj&dt)j*|jj&dd|jj&dd|jj&dd|jj&d|j|jj&d|j+|jj&d|j,j-|jj.ddS)NFrz'Notice: Could not set SIGTERM handler:  ) arg_defaultsztemporary directory  z"does not exist or is not writable zsos.)prefixdirversioncmdlineZ start_timeZend_timerZrun_timeZ compressiontmpdirZtmpdir_fs_typepolicyZ components)/parserargsr$ exit_processarchiver% tempfile_utilmanifestsignalSIGTERMget_exit_handler Exceptionsysstdoutwriter _arg_defaultsopts load_policyload_local_policyupdater load_optionsconfigure_loggingget_tmpdir_default SoSMetadataospathisdiraccessW_OKstderr_exitZsys_tmptempfileZmkdtempr _setup_logging add_fieldrjoinrZnow tmpfstyper&Zos_release_name add_section)selfr'Z parsed_argsZ cmdline_argsr-errr%msgrM/usr/lib/python3.6/component.py__init__RsT&      zSoSComponent.__init__c Cs^y.ddl}|jj|jj|jd|_|jj|_Wntk rL|jdYnX|jj |_ dS)Nr)rZ probe_runtime) Z sos.policiesZpoliciesloadr5r load_prober&KeyboardInterruptrCZis_rootZ_is_root)rJsosrMrMrNr7s  zSoSComponent.load_local_policycCstdS)N)NotImplementedError)rJrMrMrNexecuteszSoSComponent.executecsfdd}|S)Ncsd_jdS)NT)r)rC)Zsignumframe)rJrMrN exit_handlersz3SoSComponent.get_exit_handler..exit_handlerrM)rJrWrM)rJrNr/s zSoSComponent.get_exit_handlercCs(|r|jjd|jj|t|dS)Nr)ui_logerror SystemExit)rJrYrLrMrMrNrCs  zSoSComponent._exitcCs|jjrtjj|jj}ntjddp(d}tjddr\tjddr\tjjtjd|jd}td|j |_ |j dkrt d t j d |S) zIf --tmp-dir is not specified, provide a default location. Normally this is /var/tmp, but if we detect we are in a container, then use a standardized env var to redirect to the host's filesystem instead ZTMPDIRNz/var/tmpZHOSTZ container/zstat --file-system --format=%T ZtmpfszWARNING: tmp-dir is set to a tmpfs filesystem. This may increase memory pressure and cause instability on low memory systems, or when using --all-logs.)r5rr=r>abspathgetenvrGlstripr striprHprinttimeZsleep)rJr%rMrMrNr;s  zSoSComponent.get_tmpdir_defaultcs6ddjjjD}|r2tfdd|DSdS)NcSsg|]}|jdr|qS)list) startswith).0orMrMrN sz6SoSComponent.check_listing_options..c3s|]}tj|VqdS)N)getattrr5)reopt)rJrMrN sz5SoSComponent.check_listing_options..F)r5dictkeysany)rJr5rM)rJrNcheck_listing_optionssz"SoSComponent.check_listing_optionscCstdS)ziThis should be overridden by each subcommand to add its own unique options to the parser N)rT)clsr'rMrMrNadd_parser_optionsszSoSComponent.add_parser_optionsc sZtj|jj|j|jddd}|jdd}x$|jD]\}}||jjks:|dgdgkrbq:|dkrxFdD]>}t |t t ||@rpt ||fd d t ||DqpW|d kr8t ||}d d |D}d d |D} x6t |t | @D]"dfdd |D}qWt ||t t |t |Bq:||j|kr:t |||q:W|S)a.(Re-)apply options specified via the cmdline to an options instance There are several cases where we may need to re-apply the options from the cmdline over previously loaded options - for instance when an option is specified in both a config file and cmdline, or a preset and the cmdline, or all three. Use this to re-apply cmdline option overrides to anything that may change the default values of options Positional arguments: :param opts: SoSOptions object to update F)Z preset_filter)rNrenable_plugins skip_plugins only_pluginscsg|]}|kr|qSrMrM)rex)commonrMrNrgsz;SoSComponent.apply_options_from_cmdline..ZplugoptscSsg|]}|jddqS)=r)split)revrMrMrNrgscSsg|]}|jddqS)rvr)rw)rerxrMrMrNrgsrvcsg|]}|js|qSrM)rd)reoopt)cstringrMrNrgs)rqrrrs)rqrrrs) r Z from_argsr' parse_argsr$rkitemsrrlsetrhsetattrrc) rJr5ZcmdoptsZcodictrivalryZ oplugoptsZvalnamesZ ovalnamesrM)rurzrNapply_options_from_cmdlines.   $   z'SoSComponent.apply_options_from_cmdlinecCsnt|jd}x |jjD]}|jtkrd|_qW|j|jj|jj d|j ksVd|j krf|j|jjdt j dkrt j jtjd}t j j|r|j||jj |j|}d|_t|drj|j|jdkr|jj|j|_|jstjjd |jd |jj|_d |_|js"|jj|_|j|jj|j|}t|jjd rj|jjjdkrj|j|jjj|S) zxCompile arguments loaded from defaults, config files, and the command line into a usable set of options )rNz--cleanz--maskcleanrz.config/sos/sos.confpresetzUnknown preset: 'z' Tr) r r4r'Z_actionsdefaultrZupdate_from_confr(rZ componentr$r=getuidr>rGrhomeexistsrrhasattrr&Z find_presetr1rBr3Z probe_presetZ list_presetsmerger5rset_loggers_verbosity)rJr5ZoptionZuserconfrMrMrNr9 s8          zSoSComponent.load_optionscCsry4|jr|jj|jr"|jj|jr2t|jWn8tk rl}ztd|d|jWYdd}~XnXdS)NzFailed to finish cleanup: z Contents may remain in )r*cleanupr+rr%rr0ra)rJrKrMrMrNr<s  zSoSComponent.cleanupcCsbd}tjjdr&tjjd|j_d}n tjjdrFtjjd|j_d}|jj||jj|dS)NzFNo encryption environment variables set, archive will not be encryptedZ SOSENCRYPTKEYz+Encryption key set via environment variableZSOSENCRYPTPASSz2Encryption passphrase set via environment variable) r=environgetr5rrsosloginforX)rJrLrMrMrN_set_encrypt_from_env_varsJs   z'SoSComponent._set_encrypt_from_env_varscCs|jjsd}x|d kr$tdj}qW|dkr|jj}tjj |j |}|jj dkr|jj }|||j |j|jj ||j|j|_n"t||j |j|jj ||j|j|_|jj|jjdkdS)N)rkeyZpasswordrr\)r5rrrrr&Zget_archive_namer=r>rGr%rZget_preferred_archiverrr,r*r Z set_debugr)rJnameZenc_optsZ archive_nameZ auto_archiverMrMrN setup_archivens&        zSoSComponent.setup_archivecCsb|jj}xR|jD]H}xBt|j|D]0}tjj||}xdD]}|jj||dq>Wq&WqWdS)N(--upload-pass[\s=]+)\S+$(--upload-url[\s=]+\S+://.*:)([^@]*)!(--upload-s3-secret-key[\s=]+)\S+z \1********)rrr) r*Zget_archive_pathfiles_with_upload_passwdrZglobr=r>relpathZ do_file_sub)rJZ _arc_pathr>frerMrMrN_obfuscate_upload_passwordss  z(SoSComponent._obfuscate_upload_passwordscCsJtjtj}|jtjd|j|jjdkr2tj ntj |j j |dS)Nz %(message)sr ) logging StreamHandlerr1r2 setFormatter FormattersetLevelr5rDEBUGINFOrX addHandler)rJZ ui_consolerMrMrNadd_ui_log_to_stdouts  z!SoSComponent.add_ui_log_to_stdoutcCs\t|ddr|r|jjtjt|ddrX|rJ|jjdkrJ|jjtjn|jjtjdS)Nflogconsoler ) rhrrrrr5rrZWARNING)rJrrMrMrNrs  z"SoSComponent.set_loggers_verbositycCshtjd|_|jjtjd|_|jsn|j|_tj |j|_|jj tj d|jjtj |jj |j|jjstj tj|_|jj tj d|j|jj|jj |jn4tj tj}|j tj d|jtj|jj |tjd|_|jj|jjdkrtjntj |jsR|j|_tj |j}|j tj d|jj ||jjsd|jdS)zCreates the log handler that shall be used by all components and any and all related bits to those components that need to log either to the console or to the log file for that run of sos. rSNz&%(asctime)s %(levelname)s: %(message)sz %(message)sZsos_uir )rZ getLoggerrrrrrn get_temp_fileZ sos_log_filerrrrrr5rr1r2rrrrBZERRORrXZsos_ui_log_filer)rJZ console_errZ ui_fhandlerrMrMrNrEs<            zSoSComponent._setup_loggingcCs |jjS)N)r+new)rJrMrMrNrszSoSComponent.get_temp_filecCs4d}d}x&|jD]}|t||ddd}qW|S)NPrF)Zreplace_whitespacer) splitlinesr)rJrLwidthZ_fmtlinerMrMrN_fmt_msgs zSoSComponent._fmt_msg)rN)r)!__name__ __module__ __qualname____doc__Zdescrr:r6rQZ root_requiredr4rrOr7rUr/rCr;rn classmethodrprr9rrrrrrrrErrrMrMrMrNr sX8   <3   .r c@sVeZdZdZddZddZddZdd Zd d Zd d Z gfddZ dddZ dS)r<a8This class is used to record metadata from a sos execution that will then be stored as a JSON-formatted manifest within the final tarball. It can be extended by adding further instances of SoSMetadata to represent dict-like structures throughout the various sos bits that record to metadata cCs i|_dS)N)_values)rJrMrMrNrOszSoSMetadata.__init__ccs"x|jjD]}|dVq WdS)Nr )rr|)rJitemrMrMrN__iter__szSoSMetadata.__iter__cCs |j|S)N)r)rJrrMrMrN __getitem__szSoSMetadata.__getitem__cCs |j|S)N)r)rJattrrMrMrN __getattr__szSoSMetadata.__getattr__cCs||j|<dS)z@Add a key, value entry to the current metadata instance N)r)rJZ field_namecontentrMrMrNrFszSoSMetadata.add_fieldcCst|j|<|j|S)zCAdds a new instance of SoSMetadata to the current instance )r<r)rJZ section_namerMrMrNrIs zSoSMetadata.add_sectioncCs t|tstd||j|<dS)zAdd a named list element to the current instance. If content is not supplied, then add an empty list that can alter be appended to zcontent added must be listN) isinstancerc TypeErrorr)rJZ list_namerrMrMrNadd_lists zSoSMetadata.add_listNcCstj|dd|dS)zConvert contents of this SoSMetdata instance, and all other nested instances (sections), into a json-formatted output. Used to write manifest.json to the final archives. cSst|dt|S)Nr)rhstr)rfrMrMrNsz&SoSMetadata.get_json..)rindent)jsondumps)rJrrMrMrNget_json szSoSMetadata.get_json)N) rrrrrOrrrrFrIrrrMrMrMrNr<s r<)rrr=rDr1rbtextwraprargparserrrZshutilrZpathlibrrSrZ sos.archiver Z sos.optionsr Z sos.utilitiesr r r r<rMrMrMrN s(         G