From de881f49a62a0f753c871a68d65c74a0f7097590 Mon Sep 17 00:00:00 2001 From: "Eric D. Helms" Date: Thu, 7 Sep 2023 10:05:05 -0400 Subject: [PATCH] Refactor repoclosure into a module --- obal/data/modules/repoclosure.py | 54 +++++++++++++++++++ obal/data/roles/repoclosure/tasks/main.yml | 12 ----- .../roles/repoclosure/tasks/repoclosure.yml | 49 ++++++++++------- .../testrepo/upstream/package_manifest.yaml | 3 ++ tests/test_functional.py | 15 +++--- 5 files changed, 97 insertions(+), 36 deletions(-) create mode 100644 obal/data/modules/repoclosure.py diff --git a/obal/data/modules/repoclosure.py b/obal/data/modules/repoclosure.py new file mode 100644 index 00000000..30df5b38 --- /dev/null +++ b/obal/data/modules/repoclosure.py @@ -0,0 +1,54 @@ +""" +Run repoclosure on a set of repositories +""" + +from subprocess import check_output, CalledProcessError, STDOUT +from ansible.module_utils.basic import AnsibleModule + + +def main(): + """ + Run repoclosure on a set of repositories + """ + module = AnsibleModule( + argument_spec=dict( + config=dict(type='str', required=True), + check=dict(type='list', required=True), + additional_repos=dict(type='list', required=False, default=[]), + lookaside=dict(type='list', required=False, default=[]), + ) + ) + + config = module.params['config'] + check = module.params['check'] + additional_repos = module.params['additional_repos'] + lookaside = module.params['lookaside'] + + command = [ + 'dnf', + 'repoclosure', + '--refresh', + '--newest', + '--config', + config + ] + + for name in check: + command.extend(['--check', name]) + + for repo in additional_repos: + command.extend(['--repofrompath', f"{repo['name']},{repo['url']}"]) + + for repo in lookaside: + command.extend(['--repo', repo]) + + try: + output = check_output(command, universal_newlines=True, stderr=STDOUT) + except CalledProcessError as error: + module.fail_json(msg='Repoclosure failed', command=' '.join(command), output=error.output) + + module.exit_json(changed=False, output=output) + + +if __name__ == '__main__': + main() diff --git a/obal/data/roles/repoclosure/tasks/main.yml b/obal/data/roles/repoclosure/tasks/main.yml index 4d2cbdaf..0237f8df 100644 --- a/obal/data/roles/repoclosure/tasks/main.yml +++ b/obal/data/roles/repoclosure/tasks/main.yml @@ -1,16 +1,4 @@ --- -- name: 'Initialize additional_repos' - set_fact: - additional_repos: [] - -- name: 'Build additional repos' - set_fact: - additional_repos: "{{ additional_repos + ['--repofrompath=' + item.name + ',' + item.url] }}" - with_items: "{{ repoclosure_additional_repos }}" - -- debug: - msg: "{{ additional_repos }}" - - name: Find all downloaded repositories find: paths: "{{ inventory_dir }}/downloaded_rpms" diff --git a/obal/data/roles/repoclosure/tasks/repoclosure.yml b/obal/data/roles/repoclosure/tasks/repoclosure.yml index 5bc9d1b7..fb8ffdce 100644 --- a/obal/data/roles/repoclosure/tasks/repoclosure.yml +++ b/obal/data/roles/repoclosure/tasks/repoclosure.yml @@ -1,7 +1,32 @@ - block: + - name: Generate downloaded_rpms dictionary for additional repositories + set_fact: + download_rpm_repos: "{{ dict([['name', 'downloaded_rpms'], ['url', inventory_dir + '/downloaded_rpms/' + dist]]) }}" + when: downloaded_rpms_dists + - name: Generate list of repositories set_fact: repoclosure_target_repos_all: "{{ repoclosure_target_repos[dist] | default([]) + ['downloaded_rpms'] }}" + when: downloaded_rpms_dists + + - name: Generate list of repositories + set_fact: + repoclosure_target_repos_all: "{{ repoclosure_target_repos[dist] }}" + when: not downloaded_rpms_dists + + - name: Set additional_repos + set_fact: + additional_repos: "{{ repoclosure_additional_repos + [download_rpm_repos] }}" + when: download_rpm_repos is defined + + - name: Set additional_repos + set_fact: + additional_repos: "{{ repoclosure_additional_repos }}" + when: download_rpm_repos is not defined + + - name: Set config filename for repoclosure + set_fact: + config_filename: "{{ repoclosure_config | basename }}" - name: Create temp directory tempfile: @@ -9,10 +34,6 @@ suffix: repoclosure register: temp_directory - - name: Set config filename for repoclosure - set_fact: - config_filename: "{{ repoclosure_config | basename }}" - - name: Copy repoclosure config to temp directory copy: src: "{{ inventory_dir }}/{{ repoclosure_config }}" @@ -27,23 +48,15 @@ replace: "cachedir={{ temp_directory.path }}" - name: 'Run repoclosure for {{ dist }}' - command: - cmd: > - dnf repoclosure - --config {{ temp_directory.path }}/{{ config_filename }} - --refresh - --newest - --check {{ repoclosure_target_repos_all | join(' --check ') }} - {{ '--repofrompath=downloaded_rpms,./downloaded_rpms/' + dist if repoclosure_use_downloaded_rpms else '' }} - {{ additional_repos | join(' ') }} - {{ '--repo ' if repoclosure_lookaside_repos else '' }} - {{ repoclosure_lookaside_repos[dist] | join(' --repo ') }} + repoclosure: + config: "{{ temp_directory.path }}/{{ config_filename }}" + check: "{{ repoclosure_target_repos_all }}" + additional_repos: "{{ additional_repos }}" + lookaside: "{{ repoclosure_lookaside_repos[dist] }}" register: output - args: - chdir: "{{ inventory_dir }}" - debug: - msg: "{{ output.stdout_lines | join('\n') }}" + msg: "{{ output.output }}" always: - name: Clean up temporary directory file: diff --git a/tests/fixtures/testrepo/upstream/package_manifest.yaml b/tests/fixtures/testrepo/upstream/package_manifest.yaml index b613cbd1..46533e2b 100644 --- a/tests/fixtures/testrepo/upstream/package_manifest.yaml +++ b/tests/fixtures/testrepo/upstream/package_manifest.yaml @@ -33,6 +33,9 @@ repoclosures: hosts: core-repoclosure: repoclosure_target_dist: rhel7 + repoclosure_target_repos: + rhel7: + - el7-katello dist-repoclosure: {} katello-repoclosure: repoclosure_target_repos: diff --git a/tests/test_functional.py b/tests/test_functional.py index 199824de..c4c3fa95 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -607,10 +607,11 @@ def test_obal_repoclosure(): expected_log = [ "dnf repoclosure", + "--refresh", + "--newest", "--config", "repoclosure/yum.conf", - "--newest", - "--check downloaded_rpms", + "--check el7-katello", "--repo el7-base" ] assert_in_mockbin_log(expected_log) @@ -625,11 +626,12 @@ def test_obal_repoclosure_with_downloaded_rpms(): expected_log = [ "dnf repoclosure", + "--newest", + "--refresh", "--config", "repoclosure/yum.conf", - "--newest", + "--repofrompath downloaded_rpms,{pwd}/downloaded_rpms/rhel7", "--check downloaded_rpms", - "--repofrompath=downloaded_rpms,./downloaded_rpms/rhel7", "--repo el7-base" ] assert_in_mockbin_log(expected_log) @@ -644,12 +646,13 @@ def test_obal_repoclosure_katello_with_downloaded_rpms(): expected_log = [ "dnf repoclosure", + "--newest", + "--refresh", "--config", "repoclosure/yum.conf", - "--newest", + "--repofrompath downloaded_rpms,{pwd}/downloaded_rpms/rhel7", "--check el7-katello", "--check downloaded_rpms", - "--repofrompath=downloaded_rpms,./downloaded_rpms/rhel7", "--repo el7-base" ] assert_in_mockbin_log(expected_log)