a >h%@sLddlZddlZddlZddlmZddlmZmZmZGdddeZ dS)N)RemoteTransport) is_executablesos_get_command_outputSoSTimeoutErrorcs~eZdZdZdZdZddZeddZdd Z d d Z fd d Z dfdd Z ddZ eddZddZddZZS) OCTransportaq This transport leverages the execution of commands via a locally available and configured ``oc`` binary for OCPv4 environments. The location of the oc binary MUST be in the $PATH used by the locally loaded SoS policy. Specifically this means that the binary cannot be in the running user's home directory, such as ~/.local/bin. OCPv4 clusters generally discourage the use of SSH, so this transport may be used to remove our use of SSH in favor of the environment provided method of connecting to nodes and executing commands via debug pods. The debug pod created will be a privileged pod that mounts the host's filesystem internally so that sos report collections reflect the host, and not the container in which it runs. This transport will execute within a temporary 'sos-collect-tmp' project created by the OCP cluster profile. The project will be removed at the end of execution. In the event of failures due to a misbehaving OCP API or oc binary, it is recommended to fallback to the control_persist transport by manually setting the --transport option. oczsos-collect-tmpcKstd|jd|fi|S)z\Format and run a command with `oc` in the project defined for our execution oc -n  )rproject)selfcmdkwargsr?/usr/lib/python3.9/site-packages/sos/collector/transports/oc.pyrun_oc1s zOCTransport.run_occCs|d|j}|ddkS)Nz,wait --timeout=0s --for=condition=ready pod/statusrrpod_name)r uprrr connected:s zOCTransport.connectedcCsdd|jddd|jdddd d d d d dd d d ddd d d dddd d gd|jjsjdn|jjdgdddgidddd ddddddddgddddddd g|jjrdndd |jdddd!d"S)#zBased on our template for the debug container, add the node-specific items so that we can deploy one of these on each node we're collecting from ZPodZv1.rz-sos-collector)name namespacezsystem-cluster-criticalhost/Z Directory)pathtype)rZhostPathrunz/runZvarlogz/var/logz machine-idz/etc/machine-idZFilezsos-collector-tmpz®istry.redhat.io/rhel8/support-toolsz /bin/bashZHOSTz/host)rvalue)rZ mountPathT)Z privilegedZ runAsUser) rimageZcommandenvZ resourcesZ volumeMountsZsecurityContextstdinZ stdinOnceZttyZAlwaysZ IfNotPresentZNever)ZvolumesZ containersZimagePullPolicyZ restartPolicyZnodeNameZ hostNetworkZhostPIDZhostIPC)kindZ apiVersionmetadataZpriorityClassNamespec)addresssplitr ZoptsrZforce_pull_imager rrrget_node_pod_configAs +zOCTransport.get_node_pod_configc Cstds dS|}|dd|_tj|jd\}|_t|ddd}t ||Wdn1sf0Y| d |jd t d |j}|d d ksd|jd|dvr| d| d|ddS| d|jdz:|j d|jddd}|d d ks$| dWdSWnVtyF| dYdSty|}z| d|WYd}~dSd}~00dS)NrFr#r)dirwzutf-8)encodingz"Starting sos collector container ''z oc create -f rrzpod/z createdoutputz Unable to deploy sos collect podzDebug pod deployment failed: zPod 'z=' successfully deployed, waiting for pod to enter ready statezwait --for=condition=Ready pod/z --timeout=30s(timeoutz"Pod not available after 30 secondsz'Timeout while polling for pod readinessz)Error while waiting for pod to be ready: T)rr(rtempfileZmkstempZtmpdir pod_tmp_confopenjsondump log_debugrZ log_errorrr Exception)r passwordZpodconffdcfileoutrerrrrr_connects@*     zOCTransport._connectcs0|dr$d|jd|jd|St|S)Nrr exec --request-timeout=0 z -- chroot /host ) startswithr rsuper_format_cmd_for_exec)r r  __class__rrrAs  z OCTransport._format_cmd_for_execFNcs"|r |d7}tj||||ddS)N F) use_shell)r@ run_command)r r r0Z need_rootr rFrBrrrGs  zOCTransport.run_commandcCsXtj|jrt|j|d|j}d|dvrT|d|jd|dSdS)Nz delete pod Zdeletedr-zCalling delete on pod 'z ' failed: FT)osrexistsr2unlinkrrr6)r Zremovedrrr _disconnects  zOCTransport._disconnectcCsd|jd|jdS)Nrr>z -- /bin/bash -c)r rr'rrr remote_execs zOCTransport.remote_execc CsV|jddd}d|dvrdnd}|jd|d |d |jd |d d }|d dkS)N cp --retriesTstderr unknown flagr- --retries=5cp r :r/rrrr fnamedestresultflagsr rrr_copy_file_to_remotes  z OCTransport._copy_file_to_remotec CsR|jddd}d|dvrdnd}|d|d |jd |d |}|d d kS) NrMTrNrPr-rQrRrSr rTrrrrVrrr_retrieve_files$zOCTransport._retrieve_file)rDFNF)__name__ __module__ __qualname____doc__rr rpropertyrr(r=rArGrKrLr[r\ __classcell__rrrBrrs"  `*    r) r4r1rHZsos.collector.transportsrZ sos.utilitiesrrrrrrrr s