1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Support older version of psutil (RHEL7 and RHEL6) (#2808)

* Support older version of psutil (RHEL7 and RHEL6)

The psutil python module is a true mess, they changed the API twice. The function arguments, as well as the objects that are returned.
The documentation does not make it clear which version supports what so the safest implementation is this waterfall approach.

A better approach would be to inspect the returned information, rather than trust a version, but that would not be any more efficient.
In the end it is better to have something that at least works out-of-the-box on all platforms than something that requires custom updates to system packages before it works as expected. Especially for something as basic as `pids`.

* A little bit more concise

* Apply suggestions from code review

* Add changelog fragment.

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Dag Wieers 2021-08-10 07:49:18 +02:00 committed by GitHub
parent 1705335ba7
commit b5d6457611
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 5 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- "pids - avoid crashes for older ``psutil`` versions, like on RHEL6 and RHEL7 (https://github.com/ansible-collections/community.general/pull/2808)."

View file

@ -79,11 +79,20 @@ def compare_lower(a, b):
def get_pid(name):
pids = []
for proc in psutil.process_iter(attrs=['name', 'cmdline']):
if compare_lower(proc.info['name'], name) or \
proc.info['cmdline'] and compare_lower(proc.info['cmdline'][0], name):
pids.append(proc.pid)
try:
for proc in psutil.process_iter(attrs=['name', 'cmdline']):
if compare_lower(proc.info['name'], name) or \
proc.info['cmdline'] and compare_lower(proc.info['cmdline'][0], name):
pids.append(proc.pid)
except TypeError: # EL6, EL7: process_iter() takes no arguments (1 given)
for proc in psutil.process_iter():
try: # EL7
proc_name, proc_cmdline = proc.name(), proc.cmdline()
except TypeError: # EL6: 'str' object is not callable
proc_name, proc_cmdline = proc.name, proc.cmdline
if compare_lower(proc_name, name) or \
proc_cmdline and compare_lower(proc_cmdline[0], name):
pids.append(proc.pid)
return pids