Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pids: Search for process name matches in the cmdline #59318

Merged
merged 4 commits into from
Sep 20, 2019

Conversation

wmedlar
Copy link
Contributor

@wmedlar wmedlar commented Jul 19, 2019

SUMMARY

This more appropriately matches the behavior of pidof and allows the pids module to work with processes invoked with exec -a SOMENAME.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

pids

ADDITIONAL INFORMATION

pidof behavior:

$ (exec -a PIDOF-TEST sleep 100) &
[1] 373
$ pidof PIDOF-TEST
373

pids module behavior prior to this change:

$ cat playbook.yaml
- hosts: localhost
  tasks:
  - name: fire off a test process
    shell: (exec -a ANSIBLE-PIDS-TEST sleep 99999) &
    args:
      executable: /bin/bash

  - name: locate the test process
    pids: name=ANSIBLE-PIDS-TEST
    register: p

  - debug:
      var: p.pids

$ ansible-playbook playbook.yaml
TASK [fire off a test process] ***************************************************************************
changed: [localhost]

TASK [locate the test process] ***************************************************************************
ok: [localhost]

TASK [debug] *********************************************************************************************
ok: [localhost] =>
  p.pids: []

pids module behavior after this change:

$ ansible-playbook playbook.yaml
TASK [fire off a test process] ***************************************************************************
changed: [localhost]

TASK [locate the test process] ***************************************************************************
ok: [localhost]

TASK [debug] *********************************************************************************************
ok: [localhost] =>
  p.pids: [420]

This code is inspired by psutil's Python clone of pidof.

@wmedlar wmedlar marked this pull request as ready for review July 19, 2019 19:07
@ansibot
Copy link
Contributor

ansibot commented Jul 19, 2019

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 community_review In order to be merged, this PR must follow the community review workflow. feature This issue/PR relates to a feature request. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. new_contributor This PR is the first contribution by a new community member. support:community This issue/PR relates to code supported by the Ansible community. system System category labels Jul 19, 2019
@ansibot
Copy link
Contributor

ansibot commented Jul 19, 2019

The test ansible-test sanity --test pylint [explain] failed with 3 errors:

lib/ansible/modules/system/pids.py:53:0: trailing-whitespace Trailing whitespace
lib/ansible/modules/system/pids.py:58:0: trailing-whitespace Trailing whitespace
lib/ansible/modules/system/pids.py:63:0: trailing-whitespace Trailing whitespace

The test ansible-test sanity --test pep8 [explain] failed with 6 errors:

lib/ansible/modules/system/pids.py:53:1: W293 blank line contains whitespace
lib/ansible/modules/system/pids.py:54:1: E302 expected 2 blank lines, found 1
lib/ansible/modules/system/pids.py:58:1: W293 blank line contains whitespace
lib/ansible/modules/system/pids.py:61:1: E302 expected 2 blank lines, found 1
lib/ansible/modules/system/pids.py:63:1: W293 blank line contains whitespace
lib/ansible/modules/system/pids.py:71:1: E302 expected 2 blank lines, found 1

click here for bot help

@ansibot ansibot added ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Jul 19, 2019
@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Jul 19, 2019
@wmedlar
Copy link
Contributor Author

wmedlar commented Jul 19, 2019

fixed, thanks ansibae ❤️

ready_for_review

@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jul 19, 2019
@wmedlar
Copy link
Contributor Author

wmedlar commented Jul 25, 2019

@Akasurde you've reviewed changes to this module before, mind taking a look at this one?

@Akasurde Akasurde self-assigned this Jul 26, 2019
@Akasurde
Copy link
Member

@wmedlar Sure, Thanks.

@ansibot ansibot removed the needs_triage Needs a first human triage before being processed. label Jul 26, 2019
return [p.info['pid'] for p in psutil.process_iter(attrs=['pid', 'name']) if p.info and p.info.get('name', None) and p.info['name'].lower() == name.lower()]
pids = []

for proc in psutil.process_iter(attrs=['name', 'cmdline']):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be including 'pid' as per original code ?

Copy link
Contributor Author

@wmedlar wmedlar Jul 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only if you access the pid through the p.info dict attribute; the Process object itself will always have a pid attribute set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Akasurde thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Akasurde ping

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wmedlar Sorry for delay, I will take a look.

@Akasurde Akasurde changed the title [2.8] Search for process name matches in the cmdline pidof: Search for process name matches in the cmdline Jul 26, 2019
@Akasurde Akasurde changed the title pidof: Search for process name matches in the cmdline pids: Search for process name matches in the cmdline Jul 26, 2019
@ansibot ansibot removed the community_review In order to be merged, this PR must follow the community review workflow. label Jul 26, 2019
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Jul 26, 2019
@wmedlar
Copy link
Contributor Author

wmedlar commented Sep 18, 2019

Can anyone else take a look at this?

@Akasurde Akasurde merged commit 6541bb0 into ansible:devel Sep 20, 2019
anas-shami pushed a commit to anas-shami/ansible that referenced this pull request Sep 23, 2019
* Also search for process name matches in the cmdline
* Return integer pids
* Use correct variable name
* Fix linter concerns
@ansible ansible locked and limited conversation to collaborators Oct 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 feature This issue/PR relates to a feature request. module This issue/PR relates to a module. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. new_contributor This PR is the first contribution by a new community member. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:community This issue/PR relates to code supported by the Ansible community. system System category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants