From ebdb57bbb841d2fe0b5607787c7f626b5b7fdc4c Mon Sep 17 00:00:00 2001
From: Thomas Bellman <bellman@lysator.liu.se>
Date: Tue, 5 Oct 2021 19:17:37 +0200
Subject: [PATCH] Use systemd Puppet module for systemd unit files.

The systemd module makes it a little bit easier to manage systemd
unit files.  Specifying unit options as a hash is easier than e.g.
having to build a string from a list of lines ourselves.  The module
also creates the /etc/systemd/system/<UNIT>.d directory, and tells
systemd to reload its configuration, without us having to do it.
---
 README                           |  2 ++
 manifests/screensaver/disable.pp | 34 ++++++++++++++------------------
 manifests/serial/login.pp        | 21 +++++++-------------
 3 files changed, 24 insertions(+), 33 deletions(-)

diff --git a/README b/README
index 3429923..32dd402 100644
--- a/README
+++ b/README
@@ -31,6 +31,8 @@ If not, see <http://www.gnu.org/licenses/>.
 - bootloader
   Can be found at https://git.lysator.liu.se/bellman/puppet-bootloader.git.
 
+- systemd
+  Can be found at https://git.lysator.liu.se/bellman/puppet-systemd.git.
 
 
 ===== OFFICIAL SOURCE =====
diff --git a/manifests/screensaver/disable.pp b/manifests/screensaver/disable.pp
index e33c69a..47e9331 100644
--- a/manifests/screensaver/disable.pp
+++ b/manifests/screensaver/disable.pp
@@ -24,25 +24,21 @@ class console::screensaver::disable
 	    }
 	}
 	'systemd': {
-	    $lines = [
-		'# This file is under Puppet control',
-		'',
-		'[Unit]',
-		'Description=Disable screensaver on the console',
-		'',
-		'[Service]',
-		'Type=oneshot',
-		"ExecStart=/bin/sh -c '${setterm_cmd}'",
-		'',
-		'[Install]',
-		'WantedBy=basic.target',
-	    ]
-	    file {
-		'/etc/systemd/system/screensaver-disable.service':
-		    ensure => file,
-		    content => inline_template('<%=@lines.join("\n")+"\n" %>'),
-		    owner => 'root', group => 'root', mode => '0444',
-		    before => Service['screensaver-disable'];
+	    systemd::unit {
+		'screensaver-disable.service':
+		    options => {
+			'Unit' => {
+			    'Description'=>'Disable screensaver on the console',
+			},
+			'Service' => {
+			    'Type' => 'oneshot',
+			    'ExecStart' => "/bin/sh -c '${setterm_cmd}'",
+			},
+			'Install' => {
+			    'WantedBy' => 'basic.target',
+			},
+		    },
+		    notify => Service['screensaver-disable'];
 	    }
 	    service {
 		'screensaver-disable':
diff --git a/manifests/serial/login.pp b/manifests/serial/login.pp
index 5761d18..6483878 100644
--- a/manifests/serial/login.pp
+++ b/manifests/serial/login.pp
@@ -97,7 +97,6 @@ define console::serial::login::systemd(
 	$termtype,
 )
 {
-    $svcdir = "/etc/systemd/system/serial-getty@${name}.service.d"
     # Systemd cmdline quoting is not *exactly* like shell, but close enough.
     $agetty_cmdline = shellquote(
 	# agetty(8) doc says tty name should be before speeds, but default
@@ -108,23 +107,17 @@ define console::serial::login::systemd(
 	'%I',
 	$termtype
     )
-    $svc_params_cfg = (
-	"[Service]\nExecStart=\nExecStart=-${agetty_cmdline}\n"
-    )
 
-    file {
-	$svcdir:
+    systemd::unit_options {
+	"serial-getty@${name}.service/param":
 	    ensure => $ensure ? {
-		'enabled' => directory, 'disabled' => undef,
+		'enabled' => 'present', 'disabled' => 'absent',
 	    },
-	    owner => 'root', group => 'root', mode => '0755';
-
-	"${svcdir}/param.conf":
-	    ensure => $ensure ? {
-		'enabled' => file, 'disabled' => absent,
+	    options => {
+		'Service' => {
+		    '-ExecStart' => "-${agetty_cmdline}",
+		},
 	    },
-	    content => $svc_params_cfg,
-	    owner => 'root', group => 'root', mode => '0444',
 	    notify => Service["serial-getty@${name}"];
     }
 
-- 
GitLab