Removal of deprecated functionality
[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   $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 is_hash($key) {
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       validate_legacy(String, 'validate_string', $key)
46       $_key = { 'id' => $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     '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 is_hash($pin) {
70       $_pin = merge($pin, { 'ensure' => $ensure, 'before' => $_before })
71     } elsif (is_numeric($pin) or is_string($pin)) {
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 is_hash($_key) {
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 }