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