o idF@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 Z ddZ ddZ ddZd0ddZddZddZeddZddZddZd d!Zd"d#Zd$d%Zd1d&d'Zd(d)Zd*d+Zd,d-Zd.d/ZdS)2 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 unsetTFautoz/etc/sos/sos.confNr) batchcompression_type config_filedebugencrypt encrypt_key encrypt_passquietthreadstmp_dirsysroot verbositycCs||_||_||_d|_d|_d|_d|_d|_zddl}||j | Wn t y0Ynw|j |j||_|jr~|}t|_tj|rUt|tjshd|}|d7}tj||d||_tjd|jd|_t |j|_|!|j"r|#|jdur|j$dt%|j$d d &|j|j$d t'(|j$d d |j$dd |j$dd |j$d|j|j$d|j)|j$d|j*j+|j,ddSdS)NFrztemporary directory %s z"does not exist or is not writable zsos.)prefixdirversioncmdline start_timeend_timerrun_time compressiontmpdirtmpdir_fs_typepolicy components)-parserargsr! exit_processarchiver' tempfile_utilmanifestsignalSIGTERMget_exit_handler Exception _arg_defaultsupdate arg_defaults load_optionsoptsconfigure_loggingget_tmpdir_default SoSMetadataospathisdiraccessW_OKsysstderrwrite_exitsys_tmptempfilemkdtempr _setup_logging load_policyload_local_policy add_fieldrjoinrnow tmpfstyper)distro add_section)selfr+ parsed_args cmdline_argsr1r'msgrV//usr/lib/python3/dist-packages/sos/component.py__init__HsX        zSoSComponent.__init__cCs\zddl}|jj|jj|jd|_|jj|_Wnty%|dYnw|j |_ dS)Nr)r probe_runtime) sos.policiespoliciesloadr9r load_prober)KeyboardInterruptrEis_root_is_root)rRsosrVrVrWrKs  zSoSComponent.load_local_policycCstN)NotImplementedErrorrRrVrVrWexecuteszSoSComponent.executecsfdd}|S)Ncsd_dS)NT)r-rE)signumframerdrVrW exit_handlers z3SoSComponent.get_exit_handler..exit_handlerrV)rRrhrVrdrWr3s zSoSComponent.get_exit_handlercCs$|r|jd|j|t|)Nr)ui_logerror SystemExit)rRrjrUrVrVrWrEs  zSoSComponent._exitcCs|jjr tj|jj}ntddpd}tddr.tddr.tjtd|d}tdd|f |_ |j d krGt d t 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 TMPDIRNz/var/tmpHOST container/z!stat --file-system --format=%s %sz%TtmpfszWARNING: 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.)r9rr=r>abspathgetenvrMlstripr striprOprinttimesleep)rRr'rVrVrWr;s   zSoSComponent.get_tmpdir_defaultcs6ddjD}|rtfdd|DSdS)NcSsg|] }|dr|qS)list) startswith).0orVrVrW sz6SoSComponent.check_listing_options..csg|]}tj|qSrV)getattrr9)r{optrdrVrWr}s)r9dictkeysany)rRr9rVrdrWcheck_listing_optionssz"SoSComponent.check_listing_optionscCsdS)ziThis should be overridden by each subcommand to add its own unique options to the parser NrV)clsr+rVrVrWadd_parser_optionsszSoSComponent.add_parser_optionsc stj|j|j|jddd}|jdd}|D]F\}}||jvs-|dgdfvr.q|dvrTdD]}t |t t ||@rSt ||fddt ||Dq4||j|krat |||q|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) preset_filterr7Nr)enable_plugins skip_plugins only_pluginscsg|]}|vr|qSrVrV)r{xcommonrVrWr}sz;SoSComponent.apply_options_from_cmdline..) r from_argsr+ parse_argsr!ritemsr7rsetr~setattr)rRr9cmdoptscodictrvalooptrVrrWapply_options_from_cmdlines$     z'SoSComponent.apply_options_from_cmdlinecCst|jd}|jjD] }|jtkrd|_q ||jj|jj d|j vs)d|j vr1||jjdt dkrNt j td}t j |rN|||jj ||}|S)zxCompile arguments loaded from defaults, config files, and the command line into a usable set of options rNz--cleanz--maskcleanrz.config/sos/sos.conf)r r5r+_actionsdefaultrupdate_from_confr,r componentr!r=getuidr>rMrhomeexistsr)rRr9optionuserconfrVrVrWr8s      zSoSComponent.load_optionsc Csvz|jr |j|jr|j|jrt|jWdSWdSty:}ztd||jfWYd}~dSd}~ww)Nz6Failed to finish cleanup: %s Contents may remain in %s)r.cleanupr/rr'rr4rv)rRerrrVrVrWr s  zSoSComponent.cleanupcCsbd}tjdrtjd|j_d}ntjdr#tjd|j_d}|j||j|dS)NzFNo encryption environment variables set, archive will not be encrypted SOSENCRYPTKEYz+Encryption key set via environment variableSOSENCRYPTPASSz2Encryption passphrase set via environment variable) r=environgetr9rrsosloginfori)rRrUrVrVrW_set_encrypt_from_env_varss   z'SoSComponent._set_encrypt_from_env_varscCs|jjsLd}|dvrtd}|dvs |dkr td|j_dS|dkr,td|j_dS|dkr6|dSd|j_d|j_|j d|j d dS|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) r9rinputupperrrrrrrriwarning)rR_encrVrVrW_get_encryption_method's(   z#SoSComponent._get_encryption_methodcCs|jjr||jjs|jjrdnd|jj|jjd}|s#|j}tj |j |}|jj dkrH|j }|||j |j|jj ||j|j|_nt||j |j|jj ||j|j|_|j|jjdkdS)NTF)rkeypasswordrrq)r9rrrrr)get_archive_namer=r>rMr'rget_preferred_archiverrr0r.r set_debugr)rRnameenc_opts archive_name auto_archiverVrVrW setup_archive?s2     zSoSComponent.setup_archivecCsJttj}|td||jjdkrtj ntj |j |dS)N %(message)sr) logging StreamHandlerrBstdout setFormatter FormattersetLevelr9rDEBUGINFOri addHandler)rR ui_consolerVrVrWadd_ui_log_to_stdoutZs z!SoSComponent.add_ui_log_to_stdoutcCsZ|r$|jr |jtj|jjdkr|jtjdS|jtjdS|jtjdSNr)flogrrrr9rconsoleWARNING)rRrrVrVrWset_loggers_verbositybs z"SoSComponent.set_loggers_verbositycCsftd|_|jtjd|_|s7||_t |j|_|j t d|jtj |j |j|jjsZt tj|_|j t d||jj|j |jnt tj}| t d|tj|j |td|_|j|jjdkrtjntj |s||_t |j}| t d|j ||jjs|dSdS)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. raNz&%(asctime)s %(levelname)s: %(message)srsos_uir)r getLoggerrrrrr get_temp_file sos_log_filerrrrrr9rrBrrrrrCERRORrisos_ui_log_filer)rR console_err ui_fhandlerrVrVrWrImsD           zSoSComponent._setup_loggingcCs |jSrb)r/newrdrVrVrWr zSoSComponent.get_temp_file)rN)r)__name__ __module__ __qualname____doc__descr7r:rJr] root_requiredr5rXrKrer3rEr;r classmethodrrr8rrrrrrrIrrVrVrVrWr sP7   /  .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|_dSrb_valuesrdrVrVrWrXrzSoSMetadata.__init__ccs |jD]}|dVqdSr)rrrRitemrVrVrW__iter__s zSoSMetadata.__iter__cCs |j|SrbrrrVrVrW __getitem__rzSoSMetadata.__getitem__cCs$z|j|WStyt|wrb)rr4AttributeError)rRattrrVrVrW __getattr__s   zSoSMetadata.__getattr__cCs||j|<dS)z@Add a key, value entry to the current metadata instance Nr)rR field_namecontentrVrVrWrLszSoSMetadata.add_fieldcCst|j|<|j|S)zCAdds a new instance of SoSMetadata to the current instance )r<r)rR section_namerVrVrWrQs  zSoSMetadata.add_sectioncCs t|ts td||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) isinstancery TypeErrorr)rR list_namerrVrVrWadd_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)r~str)r|rVrVrWsz&SoSMetadata.get_json..)rindent)jsondumps)rRrrVrVrWget_jsonszSoSMetadata.get_jsonrb) rrrrrXrrrrLrQrrrVrVrVrWr<s r<)rrr=rGrBrwargparserrrshutilrpathlibrrar sos.archiver sos.optionsr sos.utilitiesr r r r<rVrVrVrWs(