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

Add repo sync option #1127

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions changelogs/fragments/1119-repositories_sync.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- Add a role `repositories_sync` to sync products and repositories on-demand.
2 changes: 1 addition & 1 deletion roles/repositories/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@
verify_ssl_on_sync: "{{ item.1.verify_ssl_on_sync | default(omit) }}"
with_subelements:
- "{{ products | selectattr('repositories', 'defined') | list }}"
- repositories
- repositories
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks unrelated and unneeded?

117 changes: 117 additions & 0 deletions roles/repositories_sync/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
theforeman.foreman.repositories_sync
===============================

This role syncs Products and Repositories. This role uses the same data structure as `theforeman.foreman.repositories`.

Role Variables
--------------

This role supports the [Common Role Variables](https://github.com/theforeman/foreman-ansible-modules/blob/develop/README.md#common-role-variables).

- `products`: List of products to manage.
Each product is represented as a dictionary and can include `repository_sets` which represent Red Hat Repositories and should be used when the product name matches an existing Red Hat Product.
Each element of `repository_sets` must have a `name` and should specify the `basearch` and/or `releasever` only when multiple versions are available for that Product.
All repository sets for a Red Hat Product can be enabled by omitting `repository_sets` and instead specifying that the Product has `all_repositories: True`. When using this option it is also necessary to specify a list of repository `label`s for the Product (e.g. rhel-7-server-rpms). Be wary that this option can result in enabling a large number of unused repositories that, if added to sync plans, can greatly increase sync times and rapidly fill disk space.
Custom (i.e. non Red Hat) Products can also be defined, with associated `repositories` which represent custom repositories, and are required to have a `name`, `url`, and `content_type`; they may require additional fields and can take any parameter supported by [theforeman.foreman.repository](https://theforeman.github.io/foreman-ansible-modules/develop/plugins/repository_module.html). Repositories will not be synced automatically when enabled. Specify the `sync` option on the product to run an initial sync of all repositories in the product.
A variety of examples are demonstrated in the data structure below:

```yaml
products:
- name: Red Hat Enterprise Linux Server
repository_sets:
- name: Red Hat Enterprise Linux 7 Server (RPMs)
basearch: x86_64
releasever: 7Server
- name: Red Hat Enterprise Linux 6 Server (RPMs)
basearch: x86_64
releasever: 6Server
- name: Red Hat Enterprise Linux 7 Server - Extras (RPMs)
basearch: x86_64
- name: Red Hat Enterprise Linux 7 Server - Optional (RPMs)
basearch: x86_64
- name: Red Hat Software Collections (for RHEL Server)
repository_sets:
- name: Red Hat Software Collections RPMs for Red Hat Enterprise Linux 7 Server
basearch: x86_64
releasever: 7Server
- name: Red Hat Software Collections RPMs for Red Hat Enterprise Linux 6 Server
basearch: x86_64
releasever: 6Server
- name: Red Hat Enterprise Linux for x86_64
repository_sets:
- name: Red Hat Enterprise Linux 8 for x86_64 - BaseOS (RPMs)
releasever: 8
- name: Red Hat Enterprise Linux 8 for x86_64 - AppStream (RPMs)
releasever: 8
- name: Red Hat Software Collections (for RHEL Server)
all_repositories: True
labels:
- rhel-server-rhscl-7-rpms
- name: CentOS 8
repositories:
- name: BaseOS x86_64
content_type: yum
url: http://mirror.centos.org/centos/8/BaseOS/x86_64/os/
- name: AppStream x86_64
content_type: yum
url: http://mirror.centos.org/centos/8/AppStream/x86_64/os/
- name: Debian 10
repositories:
- name: Debian 10 main
content_type: deb
url: http://deb.debian.org/debian
deb_components: main
deb_architectures: amd64
deb_releases: buster
- name: Foreman Client
repositories:
- name: Foreman Client Debian 10
url: https://apt.atix.de/debian
content_type: deb
deb_components: main
deb_architectures: amd64
deb_releases: stable
- name: Foreman Client CentOS 7
url: https://yum.theforeman.org/client/latest
content_type: yum
```

Example Playbooks
-----------------

This example enables several Red Hat Repositories. There are a few important points to note about the structure of the data in the example:
- RHEL 8 repos have a different product name than previous RHEL versions.
- The RHEL 8 product already contains the `basearch` so it should not be specified on the RHEL 8 `repository_sets`, and the naming convention for `releasever` changed with RHEL 8 since system purpose removes the need for separate distributions like `Server` and `Workstation`.
- The optional and extras repositories do not have point releases so `releasever` should be omitted.
- RHEL 8 repos will be synced after being enabled. Without the `sync` parameter, the RHEL 6 and 7 repositories will only be enabled.

```yaml
- hosts: localhost
roles:
- role: theforeman.foreman.repositories
vars:
server_url: https://foreman.example.com
username: "admin"
password: "changeme"
organization: "Default Organization"
products:
- name: Red Hat Enterprise Linux Server
repository_sets:
- name: Red Hat Enterprise Linux 7 Server (RPMs)
basearch: x86_64
releasever: 7Server
- name: Red Hat Enterprise Linux 6 Server (RPMs)
basearch: x86_64
releasever: 6Server
- name: Red Hat Enterprise Linux 7 Server - Extras (RPMs)
basearch: x86_64
- name: Red Hat Enterprise Linux 7 Server - Optional (RPMs)
basearch: x86_64
- name: Red Hat Enterprise Linux for x86_64
sync: true
repository_sets:
- name: Red Hat Enterprise Linux 8 for x86_64 - BaseOS (RPMs)
releasever: 8
- name: Red Hat Enterprise Linux 8 for x86_64 - AppStream (RPMs)
releasever: 8
```
2 changes: 2 additions & 0 deletions roles/repositories_sync/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
products: []
13 changes: 13 additions & 0 deletions roles/repositories_sync/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
- name: sync product
theforeman.foreman.repository_sync:
username: "{{ username | default(omit) }}"
password: "{{ password | default(omit) }}"
server_url: "{{ server_url | default(omit) }}"
validate_certs: "{{ validate_certs | default(omit) }}"
organization: "{{ organization }}"
product: "{{ item.0.name }}"
with_subelements:
- "{{ products | selectattr('repository_sets', 'defined') | list }}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this means it won't sync any custom repositories that the repositories role can create.

- repository_sets
when: item.0.sync | default(false) | bool
Comment on lines +9 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
product: "{{ item.0.name }}"
with_subelements:
- "{{ products | selectattr('repository_sets', 'defined') | list }}"
- repository_sets
when: item.0.sync | default(false) | bool
product: "{{ item.name }}"
loop: "{{ products }}"
when: item.sync | default(false) | bool

There is no need to iterate over the repositories anymore, as we're syncing by-product now.
Also loop over all products, regardless of repository_sets (RH repos) or repositories (custom repos) attributes.