Merge pull request #336 from mhaskel/fix_acceptance_tests
[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   $fancy_progress       = undef
41 ) {
42
43   if $::osfamily != 'Debian' {
44     fail('This module only works on Debian or derivatives like Ubuntu')
45   }
46
47   include apt::params
48   include apt::update
49
50   validate_bool($purge_sources_list, $purge_sources_list_d,
51                 $purge_preferences, $purge_preferences_d)
52
53   $sources_list_content = $purge_sources_list ? {
54     false => undef,
55     true  => "# Repos managed by puppet.\n",
56   }
57
58   if $always_apt_update == true {
59     Exec <| title=='apt_update' |> {
60       refreshonly => false,
61     }
62   }
63
64   $root           = $apt::params::root
65   $apt_conf_d     = $apt::params::apt_conf_d
66   $sources_list_d = $apt::params::sources_list_d
67   $preferences_d  = $apt::params::preferences_d
68   $provider       = $apt::params::provider
69
70   file { 'sources.list':
71     ensure  => present,
72     path    => "${root}/sources.list",
73     owner   => root,
74     group   => root,
75     mode    => '0644',
76     content => $sources_list_content,
77     notify  => Exec['apt_update'],
78   }
79
80   file { 'sources.list.d':
81     ensure  => directory,
82     path    => $sources_list_d,
83     owner   => root,
84     group   => root,
85     purge   => $purge_sources_list_d,
86     recurse => $purge_sources_list_d,
87     notify  => Exec['apt_update'],
88   }
89
90   if $purge_preferences {
91     file { 'apt-preferences':
92       ensure  => absent,
93       path    => "${root}/preferences",
94     }
95   }
96
97   file { 'preferences.d':
98     ensure  => directory,
99     path    => $preferences_d,
100     owner   => root,
101     group   => root,
102     purge   => $purge_preferences_d,
103     recurse => $purge_preferences_d,
104   }
105
106   case $fancy_progress {
107     true: {
108       file { '99progressbar':
109         ensure  => present,
110         content => 'Dpkg::Progress-Fancy "1";',
111         path    => "${apt_conf_d}/99progressbar",
112       }
113     }
114     false: {
115       file { '99progressbar':
116         ensure  => absent,
117         path    => "${apt_conf_d}/99progressbar",
118       }
119     }
120     undef: {} # do nothing
121     default: { fail('Valid values for fancy_progress are true or false') }
122   }
123
124   case $disable_keys {
125     true: {
126       file { '99unauth':
127         ensure  => present,
128         content => "APT::Get::AllowUnauthenticated 1;\n",
129         path    => "${apt_conf_d}/99unauth",
130       }
131     }
132     false: {
133       file { '99unauth':
134         ensure => absent,
135         path   => "${apt_conf_d}/99unauth",
136       }
137     }
138     undef:   { } # do nothing
139     default: { fail('Valid values for disable_keys are true or false') }
140   }
141
142   case $proxy_host {
143     false, '', undef: {
144       file { '01proxy':
145         ensure  => absent,
146         path    => "${apt_conf_d}/01proxy",
147         notify  => Exec['apt_update'],
148       }
149     }
150     default: {
151       file { '01proxy':
152         ensure  => present,
153         path    => "${apt_conf_d}/01proxy",
154         content => "Acquire::http::Proxy \"http://${proxy_host}:${proxy_port}\";\n",
155         notify  => Exec['apt_update'],
156         mode    => '0644',
157         owner   => root,
158         group   => root,
159       }
160     }
161   }
162
163   file { 'old-proxy-file':
164     ensure  => absent,
165     path    => "${apt_conf_d}/proxy",
166     notify  => Exec['apt_update'],
167   }
168
169   # Need anchor to provide containment for dependencies.
170   anchor { 'apt::update':
171     require => Class['apt::update'],
172   }
173
174   # manage sources if present
175   if $sources != undef {
176     validate_hash($sources)
177     create_resources('apt::source', $sources)
178   }
179 }