idFddlZddlZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ddl m Z ddlmZddlmZdd lmZdd lmZmZGd d ZGd dZy)N)SUPPRESS)datetime)getpass)rmtree)Path) __version__)TarFileArchive) SoSOptions) TempFileUtil shell_outc eZdZdZdZiZdZdZdZdZ dddddddddd dd d Z d Z d Z dZ dZddZdZdZedZdZdZdZdZdZddZdZdZdZdZy) 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 verbosityc||_||_||_d|_d|_d|_d|_d|_ ddl}|j|j|j|jj|j|j|_|j"r|j%}t'|_t(j*j-|r$t)j.|t(j0s:d|z}|dz }t2j4j7||j9d||_t=j>d|j:|_tA|j |_|jC|jDr|jG|jo|jjIdtJ|jjId d jM|j|jjId tOjP|jjId d |jjIdd |jjIdd |jjId|j |jjId|jR|jjId|jTjV|jjYdyy#t$rYwxYw)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_argsr4r*msgs //usr/lib/python3/dist-packages/sos/component.py__init__zSoSComponent.__init__Hs_  # !  !    MM&..$*?*?*A B !!$"3"34%%'  ! !,,.F'MDM77==(99VRWW5/&8<<   % 1 !DL"**&dllKDK!-dkk!:D     !     " " $ == $ MM # #I{ ; MM # #Isxx /E F MM # #L(,,. A MM # #J 3 MM # #J 3 MM # #M2 6 MM # #Hdkk : MM # #$4dnn E MM # #Hdkk.@.@ A MM % %l 3 %;   s.L:: MMcB ddl}|jj|jj|j |_|j j|_|j j|_ y#t$r|jdY//,KDK;;..DL ++- !  JJqM sABBBctN)NotImplementedErrorrUs rYexecutezSoSComponent.executes!!cfd}|S)Nc4d_jy)NT)r0rH)signumframerUs rY exit_handlerz3SoSComponent.get_exit_handler..exit_handlers $D  JJLrj)rUros` rYr6zSoSComponent.get_exit_handlers rjc|r6|jjd|jj|t|)Nr)ui_logerror SystemExit)rUrsrXs rYrHzSoSComponent._exits4  KK  b ! KK  c "rjcP|jjr4tjj |jj}ntj ddxsd}tj ddrXtj ddrBtjj tj d|jd}tddd |j|_ |jd k(r td tjd |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/zstat --file-system --format=z%Tr%tmpfszWARNING: 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.)r<rr@rAabspathgetenvrPlstripr striprRprinttimesleep)rUr*s rYr>zSoSComponent.get_tmpdir_defaults 99  WW__TYY%6%67FYYx.<*F 99VT "ryyd'CWW\\"))F"3V]]35GHF#37 @ %'  >>W $ > ? JJqM rjc |jjjDcgc]}|jds|}}|r-t |Dcgc]}t |j|c}Sycc}wcc}w)Nlist)r<dictkeys startswithanygetattr)rUor<opts rYcheck_listing_optionsz"SoSComponent.check_listing_optionssf99>>+002Kaall66JKK 4@C 3/@A A L@sA8A8A=cy)ziThis should be overridden by each subcommand to add its own unique options to the parser Nrp)clsr.s rYadd_parser_optionszSoSComponent.add_parser_optionss rjc 8tj|jj|j|j d}|j d}|j D]\}}||jjvs|dgdfvr*|dvrTdD]O}t|tt||z}|s't||t||Dcgc]}||vr| c}Q||j|k7st||||Scc}w)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_filterr:Nr)enable_plugins skip_plugins only_plugins) r from_argsr. parse_argsr$ritemsr:rsetrsetattr) rUr<cmdoptscodictrvalooptcommonxs rYapply_options_from_cmdlinez'SoSComponent.apply_options_from_cmdlines%&,(( KK " "4<< 07) E2  (HC'..3355r29NHHN5D XGD$,?(@@Fdd8K-41#$F?./-45 5d'',,c3'! ($ -4s Dct|j}|jjD]}|jt k7sd|_|j |jj|jjd|jvsd|jvr&|j |jjdtjdk7rwtjjtj d}tjj#|r&|j ||jj|j%|}|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 r8r._actionsdefaultrupdate_from_confr/r componentr$r@getuidrArPrhomeexistsr)rUr<optionuserconfs rYr;zSoSComponent.load_optionss t'9'9:kk** &F~~)!% & dii33TYY5H5HI  $DLL(@  ! !$))"7"7 A 99;! ww||DIIK1GHHww~~h'%%h 0C0CD..t4 rjcD |jr|jj|jr|jj|jrt |jyy#t $r%}td|d|jYd}~yd}~wwxYw)NzFailed to finish cleanup: z Contents may remain in )r1cleanupr2rr*rr7r)rUerrs rYrzSoSComponent.cleanup s| (|| $$&!!""((*{{t{{# ( $++' ( ( (sA-A11 B:BBcd}tjjdr1tjjd|j_d}nOtjjdr0tjjd|j_d}|j j||jj|y)NzFNo encryption environment variables set, archive will not be encrypted SOSENCRYPTKEYz+Encryption key set via environment variableSOSENCRYPTPASSz2Encryption passphrase set via environment variable) r@environgetr<rrsosloginforr)rUrXs rY_set_encrypt_from_env_varsz'SoSComponent._set_encrypt_from_env_varss ::>>/ *$&JJNN?$CDII !?C ZZ^^, -%'ZZ^^4D%EDII "FC  rjc|jjsd}|dvrtdj}|dvr|dk(rt d|j_y|dk(rtd|j_y|dk(r|jyd|j_d|j_|jjd|jjd y|jy) 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) r<rinputupperrrrrrrrrwarning)rU_encs rY_get_encryption_methodz#SoSComponent._get_encryption_method'syyD22.5722 s{)02@*A &(-.H(I %//1(, %)- &   "=> ##$CD  + + -rjc |jjr|j|jjs|jjrdnd|jj|jjd}|s|j j }tjj|j|}|jjdk(rj|j j}|||j|j |jj||j|j|_nRt#||j|j |jj||j|j|_|j j%|jj&dkDy)NTF)rkeypasswordrr{)r<rrrrr,get_archive_namer@rArPr*rget_preferred_archiverrr3r1r set_debugr)rUnameenc_opts archive_name auto_archives rY setup_archivezSoSComponent.setup_archive?sD 99    ' ' ) $ 6 6 $ 5 5L' dkk(, TYY5F5F(0$,,(, 7DL *, *.++tyy7H7H*2DLL*.--9DL tyy22Q67rjcXtjtj}|j tj d|j |jjdkDrtjntj|jj|y)N %(message)sr ) logging StreamHandlerrEstdout setFormatter FormattersetLevelr<rDEBUGINFOrr addHandler)rU ui_consoles rYadd_ui_log_to_stdoutz!SoSComponent.add_ui_log_to_stdoutZso**3::6  1 1- @A!YY0014GMM',,  z*rjc|r|jr)|jjtj|jj dkDr*|j jtjy|j jtjy|j jtjyNr )flogrrrr<rconsoleWARNING)rUrs rYset_loggers_verbosityz"SoSComponent.set_loggers_verbositybsv yy ""7==1yy""Q& %%gmm4 %%goo6 LL ! !'// 2rjctjd|_|jjtjd|_|j s|j|_tj|j|_|j jtjd|j jtj|jj|j |jjstjt j"|_|j$jtjd|j'|jj(|jj|j$ntjt j*}|jtjd|jtj,|jj|tjd|_|j.j|jj(dkDrtjntj|j ss|j|_tj|j0}|jtjd|j.j||jjs|j3yy)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. rdNz&%(asctime)s %(levelname)s: %(message)srsos_uir )r getLoggerrrrrr get_temp_file sos_log_filerrrrrr<rrErrrrrFERRORrrsos_ui_log_filer)rU console_err ui_fhandlers rYrLzSoSComponent._setup_loggingms< ''.  W]]+ ))+ $ 2 2 4D --d.?.?@DI II " "7#4#48$: ; II  w|| , KK " "499 -yy"00
r classmethodrrr;rrrrrrrLrrprjrYrrs$ DLKJM"* M54n."  :B   -^: ( .086+ 3,(\(rjrcFeZdZdZdZdZdZdZdZdZ gfdZ d d Z y ) 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 ci|_yrf_valuesrhs rYrZzSoSMetadata.__init__s  rjc#XK|jjD] }|d ywr)rrrUitems rY__iter__zSoSMetadata.__iter__s+LL&&( Dq'M s(*c |j|Srfrrs rY __getitem__zSoSMetadata.__getitem__s||D!!rjcR |j|S#t$r t|wxYwrf)rr7AttributeError)rUattrs rY __getattr__zSoSMetadata.__getattr__s0 '<<% % ' & & 's&c"||j|<y)z@Add a key, value entry to the current metadata instance Nr)rU field_namecontents rYrOzSoSMetadata.add_fields$+ Z rjcNt|j|<|j|S)zCAdds a new instance of SoSMetadata to the current instance )r?r)rU section_names rYrTzSoSMetadata.add_sections#&1] \"||L))rjcXt|ts td||j|<y)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)rU list_namers rYadd_listzSoSMetadata.add_lists('4(89 9") YrjNc4tj|d|S)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. c.t|dt|S)Nr)rstr)rs rYz&SoSMetadata.get_json..sGAy#a&,Irj)rindent)jsondumps)rUrs rYget_jsonzSoSMetadata.get_jsons zz$"I!') )rjrf) rrrrrZrrrrOrTr rrprjrYr?r?s4"' + * +-*)rjr?)rrr@rJrErargparserrrshutilrpathlibrrdr sos.archiver sos.optionsr sos.utilitiesr r rr?rprjrYrsK   &"1~(~(B 4)4)rj