Merge pull request #484 from mhaskel/merge_master_to_next
[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            = 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   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 != false) {
48     # Get the host portion out of the url so we can pin to origin
49     $url_split = split($location, '/')
50     $host      = $url_split[2]
51
52     apt::pin { $name:
53       ensure   => $ensure,
54       priority => $pin,
55       before   => $_before,
56       origin   => $host,
57     }
58   }
59
60   # We do not want to remove keys when the source is absent.
61   if $key and ($ensure == 'present') {
62     if is_hash($_key) {
63       apt::key { "Add key: ${_key['id']} from Apt::Source ${title}":
64         ensure  => present,
65         id      => $_key['id'],
66         server  => $_key['server'],
67         content => $_key['content'],
68         source  => $_key['source'],
69         options => $_key['options'],
70         before  => $_before,
71       }
72     } else {
73       apt::key { "Add key: ${_key} from Apt::Source ${title}":
74         ensure => present,
75         id     => $_key,
76         before => $_before,
77       }
78     }
79   }
80 }