From: Zack Cerza Date: Wed, 12 Jan 2022 19:21:27 +0000 (-0700) Subject: kill: Check if sudo works before using it X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=5c463b405580df6154e48ad1c438f3ab0cf79764;p=teuthology.git kill: Check if sudo works before using it Also, use sudo's -n flag to avoid the password prompt and resulting delay. Fixes: https://tracker.ceph.com/issues/53853 Signed-off-by: Zack Cerza --- diff --git a/teuthology/kill.py b/teuthology/kill.py index 213600f0ff..5af11b628c 100755 --- a/teuthology/kill.py +++ b/teuthology/kill.py @@ -179,12 +179,18 @@ def kill_processes(run_name, pids=None): log.info("No teuthology processes running") else: log.info("Killing Pids: " + str(to_kill)) + may_need_sudo = \ + psutil.Process(int(pid)).username() != getpass.getuser() + if may_need_sudo: + sudo_works = subprocess.Popen(['sudo', '-n', 'true']).wait() == 0 + if not sudo_works: + log.debug("Passwordless sudo not configured; not using sudo") + use_sudo = may_need_sudo and sudo_works for pid in to_kill: args = ['kill', str(pid)] # Don't attempt to use sudo if it's not necessary - proc_user = psutil.Process(int(pid)).username() - if proc_user != getpass.getuser(): - args.insert(0, 'sudo') + if use_sudo: + args = ['sudo', '-n'] + args subprocess.call(args)