From b748aee9d9b27c0accc5741ff97f69a9fae217d5 Mon Sep 17 00:00:00 2001
From: espiv <servers@espiv.net>
Date: Sat, 25 Jul 2015 23:35:50 +0300
Subject: [PATCH] Refactors mirror class

This commit merges nginx and apache2 classes, removes params class,
introduces package name and logging parameters.
---
 manifests/mirror.pp                              | 83 ++++++++++++++++++++----
 manifests/mirror/apache2.pp                      | 45 -------------
 manifests/mirror/nginx.pp                        | 47 --------------
 manifests/mirror/params.pp                       |  8 ---
 templates/mirror/apache/dl.amnesia.boum.org.erb  | 15 -----
 templates/mirror/apache2/dl.amnesia.boum.org.erb | 19 ++++++
 templates/mirror/nginx/dl.amnesia.boum.org.erb   |  3 +-
 7 files changed, 93 insertions(+), 127 deletions(-)
 delete mode 100644 manifests/mirror/apache2.pp
 delete mode 100644 manifests/mirror/nginx.pp
 delete mode 100644 manifests/mirror/params.pp
 delete mode 100644 templates/mirror/apache/dl.amnesia.boum.org.erb
 create mode 100644 templates/mirror/apache2/dl.amnesia.boum.org.erb

diff --git a/manifests/mirror.pp b/manifests/mirror.pp
index a58acfc..556588e 100644
--- a/manifests/mirror.pp
+++ b/manifests/mirror.pp
@@ -1,19 +1,53 @@
-# This class manages the rsync cronjob and the mirror path.
+# This class manages a tails mirror.
+#
+# == Notes ==
+#
+# When class is declared with "ensure => absent" it
+# will not uninstall any packages.
+#
+# == Parameters ==
+#
+# - $webserver: nginx or apache2, defaults to nginx
+#
+# - $package: lets you explicitly define the webserver
+#   package name. This can be useful in case default
+#   packages to be installed conflict with already
+#   installed ones. Defaults to webserver's name.
+#
+# - $access_log: access log file path. Defaults to keep
+#   no logs.
+#
+# - $error_log: error log file path. Default to keep
+#   no logs.
+#
+# == Usage ==
+#
+# You can either include this class:
+#
+#   include tails::mirror
+#
+# or declare it and provide parameters:
+#
+#   class { 'tails::mirror':
+#     ensure    => 'present',
+#     webserver => 'apache2',
+#   }
 #
