Sindbad~EG File Manager

Current Path : /lib/python3.6/site-packages/sos/collector/clusters/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/sos/collector/clusters/__pycache__/ocp.cpython-36.opt-1.pyc

3

�f�?�@s@ddlZddlmZddlmZddlmZGdd�de�ZdS)�N)�quote)�Cluster)�
is_executablecs�eZdZdZdZd4ZdZdZdZdZ	dZ
d5d6d7d8d9d:gZedd��Z
dd�Zdd�Z�fdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Z�ZS);�ocpa�
    This profile is for use with OpenShift Container Platform (v4) clusters
    instead of the kubernetes profile.

    This profile will favor using the `oc` transport type, which means it will
    leverage a locally installed `oc` binary. This is also how node enumeration
    is done. To instead use SSH to connect to the nodes, use the
    '--transport=control_persist' option.

    Thus, a functional `oc` binary for the user executing sos collect is
    required. Functional meaning that the user can run `oc` commands with
    clusterAdmin privileges.

    If this requires the use of a secondary configuration file, specify that
    path with the 'kubeconfig' cluster option. This config file will also be
    used on a single master node to perform API collections if the `with-api`
    option is enabled (default disabled). If no `kubeconfig` option is given,
    but `with-api` is enabled, the cluster profile will attempt to use a
    well-known default kubeconfig file if it is available on the host.

    Alternatively, provide a clusterAdmin access token either via the 'token'
    cluster option or, preferably, the SOSOCPTOKEN environment variable.

    By default, this profile will enumerate only master nodes within the
    cluster, and this may be changed by overriding the 'role' cluster option.
    To collect from all nodes in the cluster regardless of role, use the form
    -c ocp.role=''.

    Filtering nodes by a label applied to that node is also possible via the
    label cluster option, though be aware that this is _combined_ with the role
    option mentioned above.

    To avoid redundant collections of OCP API information (e.g. 'oc get'
    commands), this profile will attempt to enable the API collections on only
    a single master node. If the none of the master nodes have a functional
    'oc' binary available, *and* the --no-local option is used, that means that
    no API data will be collected.
    zOpenShift Container Platform v4�openshift-hyperkube�openshift-clientsFNzsos-collect-tmp��label�3Colon delimited list of labels to select nodes with�role�master�*Colon delimited list of roles to filter on�
kubeconfig�Path to the kubeconfig file�token�1Service account token to use for oc authorization�with-api�'Collect OCP API data from a master node�api-url�.Alternate API URL of an external control-planecCs�|js�d|_|jjj�r||jjd|jjjd�}|ddkr`tjj|jjj|dj	�j
d��|_n|jd�|jd	|d�|j
d
�r�|jd|j
d
���7_|jd|j�|jS)
N�oczwhich oc)�chroot�statusr�output�/zHUnable to to determine PATH for 'oc' command, node enumeration may fail.zLocating 'oc' failed: %srz --kubeconfig zoc base command set to %s)�_oc_cmd�primary�hostZin_container�run_command�sysroot�os�path�join�strip�lstrip�log_warn�	log_debug�
get_option)�selfZ_oc_path�r)�/usr/lib/python3.6/ocp.py�oc_cmdLs"
z
ocp.oc_cmdcCsd|j|fS)zcFormat the oc command to optionall include the kubeconfig file if
        one is specified
        z%s %s)r+)r(�cmdr)r)r*�
fmt_oc_cmdfszocp.fmt_oc_cmdcCs0|j|jd|j�d|jd�����}|ddkS)zXAttempt to login to the API using the oc command using a provided
        token
        z.login --insecure-skip-tls-verify=True --token=� zapi-urlrr)�exec_primary_cmdr-rr')r(Z_resr)r)r*�_attempt_oc_loginls zocp._attempt_oc_logincsTtt|�j�rdS|jd�p&tjdd�|_|jr8|j�|jd�}|j	|�ddkS)NTr�SOSOCPTOKENZwhoamirr)
