Format documentation a little
[puppet-modules/puppetlabs-apt.git] / manifests / init.pp
1 # == Class: apt
2 #
3 # This module manages the initial configuration of apt.
4 #
5 # The parameters listed here are not required in general and were
6 # added for use cases related to development environments.
7 #
8 # === Parameters
9 #
10 # [*disable_keys*]
11 #   Disables the requirement for all packages to be signed
12 #
13 # [*always_apt_update*]
14 #   Rather apt should be updated on every run (intended
15 #   for development environments where package updates are frequent)
16 #
17 # [*apt_update_frequency*]
18 #   String: Supported values:
19 #   **always**: Will fire `apt-get update` at every puppet run. Intended to
20 #       deprecate the `always_apt_update` parameter.
21 #   *daily**: Trigger `apt-get update` if the value of the fact
22 #       `apt_update_last_success` is less than current epoch time - 86400.
23 #        *notifying the apt_update exec will trigger apt-get update regardless*
24 #   *weekly**: Trigger `apt-get update` if the value of the fact
25 #       `apt_update_last_success` is less than current epoch time - 604800.
26 #        *notifying the apt_update exec will trigger apt-get update regardless*
27 #   *reluctantly**: *Default* only run apt-get update if the exec resource `apt_update` is notified.
28 #
29 # [*purge_sources_list*]
30 #   Accepts true or false. Defaults to false If set to
31 #   true, Puppet will purge all unmanaged entries from sources.list
32 #
33 # [*purge_sources_list_d*]
34 #   Accepts true or false. Defaults to false. If set
35 #   to true, Puppet will purge all unmanaged entries from sources.list.d
36 #
37 # [*update_timeout*]
38 #   Overrides the exec timeout in seconds for apt-get update.
39 #   If not set defaults to Exec's default (300)
40 #
41 # [*update_tries*]
42 #   Number of times that `apt-get update` will be tried. Use this
43 #   to work around transient DNS and HTTP errors. By default, the command
44 #   will only be run once.
45 #
46 # === Examples
47 #
48 # class { 'apt': }
49 #
50 # === Requires
51 #
52 # puppetlabs/stdlib >= 2.2.1
53 #
54 class apt(
55   $always_apt_update    = false,
56   $apt_update_frequency = 'reluctantly',
57   $disable_keys         = undef,
58   $proxy_host           = undef,
59   $proxy_port           = '8080',
60   $purge_sources_list   = false,
61   $purge_sources_list_d = false,
62   $purge_preferences    = false,
63   $purge_preferences_d  = false,
64   $update_timeout       = undef,
65   $update_tries         = undef,
66   $sources              = undef,
67   $fancy_progress       = undef
68 ) {
69
70   if $::osfamily != 'Debian' {
71     fail('This module only works on Debian or derivatives like Ubuntu')
72   }
73
74   $frequency_options = ['always','daily','weekly','reluctantly']
75   validate_re($apt_update_frequency, $frequency_options)
76   include apt::params
77   include apt::update
78
79   validate_bool($purge_sources_list, $purge_sources_list_d,
80                 $purge_preferences, $purge_preferences_d)
81
82   $sources_list_content = $purge_sources_list ? {
83     false => undef,
84     true  => "# Repos managed by puppet.\n",
85   }
86
87   if $always_apt_update == true {
88     Exec <| title=='apt_update' |> {
89       refreshonly => false,
90     }
91   }
92
93   file { '/etc/apt/apt.conf.d/15update-stamp':
94     ensure  => 'file',
95     content => 'APT::Update::Post-Invoke-Success {"touch /var/lib/apt/periodic/update-success-stamp 2>/dev/null || true";};',
96     group   => 'root',
97     mode    => '0644',
98     owner   => 'root',
99   }
100
101   $root           = $apt::params::root
102   $apt_conf_d     = $apt::params::apt_conf_d
103   $sources_list_d = $apt::params::sources_list_d
104   $preferences_d  = $apt::params::preferences_d
105   $provider       = $apt::params::provider
106
107   file { 'sources.list':
108     ensure  => present,
109     path    => "${root}/sources.list",
110     owner   => root,
111     group   => root,
112     mode    => '0644',
113     content => $sources_list_content,
114     notify  => Exec['apt_update'],
115   }
116
117   file { 'sources.list.d':
118     ensure  => directory,
119     path    => $sources_list_d,
120     owner   => root,
121     group   => root,
122     purge   => $purge_sources_list_d,
123     recurse => $purge_sources_list_d,
124     notify  => Exec['apt_update'],
125   }
126
127   if $purge_preferences {
128     file { 'apt-preferences':
129       ensure => absent,
130       path   => "${root}/preferences",
131     }
132   }
133
134   file { 'preferences.d':
135     ensure  => directory,
136     path    => $preferences_d,
137     owner   => root,
138     group   => root,
139     purge   => $purge_preferences_d,
140     recurse => $purge_preferences_d,
141   }
142
143   case $fancy_progress {
144     true: {
145       file { '99progressbar':
146         ensure  => present,
147         content => 'Dpkg::Progress-Fancy "1";',
148         path    => "${apt_conf_d}/99progressbar",
149       }
150     }
151     false: {
152       file { '99progressbar':
153         ensure => absent,
154         path   => "${apt_conf_d}/99progressbar",
155       }
156     }
157     undef: {} # do nothing
158     default: { fail('Valid values for fancy_progress are true or false') }
159   }
160
161   case $disable_keys {
162     true: {
163       file { '99unauth':
164         ensure  => present,
165         content => "APT::Get::AllowUnauthenticated 1;\n",
166         path    => "${apt_conf_d}/99unauth",
167       }
168     }
169     false: {
170       file { '99unauth':
171         ensure => absent,
172         path   => "${apt_conf_d}/99unauth",
173       }
174     }
175     undef:   { } # do nothing
176     default: { fail('Valid values for disable_keys are true or false') }
177   }
178
179   case $proxy_host {
180     false, '', undef: {
181       file { '01proxy':
182         ensure => absent,
183         path   => "${apt_conf_d}/01proxy",
184         notify => Exec['apt_update'],
185       }
186     }
187     default: {
188       file { '01proxy':
189         ensure  => present,
190         path    => "${apt_conf_d}/01proxy",
191         content => "Acquire::http::Proxy \"http://${proxy_host}:${proxy_port}\";\n",
192         notify  => Exec['apt_update'],
193         mode    => '0644',
194         owner   => root,
195         group   => root,
196       }
197     }
198   }
199
200   file { 'old-proxy-file':
201     ensure => absent,
202     path   => "${apt_conf_d}/proxy",
203     notify => Exec['apt_update'],
204   }
205
206   # Need anchor to provide containment for dependencies.
207   anchor { 'apt::update':
208     require => Class['apt::update'],
209   }
210
211   # manage sources if present
212   if $sources != undef {
213     validate_hash($sources)
214     create_resources('apt::source', $sources)
215   }
216 }