Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Check for 'eap' processes in JBoss scan
Browse files Browse the repository at this point in the history
See whether any processes on the system have 'eap' in their process
name or arguments. This will help detect JBoss EAP installations.
  • Loading branch information
Noah Lavine committed Oct 9, 2017
1 parent a033003 commit 98c2b48
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 28 deletions.
107 changes: 81 additions & 26 deletions library/spit_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,45 @@ def classify(key, fact_names, classifications):
return result


def raw_output_present(fact_names, host_vars, this_fact, this_var, command):
"""Basic sanity checks for processing an Ansible raw command.
:param fact_names: the facts to be collected
:param host_vars: all variables collected for a host
:param this_fact: the name of the fact we are processing
:param this_var: the name that Ansible has for our output
:param command: the command that was run
:returns: a tuple of
(None or error dict, None or raw command output).
The error dict is suitable for inclusion in the rho output
dictionary. There will not be both errors and raw command
output. If raw command output is returned, it will have
fields 'rc' and 'stdout_lines' or 'results'.
Usage:
err, output = raw_output_present(...)
if err is not None:
return err
... process output ...
"""

if this_fact not in fact_names:
return {}, None

if this_var not in host_vars:
return {this_fact: 'Error: "{0}" not run'.format(command)}, None

raw_output = host_vars[this_var]

if (('rc' not in raw_output or
'stdout_lines' not in raw_output) and 'results' not in raw_output):
return {this_fact: 'Error: "{0}" not run'.format(command)}, None

return None, raw_output


JBOSS_EAP_JBOSS_USER = 'jboss.eap.jboss-user'


Expand All @@ -301,31 +340,26 @@ def process_id_u_jboss(fact_names, host_vars):
# GNU coreutils since 1992, so it should be present on every
# system we encounter.

if 'jboss.eap.jboss-user' not in fact_names:
return {}

if 'jboss_eap_id_jboss' not in host_vars:
return {JBOSS_EAP_JBOSS_USER:
'Error: "id -u jboss" not run'}

raw_output = host_vars['jboss_eap_id_jboss']
err, output = raw_output_present(fact_names, host_vars,
'jboss.eap.jboss-user',
'jboss_eap_id_jboss',
'id -u jboss')
if err is not None:
return err

if 'rc' not in raw_output:
return {JBOSS_EAP_JBOSS_USER: 'Error: "id -u jboss" not run'}

if raw_output['rc'] == 0:
if output['rc'] == 0:
return {JBOSS_EAP_JBOSS_USER: "User 'jboss' present"}

# Don't output a definitive "not found" unless we see an error
# string that we recognize. We don't want to assume that any
# nonzero error code means "not found", because then we would give
# false negatives if the user didn't have permission to read
# /etc/passwd (or other errors).
if raw_output['stdout_lines'] == ['id: jboss: no such user']:
if output['stdout_lines'] == ['id: jboss: no such user']:
return {JBOSS_EAP_JBOSS_USER: 'No user "jboss" found'}

return {JBOSS_EAP_JBOSS_USER:
'Error: unexpected output from "id -u jboss": %s' % raw_output}
'Error: unexpected output from "id -u jboss": %s' % output}


JBOSS_EAP_COMMON_DIRECTORIES = 'jboss.eap.common-directories'
Expand All @@ -337,20 +371,15 @@ def process_jboss_eap_common_dirs(fact_names, host_vars):
:returns: a dict of key, value pairs to add to the output.
"""

if 'jboss.eap.common-directories' not in fact_names:
return {}

if 'jboss_eap_common_directories' not in host_vars:
return {JBOSS_EAP_COMMON_DIRECTORIES:
'Error: common install directory tests not run'}
err, output = raw_output_present(fact_names, host_vars,
'jboss.eap.common-directories',
'jboss_eap_common_directories',
'common install directory tests')

raw_output = host_vars['jboss_eap_common_directories']
if err is not None:
return err

if 'results' not in raw_output:
return {JBOSS_EAP_COMMON_DIRECTORIES:
'Error: common install directory tests not run'}

items = raw_output['results']
items = output['results']

out_list = []
for item in items:
Expand All @@ -365,6 +394,31 @@ def process_jboss_eap_common_dirs(fact_names, host_vars):
return {JBOSS_EAP_COMMON_DIRECTORIES: ';'.join(out_list)}


JBOSS_EAP_PROCESSES = 'jboss.eap.processes'


def process_jboss_eap_processes(fact_names, host_vars):
"""Process the output of 'pgrep -f eap'
:returns: a dict of key, value pairs to add to the output.
"""

err, output = raw_output_present(fact_names, host_vars,
JBOSS_EAP_PROCESSES,
JBOSS_EAP_PROCESSES,
'pgrep -f eap')
if err is not None:
return err

# pgrep exists with status 0 if it finds processes matching its
# pattern, and status 1 if not.
if output['rc']:
return {JBOSS_EAP_PROCESSES: 'No EAP processes found'}

return {JBOSS_EAP_PROCESSES:
'{0} EAP processes found'.format(len(output['stdout_lines']))}


def remove_newlines(data):
""" Processes input data values and strips out any newlines
"""
Expand Down Expand Up @@ -556,6 +610,7 @@ def write_to_csv(self):
host_vals.update(process_addon_versions(keys, host_vars))
host_vals.update(process_id_u_jboss(keys, host_vars))
host_vals.update(process_jboss_eap_common_dirs(keys, host_vars))
host_vals.update(process_jboss_eap_processes(keys, host_vars))

# Process System ID.
for data in self.vals:
Expand Down
3 changes: 2 additions & 1 deletion rho/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@

JBOSS_FACTS_TUPLE = ('jboss.eap.running-versions',
'jboss.eap.jboss-user',
'jboss.eap.common-directories')
'jboss.eap.common-directories',
'jboss.eap.processes')

JBOSS_SCAN_FACTS_TUPLE = ('jboss.eap.installed-versions',
'jboss.eap.deploy-dates')
Expand Down
5 changes: 5 additions & 0 deletions roles/jboss_eap/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@
- /var/log/jboss-as
- /usr/log/jboss-as
when: '"jboss.eap.common-directories" in facts_to_collect'
- name: gather jboss.eap.processes
raw: pgrep -f eap
register: jboss.eap.processes
ignore_errors: yes
when: '"jboss.eap.processes" in facts_to_collect'
19 changes: 18 additions & 1 deletion test/test_spit_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_wrongly_skipped(self):

def test_user_found(self):
self.assertEqual(
self.run_func({'rc': 0}),
self.run_func({'rc': 0, 'stdout_lines': []}),
{'jboss.eap.jboss-user': "User 'jboss' present"})

def test_no_such_user(self):
Expand Down Expand Up @@ -167,3 +167,20 @@ def test_three_states(self):
'Error: "test -d dir1" not run;'
'dir2 not found;'
'dir3 found'})


class TestProcessJbossEapProcesses(unittest.TestCase):
def run_func(self, output):
return spit_results.process_jboss_eap_processes(
['jboss.eap.processes'],
{'jboss.eap.processes': output})

def test_no_processes(self):
self.assertEqual(self.run_func({
'rc': 1, 'stdout_lines': []}),
{'jboss.eap.processes': 'No EAP processes found'})

def test_found_processes(self):
self.assertEqual(self.run_func({
'rc': 0, 'stdout_lines': [1,2,3]}),
{'jboss.eap.processes': '3 EAP processes found'})

0 comments on commit 98c2b48

Please sign in to comment.