Updated regex after carefull reading of policy
[puppet-modules/puppetlabs-apt.git] / manifests / mark.pp
1 # @summary Manages apt-mark settings
2 #
3 # @param setting
4 #   auto, manual, hold, unhold
5 #   specifies the behavior of apt in case of no more dependencies installed
6 #   https://manpages.debian.org/stable/apt/apt-mark.8.en.html
7 #
8 define apt::mark (
9   Enum['auto','manual','hold','unhold'] $setting,
10 ) {
11   if $title !~ /^[a-z0-9][a-z0-9.+\-]+$/ {
12     fail("Invalid package name: ${title}")
13   }
14
15   if $setting == 'unhold' {
16     $unless_cmd = undef
17   } else {
18     $action = "show${setting}"
19
20     # It would be ideal if we could break out this command in to an array of args, similar
21     # to $onlyif_cmd and $command. However, in this case it wouldn't work as expected due
22     # to the inclusion of a pipe character.
23     # When passed to the exec function, the posix provider will strip everything to the right of the pipe,
24     # causing the command to return a full list of packages for the given action.
25     # The trade off is to use an interpolated string knowing that action is built from an enum value and
26     # title is pre-validated.
27     $unless_cmd = ["/usr/bin/apt-mark ${action} ${title} | grep ${title} -q"]
28   }
29
30   $onlyif_cmd = [['/usr/bin/dpkg', '-l', $title]]
31   $command = ['/usr/bin/apt-mark', $setting, $title]
32
33   exec { "apt-mark ${setting} ${title}":
34     command => $command,
35     onlyif  => $onlyif_cmd,
36     unless  => $unless_cmd,
37   }
38 }