dsa_systemd::override: cleanup and make sure removal cleans the dir also
authorPeter Palfrader <peter@palfrader.org>
Wed, 25 Sep 2019 11:05:06 +0000 (13:05 +0200)
committerPeter Palfrader <peter@palfrader.org>
Wed, 25 Sep 2019 11:05:06 +0000 (13:05 +0200)
modules/dsa_systemd/manifests/override.pp

index b2d210f..f05ab47 100644 (file)
@@ -1,54 +1,32 @@
+# declare a systemd service override
+# @param content  content of systemd override file
+# @param source   source of systemd override file content
+# @param ensure   present or avsent
 define dsa_systemd::override (
-       $source=undef,
-       $content=undef,
-       $ensure=present
+  Optional[String] $source = undef,
+  Optional[String] $content = undef,
+  Enum['present','absent'] $ensure = 'present',
 ) {
+  $dir = "/etc/systemd/system/${name}.service.d"
+  $dest = "${dir}/override.conf"
 
-       $dir = "/etc/systemd/system/${name}.service.d"
-       $dest = "${dir}/override.conf"
-       case $ensure {
-               present: {
-                       if ! ($source or $content) {
-                               fail ( "No configuration found for ${name}" )
-                       }
 
-                       file { "${dir}":
-                               ensure => directory,
-                               mode   => '0755'
-                       }
-                       if $content {
-                               file { "${dest}":
-                                       ensure  => $ensure,
-                                       content => $content,
-                                       notify  => [ Exec['systemctl daemon-reload'],
-                                                    Service["${name}"],
-                                                   ]
-                               }
-                       } elsif $source {
-                               file { "${dest}":
-                                       ensure  => $ensure,
-                                       source  => $source,
-                                       notify  => [ Exec['systemctl daemon-reload'],
-                                                    Service["${name}"],
-                                                  ]
-                                       }
-                       }
-               }
-               absent:  {
-                       if defined(Service["${name}"]) {
-                               $notify = [ Exec['systemctl daemon-reload'], Service["${name}"] ]
-                       } else {
-                               $notify = [ Exec['systemctl daemon-reload'] ]
-                       }
+  if defined(Service[$name]) {
+    $notify = [ Exec['systemctl daemon-reload'], Service[$name] ]
+  } else {
+    $notify = [ Exec['systemctl daemon-reload'] ]
+  }
 
-                       file { "${dest}":
-                               ensure  => $ensure,
-                               notify  => $notify,
-                       }
-                       file { "${dir}":
-                               ensure => $ensure
-                       }
-               }
-               default: { fail ( "Unknown ensure value: '$ensure'" ) }
-       }
+  $directory_ensure = $ensure ? { 'present' => 'directory', 'absent' => 'absent' }
+  file { $dir:
+    ensure => $directory_ensure,
+    mode   => '0755',
+    force  => true,
+  }
+  file { $dest:
+    ensure  => $ensure,
+    source  => $source,
+    content => $content,
+    notify  => $notify,
+  }
 }