Skip to content

Commit

Permalink
Dev: crm_rpmcheck: use ansible to get package versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei Burlakov committed Jul 30, 2024
1 parent 3e96f90 commit 01fb54e
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions utils/crm_rpmcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def package_data(pkg):
"""
Gathers version and release information about a package.
"""
if os.path.isfile('/bin/ansible'):
rc, data = ansible_package_data(pkg)
if rc == 0:
return data

if os.path.isfile('/bin/rpm'):
return rpm_package_data(pkg)

Expand All @@ -31,6 +36,37 @@ def package_data(pkg):

return {'name': pkg, 'error': "unknown package manager"}

packages = None
def ansible_package_data(pkg) -> tuple[int, dict]:
"""
Gathers version and release information about a package.
Using ansible.
"""
global packages
if not packages:
# if packages is None, then get it
rc, out, err = run(['ansible', '-m', 'package_facts', 'localhost'])
if rc == -1:
return -1, {}
# output format 'localhost | SUCCESS => { json...'
bracket_pos = out.find('{')
if bracket_pos == -1:
return -1, {}
is_ok = out[:bracket_pos].find('SUCCESS =>')
if is_ok == -1:
return -1, {}

# get the json part
out = out[bracket_pos:]
json_tree = json.loads(out)
# get packages
packages = json_tree['ansible_facts']['packages']

if pkg not in packages:
return 0, {'name': pkg, 'error': "package not installed"}
else:
return 0, packages[pkg][0]


def rpm_package_data(pkg):
"""
Expand Down

0 comments on commit 01fb54e

Please sign in to comment.