Merge pull request #452 from puppetlabs/daenney/rename-trusted-source
[puppet-modules/puppetlabs-apt.git] / manifests / source.pp
1 # source.pp
2 # add an apt source
3 define apt::source(
4   $comment        = $name,
5   $ensure         = present,
6   $location       = '',
7   $release        = $::apt::xfacts['lsbdistcodename'],
8   $repos          = 'main',
9   $include        = {},
10   $key            = undef,
11   $pin            = false,
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   $_before = Apt::Setting["list-${title}"]
24   $_include = merge($::apt::include_defaults, $include)
25
26   if $key {
27     if is_hash($key) {
28       unless $key['id'] {
29         fail('key hash must contain at least an id entry')
30       }
31       $_key = merge($::apt::source_key_defaults, $key)
32     } else {
33       validate_string($key)
34       $_key = $key
35     }
36   }
37
38   apt::setting { "list-${name}":
39     ensure  => $ensure,
40     content => template('apt/_header.erb', 'apt/source.list.erb'),
41   }
42
43   if ($pin != false) {
44     # Get the host portion out of the url so we can pin to origin
45     $url_split = split($location, '/')
46     $host      = $url_split[2]
47
48     apt::pin { $name:
49       ensure   => $ensure,
50       priority => $pin,
51       before   => $_before,
52       origin   => $host,
53     }
54   }
55
56   # We do not want to remove keys when the source is absent.
57   if $key and ($ensure == 'present') {
58     if is_hash($_key) {
59       apt::key { "Add key: ${_key['id']} from Apt::Source ${title}":
60         ensure  => present,
61         id      => $_key['id'],
62         server  => $_key['server'],
63         content => $_key['content'],
64         source  => $_key['source'],
65         options => $_key['options'],
66         before  => $_before,
67       }
68     } else {
69       apt::key { "Add key: ${_key} from Apt::Source ${title}":
70         ensure => present,
71         id     => $_key,
72         before => $_before,
73       }
74     }
75   }
76 }