Skip to content

Commit 4a8381e

Browse files
committed
pulp_distribution: fix distribution promotion
Promotion of one distribution to the publication of another distribution currently fails. We see: 'str object' has no attribute 'pulp_href' This change fixes the issue by refactoring the complex jinja logic into ansible filters. Also fixes an issue when a distribution has a state of absent - we should not need to specify a repository or base_path. Fixes: #34 Fixes: #35
1 parent 32ba370 commit 4a8381e

File tree

5 files changed

+126
-103
lines changed

5 files changed

+126
-103
lines changed

plugins/filter/filters.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15+
from ansible import errors
16+
17+
1518
def sort_publications(pubs):
1619
"""Sort a list of publications by repository version.
1720
@@ -25,9 +28,71 @@ def key(pub):
2528
return sorted(pubs, key=key, reverse=True)
2629

2730

31+
def find_publication_for_distribution(dist, repos, pubs, dists):
32+
"""Find a publication to distribute.
33+
34+
The distribution object, `dist`, may include different fields to control
35+
the search:
36+
37+
* repository: use latest version
38+
* repository & version: use a specific version
39+
* distribution: use the same publication as another distribution
40+
41+
:param dist: Distribution to find a publication for.
42+
:param repos: Existing repos in Pulp.
43+
:param pubs: Existing publications in Pulp.
44+
:param dists: Existing distributions in Pulp.
45+
"""
46+
if dist.get("state") == "absent":
47+
return
48+
49+
if "repository" in dist:
50+
if "distribution" in dist:
51+
raise errors.AnsibleFilterError("Cannot specify 'distribution' with 'repository'")
52+
repository = dist["repository"]
53+
repository_href = [repo for repo in repos if repo["name"] == repository][0]["pulp_href"]
54+
if "version" in dist:
55+
version = dist["version"]
56+
version_href = "%sversions/%d/" % (repository_href, version)
57+
else:
58+
version_href = None
59+
60+
for pub in pubs:
61+
if pub["repository"] == repository_href:
62+
if not version_href or version_href == pub["repository_version"]:
63+
return pub
64+
elif "distribution" in dist:
65+
if "version" in dist:
66+
raise errors.AnsibleFilterError("Cannot specify 'version' with 'distribution'")
67+
other_dist = dist["distribution"]
68+
pub_href = [d for d in dists if d["name"] == other_dist][0]["publication"]
69+
for pub in pubs:
70+
if pub["pulp_href"] == pub_href:
71+
return pub
72+
raise errors.AnsibleFilterError("Could not find a matching publication")
73+
74+
75+
def publication_has_distributions(pub, pubs, dists):
76+
"""Return whether a publication has any distributions.
77+
78+
:param pub: Publication to find a distribution for.
79+
:param pubs: Existing publications in Pulp.
80+
:param dists: Existing distributions in Pulp.
81+
"""
82+
if not pub:
83+
return False
84+
pub_href = pub["pulp_href"]
85+
for dist in dists:
86+
if dist["publication"] == pub_href:
87+
return True
88+
return False
89+
90+
2891
class FilterModule(object):
2992

3093
def filters(self):
3194
return {
32-
"sort_publications": sort_publications
95+
"sort_publications": sort_publications,
96+
"find_publication_for_distribution": find_publication_for_distribution,
97+
"publication_has_distributions": publication_has_distributions,
3398
}

roles/pulp_distribution/tasks/deb.yml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,22 @@
3636

3737
- name: Ensure Deb distributions are defined
3838
vars:
39-
repo: "{{ pulp_repos_list.repositories | selectattr('name', 'equalto', item.repository) | first }}"
4039
all_pubs_list: "{{ pulp_pubs_list.publications + pulp_verbatim_pubs_list.publications }}"
41-
# If the distribution references a specific version:
42-
specific_pub: "{{ all_pubs_list | selectattr('repository_version', 'equalto', repo.pulp_href ~ 'versions/' ~ item.version | default ~ '/') }}"
43-
# If the distribution uses the latest version:
44-
latest_pub: "{{ all_pubs_list | selectattr('repository', 'equalto', repo.pulp_href) | stackhpc.pulp.sort_publications }}"
45-
# If another distribution is being promoted to this one:
46-
promoted_dist: "{{ pulp_dists_list.distributions | selectattr('name', 'equalto', item.distribution | default) }}"
47-
promoted_pub: "{{ all_pubs_list | selectattr('pulp_href', 'equalto', (promoted_dist | first).publication | default) }}"
48-
# Pick the right publication based on the type of distribution.
49-
pubs: "{{ specific_pub if item.version is defined else promoted_pub if item.distribution is defined else latest_pub }}"
50-
# Whether any distributions exist for this publication.
40+
publication: >-
41+
{{ item | stackhpc.pulp.find_publication_for_distribution(pulp_repos_list.repositories,
42+
all_pubs_list,
43+
pulp_dists_list.distributions) }}
5144
pub_has_dists: >-
52-
{{ pulp_dists_list.distributions | selectattr('publication', 'equalto', pubs[0].pulp_href if item.state == 'present' else None) | list | length > 0 }}
45+
{{ publication | stackhpc.pulp.publication_has_distributions(all_pubs_list,
46+
pulp_dists_list.distributions) }}
5347
pulp.squeezer.deb_distribution:
5448
pulp_url: "{{ pulp_url }}"
5549
username: "{{ pulp_username }}"
5650
password: "{{ pulp_password }}"
5751
validate_certs: "{{ pulp_validate_certs | bool }}"
5852
name: "{{ item.name }}"
59-
base_path: "{{ item.base_path }}"
60-
publication: "{{ pubs[0].pulp_href if item.state == 'present' else omit }}"
53+
base_path: "{{ item.base_path | default(omit) }}"
54+
publication: "{{ publication.pulp_href if item.state == 'present' else omit }}"
6155
content_guard: "{{ item.content_guard | default(omit) }}"
6256
state: "{{ item.state }}"
6357
with_items: "{{ pulp_distribution_deb }}"

roles/pulp_distribution/tasks/rpm.yml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,21 @@
2525

2626
- name: Ensure RPM distributions are defined
2727
vars:
28-
repo: "{{ pulp_repos_list.repositories | selectattr('name', 'equalto', item.repository) | first }}"
29-
# If the distribution references a specific version:
30-
specific_pub: "{{ pulp_pubs_list.publications | selectattr('repository_version', 'equalto', repo.pulp_href ~ 'versions/' ~ item.version | default ~ '/') }}"
31-
# If the distribution uses the latest version:
32-
latest_pub: "{{ pulp_pubs_list.publications | selectattr('repository', 'equalto', repo.pulp_href) | stackhpc.pulp.sort_publications }}"
33-
# If another distribution is being promoted to this one:
34-
promoted_dist: "{{ pulp_dists_list.distributions | selectattr('name', 'equalto', item.distribution | default) }}"
35-
promoted_pub: "{{ pulp_pubs_list.publications | selectattr('pulp_href', 'equalto', (promoted_dist | first).publication | default) }}"
36-
# Pick the right publication based on the type of distribution.
37-
pubs: "{{ specific_pub if item.version is defined else promoted_pub if item.distribution is defined else latest_pub }}"
38-
# Whether any distributions exist for this publication.
28+
publication: >-
29+
{{ item | stackhpc.pulp.find_publication_for_distribution(pulp_repos_list.repositories,
30+
pulp_pubs_list.publications,
31+
pulp_dists_list.distributions) }}
3932
pub_has_dists: >-
40-
{{ pulp_dists_list.distributions | selectattr('publication', 'equalto', pubs[0].pulp_href if item.state == 'present' else None) | list | length > 0 }}
33+
{{ publication | stackhpc.pulp.publication_has_distributions(pulp_pubs_list.publications,
34+
pulp_dists_list.distributions) }}
4135
pulp.squeezer.rpm_distribution:
4236
pulp_url: "{{ pulp_url }}"
4337
username: "{{ pulp_username }}"
4438
password: "{{ pulp_password }}"
4539
validate_certs: "{{ pulp_validate_certs | bool }}"
4640
name: "{{ item.name }}"
47-
base_path: "{{ item.base_path }}"
48-
publication: "{{ pubs[0].pulp_href if item.state == 'present' else omit }}"
41+
base_path: "{{ item.base_path | default(omit) }}"
42+
publication: "{{ publication.pulp_href if item.state == 'present' else omit }}"
4943
content_guard: "{{ item.content_guard | default(omit) }}"
5044
state: "{{ item.state }}"
5145
with_items: "{{ pulp_distribution_rpm }}"

tests/test_deb_distribution.yml

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,14 @@
3939
version: 1
4040
state: present
4141

42-
# FIXME: This currently fails due to
43-
# https://github.com/stackhpc/ansible-collection-pulp/issues/34.
44-
# - include_role:
45-
# name: pulp_distribution
46-
# vars:
47-
# pulp_distribution_deb:
48-
# - name: test_deb_distribution_distribution
49-
# base_path: test_deb_distribution_distribution
50-
# distribution: test_deb_distribution
51-
# state: present
42+
- include_role:
43+
name: pulp_distribution
44+
vars:
45+
pulp_distribution_deb:
46+
- name: test_deb_distribution_distribution
47+
base_path: test_deb_distribution_distribution
48+
distribution: test_deb_distribution
49+
state: present
5250

5351
- name: Query repository
5452
pulp.squeezer.deb_repository:
@@ -87,16 +85,14 @@
8785
name: test_deb_distribution_version_1
8886
register: dist_version_1_result
8987

90-
# FIXME: This currently fails due to
91-
# https://github.com/stackhpc/ansible-collection-pulp/issues/34.
92-
# - name: Query distribution distribution
93-
# pulp.squeezer.deb_distribution:
94-
# pulp_url: "{{ pulp_url }}"
95-
# username: "{{ pulp_username }}"
96-
# password: "{{ pulp_password }}"
97-
# validate_certs: "{{ pulp_validate_certs }}"
98-
# name: test_deb_distribution_distribution
99-
# register: dist_distribution_result
88+
- name: Query distribution distribution
89+
pulp.squeezer.deb_distribution:
90+
pulp_url: "{{ pulp_url }}"
91+
username: "{{ pulp_username }}"
92+
password: "{{ pulp_password }}"
93+
validate_certs: "{{ pulp_validate_certs }}"
94+
name: test_deb_distribution_distribution
95+
register: dist_distribution_result
10096

10197
- name: Verify publication creation
10298
assert:
@@ -117,33 +113,22 @@
117113
- dist_version_1_result.distribution.base_path == "test_deb_distribution_version_1"
118114
- dist_version_1_result.distribution.publication == pub_result.publication.pulp_href
119115

120-
# FIXME: This currently fails due to
121-
# https://github.com/stackhpc/ansible-collection-pulp/issues/34.
122-
# - name: Verify distribution creation
123-
# assert:
124-
# that:
125-
# - dist_distribution_result.distribution.name == "test_deb_distribution_distribution"
126-
# - dist_distribution_result.distribution.base_path == "test_deb_distribution_distribution"
127-
# - dist_distribution_result.distribution.publication == pub_result.publication.pulp_href
116+
- name: Verify distribution creation
117+
assert:
118+
that:
119+
- dist_distribution_result.distribution.name == "test_deb_distribution_distribution"
120+
- dist_distribution_result.distribution.base_path == "test_deb_distribution_distribution"
121+
- dist_distribution_result.distribution.publication == pub_result.publication.pulp_href
128122

129123
- include_role:
130124
name: pulp_distribution
131125
vars:
132126
pulp_distribution_deb:
133127
- name: test_deb_distribution
134-
# FIXME: These should not be required.
135-
repository: test_deb_repo
136-
base_path: foo
137128
state: absent
138129
- name: test_deb_distribution_version_1
139-
# FIXME: These should not be required.
140-
repository: test_deb_repo
141-
base_path: foo
142130
state: absent
143131
- name: test_deb_distribution_distribution
144-
# FIXME: These should not be required.
145-
repository: test_deb_repo
146-
base_path: foo
147132
state: absent
148133

149134
- include_role:

tests/test_rpm_distribution.yml

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@
3838
version: 1
3939
state: present
4040

41-
# FIXME: This currently fails due to
42-
# https://github.com/stackhpc/ansible-collection-pulp/issues/34.
43-
# - include_role:
44-
# name: pulp_distribution
45-
# vars:
46-
# pulp_distribution_rpm:
47-
# - name: test_rpm_distribution_distribution
48-
# base_path: test_rpm_distribution_distribution
49-
# distribution: test_rpm_distribution
50-
# state: present
41+
- include_role:
42+
name: pulp_distribution
43+
vars:
44+
pulp_distribution_rpm:
45+
- name: test_rpm_distribution_distribution
46+
base_path: test_rpm_distribution_distribution
47+
distribution: test_rpm_distribution
48+
state: present
5149

5250
- name: Query repository
5351
pulp.squeezer.rpm_repository:
@@ -86,16 +84,14 @@
8684
name: test_rpm_distribution_version_1
8785
register: dist_version_1_result
8886

89-
# FIXME: This currently fails due to
90-
# https://github.com/stackhpc/ansible-collection-pulp/issues/34.
91-
# - name: Query distribution distribution
92-
# pulp.squeezer.rpm_distribution:
93-
# pulp_url: "{{ pulp_url }}"
94-
# username: "{{ pulp_username }}"
95-
# password: "{{ pulp_password }}"
96-
# validate_certs: "{{ pulp_validate_certs }}"
97-
# name: test_rpm_distribution_distribution
98-
# register: dist_distribution_result
87+
- name: Query distribution distribution
88+
pulp.squeezer.rpm_distribution:
89+
pulp_url: "{{ pulp_url }}"
90+
username: "{{ pulp_username }}"
91+
password: "{{ pulp_password }}"
92+
validate_certs: "{{ pulp_validate_certs }}"
93+
name: test_rpm_distribution_distribution
94+
register: dist_distribution_result
9995

10096
- name: Verify publication creation
10197
assert:
@@ -116,33 +112,22 @@
116112
- dist_version_1_result.distribution.base_path == "test_rpm_distribution_version_1"
117113
- dist_version_1_result.distribution.publication == pub_result.publication.pulp_href
118114

119-
# FIXME: This currently fails due to
120-
# https://github.com/stackhpc/ansible-collection-pulp/issues/34.
121-
# - name: Verify distribution creation
122-
# assert:
123-
# that:
124-
# - dist_distribution_result.distribution.name == "test_rpm_distribution_distribution"
125-
# - dist_distribution_result.distribution.base_path == "test_rpm_distribution_distribution"
126-
# - dist_distribution_result.distribution.publication == pub_result.publication.pulp_href
115+
- name: Verify distribution creation
116+
assert:
117+
that:
118+
- dist_distribution_result.distribution.name == "test_rpm_distribution_distribution"
119+
- dist_distribution_result.distribution.base_path == "test_rpm_distribution_distribution"
120+
- dist_distribution_result.distribution.publication == pub_result.publication.pulp_href
127121

128122
- include_role:
129123
name: pulp_distribution
130124
vars:
131125
pulp_distribution_rpm:
132126
- name: test_rpm_distribution
133-
# FIXME: These should not be required.
134-
repository: test_rpm_repo
135-
base_path: foo
136127
state: absent
137128
- name: test_rpm_distribution_version_1
138-
# FIXME: These should not be required.
139-
repository: test_rpm_repo
140-
base_path: foo
141129
state: absent
142130
- name: test_rpm_distribution_distribution
143-
# FIXME: These should not be required.
144-
repository: test_rpm_repo
145-
base_path: foo
146131
state: absent
147132

148133
- include_role:

0 commit comments

Comments
 (0)