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