From 531a9fe3ac1a7f0be43fdccad4fdc4551f03ca33 Mon Sep 17 00:00:00 2001 From: Mike Graves Date: Wed, 11 May 2022 14:56:27 -0400 Subject: [PATCH] Remove distutils from connection plugin (#456) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove distutils from connection plugin Depends-On: ansible/ansible-zuul-jobs#1527 SUMMARY distutils.spawn.find_executable is deprecated and shutils.which is a suitable replacement. ISSUE TYPE Bugfix Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Gonéri Le Bouder Reviewed-by: Joseph Torcasso --- changelogs/fragments/456-replace-distutils.yml | 2 ++ plugins/connection/kubectl.py | 13 ++++--------- 2 files changed, 6 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/456-replace-distutils.yml diff --git a/changelogs/fragments/456-replace-distutils.yml b/changelogs/fragments/456-replace-distutils.yml new file mode 100644 index 00000000..dc70d6f6 --- /dev/null +++ b/changelogs/fragments/456-replace-distutils.yml @@ -0,0 +1,2 @@ +minor_changes: + - kubectl.py - replace distutils.spawn.find_executable with shutil.which in the kubectl connection plugin (https://github.com/ansible-collections/kubernetes.core/pull/456). diff --git a/plugins/connection/kubectl.py b/plugins/connection/kubectl.py index 1f4dca07..4a4452ad 100644 --- a/plugins/connection/kubectl.py +++ b/plugins/connection/kubectl.py @@ -171,9 +171,9 @@ DOCUMENTATION = r""" aliases: [ kubectl_verify_ssl ] """ -import distutils.spawn import os import os.path +import shutil import subprocess from ansible.parsing.yaml.loader import AnsibleLoader @@ -219,14 +219,9 @@ class Connection(ConnectionBase): # Note: kubectl runs commands as the user that started the container. # It is impossible to set the remote user for a kubectl connection. cmd_arg = "{0}_command".format(self.transport) - if cmd_arg in kwargs: - self.transport_cmd = kwargs[cmd_arg] - else: - self.transport_cmd = distutils.spawn.find_executable(self.transport) - if not self.transport_cmd: - raise AnsibleError( - "{0} command not found in PATH".format(self.transport) - ) + self.transport_cmd = kwargs.get(cmd_arg, shutil.which(self.transport)) + if not self.transport_cmd: + raise AnsibleError("{0} command not found in PATH".format(self.transport)) def _build_exec_cmd(self, cmd): """Build the local kubectl exec command to run cmd on remote_host"""