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

Commit

Permalink
Check for user 'jboss'
Browse files Browse the repository at this point in the history
Use "id -u jboss" to check if a user jboss exists on the system. This
will be one of our indicators that a system might have jboss on it.
  • Loading branch information
Noah Lavine committed Oct 3, 2017
1 parent 7158503 commit 9bc58eb
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
41 changes: 41 additions & 0 deletions library/spit_results.py
Expand Up @@ -288,6 +288,46 @@ def classify(key, fact_names, classifications):
return result


JBOSS_EAP_JBOSS_USER = 'jboss.eap.jboss-user'


def process_id_u_jboss(fact_names, host_vars):
"""Process the output from 'id -u jboss', as run by Ansible
:returns: a dict of key-value pairs to add to the output.
"""

# We use the 'id' command to check for jboss because it's been in
# 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 in Ansible playbook'}

raw_output = host_vars['jboss_eap_id_jboss']

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

if raw_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']:
return {JBOSS_EAP_JBOSS_USER: 'No user "jboss" found'}

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


def remove_newlines(data):
""" Processes input data values and strips out any newlines
"""
Expand Down Expand Up @@ -477,6 +517,7 @@ def write_to_csv(self):

host_vals.update(process_jboss_versions(keys, host_vars))
host_vals.update(process_addon_versions(keys, host_vars))
host_vals.update(process_id_u_jboss(keys, host_vars))

# Process System ID.
for data in self.vals:
Expand Down
3 changes: 2 additions & 1 deletion rho/utilities.py
Expand Up @@ -113,7 +113,8 @@
'subman.virt.uuid',
'subman.has_facts_file')

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

JBOSS_SCAN_FACTS_TUPLE = ('jboss.eap.installed-versions',
'jboss.eap.deploy-dates')
Expand Down
4 changes: 4 additions & 0 deletions roles/jboss_eap/tasks/main.yml
Expand Up @@ -5,3 +5,7 @@
register: jboss.eap.running-versions
ignore_errors: yes
when: 'have_java and "jboss.eap.running-versions" in facts_to_collect'
- name: check for user 'jboss'
raw: id -u jboss
register: jboss_eap_id_jboss
ignore_errors: yes
37 changes: 37 additions & 0 deletions test/test_spit_results.py
Expand Up @@ -94,3 +94,40 @@ def test_skipped_not_present(self):
spit_results.safe_ansible_property(
{'foo': fact}, 'foo', 'property'),
'value')


class TestProcessIdUJboss(unittest.TestCase):
def run_func(self, output):
return spit_results.process_id_u_jboss(
['jboss.eap.jboss-user'],
{'jboss_eap_id_jboss': output})

def test_fact_not_requested(self):
self.assertEqual(
spit_results.process_id_u_jboss([], None),
{})

def test_wrongly_skipped(self):
res = self.run_func({'skipped': True})
self.assertTrue('jboss.eap.jboss-user' in res and
res['jboss.eap.jboss-user'].startswith('Error:'),
msg=res['jboss.eap.jboss-user'])

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

def test_no_such_user(self):
self.assertEqual(
self.run_func({'rc': 1,
'stdout_lines': ['id: jboss: no such user']}),
{'jboss.eap.jboss-user': 'No user "jboss" found'})

def test_unknown_error(self):
res = self.run_func({'rc': 1,
'stdout_lines': ['id: something went wrong!']})

self.assertTrue('jboss.eap.jboss-user' in res and
res['jboss.eap.jboss-user'].startswith('Error:'),
msg=res['jboss.eap.jboss-user'])
4 changes: 4 additions & 0 deletions vagrant/setup-test-vms.yml
Expand Up @@ -8,6 +8,10 @@
- hosts: test_2
roles:
- jboss-standalone
tasks:
- user:
name: jboss
become: yes

- hosts: askpass
tasks:
Expand Down

0 comments on commit 9bc58eb

Please sign in to comment.