Merge branch 'master' into remove_ppa_source
[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     # Newer oses, do not need the package for HTTPS transport.
36     $_transport_https_releases = [ 'wheezy', 'jessie', 'stretch', 'trusty', 'xenial' ]
37     if $_release in $_transport_https_releases and $location =~ /(?i:^https:\/\/)/ {
38       ensure_packages('apt-transport-https')
39     }
40   }
41
42   $includes = merge($::apt::include_defaults, $include)
43
44   if $key {
45     if $key =~ Hash {
46       unless $key['id'] {
47         fail('key hash must contain at least an id entry')
48       }
49       $_key = merge($::apt::source_key_defaults, $key)
50     } else {
51       $_key = { 'id' => assert_type(String[1], $key) }
52     }
53   }
54
55   $header = epp('apt/_header.epp')
56
57   $sourcelist = epp('apt/source.list.epp', {
58     'comment'          => $comment,
59     'includes'         => $includes,
60     'opt_architecture' => $architecture,
61     'allow_unsigned'   => $allow_unsigned,
62     'location'         => $location,
63     'release'          => $_release,
64     'repos'            => $repos,
65   })
66
67   apt::setting { "list-${name}":
68     ensure        => $ensure,
69     content       => "${header}${sourcelist}",
70     notify_update => $notify_update,
71   }
72
73   if $pin {
74     if $pin =~ Hash {
75       $_pin = merge($pin, { 'ensure' => $ensure, 'before' => $_before })
76     } elsif ($pin =~ Numeric or $pin =~ String) {
77       $url_split = split($location, '[:\/]+')
78       $host      = $url_split[1]
79       $_pin = {
80         'ensure'   => $ensure,
81         'priority' => $pin,
82         'before'   => $_before,
83         'origin'   => $host,
84       }
85     } else {
86       fail('Received invalid value for pin parameter')
87     }
88     create_resources('apt::pin', { "${name}" => $_pin })
89   }
90
91   # We do not want to remove keys when the source is absent.
92   if $key and ($ensure == 'present') {
93     if $_key =~ Hash {
94       apt::key { "Add key: ${$_key['id']} from Apt::Source ${title}":
95         ensure  => present,
96         id      => $_key['id'],
97         server  => $_key['server'],
98         content => $_key['content'],
99         source  => $_key['source'],
100         options => $_key['options'],
101         before  => $_before,
102       }
103     }
104   }
105 }