Skip to content

Commit

Permalink
Refactor repoclosure into a module
Browse files Browse the repository at this point in the history
  • Loading branch information
ehelms committed Sep 13, 2023
1 parent f951bab commit de881f4
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 36 deletions.
54 changes: 54 additions & 0 deletions obal/data/modules/repoclosure.py
Original file line number Diff line number Diff line change
@@ -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()
12 changes: 0 additions & 12 deletions obal/data/roles/repoclosure/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
49 changes: 31 additions & 18 deletions obal/data/roles/repoclosure/tasks/repoclosure.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
- 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:
state: directory
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 }}"
Expand All @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/testrepo/upstream/package_manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 9 additions & 6 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit de881f4

Please sign in to comment.