"This change pins the puppetlabs-puppet_agent module to v4.12.1. Previosuly the fixut...
[puppet-modules/puppetlabs-apt.git] / manifests / pin.pp
1 # @summary Manages Apt pins. Does not trigger an apt-get update run.
2 #
3 # @see http://linux.die.net/man/5/apt_preferences for context on these parameters
4 #
5 # @param ensure
6 #   Specifies whether the pin should exist. Valid options: 'file', 'present', and 'absent'.
7 #
8 # @param explanation
9 #   Supplies a comment to explain the pin. Default: "${caller_module_name}: ${name}".
10 #
11 # @param order
12 #   Determines the order in which Apt processes the pin file. Files with lower order numbers are loaded first.
13 #
14 # @param packages
15 #   Specifies which package(s) to pin.
16 #
17 # @param priority
18 #   Sets the priority of the package. If multiple versions of a given package are available, `apt-get` installs the one with the highest 
19 #   priority number (subject to dependency constraints). Valid options: an integer.
20 #
21 # @param release
22 #   Tells APT to prefer packages that support the specified release. Typical values include 'stable', 'testing', and 'unstable'.
23 #
24 # @param release_version
25 #   Tells APT to prefer packages that support the specified operating system release version (such as Debian release version 7).
26 #
27 # @param component
28 #   Names the licensing component associated with the packages in the directory tree of the Release file.
29 #
30 # @param originator
31 #   Names the originator of the packages in the directory tree of the Release file.
32 #
33 # @param label
34 #   Names the label of the packages in the directory tree of the Release file.
35 #
36 # @param origin
37 #   The package origin
38 #
39 # @param version
40 #   The version of the package
41 #
42 # @param codename
43 #   The codename of the package
44 #
45 define apt::pin (
46   Enum['file', 'present', 'absent'] $ensure = present,
47   Optional[String] $explanation             = undef,
48   Variant[Integer] $order                   = 50,
49   Variant[String, Array] $packages          = '*',
50   Variant[Numeric, String] $priority        = 0,
51   Optional[String] $release                 = undef, # a=
52   Optional[String] $origin                  = undef,
53   Optional[String] $version                 = undef,
54   Optional[String] $codename                = undef, # n=
55   Optional[String] $release_version         = undef, # v=
56   Optional[String] $component               = undef, # c=
57   Optional[String] $originator              = undef, # o=
58   Optional[String] $label                   = undef,  # l=
59 ) {
60   if $explanation {
61     $_explanation = $explanation
62   } else {
63     if defined('$caller_module_name') { # strict vars check
64       $_explanation = "${caller_module_name}: ${name}"
65     } else {
66       $_explanation = ": ${name}"
67     }
68   }
69
70   $pin_release_array = [
71     $release,
72     $codename,
73     $release_version,
74     $component,
75     $originator,
76     $label,
77   ]
78   $pin_release = join($pin_release_array, '')
79
80   # Read the manpage 'apt_preferences(5)', especially the chapter
81   # 'The Effect of APT Preferences' to understand the following logic
82   # and the difference between specific and general form
83   if $packages =~ Array {
84     $packages_string = join($packages, ' ')
85   } else {
86     $packages_string = $packages
87   }
88
89   if $packages_string != '*' { # specific form
90     if ( $pin_release != '' and ( $origin or $version )) or
91     ( $version and ( $pin_release != '' or $origin )) {
92       fail('parameters release, origin, and version are mutually exclusive')
93     }
94   } else { # general form
95     if $version {
96       fail('parameter version cannot be used in general form')
97     }
98     if ( $pin_release != '' and $origin ) {
99       fail('parameters release and origin are mutually exclusive')
100     }
101   }
102
103   # According to man 5 apt_preferences:
104   # The files have either no or "pref" as filename extension
105   # and only contain alphanumeric, hyphen (-), underscore (_) and period
106   # (.) characters. Otherwise APT will print a notice that it has ignored a
107   # file, unless that file matches a pattern in the
108   # Dir::Ignore-Files-Silently configuration list - in which case it will
109   # be silently ignored.
110   $file_name = regsubst($title, '[^0-9a-z\-_\.]', '_', 'IG')
111
112   $headertmp = epp('apt/_header.epp')
113
114   $pinpreftmp = epp('apt/pin.pref.epp', {
115       'name'            => $name,
116       'pin_release'     => $pin_release,
117       'release'         => $release,
118       'codename'        => $codename,
119       'release_version' => $release_version,
120       'component'       => $component,
121       'originator'      => $originator,
122       'label'           => $label,
123       'version'         => $version,
124       'origin'          => $origin,
125       'explanation'     => $_explanation,
126       'packages_string' => $packages_string,
127       'priority'        => $priority,
128   })
129
130   apt::setting { "pref-${file_name}":
131     ensure        => $ensure,
132     priority      => $order,
133     content       => "${headertmp}${pinpreftmp}",
134     notify_update => false,
135   }
136 }