3 ܓi%@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|f|S)z\Format and run a command with `oc` in the project defined for our execution zoc -n  )rproject)selfcmdkwargsr /usr/lib/python3.6/oc.pyrun_oc1szOCTransport.run_occCs|jd|j}|ddkS)Nz,wait --timeout=0s --for=condition=ready pod/statusr)rpod_name)r upr r r connected:szOCTransport.connectedcCsdd|jjddd|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)rZhostPathZrunz/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)ZkindZ apiVersionmetadataZpriorityClassNamespec)Zaddresssplitr ZoptsrZforce_pull_image)r r r rget_node_pod_configAsT    zOCTransport.get_node_pod_configcCsftds dS|j}|dd|_tj|jd\}|_t|ddd}tj ||WdQRX|j d |jd t d |j}|d d ksd|jd|dkr|j d|j d|ddS|j d|jdy8|j d|jddd}|d d ks|j ddSWnPtk r0|j ddStk r`}z|j d|dSd}~XnXdS)NrFrr)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 ZpasswordZpodconffdcfileoutrerrr r r_connects:    zOCTransport._connectcs0|jdr$d|jd|jd|Stj|S)Nrzoc -n z exec --request-timeout=0 z -- chroot /host ) startswithr rsuper_format_cmd_for_exec)r r ) __class__r rr8s z OCTransport._format_cmd_for_execFNcs"|r |d7}tj||||ddS)N F) use_shell)r7 run_command)r r r)Z need_rootrr<)r9r rr=s zOCTransport.run_commandcCsXtjj|jrtj|j|jd|j}d|dkrT|jd|jd|dSdS)Nz delete pod Zdeletedr'zCalling delete on pod 'z ' failed: FT)osrexistsr+unlinkrrr/)r Zremovedr r r _disconnects  zOCTransport._disconnectcCsd|jd|jdS)Nzoc -n z exec --request-timeout=0 z -- /bin/bash -c)r r)r r r r remote_execszOCTransport.remote_execc CsV|jddd}d|dkrdnd}|jd|d |d |jd |d d }|d dkS)Nz cp --retriesT)stderrz unknown flagr'z --retries=5zcp r:)r)rr)rr)r fnamedestresultflagsr r r r_copy_file_to_remotes  z OCTransport._copy_file_to_remotec CsR|jddd}d|dkrdnd}|jd|d |jd |d |}|d d kS) Nz cp --retriesT)rCz unknown flagr'rDz --retries=5zcp rrErr)rr)r rGrHrIrJr r r r_retrieve_files$zOCTransport._retrieve_file)r:FNF)__name__ __module__ __qualname____doc__rr rpropertyrr"r5r8r=rArBrKrL __classcell__r r )r9rrs `*   r) r-r*r>Zsos.collector.transportsrZ sos.utilitiesrrrrr r r r s