grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026a Û1jFSã@sÌddlZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z ddl m Z ddlmZddlmZdd lmZdd lmZdd lmZmZGd d „d ƒZGdd„dƒZdS)éN)Úfill)ÚSUPPRESS)Údatetime)Úgetpass)Úrmtree)ÚPath)Ú __version__)ÚTarFileArchive)Ú SoSOptions)Ú TempFileUtilÚ shell_outc @söeZdZdZdZiZdZdZdZdZ dddddddddd dd d œ Z gd ¢Z d d„Z dd„Z dd„Zdd„Zd5dd„Zdd„Zdd„Zedd„ƒZdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd6d'd(„Zd)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Zd3d4„Z dS)7Ú SoSComponentaùAny 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 ZunsetTFÚautoz/etc/sos/sos.confNéÚr) ÚbatchÚcompression_typeÚ config_fileÚdebugÚencryptÚ encrypt_keyÚ encrypt_passÚquietÚthreadsÚtmp_dirÚsysrootÚ 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|›d�¡WYd}~n d}~00t|jd�|_|jr¤| ¡|j |j¡| ¡|_|j�rF| ¡}tƒ|_tj |¡rðt |tj¡�sd|›d�}|d7}t j |¡|  d ¡||_!t"j#d |j!d �|_t$|jƒ|_| %¡|jdu�rð|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 .d¡dS)NFrz'Notice: Could not set SIGTERM handler: Ú ©Ú arg_defaultsztemporary directory ú z"does not exist or is not writable ézsos.)ÚprefixÚdirÚversionÚcmdlineZ start_timeZend_timerZrun_timeÚ compressionÚtmpdirZtmpdir_fs_typeÚpolicyÚ components)/ÚparserÚargsr%Ú exit_processÚarchiver'Ú tempfile_utilÚmanifestÚsignalÚSIGTERMÚget_exit_handlerÚ ExceptionÚsysÚstdoutÚwriter Ú _arg_defaultsÚoptsÚ load_policyÚload_local_policyÚupdaterÚ load_optionsÚconfigure_loggingÚget_tmpdir_defaultÚ SoSMetadataÚosÚpathÚisdirÚaccessÚW_OKÚstderrÚ_exitZsys_tmpÚtempfileZmkdtempr Ú_setup_loggingÚ add_fieldrÚjoinrZnowÚ tmpfstyper(Zos_release_nameÚ add_section)Úselfr*Z parsed_argsZ cmdline_argsr0Úerrr'Úmsg©rPú1/usr/lib/python3.9/site-packages/sos/component.pyÚ__init__RsV*   ÿ     zSoSComponent.__init__cCs\z.ddl}|jj|jj|jd�|_|jj|_WntyJ| d¡Yn0|j  ¡|_ dS)Nr)rZ probe_runtime) Z sos.policiesZpoliciesÚloadr8rÚ load_prober(ÚKeyboardInterruptrFZis_rootZ_is_root)rMÚsosrPrPrQr:Šs ÿ zSoSComponent.load_local_policycCst‚dS©N©ÚNotImplementedError©rMrPrPrQÚexecute”szSoSComponent.executecs‡fdd„}|S)Ncsdˆ_ˆ ¡dS)NT)r,rF)ZsignumÚframerZrPrQÚ exit_handler˜sz3SoSComponent.get_exit_handler..exit_handlerrP)rMr]rPrZrQr2—s zSoSComponent.get_exit_handlercCs(|r|j d¡|j |¡t|ƒ‚dS)Nr)Úui_logÚerrorÚ SystemExit)rMr_rOrPrPrQrF�s  zSoSComponent._exitcCsf|jjrtj |jj¡}nt dd¡p0|j d¡}td|›�ƒ  ¡|_ |j dkrbt dƒt   d¡|S)zãIf --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 Ztmpfsz—WARNING: 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@rAÚabspathÚgetenvr(Z get_tmp_dirr ÚstriprKÚprintÚtimeÚsleep)rMr'rPrPrQr>£sÿ   zSoSComponent.get_tmpdir_defaultcs6dd„ˆj ¡ ¡Dƒ}|r2t‡fdd„|DƒƒSdS)NcSsg|]}| d¡r|‘qS)Úlist©Ú startswith)Ú.0ÚorPrPrQÚ ¾óz6SoSComponent.check_listing_options..c3s|]}tˆj|ƒVqdSrW)Úgetattrr8)rkÚoptrZrPrQÚ Àrnz5SoSComponent.check_listing_options..F)r8ÚdictÚkeysÚany)rMr8rPrZrQÚcheck_listing_options½sz"SoSComponent.check_listing_optionscCst‚dS)ziThis should be overridden by each subcommand to add its own unique options to the parser NrX)Úclsr*rPrPrQÚadd_parser_optionsÃszSoSComponent.add_parser_optionsc sxtƒj|j |j¡|jdd�d�}|jdd�}| ¡D�]:\}}||j ¡vs6|dgdfvr^q6|dvrªdD]>}t |ƒt t ||ƒƒ@‰ˆrjt ||‡fdd„t ||ƒDƒƒqj|d k�r*t ||ƒ}d d„|Dƒ}d d„|Dƒ} t |ƒt | ƒ@D] ‰ˆ›d �‰‡fd d„|Dƒ}qêt ||t t |ƒt |ƒBƒƒq6|dv�rXt ||t t |ƒt t ||ƒƒBƒƒq6||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)Úenable_pluginsÚ skip_pluginsZ only_pluginscsg|]}|ˆvr|‘qSrPrP)rkÚx)ÚcommonrPrQrmñsÿz;SoSComponent.apply_options_from_cmdline..ZplugoptscSsg|]}| d¡d‘qS©ú=r©Úsplit©rkÚvrPrPrQrmúrncSsg|]}| d¡d‘qSr|r~r€rPrPrQrmûrnr}csg|]}| ˆ¡s|‘qSrPri)rkÚoopt)ÚcstringrPrQrmþs ÿ)ryrx) r Z from_argsr*Ú parse_argsr%rrÚitemsrrsÚsetroÚsetattrrh) rMr8ZcmdoptsZcodictrpÚvalr‚Z oplugoptsZvalnamesZ ovalnamesrP)r{rƒrQÚapply_options_from_cmdlineÊs4  þ "    $z'SoSComponent.apply_options_from_cmdlinecCsnt|jd�}|jjD]}|jtkrd|_q| |jj|jj ¡d|j vsRd|j vrb| |jjd¡t   ¡dkrœt j  t ¡d¡}t j  |¡rœ| ||jj ¡| |¡}d|_t|dƒ�rj|j|jdk�r |j |j¡|_|j�s tj d |j›d �¡|j ¡|_d |_|j�s|j ¡|_|j|jjd d �| |¡}t|jjd ƒ�rj|jjjdk�rj| |jjj¡|S)zxCompile arguments loaded from defaults, config files, and the command line into a usable set of options rNz--cleanz--maskÚcleanrz.config/sos/sos.confÚpresetzUnknown preset: 'z' T)Z prefer_newr) r r7r*Ú_actionsÚdefaultrZupdate_from_confr+rZ componentr%r@ÚgetuidrArJrÚhomeÚexistsr‰r‹Úhasattrr(Z find_presetr4rEr6Z probe_presetZ list_presetsÚmerger8rÚset_loggers_verbosity)rMr8ÚoptionZuserconfrPrPrQr<s:           ÿzSoSComponent.load_optionsc Cstz4|jr|j ¡|jr"|j ¡|jr2t|jƒWn:tyn}z"td|›d|j›�ƒWYd}~n d}~00dS)NzFailed to finish cleanup: z Contents may remain in )r-Úcleanupr.rŠr'rr3re)rMrNrPrPrQr•;s   ÿzSoSComponent.cleanupcCsbd}tj d¡r&tj d¡|j_d}n tj d¡rFtj d¡|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@ÚenvironÚgetr8rrÚsoslogÚinfor^)rMrOrPrPrQÚ_set_encrypt_from_env_varsIs   z'SoSComponent._set_encrypt_from_env_varscCs–|jjsŠd}|dvr"tdƒ ¡}q |dkr8tdƒ|j_q’|dkrNtdƒ|j_q’|dkr`| ¡q’d|j_d|j_|j  d¡|j   d ¡n| ¡dS) N)ÚPÚKÚEÚNzLSpecify encryption method [P]assphrase, [K]ey, [E]nv vars, [N]o encryption: r›zSpecify encryption passphrase: rœzSpecify encryption key: r�z@User specified --encrypt, but chose no encryption when prompted.zArchive will not be encrypted) r8rÚinputÚupperrrrršr˜r™r^Zwarning)rMZ_encrPrPrQÚ_get_encryption_methodUs"ÿ   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 |jjdk¡dS)N)rÚkeyÚpasswordrra)r8rr¡rrr(Zget_archive_namer@rArJr'rZget_preferred_archiverrr/r-r Z set_debugr)rMÚnameZenc_optsÚ archive_nameZ auto_archiverPrPrQÚ setup_archivems,ý    ý ýzSoSComponent.setup_archivecCsV|j ¡}|jD]@}t|ƒ |¡D],}tj ||¡}dD]}|j ||d¡q8q"qdS)N)z(--upload-pass[\s=]+)\S+z$(--upload-url[\s=]+\S+://.*:)([^@]*)z!(--upload-s3-secret-key[\s=]+)\S+z \1********) r-Zget_archive_pathÚfiles_with_upload_passwdrÚglobr@rAÚrelpathZ do_file_sub)rMZ _arc_pathrAÚfÚrerPrPrQÚ_obfuscate_upload_passwords‡s   z(SoSComponent._obfuscate_upload_passwordscCsJt tj¡}| t d¡¡| |jjdkr2tj ntj ¡|j   |¡dS)Nú %(message)sr!) ÚloggingÚ StreamHandlerr4r5Ú setFormatterÚ FormatterÚsetLevelr8rÚDEBUGÚINFOr^Ú addHandler)rMZ ui_consolerPrPrQÚadd_ui_log_to_stdout˜s  ÿz!SoSComponent.add_ui_log_to_stdoutcCs\t|ddƒr|r|j tj¡t|ddƒrX|rJ|jjdkrJ|j tj¡n|j tj¡dS)NÚflogÚconsoler!) ror·r²r®r³r8rr¸ZWARNING)rMrrPrPrQr“ s  z"SoSComponent.set_loggers_verbositycCsht d¡|_|j tj¡d|_| ¡sn| ¡|_t  |j¡|_|j  t  d¡¡|j tj ¡|j  |j¡|jjs´t  tj¡|_|j  t  d¡¡| |jj¡|j  |j¡n4t  tj¡}|  t  d¡¡| tj¡|j  |¡t d¡|_|j |jjdk�rtjntj ¡| ¡�sR| ¡|_t  |j¡}|  t  d¡¡|j  |¡|jj�sd| ¡dS)zÕCreates 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)sr­Zsos_uir!)r®Z getLoggerr˜r²r³r·ruÚ get_temp_fileZ sos_log_filer¯r°r±r´rµr8rr4r5r¸r“rrEZERRORr^Zsos_ui_log_filer¶)rMZ console_errZ ui_fhandlerrPrPrQrH©sB   ÿ    ÿ   ÿ  zSoSComponent._setup_loggingcCs |j ¡SrW)r.ÚnewrZrPrPrQr¹×szSoSComponent.get_temp_filecCs0d}d}| ¡D]}|t||dd�d}q|S)NéPrF)Zreplace_whitespacer)Ú splitlinesr)rMrOÚwidthZ_fmtÚlinerPrPrQÚ_fmt_msgÚs  zSoSComponent._fmt_msg)rN)r)!Ú__name__Ú __module__Ú __qualname__Ú__doc__Zdescrr=r9rTZ root_requiredr7r§rRr:r[r2rFr>ruÚ classmethodrwr‰r<r•ršr¡r¦r¬r¶r“rHr¹r¿rPrPrPrQr sTô8   >3   .r c@sVeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z gfdd„Z ddd„Z 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©Ú_valuesrZrPrPrQrRëszSoSMetadata.__init__ccs|j ¡D]}|dVq dS)Nr!)rÆr…©rMÚitemrPrPrQÚ__iter__îszSoSMetadata.__iter__cCs |j|SrWrÅrÇrPrPrQÚ __getitem__òszSoSMetadata.__getitem__cCs |j|SrWrÅ)rMÚattrrPrPrQÚ __getattr__õszSoSMetadata.__getattr__cCs||j|<dS)z@Add a key, value entry to the current metadata instance NrÅ)rMÚ field_nameÚcontentrPrPrQrIøszSoSMetadata.add_fieldcCstƒ|j|<|j|S)zCAdds a new instance of SoSMetadata to the current instance )r?rÆ)rMZ section_namerPrPrQrLýs zSoSMetadata.add_sectioncCs t|tƒstdƒ‚||j|<dS)z“Add 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_namerÎrPrPrQÚadd_lists zSoSMetadata.add_listNcCstj|dd„|d�S)zÃConvert 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Æ)roÚstr)rlrPrPrQÚrnz&SoSMetadata.get_json..)r�Úindent)ÚjsonÚdumps)rMrÔrPrPrQÚget_json sþzSoSMetadata.get_json)N) rÀrÁrÂrÃrRrÉrÊrÌrIrLrÑr×rPrPrPrQr?âs r?)rÕr®r@rGr4rfÚtextwraprÚargparserrrÚshutilrÚpathlibrrVrZ sos.archiver Z sos.optionsr Z sos.utilitiesr r r r?rPrPrPrQÚ s(         F