Merge pull request #41 from tbroyer/install-proxy-first
[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 false, Puppet will purge all unmanaged entries from sources.list.d
15 #
16 # Actions:
17 #
18 # Requires:
19 #
20 # Sample Usage:
21 #  class { 'apt': }
22 class apt(
23   $always_apt_update = false,
24   $disable_keys = undef,
25   $proxy_host = false,
26   $proxy_port = '8080',
27   $purge_sources_list = false,
28   $purge_sources_list_d = false
29 ) {
30
31   include apt::params
32
33   validate_bool($purge_sources_list, $purge_sources_list_d)
34
35   $refresh_only_apt_update = $always_apt_update? {
36     true  => false,
37     false => true,
38   }
39
40   if ! defined(Package['python-software-properties']) {
41     package { 'python-software-properties': }
42   }
43
44   $sources_list_content = $purge_sources_list ? {
45     false =>  undef,
46     true  => "# Repos managed by puppet.\n",
47   }
48
49   $root           = $apt::params::root
50   $apt_conf_d     = $apt::params::apt_conf_d
51   $sources_list_d = $apt::params::sources_list_d
52   $provider       = $apt::params::provider
53
54   file { 'sources.list':
55     ensure  => present,
56     path    => "${root}/sources.list",
57     owner   => root,
58     group   => root,
59     mode    => '0644',
60     content => $sources_list_content,
61   }
62
63   file { 'sources.list.d':
64     ensure  => directory,
65     path    => $sources_list_d,
66     owner   => root,
67     group   => root,
68     purge   => $purge_sources_list_d,
69     recurse => $purge_sources_list_d,
70   }
71
72   exec { 'apt_update':
73     command     => "${provider} update",
74     subscribe   => [ File['sources.list'], File['sources.list.d'] ],
75     refreshonly => $refresh_only_apt_update,
76   }
77
78   case $disable_keys {
79     true: {
80       file { '99unauth':
81         ensure  => present,
82         content => "APT::Get::AllowUnauthenticated 1;\n",
83         path    => "${apt_conf_d}/99unauth",
84       }
85     }
86     false: {
87       file { '99unauth':
88         ensure => absent,
89         path   => "${apt_conf_d}/99unauth",
90       }
91     }
92     undef: { } # do nothing
93     default: { fail('Valid values for disable_keys are true or false') }
94   }
95
96   if ($proxy_host) {
97     file { 'configure-apt-proxy':
98       path    => "${apt_conf_d}/proxy",
99       content => "Acquire::http::Proxy \"http://${proxy_host}:${proxy_port}\";",
100       notify  => Exec['apt_update'],
101     }
102   }
103 }