public
Description: Basic module for configuring apache via puppet. Debian/Ubuntu-specific at the moment, but patches are welcome.
Homepage:
Clone URL: git://github.com/wesabe/puppet-apache2.git
100644 245 lines (221 sloc) 7.379 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# apache2 module for puppet
# by Sam Quigley <sq@wesabe.com>
#
# based in part on code by Tim Stoop <tim.stoop@gmail.com> and
# David Schmitt <david@schmitt.edv-bus.at>
 
# this file defines the base apache2 class, and does most of the heavy
# lifting. see the other subclasses for friendlier versions.
 
# note: this code is currently very deb/ubuntu-specific
$apache_sites = "/etc/apache2/sites"
$apache_includes = "/etc/apache2/site-includes"
$apache_mods = "/etc/apache2/mods"
$apache_conf = "/etc/apache2/conf.d"
 
class apache2 {
  $real_apache2_mpm = $apache2_mpm ? { '' => 'worker', default => $apache2_mpm }
  
  case $real_apache2_mpm {
    'event': {
      package { "apache2-mpm-event":
        ensure => installed,
        alias => apache2_mpm_provider
      }
      package { ["apache2-mpm-perchild", "apache2-mpm-prefork",
        "apache2-mpm-worker"]:
        ensure => absent,
      }
    }
    
    'prefork': {
      package { "apache2-mpm-prefork":
        ensure => installed,
        alias => apache2_mpm_provider
      }
      package { ["apache2-mpm-event", "apache2-mpm-perchild",
        "apache2-mpm-worker"]:
        ensure => absent,
      }
    }
 
    'worker': {
      package { "apache2-mpm-worker":
        ensure => installed,
        alias => apache2_mpm_provider
      }
      package { ["apache2-mpm-event", "apache2-mpm-perchild",
        "apache2-mpm-prefork"]:
        ensure => absent,
      }
    }
  }
package { apache2:
ensure => installed,
require => Package[apache2_mpm_provider],
}
 
service { apache2:
ensure => running,
pattern => "/usr/sbin/apache2",
hasrestart => true,
require => Package[apache2]
}
 
  # using checksum => mtime and notify ensures that any changes to this dir
  # will result in an apache reload
file { $apache_conf:
ensure => directory, checksum => mtime,
mode => 644, owner => root, group => root,
require => Package[apache2],
notify => Exec["reload-apache2"];
}
 
  # as above
file { $apache_includes:
ensure => directory, checksum => mtime,
mode => 644, owner => root, group => root,
require => Package[apache2],
notify => Exec["reload-apache2"];
}
 
# this overwrites the default distro config with one that just includes
# $apache_conf and friends
file { "/etc/apache2/apache2.conf":
ensure => present,
mode => 644,
owner => root,
group => root,
source => "puppet://$servername/apache2/apache2.conf",
require => File[$apache_conf],
}
 
  # nuke the package-provided ports.conf
  file {"/etc/apache2/ports.conf": ensure => absent }
 
  # make sure the default site isn't present.
exec { "/usr/sbin/a2dissite default":
onlyif => "/usr/bin/test -L /etc/apache2/sites-enabled/000-default",
notify => Exec["reload-apache2"],
}
 
# Notify this when apache needs a reload. This is only needed when
# sites are added or removed, since a full restart then would be
# a waste of time. When the module-config changes, a force-reload is
# needed.
exec { "reload-apache2":
command => "/etc/init.d/apache2 reload",
refreshonly => true,
before => [ Service["apache2"], Exec["force-reload-apache2"] ]
}
 
exec { "force-reload-apache2":
command => "/etc/init.d/apache2 force-reload",
refreshonly => true,
before => Service["apache2"],
}
 
