Skip to content

Commit 8db4120

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 8db4120

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-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

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

0 commit comments

Comments
 (0)