Merge pull request #667 from HelenCampbell/firstRelease
[puppet-modules/puppetlabs-apt.git] / manifests / source.pp
1 # source.pp
2 # add an apt source
3 define apt::source(
4   Optional[Variant[String, Stdlib::Compat::String]] $location                         = undef,
5   Variant[String, Stdlib::Compat::String] $comment                                    = $name,
6   Variant[String, Stdlib::Compat::String] $ensure                                     = present,
7   Optional[Variant[String, Stdlib::Compat::String]] $release                          = undef,
8   Variant[String, Stdlib::Compat::String] $repos                                      = 'main',
9   Optional[Variant[Hash, Stdlib::Compat::Hash]] $include                              = {},
10   Optional[Variant[String, Stdlib::Compat::String, Hash, Stdlib::Compat::Hash]] $key  = undef,
11   $pin                                                                                = undef,
12   Optional[Variant[String, Stdlib::Compat::String]] $architecture                     = undef,
13   Boolean $allow_unsigned                                                             = false,
14   Boolean $notify_update                                                              = true,
15   Optional[Variant[String, Stdlib::Compat::String]] $key_server                       = undef,
16   Optional[Variant[String, Stdlib::Compat::String]] $key_content                      = undef,
17   Optional[Variant[String, Stdlib::Compat::String]] $key_source                       = undef,
18   Optional[Boolean] $include_src                                                      = undef,
19   Optional[Boolean] $include_deb                                                      = undef,
20   $required_packages                                                                  = undef,
21   $trusted_source                                                                     = undef,
22 ) {
23
24   validate_legacy(String, 'validate_string', $architecture, $comment, $location, $repos)
25   validate_legacy(Boolean, 'validate_bool', $allow_unsigned)
26   validate_legacy(Hash, 'validate_hash', $include)
27
28   # This is needed for compat with 1.8.x
29   include ::apt
30
31   $_before = Apt::Setting["list-${title}"]
32
33   if $required_packages != undef {
34     deprecation('apt $required_packages', '$required_packages is deprecated and will be removed in the next major release, please use package resources instead.')
35     exec { "Required packages: '${required_packages}' for ${name}":
36       command     => "${::apt::provider} -y install ${required_packages}",
37       logoutput   => 'on_failure',
38       refreshonly => true,
39       tries       => 3,
40       try_sleep   => 1,
41       before      => $_before,
42     }
43   }
44
45   if $trusted_source != undef {
46     deprecation('apt $trusted_source', '$trusted_source is deprecated and will be removed in the next major release, please use $allow_unsigned instead.')
47     $_allow_unsigned = $trusted_source
48   } else {
49     $_allow_unsigned = $allow_unsigned
50   }
51
52   if ! $release {
53     if $facts['lsbdistcodename'] {
54       $_release = $facts['lsbdistcodename']
55     } else {
56       fail('lsbdistcodename fact not available: release parameter required')
57     }
58   } else {
59     $_release = $release
60   }
61
62   if $ensure == 'present' and ! $location {
63     fail('cannot create a source entry without specifying a location')
64   }
65
66   if $include_src != undef and $include_deb != undef {
67     $_deprecated_include = {
68       'src' => $include_src,
69       'deb' => $include_deb,
70     }
71   } elsif $include_src != undef {
72     $_deprecated_include = { 'src' => $include_src }
73   } elsif $include_deb != undef {
74     $_deprecated_include = { 'deb' => $include_deb }
75   } else {
76     $_deprecated_include = {}
77   }
78
79   $includes = merge($::apt::include_defaults, $_deprecated_include, $include)
80
81   $_deprecated_key = {
82     'key_server'  => $key_server,
83     'key_content' => $key_content,
84     'key_source'  => $key_source,
85   }
86
87   if $key {
88     if is_hash($key) {
89       unless $key['id'] {
90         fail('key hash must contain at least an id entry')
91       }
92       $_key = merge($::apt::source_key_defaults, $_deprecated_key, $key)
93     } else {
94       validate_legacy(String, 'validate_string', $key)
95       $_key = merge( { 'id' => $key }, $_deprecated_key)
96     }
97   }
98
99   $header = epp('apt/_header.epp')
100
101   $sourcelist = epp('apt/source.list.epp', {
102     'comment'        => $comment,
103     'includes'       => $includes,
104     'architecture'   => $architecture,
105     'allow_unsigned' => $_allow_unsigned,
106     'location'       => $location,
107     'release'        => $_release,
108     'repos'          => $repos,
109   })
110
111   apt::setting { "list-${name}":
112     ensure        => $ensure,
113     content       => "${header}${sourcelist}",
114     notify_update => $notify_update,
115   }
116
117   if $pin {
118     if is_hash($pin) {
119       $_pin = merge($pin, { 'ensure' => $ensure, 'before' => $_before })
120     } elsif (is_numeric($pin) or is_string($pin)) {
121       $url_split = split($location, '[:\/]+')
122       $host      = $url_split[1]
123       $_pin = {
124         'ensure'   => $ensure,
125         'priority' => $pin,
126         'before'   => $_before,
127         'origin'   => $host,
128       }
129     } else {
130       fail('Received invalid value for pin parameter')
131     }
132     create_resources('apt::pin', { "${name}" => $_pin })
133   }
134
135   # We do not want to remove keys when the source is absent.
136   if $key and ($ensure == 'present') {
137     if is_hash($_key) {
138       apt::key { "Add key: ${$_key['id']} from Apt::Source ${title}":
139         ensure      => present,
140         id          => $_key['id'],
141         server      => $_key['server'],
142         content     => $_key['content'],
143         source      => $_key['source'],
144         options     => $_key['options'],
145         key_server  => $_key['key_server'],
146         key_content => $_key['key_content'],
147         key_source  => $_key['key_source'],
148         before      => $_before,
149       }
150     }
151   }
152 }