�superr�
check_enabledr'r �getenvrr0r-r/)r(Z_who)�	__class__r)r*r3ws
zocp.check_enabledcCs�|j�dksdS|j|jd��}|ddk|_|jsF|jd�td��|jd|j�|j|jd	|j��}|ddkr�|j�d
S|jd|d�td
��dS)zpCreate the project that we will be executing in for any nodes'
        collection via a container image
        rNzauth can-i '*' '*'rrzWCheck for cluster-admin privileges returned false, cannot create project in OCP clusterzLInsufficient permissions to create temporary collection project.
Aborting...z#Creating new temporary project '%s'znew-project %sTzFailed to create project: %srz?Failed to create temporary project for collection. 
Aborting...)	�set_transport_typer/r-�oc_cluster_adminr&�	Exception�log_info�project�_label_sos_project)r(�out�retr)r)r*�setup�s
z	ocp.setupcCsXddg}xJ|D]B}|j|jd|j�d|�d���}|ddkstd|d	����qWd
S)z�Add pertinent labels to the temporary project we've created so that
        our privileged containers can properly run.
        z4security.openshift.io/scc.podSecurityLabelSync=falsez-pod-security.kubernetes.io/enforce=privilegedzlabel namespace r.z --overwriterrz!Error applying namespace labels: rN)r/r-r:r8)r(�labelsr	r=r)r)r*r;�s
zocp._label_sos_projectcCs�|jr�yz|j|jd|j���dd�}|ddksD|jd|d���|j|jd|j�d	���}|ddks~|jd
|d���Wn4tk
r�}z|jd|�d��WYd
d
}~XnX|j|jd��d
|_dS)z8Remove the project we created to execute within
        zdelete project �)Ztimeoutrrz"Error deleting temporary project: rzwait namespace/z --for=delete --timeout=30sz3Error waiting for temporary project to be deleted: zAFailed attempting to remove temporary project 'sos-collect-tmp': z.
Please manually remove the temporary project.Nzproject defaultT)r:r/r-Z	log_errorr8)r(r=�errr)r)r*�cleanup�s( zocp.cleanupc	Cs�i}d|dkr�|jd�j�}i}x8dD]0}y|j|j��||<Wq(tk
rVYq(Xq(WxF|D]>}|j�}i||d<x$|D]}|||||d|<q�WqbW|S)a�From the output of get_nodes(), construct an easier-to-reference
        dict of nodes that will be used in determining labels, primary status,
        etc...

        :param nodelist:        The split output of `oc get nodes`
        :type nodelist:         ``list``

        :returns:           A dict of nodes with `get nodes` columns as keys
        :rtype:             ``dict``
        �NAMErr�roles�version�os-image)rrDrErF)�pop�split�index�upperr8)	r(Znodelist�nodesZstatline�idx�state�nodeZ_node�columnr)r)r*�_build_dict�s



 zocp._build_dictcCsT|jjdkr|jjStd|jjjd�r*dS|jd�|jjd�|jj	sPt
d�dS)N�autor)rz]Local installation of 'oc' not found or is not correctly configured. Will use ControlPersist.z=Preferred transport 'oc' not available, will fallback to SSH.z?Press ENTER to continue connecting with SSH, or Ctrl+C toabort.Zcontrol_persist)ZoptsZ	transportrrrrr9�ui_log�warnZbatch�input)r(r)r)r*r6�s
zocp.set_transport_typec
Csg}i|_d}|jd�r>dj|jd�jd��}|dt|�7}|j|j|��}|ddkr�|jd�d	krr|jd
�dd�|jd�jd�D�}|j|d
j	��|_xl|jj
�D]>\}}|r�x0|D]}||dkr�|j|�Pq�Wq�|j|�q�Wnd}	d|d
k�rd}	t|	��|S)Nzget nodes -o wider	�,�:z -l %srrrrz�NOTE: By default, only master nodes are listed.
To collect from all/more nodes, override the role option with '-c ocp.role=role1:role2'cSsg|]}|�qSr)r))�.0�rr)r)r*�
<listcomp>	sz!ocp.get_nodes.<locals>.<listcomp>rrDz'oc' command failedzMissing or incompleteza'oc' failed due to missing kubeconfig on primary node. Specify one via '-c ocp.kubeconfig=<path>')
�	node_dictr'r"rHrr/r-r%rP�
splitlines�items�appendr8)
r(rKr,r?�resrDZ	node_namerNr�msgr)r)r*�	get_nodes�s0



