Do not add bogus line to apt preference file on Debian Wheezy
[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 $lsbdistcodename == 'wheezy' {
58     $preferences_content = undef
59   } 
60   else {
61     $preferences_content = $purge_preferences ? {
62       false => undef,
63       true => "Explanation: Preferences managed by Puppet\n
64 Explanation: We need a bogus package line because of Debian Bug #732746\n
65 Package: bogus-package\n",
66     }
67   }
68
69   if $always_apt_update == true {
70     Exec <| title=='apt_update' |> {
71       refreshonly => false,
72     }
73   }
74
75   $root           = $apt::params::root
76   $apt_conf_d     = $apt::params::apt_conf_d
77   $sources_list_d = $apt::params::sources_list_d
78   $preferences_d  = $apt::params::preferences_d
79   $provider       = $apt::params::provider
80
81   file { 'sources.list':
82     ensure  => present,
83     path    => "${root}/sources.list",
84     owner   => root,
85     group   => root,
86     mode    => '0644',
87     content => $sources_list_content,
88     notify  => Exec['apt_update'],
89   }
90
91   file { 'sources.list.d':
92     ensure  => directory,
93     path    => $sources_list_d,
94     owner   => root,
95     group   => root,
96     purge   => $purge_sources_list_d,
97     recurse => $purge_sources_list_d,
98     notify  => Exec['apt_update'],
99   }
100
101   file { 'apt-preferences':
102     ensure  => present,
103     path    => "${root}/preferences",
104     owner   => root,
105     group   => root,
106     mode    => '0644',
107     content => $preferences_content,
108   }
109
110   file { 'preferences.d':
111     ensure  => directory,
112     path    => $preferences_d,
113     owner   => root,
114     group   => root,
115     purge   => $purge_preferences_d,
116     recurse => $purge_preferences_d,
117   }
118
119   case $disable_keys {
120     true: {
121       file { '99unauth':
122         ensure  => present,
123         content => "APT::Get::AllowUnauthenticated 1;\n",
124         path    => "${apt_conf_d}/99unauth",
125       }
126     }
127     false: {
128       file { '99unauth':
129         ensure => absent,
130         path   => "${apt_conf_d}/99unauth",
131       }
132     }
133     undef:   { } # do nothing
134     default: { fail('Valid values for disable_keys are true or false') }
135   }
136
137   $proxy_set = $proxy_host ? {
138     undef   => absent,
139     default => present
140   }
141
142   file { '01proxy':
143     ensure  => $proxy_set,
144     path    => "${apt_conf_d}/01proxy",
145     content => "Acquire::http::Proxy \"http://${proxy_host}:${proxy_port}\";\n",
146     notify  => Exec['apt_update'],
147     mode    => '0644',
148     owner   => root,
149     group   => root,
150   }
151   
152   file { 'old-proxy-file':
153     ensure  => absent,
154     path    => "${apt_conf_d}/proxy",
155     notify  => Exec['apt_update'],
156   }
157
158   # Need anchor to provide containment for dependencies.
159   anchor { 'apt::update':
160     require => Class['apt::update'],
161   }
162
163   # manage sources if present
164   if $sources != undef {
165     validate_hash($sources)
166     create_resources('apt::source', $sources)
167   }
168 }