8aa8c6bb660367a61b3486eb2ada0f06901bb64f
[puppet-modules/puppetlabs-apt.git] / manifests / init.pp
1 # == Class: apt
2 #
3 # Manage APT (Advanced Packaging Tool)
4 #
5 class apt (
6   Hash $update_defaults         = $apt::params::update_defaults,
7   Hash $purge_defaults          = $apt::params::purge_defaults,
8   Hash $proxy_defaults          = $apt::params::proxy_defaults,
9   Hash $include_defaults        = $apt::params::include_defaults,
10   String $provider              = $apt::params::provider,
11   String $keyserver             = $apt::params::keyserver,
12   Optional[String] $ppa_options = $apt::params::ppa_options,
13   Optional[String] $ppa_package = $apt::params::ppa_package,
14   Optional[Hash] $backports     = $apt::params::backports,
15   Hash $confs                   = $apt::params::confs,
16   Hash $update                  = $apt::params::update,
17   Hash $purge                   = $apt::params::purge,
18   Apt::Proxy $proxy             = $apt::params::proxy,
19   Hash $sources                 = $apt::params::sources,
20   Hash $keys                    = $apt::params::keys,
21   Hash $ppas                    = $apt::params::ppas,
22   Hash $pins                    = $apt::params::pins,
23   Hash $settings                = $apt::params::settings,
24   Array[Apt::Auth_conf_entry]
25     $auth_conf_entries          = $apt::params::auth_conf_entries,
26   String $root                  = $apt::params::root,
27   String $sources_list          = $apt::params::sources_list,
28   String $sources_list_d        = $apt::params::sources_list_d,
29   String $conf_d                = $apt::params::conf_d,
30   String $preferences           = $apt::params::preferences,
31   String $preferences_d         = $apt::params::preferences_d,
32   Hash $config_files            = $apt::params::config_files,
33   Hash $source_key_defaults     = $apt::params::source_key_defaults
34 ) inherits apt::params {
35
36   if $facts['osfamily'] != 'Debian' {
37     fail('This module only works on Debian or derivatives like Ubuntu')
38   }
39
40   if $update['frequency'] {
41     assert_type(
42       Enum['always','daily','weekly','reluctantly'],
43       $update['frequency'],
44     )
45   }
46   if $update['timeout'] {
47     assert_type(Integer, $update['timeout'])
48   }
49   if $update['tries'] {
50     assert_type(Integer, $update['tries'])
51   }
52
53   $_update = merge($::apt::update_defaults, $update)
54   include ::apt::update
55
56   if $purge['sources.list'] {
57     assert_type(Boolean, $purge['sources.list'])
58   }
59   if $purge['sources.list.d'] {
60     assert_type(Boolean, $purge['sources.list.d'])
61   }
62   if $purge['preferences'] {
63     assert_type(Boolean, $purge['preferences'])
64   }
65   if $purge['preferences.d'] {
66     assert_type(Boolean, $purge['preferences.d'])
67   }
68
69   $_purge = merge($::apt::purge_defaults, $purge)
70   $_proxy = merge($apt::proxy_defaults, $proxy)
71
72   $confheadertmp = epp('apt/_conf_header.epp')
73   $proxytmp = epp('apt/proxy.epp', {'proxies' => $_proxy})
74   $updatestamptmp = epp('apt/15update-stamp.epp')
75
76   if $_proxy['ensure'] == 'absent' or $_proxy['host'] {
77     apt::setting { 'conf-proxy':
78       ensure   => $_proxy['ensure'],
79       priority => '01',
80       content  => "${confheadertmp}${proxytmp}",
81     }
82   }
83
84   $sources_list_content = $_purge['sources.list'] ? {
85     true    => "# Repos managed by puppet.\n",
86     default => undef,
87   }
88
89   $preferences_ensure = $_purge['preferences'] ? {
90     true    => absent,
91     default => file,
92   }
93
94   if $_update['frequency'] == 'always' {
95     Exec <| title=='apt_update' |> {
96       refreshonly => false,
97     }
98   }
99
100   apt::setting { 'conf-update-stamp':
101     priority => 15,
102     content  => "${confheadertmp}${updatestamptmp}",
103   }
104
105   file { 'sources.list':
106     ensure  => file,
107     path    => $::apt::sources_list,
108     owner   => root,
109     group   => root,
110     mode    => '0644',
111     content => $sources_list_content,
112     notify  => Class['apt::update'],
113   }
114
115   file { 'sources.list.d':
116     ensure  => directory,
117     path    => $::apt::sources_list_d,
118     owner   => root,
119     group   => root,
120     mode    => '0644',
121     purge   => $_purge['sources.list.d'],
122     recurse => $_purge['sources.list.d'],
123     notify  => Class['apt::update'],
124   }
125
126   file { 'preferences':
127     ensure => $preferences_ensure,
128     path   => $::apt::preferences,
129     owner  => root,
130     group  => root,
131     mode   => '0644',
132     notify => Class['apt::update'],
133   }
134
135   file { 'preferences.d':
136     ensure  => directory,
137     path    => $::apt::preferences_d,
138     owner   => root,
139     group   => root,
140     mode    => '0644',
141     purge   => $_purge['preferences.d'],
142     recurse => $_purge['preferences.d'],
143     notify  => Class['apt::update'],
144   }
145
146   if $confs {
147     create_resources('apt::conf', $confs)
148   }
149   # manage sources if present
150   if $sources {
151     create_resources('apt::source', $sources)
152   }
153   # manage keys if present
154   if $keys {
155     create_resources('apt::key', $keys)
156   }
157   # manage ppas if present
158   if $ppas {
159     create_resources('apt::ppa', $ppas)
160   }
161   # manage settings if present
162   if $settings {
163     create_resources('apt::setting', $settings)
164   }
165
166   $auth_conf_ensure = $auth_conf_entries ? {
167     []      => 'absent',
168     default => 'present',
169   }
170
171   $auth_conf_tmp = epp('apt/auth_conf.epp')
172
173   file { '/etc/apt/auth.conf':
174     ensure  => $auth_conf_ensure,
175     owner   => 'root',
176     group   => 'root',
177     mode    => '0600',
178     content => "${confheadertmp}${auth_conf_tmp}",
179     notify  => Class['apt::update'],
180   }
181
182   # manage pins if present
183   if $pins {
184     create_resources('apt::pin', $pins)
185   }
186
187   # required for adding GPG keys on Debian 9 (and derivatives)
188   ensure_packages(['dirmngr'])
189 }