From 01fb54ee7dc7db422443356eb90897d71ee3847d Mon Sep 17 00:00:00 2001 From: Aleksei Burlakov Date: Sun, 28 Jul 2024 13:04:05 +0200 Subject: [PATCH] Dev: crm_rpmcheck: use ansible to get package versions --- utils/crm_rpmcheck.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/utils/crm_rpmcheck.py b/utils/crm_rpmcheck.py index 490110656e..d65eaadd5d 100755 --- a/utils/crm_rpmcheck.py +++ b/utils/crm_rpmcheck.py @@ -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) @@ -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): """