summaryrefslogtreecommitdiff
path: root/manifests/init.pp
blob: 9e9f43fb4a214d474e2ce3868b997f4984b1cf36 (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#
# Puppet module for Apache
#
# This module is distributed under the GNU Affero General Public License:
# 
# Backup module for puppet
# Copyright (C) 2009 Sarava Group
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Using code from Debian Apache2 recipe:
#
#  http://reductivelabs.com/trac/puppet/wiki/Recipes/DebianApache2Recipe
#

$apache2_sites  = "/etc/apache2/sites"
$apache2_mods   = "/etc/apache2/mods"
$apache2_conf_d = "/etc/apache2/conf.d"
$apache2_macros = "/etc/apache2/conf.d/macros"
$apache2_conf   = "/etc/apache2/apache2.conf"

class apache {
  include ssl

  case $apache_www_folder {
    '': { $apache_www_folder = "/var/www" }
  }

  case $apache_default_folder {
    # Give the "It works!" webpage by default.
    '': { $apache_default_folder = "/var/www" }
  }

  case $apache_sites_folder {
    '': { $apache_sites_folder = "${apache_www_folder}/sites" }
  }

  case $apache_error_folder {
    '': { $apache_error_folder = "${apache_www_folder}/error" }
  }

  case $apache_error_dest {
    '': { $apache_error_dest = "${apache_error_folder}/index.html" }
  }

  case $apache_server_name {
    '': { $apache_server_name = $hostname }
  }

  package { "apache":
    name   => "apache2-mpm-itk",
    ensure => installed,
  }

  package { "mod_macro":
    name   => "libapache2-mod-macro",
    ensure => installed,                                                                                                     
  }

  service { "apache2":
    alias      => "apache",
    ensure     => running,
    require    => Package["apache"],
    hasstatus  => true,
    hasrestart => true,
  }

  module { "macro":
    ensure  => present,
    require => Package["mod_macro"],
  }

  module { "headers":
    ensure  => present,
    require => Package["apache"],
  }

  module { "ssl":
    ensure  => present,
    require => Package["apache"],
  }

  module { "rewrite":
    ensure  => present,
    require => Package["apache"],
  }

  # apache mod_macro configuration
  file { "${apache2_macros}":
    ensure  => present,
    content => template('apache/macros.erb'),
    owner   => root,
    group   => root,
    mode    => 0644,
    require => Module["macro"],
    notify  => Service["apache"],
  }

  # apache mod_macro configuration
  file { "${apache2_conf}":
    ensure  => present,
    content => template('apache/apache2.conf.erb'),
    owner   => root,
    group   => root,
    mode    => 0644,
    notify  => Service["apache"],
  }

  # apache alias configuration
  file { "${apache2_mods}-available/alias.conf":
    ensure  => present,
    content => template('apache/alias.conf.erb'),
    owner   => root,
    group   => root,
    mode    => 0644,
    notify  => Service["apache"],
  }

  # apache autoindex configuration
  file { "${apache2_mods}-available/autoindex.conf":
    ensure  => present,
    content => template('apache/autoindex.conf.erb'),
    owner   => root,
    group   => root,
    mode    => 0644,
    notify  => Service["apache"],
  }

  # icons folder
  # http://larsjung.de/h5ai/
  # http://recursive-design.com/blog/2008/12/29/styling-apache-directory-listings-with-mod_autoindex/
  # http://code.ecchi.ca/apache-tango-icons/README.html
  file { "${apache_www_folder}/icons":
    ensure  => directory,
    recurse => true,
    purge   => true,
    force   => true,
    owner   => "root",
    group   => "root",
    # This mode will also apply to files from the source directory
    mode    => 0644,
    # Puppet will automatically set +x for directories
    source  => [ "puppet:///modules/site-apache/htdocs/$domain/icons",
                 "puppet:///modules/apache/icons", ]
  }

  # default site configuration
  file { "${apache2_sites}-available/default":
    ensure  => present,
    content => template('apache/default.erb'),
    owner   => root,
    group   => root,
    mode    => 0644,
    notify  => Service["apache"],
  }

  # https proxy configuration
  # see http://www.metaltoad.com/blog/running-drupal-secure-pages-behind-proxy
  file { "$apache2_conf_d/https-proxy":
    ensure => $apache_https_proxy ? {
      ''      => absent,
      default => present,
    },
    content => $apache_https_proxy ? {
      'force' => "SetEnv HTTPS on\n",
      default => "SetEnvIf X-Forwarded-Proto https HTTPS=on\n",
    },
    owner   => root,
    group   => root,
    mode    => 0644,
    notify  => Service["apache"],
  }

  define site($ensure = present, $docroot = false, $redirect = false,
              $redirect_match = false, $protocol = 'http', $aliases = false,
              $server_alias = false, $use = false, $ticket = false,
              $source = false, $template = 'apache/site.erb', $filename = '',
              $manage_docroot = true, $owner = 'root', $group = 'root',
              $mpm = true, $mpm_user = '', $mpm_group = '', $password = '*',
              $comment = '', $sshkey = absent,
              $groups = '', $shell = '/bin/false', $manage_user = true,
              $ssl = false, $listen = '*', $https_redirect = false,
              $canonical = false, $canonical_exceptions = '') {

    $vhost = $filename ? {
      ''      => "$title",
      default => "$filename",
    }

    $hosting_domain = $base_domain ? {
      ''      => $domain,
      default => $base_domain,
    }

    $user = $mpm_user ? {
      ''      => regsubst($title, '\.', '_', 'G'),
      default => $mpm_user,
    }

    $gid = $mpm_group? {
      ''      => regsubst($title, '\.', '_', 'G'),
      default => $mpm_group,
    }

    if $mpm == true and $manage_user == true and $user != 'root' {
      if $ensure == present {
        if !defined(Group[$gid]) {
          group { "$gid":
            ensure => present,
          }
        }
  
        if !defined(User["$user"]) {
          user::manage { "$user":
            tag      => "virtual",
            password => $password,
            gid      => $gid,
            comment  => $comment,
            ticket   => $ticket,
            groups   => $groups,
            sshkey   => $sshkey,
            shell    => $shell,
            ensure   => present,
            require  => Group[$gid],
          }
        }
      }
      else {
        if !defined(User["$user"]) {
          user::manage { "$user":
            tag      => "virtual",
            password => $password,
            ensure   => absent,
          }
        }

        if !defined(Group[$gid]) {
          group { "$gid":
            ensure  => absent,
            require => User[$user],
          }
        }
      }
    }

    if $ssl == true {
      ssl::cert { "$name":
        group    => $gid,
        privmode => '0640',
        ensure   => $ensure,
      }
    
      ssl::check { "$name":
        file   => "/etc/ssl/certs/$name.crt",
        ensure => $ensure,
      }
    }

    case $source {
      true: {
               file { "${apache2_sites}-available/$vhost":
                 ensure  => $ensure,
                 source  => [ "puppet:///modules/site-apache/vhosts/$domain/$title", 
                              "puppet:///modules/site-apache/vhosts/$title" ],
                 owner   => root,
                 group   => root,
                 mode    => 0644,
                 require => File["${apache2_macros}"],
                 notify  => Service["apache"],
               }
             }
      false: {
               file { "${apache2_sites}-available/$vhost":
                 ensure  => $ensure,
                 content => template("$template"),
                 owner   => root,
                 group   => root,
                 mode    => 0644,
                 require => File["${apache2_macros}"],
                 notify  => Service["apache"],
               }
             }
    }

    # Enable the site without a2ensite
    #
    #$status = $ensure ? {
    #  'present' => "${apache2_sites}-available/$vhost",
    #  default   => 'absent',
    #}
    #
    #file { "/etc/apache2/sites-enabled/$title":
    #  ensure  => $status,
    #  owner   => root,
    #  group   => root,
    #  require => File["${apache2_sites}-available/$title"],
    #  notify  => Service["apache"],
    #}

    case $ensure {
      'present': {
        if ($docroot != false) and ($manage_docroot == true) {
          if !defined(File["${docroot}"]) {
            file { "${docroot}":
              ensure  => present,
              owner   => $owner,
              group   => $group,
              mode    => 0755,
              recurse => false,
            }
          }
          if !defined(Exec["check_docroot_${docroot}"]) {
            # Ensure parent folder exist
            exec { "check_docroot_${docroot}":
              command => "/bin/mkdir -p ${docroot}",
              unless  => "/bin/sh -c '[ -e ${docroot} ]'",
              user    => root,
              before  => File["${docroot}"], 
            }
          }
        }
        exec { "/usr/sbin/a2ensite $vhost":
          unless  => "/bin/sh -c '[ -L ${apache2_sites}-enabled/$vhost ] \
                  && [ ${apache2_sites}-enabled/$vhost -ef ${apache2_sites}-available/$vhost ]'",
          notify  => Exec["reload-apache2"],
        }
      }
      'absent': {
        exec { "/usr/sbin/a2dissite $vhost":
          onlyif  => "/bin/sh -c '[ -L ${apache2_sites}-enabled/$vhost ] \
                  && [ ${apache2_sites}-enabled/$vhost -ef ${apache2_sites}-available/$vhost ]'",
          notify  => Exec["reload-apache2"],
        }

        file { "${apache2_sites}-enabled/$vhost":
          ensure => absent,
          notify => Exec["reload-apache2"],
        }
      }
      default: { err ("Unknown ensure value: '$ensure'") }
    }
  }

  # 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.
  define module($ensure = 'present') {
    case $ensure {
      'present': {
        exec { "/usr/sbin/a2enmod $name":
          unless  => "/bin/sh -c '[ -L ${apache2_mods}-enabled/${name}.load ] \
                  && [ ${apache2_mods}-enabled/${name}.load -ef ${apache2_mods}-available/${name}.load ]'",
          notify  => Exec["force-reload-apache2"],
        }
      }
      'absent': {
        exec { "/usr/sbin/a2dismod $name":
          onlyif  => "/bin/sh -c '[ -L ${apache2_mods}-enabled/${name}.load ] \
                  && [ ${apache2_mods}-enabled/${name}.load -ef ${apache2_mods}-available/${name}.load ]'",
          notify  => Exec["force-reload-apache2"],
        }
      }
      default: { err ("Unknown ensure value: '$ensure'") }
    }
  }

  # 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,
  }

  exec { "force-reload-apache2":
    command     => "/etc/init.d/apache2 force-reload",
    refreshonly => true,
  }
}

class apache::rails inherits apache {
  package { "mod_rack":
    name   => "libapache2-mod-passenger",
    ensure => installed,                                                                                                     
  }

  # Needed by https://git.codecoop.org/projects/coquelicot
  package { [ 'libsinatra-ruby1.8', 'libruby1.8',
              'libhaml-ruby1.8',    'liblockfile-ruby',
              'libgettext-ruby1.8', 'libjson-ruby1.8' ]:
    ensure => installed,
  }

  # Already provided by libruby1.8 package
  package { 'libopenssl-ruby1.8':
    ensure => absent,
  }

  module { "passenger":
    ensure  => present,
    require => Package["mod_rack"],
  }
}