Skip to content

Commit 6004680

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 6004680

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+
# [*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 $repo = $title,
22+
Enum['enabled', 'disabled', 'removed'] $ensure = 'enabled',
23+
) {
24+
$prereq_plugin = $facts['package_provider'] ? {
25+
'yum' => 'yum-plugin-copr',
26+
default => 'dnf-plugins-core',
27+
}
28+
ensure_packages([$prereq_plugin])
29+
30+
if $facts['package_provider'] == 'yum' {
31+
$repo_name_part = regsubst($repo, '/', '-', 'G')
32+
case $ensure {
33+
'enabled': {
34+
exec { "yum -y copr enable ${repo}":
35+
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
36+
onlyif => "test ! -e /etc/yum.repos.d/_copr_${repo_name_part}.repo",
37+
require => Package[$prereq_plugin],
38+
}
39+
}
40+
'disabled', 'removed': {
41+
exec { "yum -y copr disable ${repo}":
42+
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
43+
onlyif => "test -e /etc/yum.repos.d/_copr_${repo_name_part}.repo",
44+
require => Package[$prereq_plugin],
45+
}
46+
}
47+
default: {
48+
fail("The value for ensure for `yum::copr` must be enabled, disabled or removed, but it is ${ensure}.")
49+
}
50+
}
51+
} else {
52+
case $ensure {
53+
'enabled': {
54+
exec { "dnf -y copr enable ${repo}":
55+
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
56+
unless => "dnf copr list | egrep -q '${repo}\$'",
57+
require => Package[$prereq_plugin],
58+
}
59+
}
60+
'disabled': {
61+
exec { "dnf -y copr disable ${repo}":
62+
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
63+
unless => "dnf copr list | egrep -q '${repo} (disabled)\$'",
64+
require => Package[$prereq_plugin],
65+
}
66+
}
67+
'removed': {
68+
exec { "dnf -y copr remove ${repo}":
69+
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
70+
unless => "dnf copr list | egrep -q '${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(: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_#{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_#{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_#{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)