z
ocp.get_nodescCs:|j|jkrdSx$dD]}||j|jdkr|SqWdS)Nrr�workerrD)rra)�addressrZ)r(rNr	r)r)r*�set_node_labels
zocp.set_node_labelcCs$|j|jkrdSd|j|jdkS)NFrrD)rbrZ)r(Zsosnoder)r)r*�check_node_is_primary#szocp.check_node_is_primarycCsD|jd�rd}|rdnd}nd}|r(dnd}|jjd||f�dS)a�In earlier versions of sos, the openshift plugin option that is
        used to toggle the API collections was called `no-oc` rather than
        `with-api`. This older plugin option had the inverse logic of the
        current `with-api` option.

        Use this to toggle the correct plugin option given the node's sos
        version. Note that the use of version 4.2 here is tied to the RHEL
        release (the only usecase for this cluster profile) rather than
        the upstream version given the backports for that downstream.

        :param node:    The node being inspected for API collections
        :type node:     ``SoSNode``

        :param use_api: Should this node enable API collections?
        :type use_api:  ``bool``
        z4.2-16zwith-apiZonZoffzno-oczopenshift.%s=%sN)Zcheck_sos_version�plugoptsr])r(rNZuse_apiZ_optZ_valr)r)r*�_toggle_api_opt(s
zocp._toggle_api_optcCsT|jjd�|jd�s&|j|d�dS|jr<|j|d��nd}|jd�}|rb|jd�rbd|}|ph|}d}|jjr�d	}|jd
dd�r�|d
7}|j	d||jjdd�}|ddkr�|j|d�d|_n^|j
r�|j
|jd<|j|d�d|_n8|j|��r&||k�s|jjd|�|j|d�d|_|j�rPd|j
}|jj|�|jj|�dS)NZ	openshiftzwith-apiFzl/host/etc/kubernetes/static-pod-resources/kube-apiserver-certs/secrets/node-kubeconfigs/localhost.kubeconfigrz/hostz/host/rz/host/bin/ocz/root/.kube/configT)�	need_rootz% --kubeconfig /host/root/.kube/configz	%s whoami)Z
use_containerrgrrr1zopenshift.kubeconfig=%szcAPI collections will be performed on %s
Note: API collections may extend runtime by 10s of minutes
)Zenable_pluginsr]r'rf�api_collect_enabled�
startswithrZ
containerizedZfile_existsrrZsos_env_varsrerbZsoslog�inforR)r(rNZmaster_kubeZ
_optconfigZ_kubeconfigrZcan_ocr_r)r)r*�set_primary_optionsAsJ




zocp.set_primary_optionscCs|j|d�dS)NF)rf)r(rNr)r)r*�set_node_options~szocp.set_node_options)rr)r	rr
)rrr
)rrr)rrr)rFr)rrr)�__name__�
__module__�__qualname__�__doc__Zcluster_nameZpackagesrhrr:r7rZoption_list�propertyr+r-r0r3r>r;rBrPr6r`rcrdrfrkrl�
__classcell__r)r))r5r*rs:&	#=r)r ZpipesrZsos.collector.clustersrZ
sos.utilitiesrrr)r)r)r*�<module>s

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists