a >hR@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 gd Z d dZ ddZ ddZddZd5ddZddZddZeddZddZdd Zd!d"Zd#d$Zd%d&Zd6d'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Z dS)7 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 verbosity)zsos_logs/sos.logzsos_reports/manifest.jsonzsos_commands/process/ps_*zsos_commands/selinux/ps_*z+sos_commands/systemd/systemctl_status_--allc Cs||_||_||_d|_d|_d|_d|_d|_zddl}||j | Wn8t y}z t j d|dWYd}~n d}~00t|jd|_|jr||j|j||_|jrF|}t|_tj|rt|tjsd|d}|d7}t j|| d ||_!t"j#d |j!d |_t$|j|_|%|jdur|j&d t'|j&d d(|j|j&dt)*|j&dd|j&dd|j&dd|j&d|j|j&d|j+|j&d|j,j-|j.ddS)NFrz'Notice: Could not set SIGTERM handler:   arg_defaultsztemporary directory  z"does not exist or is not writable zsos.)prefixdirversioncmdlineZ start_timeZend_timerZrun_time compressiontmpdirZtmpdir_fs_typepolicy 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_argsr0errr'msgrP1/usr/lib/python3.9/site-packages/sos/component.py__init__RsV*        zSoSComponent.__init__cCs\z.ddl}|jj|jj|jd|_|jj|_WntyJ|dYn0|j |_ dS)Nr)rZ probe_runtime) Z sos.policiesZpoliciesloadr8r load_prober(KeyboardInterruptrFZis_rootZ_is_root)rMsosrPrPrQr:s  zSoSComponent.load_local_policycCstdSNNotImplementedErrorrMrPrPrQexecuteszSoSComponent.executecsfdd}|S)Ncsd_dS)NT)r,rF)ZsignumframerZrPrQ exit_handlersz3SoSComponent.get_exit_handler..exit_handlerrP)rMr]rPrZrQr2s zSoSComponent.get_exit_handlercCs(|r|jd|j|t|dS)Nr)ui_logerror SystemExit)rMr_rOrPrPrQrFs  zSoSComponent._exitcCsf|jjrtj|jj}ntddp0|jd}td| |_ |j dkrbt dt 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 ZTMPDIRNzstat --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.)r8rr@rAabspathgetenvr(Z get_tmp_dirr striprKprinttimesleep)rMr'rPrPrQr>s   zSoSComponent.get_tmpdir_defaultcs6ddjD}|r2tfdd|DSdS)NcSsg|]}|dr|qS)list startswith).0orPrPrQ z6SoSComponent.check_listing_options..c3s|]}tj|VqdSrW)getattrr8)rkoptrZrPrQ rnz5SoSComponent.check_listing_options..F)r8dictkeysany)rMr8rPrZrQcheck_listing_optionssz"SoSComponent.check_listing_optionscCstdS)ziThis should be overridden by each subcommand to add its own unique options to the parser NrX)clsr*rPrPrQadd_parser_optionsszSoSComponent.add_parser_optionsc sJtj|j|j|jddd}|jdd}|D] \}}||jvs6|dgdfvr^q6|dvrdD]>}t |t t ||@rjt ||fddt ||Dqj|d kr*t ||}d d|D}d d|D} t |t | @D] d fd d|D}qt ||t t |t |Bq6||j|kr6t |||q6|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_filterrNr)Zenable_pluginsZ skip_pluginsZ only_pluginscsg|]}|vr|qSrPrP)rkx)commonrPrQrmsz;SoSComponent.apply_options_from_cmdline..ZplugoptscSsg|]}|ddqS=rsplitrkvrPrPrQrmrncSsg|]}|ddqSrzr|r~rPrPrQrmrnr{csg|]}|s|qSrPri)rkoopt)cstringrPrQrms ) r Z from_argsr* parse_argsr%rritemsrrssetrosetattrrh) rMr8ZcmdoptsZcodictrpvalrZ oplugoptsZvalnamesZ ovalnamesrP)ryrrQapply_options_from_cmdlines0   "   z'SoSComponent.apply_options_from_cmdlinecCsjt|jd}|jjD]}|jtkrd|_q||jj|jj d|j vsRd|j vrb||jjdt dkrt j td}t j |r|||jj ||}d|_t|drf|j|jdkr |j|j|_|js tjd |jd |j|_d |_|js|j|_||jj||}t|jjd rf|jjjdkrf||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 r7r*_actionsdefaultrZupdate_from_confr+rZ componentr%r@getuidrArJrhomeexistsrrhasattrr(Z find_presetr4rEr6Z probe_presetZ list_presetsmerger8rset_loggers_verbosity)rMr8optionZuserconfrPrPrQr<s:           zSoSComponent.load_optionsc Cstz4|jr|j|jr"|j|jr2t|jWn:tyn}z"td|d|jWYd}~n d}~00dS)NzFailed to finish cleanup: z Contents may remain in )r-cleanupr.rr'rr3re)rMrNrPrPrQr9s   zSoSComponent.cleanupcCsbd}tjdr&tjd|j_d}n tjdrFtjd|j_d}|j||j|dS)NzFNo encryption environment variables set, archive will not be encryptedZ SOSENCRYPTKEYz+Encryption key set via environment variableZSOSENCRYPTPASSz2Encryption passphrase set via environment variable) r@environgetr8rrsosloginfor^)rMrOrPrPrQ_set_encrypt_from_env_varsGs   z'SoSComponent._set_encrypt_from_env_varscCs|jjsd}|dvr"td}q |dkr8td|j_q|dkrNtd|j_q|dkr`|qd|j_d|j_|j d|j d n|dS) N)PKENzLSpecify encryption method [P]assphrase, [K]ey, [E]nv vars, [N]o encryption: rzSpecify encryption passphrase: rzSpecify encryption key: rz@User specified --encrypt, but chose no encryption when prompted.zArchive will not be encrypted) r8rinputupperrrrrrrr^Zwarning)rMZ_encrPrPrQ_get_encryption_methodSs"   z#SoSComponent._get_encryption_methodcCs|jjr||jjp|jj|jj|jjd}|s>|j}tj |j |}|jj dkr|j }|||j |j|jj ||j|j|_n"t||j |j|jj ||j|j|_|j|jjdkdS)N)rkeypasswordrra)r8rrrrr(Zget_archive_namer@rArJr'rZget_preferred_archiverrr/r-r Z set_debugr)rMnameZenc_opts archive_nameZ auto_archiverPrPrQ setup_archiveks,     zSoSComponent.setup_archivecCsV|j}|jD]@}t||D],}tj||}dD]}|j||dq8q"qdS)N)z(--upload-pass[\s=]+)\S+z$(--upload-url[\s=]+\S+://.*:)([^@]*)z!(--upload-s3-secret-key[\s=]+)\S+z \1********) r-Zget_archive_pathfiles_with_upload_passwdrglobr@rArelpathZ do_file_sub)rMZ _arc_pathrAfrerPrPrQ_obfuscate_upload_passwordss   z(SoSComponent._obfuscate_upload_passwordscCsJttj}|td||jjdkr2tj ntj |j |dS)N %(message)sr!) logging StreamHandlerr4r5 setFormatter FormattersetLevelr8rDEBUGINFOr^ addHandler)rMZ ui_consolerPrPrQadd_ui_log_to_stdouts  z!SoSComponent.add_ui_log_to_stdoutcCs\t|ddr|r|jtjt|ddrX|rJ|jjdkrJ|jtjn|jtjdS)Nflogconsoler!) rorrrrr8rrZWARNING)rMrrPrPrQrs  z"SoSComponent.set_loggers_verbositycCshtd|_|jtjd|_|sn||_t |j|_|j t d|jtj |j |j|jjst tj|_|j t d||jj|j |jn4t tj}| t d|tj|j |td|_|j|jjdkrtjntj |sR||_t |j}| t d|j ||jjsd|dS)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. rVNz&%(asctime)s %(levelname)s: %(message)srZsos_uir!)rZ getLoggerrrrrru get_temp_fileZ sos_log_filerrrrrr8rr4r5rrrrEZERRORr^Zsos_ui_log_filer)rMZ console_errZ ui_fhandlerrPrPrQrHsB            zSoSComponent._setup_loggingcCs |jSrW)r.newrZrPrPrQrszSoSComponent.get_temp_filecCs0d}d}|D]}|t||ddd}q|S)NPrF)Zreplace_whitespacer) splitlinesr)rMrOwidthZ_fmtlinerPrPrQ_fmt_msgs  zSoSComponent._fmt_msg)rN)r)!__name__ __module__ __qualname____doc__Zdescrr=r9rTZ root_requiredr7rrRr:r[r2rFr>ru classmethodrwrr<rrrrrrrrHrrrPrPrPrQr sT8   <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|_dSrW_valuesrZrPrPrQrRszSoSMetadata.__init__ccs|jD]}|dVq dS)Nr!)rrrMitemrPrPrQ__iter__szSoSMetadata.__iter__cCs |j|SrWrrrPrPrQ __getitem__szSoSMetadata.__getitem__cCs |j|SrWr)rMattrrPrPrQ __getattr__szSoSMetadata.__getattr__cCs||j|<dS)z@Add a key, value entry to the current metadata instance Nr)rM field_namecontentrPrPrQrIszSoSMetadata.add_fieldcCst|j|<|j|S)zCAdds a new instance of SoSMetadata to the current instance )r?r)rMZ section_namerPrPrQrLs 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) isinstancerh TypeErrorr)rMZ list_namerrPrPrQadd_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)rostr)rlrPrPrQrnz&SoSMetadata.get_json..)rindent)jsondumps)rMrrPrPrQget_json szSoSMetadata.get_json)N) rrrrrRrrrrIrLrrrPrPrPrQr?s r?)rrr@rGr4rftextwraprargparserrrshutilrpathlibrrVrZ sos.archiver Z sos.optionsr Z sos.utilitiesr r r r?rPrPrPrQ s(         D