Merge pull request #711 from icann-dns/fix_legacy_functions
[puppet-modules/puppetlabs-apt.git] / manifests / source.pp
1 # source.pp
2 # add an apt source
3 define apt::source(
4   Optional[String] $location                    = undef,
5   String $comment                               = $name,
6   String $ensure                                = present,
7   Optional[String] $release                     = undef,
8   String $repos                                 = 'main',
9   Optional[Variant[Hash]] $include              = {},
10   Optional[Variant[String, Hash]] $key          = undef,
11   Optional[Variant[Hash, Numeric, String]] $pin = undef,
12   Optional[String] $architecture                = undef,
13   Boolean $allow_unsigned                       = false,
14   Boolean $notify_update                        = true,
15 ) {
16
17   # This is needed for compat with 1.8.x
18   include ::apt
19
20   $_before = Apt::Setting["list-${title}"]
21
22   if !$release {
23     if $facts['lsbdistcodename'] {
24       $_release = $facts['lsbdistcodename']
25     } else {
26       fail('lsbdistcodename fact not available: release parameter required')
27     }
28   } else {
29     $_release = $release
30   }
31
32   if $ensure == 'present' and ! $location {
33     fail('cannot create a source entry without specifying a location')
34   }
35
36   $includes = merge($::apt::include_defaults, $include)
37
38   if $key {
39     if $key =~ Hash {
40       unless $key['id'] {
41         fail('key hash must contain at least an id entry')
42       }
43       $_key = merge($::apt::source_key_defaults, $key)
44     } else {
45       $_key = { 'id' => assert_type(String[1], $key) }
46     }
47   }
48
49   $header = epp('apt/_header.epp')
50
51   $sourcelist = epp('apt/source.list.epp', {
52     'comment'          => $comment,
53     'includes'         => $includes,
54     'opt_architecture' => $architecture,
55     'allow_unsigned'   => $allow_unsigned,
56     'location'         => $location,
57     'release'          => $_release,
58     'repos'            => $repos,
59   })
60
61   apt::setting { "list-${name}":
62     ensure        => $ensure,
63     content       => "${header}${sourcelist}",
64     notify_update => $notify_update,
65   }
66
67   if $pin {
68     if $pin =~ Hash {
69       $_pin = merge($pin, { 'ensure' => $ensure, 'before' => $_before })
70     } elsif ($pin =~ Numeric or $pin =~ String) {
71       $url_split = split($location, '[:\/]+')
72       $host      = $url_split[1]
73       $_pin = {
74         'ensure'   => $ensure,
75         'priority' => $pin,
76         'before'   => $_before,
77         'origin'   => $host,
78       }
79     } else {
80       fail('Received invalid value for pin parameter')
81     }
82     create_resources('apt::pin', { "${name}" => $_pin })
83   }
84
85   # We do not want to remove keys when the source is absent.
86   if $key and ($ensure == 'present') {
87     if $_key =~ Hash {
88       apt::key { "Add key: ${$_key['id']} from Apt::Source ${title}":
89         ensure  => present,
90         id      => $_key['id'],
91         server  => $_key['server'],
92         content => $_key['content'],
93         source  => $_key['source'],
94         options => $_key['options'],
95         before  => $_before,
96       }
97     }
98   }
99 }