eaiovnaovbqoebvqoeavibavo 3 f6O@sddlZddlZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ddl m Z ddlmZddlmZdd lmZdd lmZmZGd d d ZGd ddZdS)N)SUPPRESS)datetime)getpass)rmtree)Path) __version__)TarFileArchive) SoSOptions) TempFileUtil shell_outc @seZdZdZdZiZdZdZdZdZ dddddddddd dd d Z d d dddgZ ddZ ddZ ddZddZd7ddZddZddZedd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd8d+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6ZdS)9 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_--allc Cs||_||_||_d|_d|_d|_d|_d|_yddl}|j|j |j Wnt k rbYnXt |j d|_|jr|j|j j|j|j|_|jr |j}t|_tjj| stj|tj rd|}|d7}tjj||jd||_ t!j"d|j d|_t#|j|_|j$|jdk r|jj%d t&|jj%d d j'|j|jj%d t(j)|jj%d d|jj%dd|jj%dd|jj%d|j|jj%d|j*|jj%d|j+j,|jj-ddS)NFr) arg_defaultsztemporary directory %s z"does not exist or is not writable zsos.)prefixdirversioncmdline Z start_timeZend_timerZrun_timeZ compressiontmpdirZtmpdir_fs_typepolicyZ components).parserargsr! exit_processarchiver# tempfile_utilmanifestsignalSIGTERMget_exit_handler Exceptionr _arg_defaultsopts load_policyload_local_policyupdater load_optionsconfigure_loggingget_tmpdir_default SoSMetadataospathisdiraccessW_OKsysstderrwrite_exitZsys_tmptempfileZmkdtempr _setup_logging add_fieldrjoinrZnow tmpfstyper$Zdistro add_section)selfr%Z parsed_argsZ cmdline_argsr+r#msgrI/usr/lib/python3.6/component.py__init__QsT     zSoSComponent.__init__c Cs^y.ddl}|jj|jj|jd|_|jj|_Wntk rL|jdYnX|jj |_ dS)Nr)rZ probe_runtime) Z sos.policiesZpoliciesloadr0r load_prober$KeyboardInterruptr@Zis_rootZ_is_root)rGsosrIrIrJr2s  zSoSComponent.load_local_policycCstdS)N)NotImplementedError)rGrIrIrJexecuteszSoSComponent.executecsfdd}|S)Ncsd_jdS)NT)r'r@)Zsignumframe)rGrIrJ exit_handlersz3SoSComponent.get_exit_handler..exit_handlerrI)rGrSrI)rGrJr-s zSoSComponent.get_exit_handlercCs(|r|jjd|jj|t|dS)Nr)ui_logerror SystemExit)rGrUrHrIrIrJr@s  zSoSComponent._exitcCs|jjrtjj|jj}ntjddp(d}tjddr\tjddr\tjjtjd|jd}tdd|fj |_ |j d krt 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/z!stat --file-system --format=%s %sz%TZtmpfszWARNING: 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.)r0rr8r9abspathgetenvrDlstripr striprEprinttimeZsleep)rGr#rIrIrJr6s  zSoSComponent.get_tmpdir_defaultcs6ddjjjD}|r2tfdd|DSdS)NcSsg|]}|jdr|qS)list) startswith).0orIrIrJ sz6SoSComponent.check_listing_options..csg|]}tj|qSrI)getattrr0)raopt)rGrIrJrcsF)r0dictkeysany)rGr0rI)rGrJcheck_listing_optionssz"SoSComponent.check_listing_optionscCsdS)ziThis should be overridden by each subcommand to add its own unique options to the parser NrI)clsr%rIrIrJadd_parser_optionsszSoSComponent.add_parser_optionsc stj|jj|j|jddd}|jdd}x|jD]\}}||jjks8|dgdgkr^q8|d krxFd D]>}t |t t ||@rlt ||fd d t ||DqlW||j|kr8t |||q8W|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|qSrIrI)rax)commonrIrJrcsz;SoSComponent.apply_options_from_cmdline..)rlrmrn)rlrmrn) r Z from_argsr% parse_argsr!rfitemsrrgsetrdsetattr)rGr0ZcmdoptsZcodictrevalZooptrI)rprJapply_options_from_cmdlines   $z'SoSComponent.apply_options_from_cmdlinecCsjt|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|drf|j|jdkr |jj|j|_|js tjjd |j|jj|_d |_|js|jj|_|j|jj|j|}t|jjd rf|jjjdkrf|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: '%s' Tr) r r/r%Z_actionsdefaultrZupdate_from_confr&rZ componentr!r8getuidr9rDrhomeexistsrvrxhasattrr$Z find_presetr=r>r?Z probe_presetZ list_presetsmerger0rset_loggers_verbosity)rGr0ZoptionZuserconfrIrIrJr4s8          zSoSComponent.load_optionscCsny4|jr|jj|jr"|jj|jr2t|jWn4tk rh}ztd||jfWYdd}~XnXdS)Nz6Failed to finish cleanup: %s Contents may remain in %s)r(cleanupr)rwr#rr.r])rGerrrIrIrJr.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) r8environgetr0rrsosloginforT)rGrHrIrIrJ_set_encrypt_from_env_vars<s   z'SoSComponent._set_encrypt_from_env_varscCs|jjsd}x|d kr$tdj}qW|dkrWq&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_passwdrZglobr8r9relpathZ do_file_sub)rGZ _arc_pathr9frerIrIrJ_obfuscate_upload_passwords{s  z(SoSComponent._obfuscate_upload_passwordscCsJtjtj}|jtjd|j|jjdkr2tj ntj |j j |dS)Nz %(message)sr) logging StreamHandlerr=stdout setFormatter FormattersetLevelr0rDEBUGINFOrT addHandler)rGZ ui_consolerIrIrJadd_ui_log_to_stdouts  z!SoSComponent.add_ui_log_to_stdoutcCs\t|ddr|r|jjtjt|ddrX|rJ|jjdkrJ|jjtjn|jjtjdS)Nflogconsoler) rdrrrrr0rrZWARNING)rGrrIrIrJrs  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. rONz&%(asctime)s %(levelname)s: %(message)sz %(message)sZsos_uir)rZ getLoggerrrrrri get_temp_fileZ sos_log_filerrrrrr0rr=rrrrr>ZERRORrTZsos_ui_log_filer)rGZ console_errZ ui_fhandlerrIrIrJrBs<            zSoSComponent._setup_loggingcCs |jjS)N)r)new)rGrIrIrJrszSoSComponent.get_temp_file)rN)r) __name__ __module__ __qualname____doc__Zdescrr5r1rMZ root_requiredr/rrKr2rQr-r@r6ri classmethodrkrvr4rrrrrrrrBrrIrIrIrJr sV8   /3   .r c@sVeZdZdZddZddZddZdd Zd d Zd d Z gfddZ dddZ dS)r7a8This 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)rGrIrIrJrKszSoSMetadata.__init__ccs"x|jjD]}|dVq WdS)Nr)rrr)rGitemrIrIrJ__iter__szSoSMetadata.__iter__cCs |j|S)N)r)rGrrIrIrJ __getitem__szSoSMetadata.__getitem__c Cs,y |j|Stk r&t|YnXdS)N)rr.AttributeError)rGattrrIrIrJ __getattr__s zSoSMetadata.__getattr__cCs||j|<dS)z@Add a key, value entry to the current metadata instance N)r)rGZ field_namecontentrIrIrJrCszSoSMetadata.add_fieldcCst|j|<|j|S)zCAdds a new instance of SoSMetadata to the current instance )r7r)rGZ section_namerIrIrJrFs 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) isinstancer_ TypeErrorr)rGZ list_namerrIrIrJadd_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)rdstr)rbrIrIrJsz&SoSMetadata.get_json..)ryindent)jsondumps)rGrrIrIrJget_jsonszSoSMetadata.get_json)N) rrrrrKrrrrCrFrrrIrIrIrJr7s r7)rrr8rAr=r^argparserrrZshutilrZpathlibrrOrZ sos.archiverZ sos.optionsr Z sos.utilitiesr r r r7rIrIrIrJ s&        4