source: Support complex pin, like key does.
[puppet-modules/puppetlabs-apt.git] / manifests / source.pp
1 # source.pp
2 # add an apt source
3 define apt::source(
4   $location       = undef,
5   $comment        = $name,
6   $ensure         = present,
7   $release        = $::apt::xfacts['lsbdistcodename'],
8   $repos          = 'main',
9   $include        = {},
10   $key            = undef,
11   $pin            = undef,
12   $architecture   = undef,
13   $allow_unsigned = false,
14 ) {
15   validate_string($architecture, $comment, $location, $repos)
16   validate_bool($allow_unsigned)
17   validate_hash($include)
18
19   unless $release {
20     fail('lsbdistcodename fact not available: release parameter required')
21   }
22
23   if $ensure == 'present' and ! $location {
24     fail('cannot create a source entry without specifying a location')
25   }
26
27   $_before = Apt::Setting["list-${title}"]
28   $_include = merge($::apt::include_defaults, $include)
29
30   if $key {
31     if is_hash($key) {
32       unless $key['id'] {
33         fail('key hash must contain at least an id entry')
34       }
35       $_key = merge($::apt::source_key_defaults, $key)
36     } else {
37       validate_string($key)
38       $_key = $key
39     }
40   }
41
42   apt::setting { "list-${name}":
43     ensure  => $ensure,
44     content => template('apt/_header.erb', 'apt/source.list.erb'),
45   }
46
47   if $pin {
48     if is_hash($pin) {
49       $_pin = merge($pin, { 'ensure' => $ensure, 'before' => $_before })
50     } elsif (is_numeric($pin) or is_string($pin)) {
51       $url_split = split($location, '/')
52       $host      = $url_split[2]
53       $_pin = {
54         'ensure'   => $ensure,
55         'priority' => $pin,
56         'before'   => $_before,
57         'origin'   => $host,
58       }
59     } else {
60       fail('Received invalid value for pin parameter')
61     }
62     create_resources('apt::pin', { $name => $_pin })
63   }
64
65   # We do not want to remove keys when the source is absent.
66   if $key and ($ensure == 'present') {
67     if is_hash($_key) {
68       apt::key { "Add key: ${_key['id']} from Apt::Source ${title}":
69         ensure  => present,
70         id      => $_key['id'],
71         server  => $_key['server'],
72         content => $_key['content'],
73         source  => $_key['source'],
74         options => $_key['options'],
75         before  => $_before,
76       }
77     } else {
78       apt::key { "Add key: ${_key} from Apt::Source ${title}":
79         ensure => present,
80         id     => $_key,
81         before => $_before,
82       }
83     }
84   }
85 }