Merge pull request #711 from icann-dns/fix_legacy_functions
[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   Optional[Enum['file', 'present', 'absent']] $ensure = present,
6   Optional[String] $explanation                       = undef,
7   Variant[Integer] $order                             = 50,
8   Variant[String, Array] $packages                    = '*',
9   Variant[Numeric, String] $priority                  = 0,
10   Optional[String] $release                           = '', # a=
11   Optional[String] $origin                            = '',
12   Optional[String] $version                           = '',
13   Optional[String] $codename                          = '', # n=
14   Optional[String] $release_version                   = '', # v=
15   Optional[String] $component                         = '', # c=
16   Optional[String] $originator                        = '', # o=
17   Optional[String] $label                             = '',  # l=
18 ) {
19
20   if $explanation {
21     $_explanation = $explanation
22   } else {
23     if defined('$caller_module_name') { # strict vars check
24       $_explanation = "${caller_module_name}: ${name}"
25     } else {
26       $_explanation = ": ${name}"
27     }
28   }
29
30   $pin_release_array = [
31     $release,
32     $codename,
33     $release_version,
34     $component,
35     $originator,
36     $label,
37   ]
38   $pin_release = join($pin_release_array, '')
39
40   # Read the manpage 'apt_preferences(5)', especially the chapter
41   # 'The Effect of APT Preferences' to understand the following logic
42   # and the difference between specific and general form
43   if $packages =~ Array {
44     $packages_string = join($packages, ' ')
45   } else {
46     $packages_string = $packages
47   }
48
49   if $packages_string != '*' { # specific form
50     if ( $pin_release != '' and ( $origin != '' or $version != '' )) or
51       ( $version != '' and ( $pin_release != '' or $origin != '' )) {
52       fail('parameters release, origin, and version are mutually exclusive')
53     }
54   } else { # general form
55     if $version != '' {
56       fail('parameter version cannot be used in general form')
57     }
58     if ( $pin_release != '' and $origin != '' ) {
59       fail('parameters release and origin are mutually exclusive')
60     }
61   }
62
63   # According to man 5 apt_preferences:
64   # The files have either no or "pref" as filename extension
65   # and only contain alphanumeric, hyphen (-), underscore (_) and period
66   # (.) characters. Otherwise APT will print a notice that it has ignored a
67   # file, unless that file matches a pattern in the
68   # Dir::Ignore-Files-Silently configuration list - in which case it will
69   # be silently ignored.
70   $file_name = regsubst($title, '[^0-9a-z\-_\.]', '_', 'IG')
71
72   $headertmp = epp('apt/_header.epp')
73
74   $pinpreftmp = epp('apt/pin.pref.epp', {
75       'name'            => $name,
76       'pin_release'     => $pin_release,
77       'release'         => $release,
78       'codename'        => $codename,
79       'release_version' => $release_version,
80       'component'       => $component,
81       'originator'      => $originator,
82       'label'           => $label,
83       'version'         => $version,
84       'origin'          => $origin,
85       'explanation'     => $_explanation,
86       'packages_string' => $packages_string,
87       'priority'        => $priority,
88   })
89
90   apt::setting { "pref-${file_name}":
91     ensure        => $ensure,
92     priority      => $order,
93     content       => "${headertmp}${pinpreftmp}",
94     notify_update => false,
95   }
96 }