Merge pull request #666 from HelenCampbell/release
[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     = undef,
7   $order           = 50,
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   if $order and !is_integer($order) {
20     fail('Only integers are allowed in the apt::pin order param')
21   }
22
23   if $explanation {
24     $_explanation = $explanation
25   } else {
26     if defined('$caller_module_name') { # strict vars check
27       $_explanation = "${caller_module_name}: ${name}"
28     } else {
29       $_explanation = ": ${name}"
30     }
31   }
32
33   $pin_release_array = [
34     $release,
35     $codename,
36     $release_version,
37     $component,
38     $originator,
39     $label]
40   $pin_release = join($pin_release_array, '')
41
42   # Read the manpage 'apt_preferences(5)', especially the chapter
43   # 'The Effect of APT Preferences' to understand the following logic
44   # and the difference between specific and general form
45   if is_array($packages) {
46     $packages_string = join($packages, ' ')
47   } else {
48     $packages_string = $packages
49   }
50
51   if $packages_string != '*' { # specific form
52     if ( $pin_release != '' and ( $origin != '' or $version != '' )) or
53       ( $version != '' and ( $pin_release != '' or $origin != '' )) {
54       fail('parameters release, origin, and version are mutually exclusive')
55     }
56   } else { # general form
57     if $version != '' {
58       fail('parameter version cannot be used in general form')
59     }
60     if ( $pin_release != '' and $origin != '' ) {
61       fail('parameters release and origin are mutually exclusive')
62     }
63   }
64
65   # According to man 5 apt_preferences:
66   # The files have either no or "pref" as filename extension
67   # and only contain alphanumeric, hyphen (-), underscore (_) and period
68   # (.) characters. Otherwise APT will print a notice that it has ignored a
69   # file, unless that file matches a pattern in the
70   # Dir::Ignore-Files-Silently configuration list - in which case it will
71   # be silently ignored.
72   $file_name = regsubst($title, '[^0-9a-z\-_\.]', '_', 'IG')
73
74   apt::setting { "pref-${file_name}":
75     ensure        => $ensure,
76     priority      => $order,
77     content       => template('apt/_header.erb', 'apt/pin.pref.erb'),
78     notify_update => false,
79   }
80 }