Added service management functionality to core 69/169/1
authorJosh Farwell <jfarwell@linuxfoundation.org>
Wed, 17 Jun 2015 23:52:35 +0000 (16:52 -0700)
committerJosh Farwell <jfarwell@linuxfoundation.org>
Wed, 17 Jun 2015 23:52:35 +0000 (16:52 -0700)
We are now managing the mailman3 service and notifying it via the
core class when changes to the resources in the core::config class are
made.

Change-Id: Iecf2471d9b25e3fdd23b50660b94f86029b80052
Signed-off-by: Josh Farwell <jfarwell@linuxfoundation.org>
manifests/core.pp
manifests/core/service.pp [new file with mode: 0644]
manifests/params.pp
spec/classes/core__service_spec.rb [new file with mode: 0644]
spec/classes/core_spec.rb

index 5b717cf..702192a 100644 (file)
@@ -41,6 +41,8 @@ class mailman3::core (
   $core_override_hyperkittycfg = {},
   $core_override_options       = {},
   $core_override_allowed_hosts = {},
+  $core_refresh_service        = $mailman3::params::core_refresh_service,
+  $core_service_enabled        = $mailman3::params::core_service_enabled,
   $core_version                = $mailman3::params::core_version,
   $db_tag                      = $mailman3::params::core_db_tag,
   $install_core                = $mailman3::params::install_core,
@@ -50,12 +52,19 @@ class mailman3::core (
   validate_bool($install_core)
   validate_bool($core_manage_database)
   validate_bool($core_manage_firewall)
+  validate_bool($core_refresh_service)
   validate_hash($core_override_hyperkittycfg)
   validate_hash($core_override_options)
   validate_hash($core_override_allowed_hosts)
   validate_string($db_tag)
   validate_string($core_version)
 
+  unless is_bool($core_service_enabled) {
+    validate_re($core_service_enabled, '^manual$',
+    "${core_service_enabled} is not supported for service_enabled. \
+Allowed values are true, false, 'manual'.")
+  }
+
   # Merge together options with defaults from ::params, right most wins
   $core_hyperkittycfg = merge($mailman3::params::core_default_hyperkittycfg,
     $core_override_hyperkittycfg)
@@ -83,10 +92,22 @@ class mailman3::core (
     db_tag               => $db_tag,
   }
 
+  class { '::mailman3::core::service':
+    service_enabled => $core_service_enabled,
+  }
+
   Anchor['mailman3::core::begin'] ->
     Class['mailman3::core::install'] ->
     Class['mailman3::core::config'] ->
+    Class['mailman3::core::service'] ->
   Anchor['mailman3::core::end']
 
+  # The config class should notify the service class when its resources change
+  # We are doing it here because these classes don't have interdependencies and using
+  # notify or subscribe within them would cause compile / tests to fail
+  if ($core_refresh_service) {
+    Class['mailman3::core::config'] ~> Class['mailman3::core::service']
+  }
+
 }
 # vim: ts=2 sw=2 sts=2 et :
diff --git a/manifests/core/service.pp b/manifests/core/service.pp
new file mode 100644 (file)
index 0000000..0e5d1e0
--- /dev/null
@@ -0,0 +1,63 @@
+# == Class: mailman3
+#
+# Full description of class mailman3 here.
+#
+# === Parameters
+#
+# Document parameters here.
+#
+# [*sample_parameter*]
+#   Explanation of what this parameter affects and what it defaults to.
+#   e.g. "Specify one or more upstream ntp servers as an array."
+#
+# === Variables
+#
+# Here you should define a list of variables that this module would require.
+#
+# [*sample_variable*]
+#   Explanation of how this variable affects the function of this class and if
+#   it has a default. e.g. "The parameter enc_ntp_servers must be set by the
+#   External Node Classifier as a comma separated list of hostnames." (Note,
+#   global variables should be avoided in favor of class parameters as
+#   of Puppet 2.6.)
+#
+# === Examples
+#
+#  class { 'mailman3':
+#    servers => [ 'pool.ntp.org', 'ntp.local.company.com' ],
+#  }
+#
+# === Authors
+#
+# Josh Farwell <jfarwell@linuxfoundation.org>
+#
+# === Copyright
+#
+# Copyright 2015 Josh Farwell, unless otherwise noted.
+#
+class mailman3::core::service (
+  $service_enabled,
+) {
+
+  unless is_bool($service_enabled) {
+    validate_re($service_enabled, '^manual$',
+      "${service_enabled} is not supported for service_enabled. \
+Allowed values are true, false, 'manual'.")
+  }
+
+  if (is_bool($service_enabled)) {
+    $ensure = $service_enabled
+  }
+  else {
+    $ensure = undef
+  }
+
+  $enable = $service_enabled
+
+  service { 'mailman3':
+    ensure     => $ensure,
+    enable     => $enable,
+    hasrestart => true,
+    hasstatus  => true,
+  }
+}
index 4a7c1c4..61268eb 100644 (file)
@@ -42,6 +42,8 @@ class mailman3::params {
   # Mangement Booleans
   $core_manage_database = true
   $core_manage_firewall = true
+  $core_refresh_service = true
+  $core_service_enabled = true
   $install_core         = true
 
   # Shared values
diff --git a/spec/classes/core__service_spec.rb b/spec/classes/core__service_spec.rb
new file mode 100644 (file)
index 0000000..6e5f4c8
--- /dev/null
@@ -0,0 +1,60 @@
+require 'spec_helper'
+
+describe 'mailman3::core::service', :type => :class do
+  # set some default good params so we can override with bad ones in
+  # test
+  let(:params) {
+    {
+      'service_enabled' => true,
+    }
+  }
+
+  # we do not have default values so the class should fail to compile
+  context 'with defaults for all parameters' do
+    let (:params) {{}}
+
+    it do
+      expect {
+        should compile
+      }.to raise_error(RSpec::Expectations::ExpectationNotMetError,
+        /Must pass /)
+    end
+  end
+
+  # with assumed good params
+  context 'with good parameters' do
+    it { is_expected.to contain_service('mailman3').with(
+        :ensure   => true,
+        :enable   => true,
+      ) }
+  end
+
+  context 'service_enabled is incorrect' do
+    let(:params) {{ 'service_enabled' => 'invalid_val' }}
+
+    it 'should report an error when service_enabled is invalid' do
+      expect { should compile }.to \
+        raise_error(RSpec::Expectations::ExpectationNotMetError,
+          /invalid_val is not supported for service_enabled\. \
+Allowed values are true, false, 'manual'\./)
+    end
+  end
+
+  context 'service_enabled set to false' do
+    let(:params) {{ 'service_enabled' => false }}
+
+    it { is_expected.to contain_service('mailman3').with(
+        :ensure => false
+      ) }
+  end
+
+  context 'service_enabled set to manual' do
+    let(:params) {{ 'service_enabled' => 'manual' }}
+
+    it { is_expected.to contain_service('mailman3').with(
+        :enable => 'manual',
+      ) }
+  end
+end
+
+# vim: sw=2 ts=2 sts=2 et :
index c1c43c2..3206f02 100644 (file)
@@ -8,6 +8,7 @@ describe 'mailman3::core', :type => :class do
     it { is_expected.to contain_anchor('mailman3::core::begin') }
     it { is_expected.to contain_class('mailman3::core::install') }
     it { is_expected.to contain_class('mailman3::core::config') }
+    it { is_expected.to contain_class('mailman3::core::service') }
     it { is_expected.to contain_anchor('mailman3::core::end') }
   end
 end