Remove the preference file for all supported platforms when in purge mode
[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 #   disable_keys - disables the requirement for all packages to be signed
9 #   always_apt_update - rather apt should be updated on every run (intended
10 #     for development environments where package updates are frequent)
11 #   purge_sources_list - Accepts true or false. Defaults to false If set to
12 #     true, Puppet will purge all unmanaged entries from sources.list
13 #   purge_sources_list_d - Accepts true or false. Defaults to false. If set
14 #     to true, Puppet will purge all unmanaged entries from sources.list.d
15 #   update_timeout - Overrides the exec timeout in seconds for apt-get update.
16 #     If not set defaults to Exec's default (300)
17 #   update_tries - Number of times that `apt-get update` will be tried. Use this
18 #     to work around transient DNS and HTTP errors. By default, the command
19 #     will only be run once.
20 #
21 # Actions:
22 #
23 # Requires:
24 #   puppetlabs/stdlib
25 # Sample Usage:
26 #  class { 'apt': }
27
28 class apt(
29   $always_apt_update    = false,
30   $disable_keys         = undef,
31   $proxy_host           = undef,
32   $proxy_port           = '8080',
33   $purge_sources_list   = false,
34   $purge_sources_list_d = false,
35   $purge_preferences    = false,
36   $purge_preferences_d  = false,
37   $update_timeout       = undef,
38   $update_tries         = undef,
39   $sources              = undef
40 ) {
41
42   if $::osfamily != 'Debian' {
43     fail('This module only works on Debian or derivatives like Ubuntu')
44   }
45
46   include apt::params
47   include apt::update
48
49   validate_bool($purge_sources_list, $purge_sources_list_d,
50                 $purge_preferences, $purge_preferences_d)
51
52   $sources_list_content = $purge_sources_list ? {
53     false => undef,
54     true  => "# Repos managed by puppet.\n",
55   }
56
57   if $always_apt_update == true {
58     Exec <| title=='apt_update' |> {
59       refreshonly => false,
60     }
61   }
62
63   $root           = $apt::params::root
64   $apt_conf_d     = $apt::params::apt_conf_d
65   $sources_list_d = $apt::params::sources_list_d
66   $preferences_d  = $apt::params::preferences_d
67   $provider       = $apt::params::provider
68
69   file { 'sources.list':
70     ensure  => present,
71     path    => "${root}/sources.list",
72     owner   => root,
73     group   => root,
74     mode    => '0644',
75     content => $sources_list_content,
76     notify  => Exec['apt_update'],
77   }
78
79   file { 'sources.list.d':
80     ensure  => directory,
81     path    => $sources_list_d,
82     owner   => root,
83     group   => root,
84     purge   => $purge_sources_list_d,
85     recurse => $purge_sources_list_d,
86     notify  => Exec['apt_update'],
87   }
88
89   if $purge_preferences == true {
90     file { 'apt-preferences':
91       ensure  => absent,
92       path    => "${root}/preferences",
93     }
94   }
95
96   file { 'preferences.d':
97     ensure  => directory,
98     path    => $preferences_d,
99     owner   => root,
100     group   => root,
101     purge   => $purge_preferences_d,
102     recurse => $purge_preferences_d,
103   }
104
105   case $disable_keys {
106     true: {
107       file { '99unauth':
108         ensure  => present,
109         content => "APT::Get::AllowUnauthenticated 1;\n",
110         path    => "${apt_conf_d}/99unauth",
111       }
112     }
113     false: {
114       file { '99unauth':
115         ensure => absent,
116         path   => "${apt_conf_d}/99unauth",
117       }
118     }
119     undef:   { } # do nothing
120     default: { fail('Valid values for disable_keys are true or false') }
121   }
122
123   $proxy_set = $proxy_host ? {
124     undef   => absent,
125     default => present
126   }
127
128   file { '01proxy':
129     ensure  => $proxy_set,
130     path    => "${apt_conf_d}/01proxy",
131     content => "Acquire::http::Proxy \"http://${proxy_host}:${proxy_port}\";\n",
132     notify  => Exec['apt_update'],
133     mode    => '0644',
134     owner   => root,
135     group   => root,
136   }
137   
138   file { 'old-proxy-file':
139     ensure  => absent,
140     path    => "${apt_conf_d}/proxy",
141     notify  => Exec['apt_update'],
142   }
143
144   # Need anchor to provide containment for dependencies.
145   anchor { 'apt::update':
146     require => Class['apt::update'],
147   }
148
149   # manage sources if present
150   if $sources != undef {
151     validate_hash($sources)
152     create_resources('apt::source', $sources)
153   }
154 }