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