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