Cleaned up unit tests.
[puppet-modules/puppetlabs-apt.git] / manifests / pin.pp
1 # pin.pp
2 # pin a release in apt, useful for unstable repositories
3
4 define apt::pin(
5   $ensure          = present,
6   $explanation     = "${caller_module_name}: ${name}",
7   $order           = '',
8   $packages        = '*',
9   $priority        = 0,
10   $release         = '', # a=
11   $origin          = '',
12   $version         = '',
13   $codename        = '', # n=
14   $release_version = '', # v=
15   $component       = '', # c=
16   $originator      = '', # o=
17   $label           = ''  # l=
18 ) {
19   include apt::params
20
21   $preferences_d = $apt::params::preferences_d
22
23   if $order != '' and !is_integer($order) {
24     fail('Only integers are allowed in the apt::pin order param')
25   }
26
27   $pin_release_array = [
28     $release,
29     $codename,
30     $release_version,
31     $component,
32     $originator,
33     $label]
34   $pin_release = join($pin_release_array, '')
35
36   # Read the manpage 'apt_preferences(5)', especially the chapter
37   # 'The Effect of APT Preferences' to understand the following logic
38   # and the difference between specific and general form
39   if is_array($packages) {
40     $packages_string = join($packages, ' ')
41   } else {
42     $packages_string = $packages
43   }
44
45   if $packages_string != '*' { # specific form
46     if ( $pin_release != '' and ( $origin != '' or $version != '' )) or
47       ( $version != '' and ( $pin_release != '' or $origin != '' )) {
48       fail('parameters release, origin, and version are mutually exclusive')
49     }
50   } else { # general form
51     if $version != '' {
52       fail('parameter version cannot be used in general form')
53     }
54     if ( $pin_release != '' and $origin != '' ) {
55       fail('parameters release and origin are mutually exclusive')
56     }
57   }
58
59
60   # According to man 5 apt_preferences:
61   # The files have either no or "pref" as filename extension
62   # and only contain alphanumeric, hyphen (-), underscore (_) and period
63   # (.) characters. Otherwise APT will print a notice that it has ignored a
64   # file, unless that file matches a pattern in the
65   # Dir::Ignore-Files-Silently configuration list - in which case it will
66   # be silently ignored.
67   $file_name = regsubst($title, '[^0-9a-z\-_\.]', '_', 'IG')
68
69   $path = $order ? {
70     ''      => "${preferences_d}/${file_name}.pref",
71     default => "${preferences_d}/${order}-${file_name}.pref",
72   }
73   file { "${file_name}.pref":
74     ensure  => $ensure,
75     path    => $path,
76     owner   => root,
77     group   => root,
78     mode    => '0644',
79     content => template('apt/pin.pref.erb'),
80   }
81 }