Skip to content

Commit 792e781

Browse files
committed
Add yum::copr resource to handle COPR repositories.
COPR (Cool Other Package Repo) is a Fedora project for third-party package repositories.
1 parent 1f96aa5 commit 792e781

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ yum::config { 'debuglevel':
6161
}
6262
```
6363

64+
### Manage COPR repositories
65+
66+
This module also supports managing
67+
COPR ([Cool Other Package Repo](https://fedoraproject.org/wiki/Category:Copr))
68+
repositories via the `yum::copr` resource. The resource title specifies
69+
the COPR repository name, and `ensure` accepts the values `enabled`, `disabled`
70+
or `removed`. Example usage:
71+
72+
```puppet
73+
yum::copr { 'copart/restic':
74+
ensure => enabled,
75+
}
76+
```
77+
6478
### Manage a custom repo via Hiera data
6579

6680
Using Hiera and automatic parameter lookup (APL), this module can manage

manifests/copr.pp

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Define: yum::copr
2+
#
3+
# This definition manages Copr (Cool Other Package Repo) repositories.
4+
#
5+
# Parameters:
6+
# [*copr_repo*] - name of repository, defaults to title
7+
# [*ensure*] - specifies if repo should be enabled,
8+
# disabled or removed
9+
#
10+
# Actions:
11+
#
12+
# Requires:
13+
# RPM based system
14+
#
15+
# Sample usage:
16+
# yum::copr { 'copart/restic':
17+
# ensure => 'enabled',
18+
# }
19+
#
20+
define yum::copr (
21+
String $copr_repo = $title,
22+
Enum['enabled', 'disabled', 'removed'] $ensure = 'enabled',
23+
) {
24+
$prereq_plugin = $facts['package_provider'] ? {
25+
'dnf' => 'dnf-plugins-core',
26+
default => 'yum-plugin-copr',
27+
}
28+
ensure_packages([$prereq_plugin])
29+
30+
if $facts['package_provider'] == 'dnf' {
31+
case $ensure {
32+
'enabled': {
33+
exec { "dnf -y copr enable ${copr_repo}":
34+
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
35+
unless => "dnf copr list | egrep -q '${copr_repo}\$'",
36+
require => Package[$prereq_plugin],
37+
}
38+
}
39+
'disabled': {
40+
exec { "dnf -y copr disable ${copr_repo}":
41+
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
42+
unless => "dnf copr list | egrep -q '${copr_repo} (disabled)\$'",
43+
require => Package[$prereq_plugin],
44+
}
45+
}
46+
'removed': {
47+
exec { "dnf -y copr remove ${copr_repo}":
48+
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
49+
onlyif => "dnf copr list | egrep -q '${copr_repo}'",
50+
require => Package[$prereq_plugin],
51+
}
52+
}
53+
default: {
54+
fail("The value for ensure for `yum::copr` must be enabled, disabled or removed, but it is ${ensure}.")
55+
}
56+
}
57+
} else {
58+
$copr_repo_name_part = regsubst($copr_repo, '/', '-', 'G')
59+
case $ensure {
60+
'enabled': {
61+
exec { "yum -y copr enable ${copr_repo}":
62+
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
63+
onlyif => "test ! -e /etc/yum.repos.d/_copr_${copr_repo_name_part}.repo",
64+
require => Package[$prereq_plugin],
65+
}
66+
}
67+
'disabled', 'removed': {
68+
exec { "yum -y copr disable ${copr_repo}":
69+
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
70+
onlyif => "test -e /etc/yum.repos.d/_copr_${copr_repo_name_part}.repo",
71+
require => Package[$prereq_plugin],
72+
}
73+
}
74+
default: {
75+
fail("The value for ensure for `yum::copr` must be enabled, disabled or removed, but it is ${ensure}.")
76+
}
77+
}
78+
}
79+
}

spec/defines/copr_spec.rb

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'yum::copr' do
6+
context 'with package_provider set to yum' do
7+
let(:facts) { { package_provider: 'yum' } }
8+
let(:prereq_plugin) { 'yum-plugin-copr' }
9+
let(:title) { 'copart/restic' }
10+
let(:copr_repo_name_part) { title.gsub('/', '-') }
11+
12+
context 'package provider plugin installed' do
13+
it { is_expected.to compile.with_all_deps }
14+
15+
it {
16+
is_expected.to contain_package(prereq_plugin.to_s)
17+
}
18+
end
19+
20+
context 'with ensure = enabled' do
21+
let(:params) { { ensure: 'enabled' } }
22+
23+
it { is_expected.to compile.with_all_deps }
24+
25+
it {
26+
is_expected.to contain_exec("yum -y copr enable #{title}").with(
27+
'path' => '/bin:/usr/bin:/sbin/:/usr/sbin',
28+
'onlyif' => "test ! -e /etc/yum.repos.d/_copr_#{copr_repo_name_part}.repo",
29+
'require' => "Package[#{prereq_plugin}]"
30+
)
31+
}
32+
end
33+
34+
context 'with ensure = disabled' do
35+
let(:params) { { ensure: 'disabled' } }
36+
37+
it { is_expected.to compile.with_all_deps }
38+
39+
it {
40+
is_expected.to contain_exec("yum -y copr disable #{title}").with(
41+
'path' => '/bin:/usr/bin:/sbin/:/usr/sbin',
42+
'onlyif' => "test -e /etc/yum.repos.d/_copr_#{copr_repo_name_part}.repo",
43+
'require' => "Package[#{prereq_plugin}]"
44+
)
45+
}
46+
end
47+
48+
context 'with ensure = removed' do
49+
let(:params) { { ensure: 'removed' } }
50+
51+
it { is_expected.to compile.with_all_deps }
52+
53+
it {
54+
is_expected.to contain_exec("yum -y copr disable #{title}").with(
55+
'path' => '/bin:/usr/bin:/sbin/:/usr/sbin',
56+
'onlyif' => "test -e /etc/yum.repos.d/_copr_#{copr_repo_name_part}.repo",
57+
'require' => "Package[#{prereq_plugin}]"
58+
)
59+
}
60+
end
61+
end
62+
63+
context 'with package_provider set to dnf' do
64+
let(:facts) { { package_provider: 'dnf' } }
65+
let(:prereq_plugin) { 'dnf-plugins-core' }
66+
let(:title) { 'copart/restic' }
67+
68+
context 'package provider plugin installed' do
69+
it { is_expected.to compile.with_all_deps }
70+
71+
it {
72+
is_expected.to contain_package("#{prereq_plugin}")
73+
}
74+
end
75+
76+
context 'with ensure = enabled' do
77+
let(:params) { { ensure: 'enabled' } }
78+
79+
it { is_expected.to compile.with_all_deps }
80+
81+
it {
82+
is_expected.to contain_exec("dnf -y copr enable #{title}").with(
83+
'path' => '/bin:/usr/bin:/sbin/:/usr/sbin',
84+
'unless' => "dnf copr list | egrep -q '#{title}\$'",
85+
'require' => "Package[#{prereq_plugin}]"
86+
)
87+
}
88+
end
89+
90+
context 'with ensure = disabled' do
91+
let(:params) { { ensure: 'disabled' } }
92+
93+
it { is_expected.to compile.with_all_deps }
94+
95+
it {
96+
is_expected.to contain_exec("dnf -y copr disable #{title}").with(
97+
'path' => '/bin:/usr/bin:/sbin/:/usr/sbin',
98+
'unless' => "dnf copr list | egrep -q '#{title} (disabled)\$'",
99+
'require' => "Package[#{prereq_plugin}]"
100+
)
101+
}
102+
end
103+
104+
context 'with ensure = removed' do
105+
let(:params) { { ensure: 'removed' } }
106+
107+
it { is_expected.to compile.with_all_deps }
108+
109+
it {
110+
is_expected.to contain_exec("dnf -y copr remove #{title}").with(
111+
'path' => '/bin:/usr/bin:/sbin/:/usr/sbin',
112+
'onlyif' => "dnf copr list | egrep -q '#{title}'",
113+
'require' => "Package[#{prereq_plugin}]"
114+
)
115+
}
116+
end
117+
end
118+
end

0 commit comments

Comments
 (0)