-# Don't include this class directly: instead use either
-# tails::mirror::nginx or tails::mirror::apache2.
 
 class tails::mirror (
-  $ensure,
-  $mirror_path,
-  $mirror_owner,
-  $mirror_group,
-  $mirror_mode,
+  $ensure       = present,
+  $webserver    = 'nginx',
+  $package      = $webserver,
+  $mirror_path  = '/var/www/tails-mirror/',
+  $mirror_owner = 'www-data',
+  $mirror_group = 'www-data',
+  $mirror_mode  = '0755',
+  $access_log   = '/dev/null',
+  $error_log    = '/dev/null',
+  $admin_mail   = '',
 ) {
 
-  if !defined(Package['rsync']) {
-    package { 'rsync': ensure => $ensure }
-  }
+  ensure_packages(['rsync', $webserver])
 
   $dir_ensure = $ensure ? {
     'present' => 'directory',
@@ -25,6 +59,11 @@ class tails::mirror (
     'absent'  => true,
   }
 
+  $link_ensure = $ensure ? {
+    'present' => 'link',
+    'absent'  => 'absent',
+  }
+
   file { $mirror_path:
     ensure  => $dir_ensure,
     mode    => $mirror_mode,
@@ -43,4 +82,26 @@ class tails::mirror (
     require => File[$mirror_path]
   }
 
+  file { "/etc/${webserver}/sites-available/dl.amnesia.boum.org":
+    ensure  => $ensure,
+    mode    => '0644',
+    owner   => root,
+    group   => root,
+    content => template("tails/mirror/${webserver}/dl.amnesia.boum.org.erb"),
+    require => Package[$webserver],
+    notify  => Service[$webserver],
+  }
+
+  file { "/etc/${webserver}/sites-enabled/dl.amnesia.boum.org":
+    ensure  => $link_ensure,
+    target  => "/etc/${webserver}/sites-available/dl.amnesia.boum.org",
+    require => [
+      File["/etc/${webserver}/sites-available/dl.amnesia.boum.org"],
+      Service[$webserver]
+    ],
+  }
+
+  service { $webserver:
+    ensure => running,
+  }
 }
diff --git a/manifests/mirror/apache2.pp b/manifests/mirror/apache2.pp
deleted file mode 100644
index d680123..0000000
--- a/manifests/mirror/apache2.pp
+++ /dev/null
@@ -1,45 +0,0 @@
-# Manage a Tails mirror with Apache 2.
-#
-# Notes:
-#
-# * It does not uninstall package apache2.2-bin when ensure is
-#   set to absent, in case it's also used by other vhosts.
-#
-# * Notifying service apache2 is commented out since it's not
-#   required as a dependency
-
-class tails::mirror::apache2 (
-  $ensure       = $tails::mirror::params::ensure,
-  $mirror_path  = $tails::mirror::params::mirror_path,
-  $mirror_owner = $tails::mirror::params::mirror_owner,
-  $mirror_group = $tails::mirror::params::mirror_group,
-  $mirror_mode  = $tails::mirror::params::mirror_mode,
-  $admin_mail   = '',
-) inherits tails::mirror::params {
-
-  class {'tails::mirror':
-    ensure       => $ensure,
-    mirror_path  => $mirror_path,
-    mirror_owner => $mirror_owner,
-    mirror_group => $mirror_group,
-    mirror_mode  => $mirror_mode,
-  }
-
-  ensure_packages(['apache2.2-bin'])
-
-  file { '/etc/apache2/sites-available/dl.amnesia.boum.org':
-    ensure  => $ensure,
-    mode    => '0644',
-    owner   => root,
-    group   => root,
-    content => template('tails/mirror/apache/dl.amnesia.boum.org.erb'),
-  }
-
-  file { '/etc/apache2/sites-enabled/dl.amnesia.boum.org':
-    ensure  => 'link',
-    target  => '/etc/apache2/sites-available/dl.amnesia.boum.org',
-    require => File['/etc/apache2/sites-available/dl.amnesia.boum.org'],
-    #notify  => Service['apache2'],
-  }
-
-}
diff --git a/manifests/mirror/nginx.pp b/manifests/mirror/nginx.pp
deleted file mode 100644
index b7406ec..0000000
--- a/manifests/mirror/nginx.pp
+++ /dev/null
@@ -1,47 +0,0 @@
-# Manage a Tails mirror with nginx.
-#
-# Notes:
-#
-# * Installs package nginx-light which might conflict with some already
-#   installed nginx flavor. Change package name if needed.
-#
-# * This class does not uninstall package nginx-light when ensure is
-#   set to absent in case it's also used by other vhosts.
-#
-# * Notifying service nginx is commented out since it's not
-#   required as a dependency
-
-class tails::mirror::nginx (
-  $ensure       = $tails::mirror::params::ensure,
-  $mirror_path  = $tails::mirror::params::mirror_path,
-  $mirror_owner = $tails::mirror::params::mirror_owner,
-  $mirror_group = $tails::mirror::params::mirror_group,
-  $mirror_mode  = $tails::mirror::params::mirror_mode,
-) inherits tails::mirror::params {
-
-  class {'tails::mirror':
-    ensure       => $ensure,
-    mirror_path  => $mirror_path,
-    mirror_owner => $mirror_owner,
-    mirror_group => $mirror_group,
-    mirror_mode  => $mirror_mode,
-  }
-
-  ensure_packages(['nginx-light'])
-
-  file { '/etc/nginx/sites-available/dl.amnesia.boum.org':
-    ensure  => $ensure,
-    mode    => '0644',
-    owner   => root,
-    group   => root,
-    content => template('tails/mirror/nginx/dl.amnesia.boum.org.erb'),
-    require => Package['nginx-light']
-  }
-
-  file { '/etc/nginx/sites-enabled/dl.amnesia.boum.org':
-    ensure  => 'link',
-    target  => '/etc/nginx/sites-available/dl.amnesia.boum.org',
-    require => File['/etc/nginx/sites-available/dl.amnesia.boum.org'],
-    #notify  => Service['nginx'],
-  }
-}
diff --git a/manifests/mirror/params.pp b/manifests/mirror/params.pp
deleted file mode 100644
index 83e622d..0000000
--- a/manifests/mirror/params.pp
+++ /dev/null
@@ -1,8 +0,0 @@
-# Default values for tails::mirror::*'s parameters.
-class tails::mirror::params () {
-  $ensure       = 'present'
-  $mirror_path  = '/var/www/tails-mirror/'
-  $mirror_owner = 'www-data'
-  $mirror_group = 'www-data'
-  $mirror_mode  = '0755'
-}
diff --git a/templates/mirror/apache/dl.amnesia.boum.org.erb b/templates/mirror/apache/dl.amnesia.boum.org.erb
deleted file mode 100644
index 5db6abd..0000000
--- a/templates/mirror/apache/dl.amnesia.boum.org.erb
+++ /dev/null
@@ -1,15 +0,0 @@
-<VirtualHost *:80>
-   ServerName dl.amnesia.boum.org
-   <% if @admin_mail -%>
-   ServerAdmin <%= @admin_mail %>
-   <% end -%>
-
-   DocumentRoot <%= @mirror_path %>
-   <Directory <%= @mirror_path %> >
-      Options Indexes
-      AllowOverride None
-      IndexIgnore README.html
-      IndexOptions FancyIndexing FoldersFirst IgnoreCase NameWidth=50
-      IndexOrderDefault Descending Date
-   </Directory>
-</VirtualHost>
diff --git a/templates/mirror/apache2/dl.amnesia.boum.org.erb b/templates/mirror/apache2/dl.amnesia.boum.org.erb
new file mode 100644
index 0000000..b11e65f
--- /dev/null
+++ b/templates/mirror/apache2/dl.amnesia.boum.org.erb
@@ -0,0 +1,19 @@
+<VirtualHost *:80>
+   ServerName dl.amnesia.boum.org
+   <% if @admin_mail -%>
+   ServerAdmin <%= @admin_mail %>
+   <% end -%>
+
+   ErrorLog <%= @error_log %>
+   LogLevel error
+   CustomLog <%= @access_log %> combined
+
+   DocumentRoot <%= @mirror_path %>
+   <Directory <%= @mirror_path %> >
+      Options Indexes
+      AllowOverride None
+      IndexIgnore README.html
+      IndexOptions FancyIndexing FoldersFirst IgnoreCase NameWidth=50
+      IndexOrderDefault Descending Date
+   </Directory>
+</VirtualHost>
diff --git a/templates/mirror/nginx/dl.amnesia.boum.org.erb b/templates/mirror/nginx/dl.amnesia.boum.org.erb
index 9961b53..8c397d4 100644
--- a/templates/mirror/nginx/dl.amnesia.boum.org.erb
+++ b/templates/mirror/nginx/dl.amnesia.boum.org.erb
@@ -6,7 +6,8 @@ server {
     <% end -%>
 
     root <%= @mirror_path %>;
-    access_log     off;
+    access_log <%= @access_log %>;
+    error_log <%= @error_log %>;
 
     location = /favicon.ico {
         return 204;
-- 
2.1.4

