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