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