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

Dev: crm_rpmcheck: use ansible to get package versions #1497

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions utils/crm_rpmcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
import json
import subprocess

import shutil

def run(cmd):
proc = subprocess.Popen(cmd,
Expand All @@ -23,14 +23,50 @@ def package_data(pkg):
"""
Gathers version and release information about a package.
"""
if os.path.isfile('/bin/rpm'):
if shutil.which('ansible'):
rc, data = ansible_package_data(pkg)
if rc == 0:
return data

if shutil.which('rpm'):
return rpm_package_data(pkg)

if os.path.isfile('/usr/bin/dpkg'):
if shutil.which('dpkg'):
return dpkg_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