  # Define an apache2 config snippet. Places all config snippets into
  # /etc/apache2/conf.d, where they will be automatically loaded
define config ( $ensure = 'present', $content = '', $order="500") {
$real_content = $content ? { '' => template("apache2/${name}.conf.erb"),
default => $content,
}
 
file { "${apache_conf}/${order}-${name}.conf":
      ensure => $ensure,
      content => $content,
      mode => 644,
   owner => root,
   group => root,
# given the way File[$apache_conf] is defined, this might lead to
# multiple restarts. not sure.
notify => Exec["reload-apache2"],
    }
}
  
 
  # Define an apache2 site. Place all site configs into
  # /etc/apache2/sites-available and en-/disable them with this type.
  #
  # You can add a custom require (string) if the site depends on packages
  # that aren't part of the default apache2 package. Because of the
  # package dependencies, apache2 will automagically be included.
  define site ( $ensure = 'present', $content = '' ) {
   case $ensure {
   'present' : {
        apache2::install_site { $name:
          content => $content
        }
   }
   'installed' : {
        apache2::install_site { $name:
          content => $content
        }
   }
   'absent' : {
   exec { "/usr/sbin/a2dissite $name":
   onlyif => "/bin/sh -c '[ -L ${apache_sites}-enabled/$name ] \\
   && [ ${apache_sites}-enabled/$name -ef ${apache_sites}-available/$name ]'",
   notify => Exec["reload-apache2"],
   require => Package["apache2"],
   }
   }
   default: { err ( "Unknown ensure value: '$ensure'" ) }
   }
  }
 
  # helper method to actually install a site -- called by site()
  define install_site ($content = '' ) {
# first, make sure the site config exists
    case $content {
      '': {
        file { "${apache_sites}-available/${name}":
          mode => 644,
       owner => root,
       group => root,
     ensure => present,
     alias => "site-$name",
        }
      }
 
      default: {
     file { "${apache_sites}-available/${name}":
          content => $content,
          mode => 644,
       owner => root,
       group => root,
   ensure => present,
   alias => "site-$name",
        }
      }
    }
 
# now, enable it.
exec { "/usr/sbin/a2ensite $name":
unless => "/bin/sh -c '[ -L ${apache_sites}-enabled/$name ] \\
&& [ ${apache_sites}-enabled/$name -ef ${apache_sites}-available/$name ]'",
notify => Exec["reload-apache2"],
require => File["site-$name"],
}
}
 
  # Define a site config fragment
  define site_include ( $ensure = 'present', $content = '' ) {
file { "${apache_includes}/${name}.inc":
      content => $content,
      mode => 644,
   owner => root,
   group => root,
      ensure => $ensure,
      require => File[$apache_includes],
    }
  }
 
  # Define an apache2 module. Debian packages place the module config
  # into /etc/apache2/mods-available.
  #
  # You can add a custom require (string) if the module depends on
  # packages that aren't part of the default apache2 package. Because of
  # the package dependencies, apache2 will automagically be included.
  #
  # REVIEW: 20070901 <sq@wesabe.com> -- when facts can be distributed
  # within modules (see puppet trac ticket #803), the unless/onlyif clauses
  # below should get rewritten to use custom facter facts
  define module ( $ensure = 'present') {
   case $ensure {
   'present' : {
   exec { "/usr/sbin/a2enmod $name":
   unless => "/bin/sh -c '[ -L ${apache_mods}-enabled/${name}.load ] \\
   && [ ${apache_mods}-enabled/${name}.load -ef ${apache_mods}-available/${name}.load ]'",
   notify => Exec["force-reload-apache2"],
   }
   }
   'absent': {
   exec { "/usr/sbin/a2dismod $name":
   onlyif => "/bin/sh -c '[ -L ${apache_mods}-enabled/${name}.load ] \\
   && [ ${apache_mods}-enabled/${name}.load -ef ${apache_mods}-available/${name}.load ]'",
   notify => Exec["force-reload-apache2"],
   }
   }
   default: { err ( "Unknown ensure value: '$ensure'" ) }
   }
  }
}