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