a >h>@s>ddlZddlmZddlmZddlmZGdddZdS)N)Lock) ClusterOption)boldc@seZdZdZgZdZgZiZiZdZ dZ dZ ddZ e dd Ze d d Ze d d ZddZddZddZddZddZddZddZddZddZd d!Zd"d#Zd$d%Zdpz'Cluster.display_help..FnewlinezUses the following sos preset: css"|]\}}d|d|VqdS)z-- Nrr kvrrrr"{r#z Sets the following sos options: zEnables the following plugins: css|] }|VqdSrr)r Zplugrrrr"r#css |]\}}|d|VqdS)=Nrr'rrrr"r#z#Sets the following plugin options: zAvailable cluster optionsz/These options may be toggled or changed using 'z-c z.$option=$value' r&z<4z Option Namez<20ZDefault<30 DescriptionzTrue/Onz False/Offr)rdisplay_self_help set_titler__doc__add_text__mro__packagesjoin sos_preset sos_optionsitems sos_pluginssos_plugin_options option_listZ add_sectionrr isinstancebool)rsectionZ_optsr ZoptsecoptvalZ_lnrrr display_help]s~     $zCluster.display_helpcCs|d|d|dddlm}ddl}|||d}|d|jdd d d d d dd|D]<}td|d}|jdd |d |djd ddqrdS)Nz*SoS Collect Cluster Profiles Detailed Helpz Cluster profiles are used to represent different clustering technologies or platforms. Profiles define how cluster nodes are discovered, and optionally filtered, for default executions of collector.zCluster profiles are enabled similarly to SoS report plugins; usually by package, command, or configuration file presence. Clusters may also define default transports for SoS collect.r) SoSCollectorclustersz6The following cluster profiles are locally available: r&z>8Namez<40r.r-Fr$zcollect.clusters.r) r1r3Z sos.collectorrCinspectZ _load_modulesZ getmodulerr)rr?rCrFrDclusterZ_secrrrr0s2   zCluster.display_self_helpcCsB|jD]6}t|d|dj|d|j|dd}|j|qdS)zALoads the options defined by a cluster and sets the default valuerrr/)rZopt_typevaluerG descriptionN)r<rr r rr)rr@optionrrrrs   zCluster._get_optionscCsd|jdd|S)N[rz] )r rmsgrrr_fmt_msgszCluster._fmt_msgcCs|j||dS)zUsed to print info messagesN)rinforNrLrrrlog_infoszCluster.log_infocCs|j|dS)zUsed to print error messagesN)rerrorrLrrr log_errorszCluster.log_errorcCs|j||dS)zUsed to print debug messagesN)rdebugrNrLrrr log_debugszCluster.log_debugcCs|j||dS)zUsed to print warning messagesN)rZwarningrNrLrrrlog_warnszCluster.log_warncCsR|jjD]$}|j|kr|j|jvr|jSq|jD]}|j|kr4|jSq4dS)a This is used to by clusters to check if a cluster option was supplied to sos collect :param option: The name of the option to fetch :type option: ``str`` :returns: The value of the requested option if it exists, or ``False`` F)r Zcluster_optionsrrGr rHr)rrJr@rrr get_options     zCluster.get_optioncCs ||_dS)aSome clusters generate and/or deploy well-known and consistent SSH keys across environments. If this is the case, the cluster profile may call this command so that subsequent node connections will use that key rather than prompting the user for one or a password. Note this will only function if collector is being run locally on the primary node. N)r )rkeyrrradd_default_ssh_keys zCluster.add_default_ssh_keycCsdS)a If there is a need to set specific options on ONLY the non-primary nodes in a collection, override this method in the cluster profile and do that here. :param node: The non-primary node :type node: ``SoSNode`` Nrrnoderrrset_node_optionsszCluster.set_node_optionscCsdS)a`The default connection type used by sos collect is to leverage the local system's SSH installation using ControlPersist, however certain cluster types may want to use something else. Override this in a specific cluster profile to set the ``transport`` option according to what type of transport should be used. Zcontrol_persistrrrrrset_transport_typeszCluster.set_transport_typecCsdS)aIf there is a need to set specific options in the sos command being run on the cluster's primary nodes, override this method in the cluster profile and do that here. :param node: The primary node :type node: ``SoSNode`` NrrYrrrset_primary_optionsszCluster.set_primary_optionscCs|j|jjkS)aIn the event there are multiple primaries, or if the collect command is being run from a system that is technically capable of enumerating nodes but the cluster profiles needs to specify primary-specific options for other nodes, override this method in the cluster profile :param node: The node for the cluster to check :type node: ``SoSNode`` )addressrrYrrrcheck_node_is_primarys zCluster.check_node_is_primaryautocCs4|jj||||d}|dr0|ddd|d<|S)a(Used to retrieve command output from a (primary) node in a cluster :param cmd: The command to run :type cmd: ``str`` :param need_root: Does the command require root privileges :type need_root: ``bool`` :param timeout: Amount of time to allow cmd to run in seconds :type timeout: ``int`` :param use_shell: Does the command required execution within a shell? :type use_shell: ``auto`` or ``bool`` :returns: The output and status of `cmd` :rtype: ``dict`` ) need_root use_shelltimeoutoutputz Password:r)rZ run_commandreplace)rcmdrcrerdresrrrexec_primary_cmd&s  zCluster.exec_primary_cmdcCsdS)z This MAY be used by a cluster to do prep work in case there are extra commands to be run even if a node list is given by the user, and thus get_nodes() would not be called Nrr\rrrsetup?sz Cluster.setupcCs"|jD]}|j|rdSqdS)a This may be overridden by clusters This is called by sos collect on each cluster type that exists, and is meant to return True when the cluster type matches a criteria that indicates that is the cluster type is in use. Only the first cluster type to determine a match is run :returns: ``True`` if the cluster profile should be used, or ``False`` :rtype: ``bool`` TF)r5rZ is_installed)rpkgrrr check_enabledFs  zCluster.check_enabledcCsdS)z This may be overridden by clusters Perform any necessary cleanup steps required by the cluster profile. This helps ensure that sos does make lasting changes to the environment in which we are running Nrr\rrrcleanupXszCluster.cleanupcCstdS)aB This MUST be overridden by a cluster profile subclassing this class A cluster should use this method to return a list or string that contains all the nodes that a report should be collected from :returns: A list of node FQDNs or IP addresses :rtype: ``list`` or ``None`` N)NotImplementedErrorr\rrr get_nodesas zCluster.get_nodesc CsHz |WStyB}z |d|gWYd}~Sd}~00dS)NzFailed to get node list: )format_node_list ExceptionrT)rerrr _get_nodesms  zCluster._get_nodescCs||}|jd||S)aE Used by ``SosNode()`` to retrieve the appropriate label from the cluster as set by ``set_node_label()`` in the cluster profile. :param node: The name of the node to get a label for :type node: ``str`` :returns: The label to use for the node's report :rtype: ``str`` label)set_node_labelZmanifestZ add_field)rrZrurrrget_node_labelts zCluster.get_node_labelcCsdS)zThis may be overridden by clusters profiles subclassing this class If there is a distinction between primaries and nodes, or types of nodes, then this can be used to label the sos report archives as needed rrrYrrrrvszCluster.set_node_labelc Csz |}Wn4ty@}ztd||WYd}~n d}~00t|tr\dd|D}n(t|trvdd|D}ntd|tt|}|D]}|dr||q|S)z Format the returned list of nodes from a cluster into a known format. This being a list that contains no duplicates :returns: A list of nodes, without extraneous entries from cmd output :rtype: ``list`` z#Cluster failed to enumerate nodes: NcSsg|]}|r|qSr)stripr nrrr r#z,Cluster.format_node_list..cSsg|]}|dqS),)splitrxryrrrr{r#z'Cluster returned unexpected node list: )-_()rK]/\)rprrr=liststrset startswithremove)rZnodeserrrrZrrrrqs        zCluster.format_node_listcCsng}zR|}|rTt|ts"|g}|D],}|}||d|vr&||dq&WntyhYn0|S)a) Ensures that any files returned by a cluster's run_extra_cmd() method are properly typed as a list for iterative collection. If any of the files are an additional sos report (e.g. the ovirt db dump) then the md5 sum file is automatically added to the list Z sosreportz.md5)Z run_extra_cmdr=rrxrAttributeError)rfilesriZ extra_filerrr_run_extra_cmds   zCluster._run_extra_cmd)Frarb)'r __module__ __qualname__r2r<r5r:r8r;r7rZstrict_node_listr classmethodrrBr0rrNrPrRrTrUrVrXr[r]r^r`rjrkrmrnrprtrwrvrqrrrrrrsP&  E !        r)r threadingrZ sos.optionsrZ sos.utilitiesrrrrrr s