forked from willthames/ansible-testing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
assert_process
112 lines (97 loc) · 3.25 KB
/
assert_process
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2014, Will Thames <[email protected]>
# (c) 2014, Allan Denot <[email protected]>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import pprint
DOCUMENTATION = '''
---
module: assert_process
version_added: 1.5
short_description: Check the status of a process
description:
- Check if a process is running or not
options:
name:
description:
- name of the process to check
required: True
default: null
with_args:
description:
- list of one or more arguments to look for
required: False
default: []
should_be:
description:
- state of the process
choices: [ 'running', 'stopped' ]
required: True
author: Will Thames
'''
EXAMPLES = '''
# Check if http app is running
assert_process: name=httpd should_be=running with_args='/apps/apache2/conf/httpd.conf'
'''
def check_process(process, name):
if not len(process.strip().split()):
return False
if process.strip().split()[0] == name:
return True
return False
def check_arguments(process,name,args):
if not len(process.strip().split()):
return False
arg_found = 0
for arg in args:
if arg in ' '.join(process.split()[1:]):
arg_found += 1
if arg_found == len(args):
return True
return False
def main():
module = AnsibleModule(
argument_spec = dict(
name=dict(required=True, default=None),
with_args=dict(type='list', required=False, default=[]),
should_be=dict(choices=['running', 'stopped'],required=True),
),
supports_check_mode=True
)
name = module.params.get('name')
args = module.params.get('with_args')
state = module.params.get('should_be')
# Would like this to be more cross platform
rc, stdout, stderr = module.run_command(['ps', 'axww', '-o', 'command'])
processes = stdout.split('\n')[1:]
process_running = filter(lambda x: check_process(x, name), processes)
process_args = filter(lambda x: check_arguments(x, name, args), processes)
if state == 'running':
if not process_running:
module.fail_json(msg="%s is not running" % name)
if args and not process_args:
module.fail_json(msg="%s is running but does not have all the arguments required: %s" % (name, args))
if process_running:
module.exit_json()
if state == 'stopped':
if process_running:
module.fail_json(msg="%s is running, it should be stopped" % name)
else:
module.exit_json()
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()