Introduce an Apt::Proxy type to validate the hash
[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   include ::apt
18
19   $_before = Apt::Setting["list-${title}"]
20
21   if !$release {
22     if $facts['lsbdistcodename'] {
23       $_release = $facts['lsbdistcodename']
24     } else {
25       fail('lsbdistcodename fact not available: release parameter required')
26     }
27   } else {
28     $_release = $release
29   }
30
31   if $ensure == 'present' {
32     if ! $location {
33       fail('cannot create a source entry without specifying a location')
34     }
35   }
36
37   $includes = merge($::apt::include_defaults, $include)
38
39   if $key {
40     if $key =~ Hash {
41       unless $key['id'] {
42         fail('key hash must contain at least an id entry')
43       }
44       $_key = merge($::apt::source_key_defaults, $key)
45     } else {
46       $_key = { 'id' => assert_type(String[1], $key) }
47     }
48   }
49
50   $header = epp('apt/_header.epp')
51
52   $sourcelist = epp('apt/source.list.epp', {
53     'comment'          => $comment,
54     'includes'         => $includes,
55     'opt_architecture' => $architecture,
56     'allow_unsigned'   => $allow_unsigned,
57     'location'         => $location,
58     'release'          => $_release,
59     'repos'            => $repos,
60   })
61
62   apt::setting { "list-${name}":
63     ensure        => $ensure,
64     content       => "${header}${sourcelist}",
65     notify_update => $notify_update,
66   }
67
68   if $pin {
69     if $pin =~ Hash {
70       $_pin = merge($pin, { 'ensure' => $ensure, 'before' => $_before })
71     } elsif ($pin =~ Numeric or $pin =~ String) {
72       $url_split = split($location, '[:\/]+')
73       $host      = $url_split[1]
74       $_pin = {
75         'ensure'   => $ensure,
76         'priority' => $pin,
77         'before'   => $_before,
78         'origin'   => $host,
79       }
80     } else {
81       fail('Received invalid value for pin parameter')
82     }
83     create_resources('apt::pin', { "${name}" => $_pin })
84   }
85
86   # We do not want to remove keys when the source is absent.
87   if $key and ($ensure == 'present') {
88     if $_key =~ Hash {
89       apt::key { "Add key: ${$_key['id']} from Apt::Source ${title}":
90         ensure  => present,
91         id      => $_key['id'],
92         server  => $_key['server'],
93         content => $_key['content'],
94         source  => $_key['source'],
95         options => $_key['options'],
96         before  => $_before,
97       }
98     }
99   }
100 }