From 74de740c66769324b1d4be3756020d819e741cbd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= <hugo@lysator.liu.se>
Date: Tue, 9 May 2023 17:16:22 +0200
Subject: [PATCH] Move most stuff.

---
 manifests/{server.pp => config.pp} | 49 +++++++---------------
 manifests/init.pp                  |  0
 manifests/output.pp                |  2 -
 manifests/package.pp               | 30 ++++++++++++++
 manifests/system_instance.pp       | 19 +++++++++
 manifests/user_instance.pp         | 65 ++++++++++++++++++++++++++++++
 6 files changed, 129 insertions(+), 36 deletions(-)
 rename manifests/{server.pp => config.pp} (89%)
 create mode 100644 manifests/init.pp
 create mode 100644 manifests/package.pp
 create mode 100644 manifests/system_instance.pp
 create mode 100644 manifests/user_instance.pp

diff --git a/manifests/server.pp b/manifests/config.pp
similarity index 89%
rename from manifests/server.pp
rename to manifests/config.pp
index fd79522..384b6be 100644
--- a/manifests/server.pp
+++ b/manifests/config.pp
@@ -76,7 +76,13 @@
 #   changes.
 # @param auto_update_depth
 #   How deep the auto update scan should go.
-class mpd::server (
+# @param config_file
+#   Path to generated configuration file
+# @param owner
+#   Owner of generated configuration file.
+# @param group
+#   Group of generated configuration file.
+define mpd::config (
   String $music_directory,
   String $mpd_home                                            = '/var/lib/mpd',
   Boolean $follow_outside_symlinks                            = true,
@@ -108,9 +114,14 @@ class mpd::server (
   Optional[Metadata] $metadata_to_use                         = undef,
   Boolean $auto_update                                        = true,
   Optional[Integer] $auto_update_depth                        = undef,
+  String $config_file = '/etc/mpd.conf',
+  Optional[Variant[String, Integer]] $owner = undef,
+  Optional[Variant[String, Integer]] $group = undef,
 ) {
-  concat { '/etc/mpd.conf':
+  concat { $config_file:
     ensure => present,
+    owner  => $owner,
+    group  => $group,
   }
 
   $hash = {
@@ -142,8 +153,8 @@ class mpd::server (
     auto_update_depth                => $auto_update_depth,
   }
 
-  concat::fragment { 'mpd_base_config':
-    target  => '/etc/mpd.conf',
+  concat::fragment { "mpd_base_config ${name}":
+    target  => $config_file,
     content => $hash
     .filter |$k, $v| { $v != undef }
     .map |$k, $v| {
@@ -155,34 +166,4 @@ class mpd::server (
     }
     .join(),
   }
-
-  service { 'mpd':
-    ensure => 'running',
-    enable => true,
-  }
-
-  case $facts['os']['family'] {
-    'RedHat': {
-      yumrepo { 'rpm-fusion':
-        ensure   => present,
-        baseurl  => 'https://download1.rpmfusion.org/free/el/updates/7/x86_64',
-        enabled  => true,
-        gpgcheck => true,
-        gpgkey   => 'https://rpmfusion.org/keys?action=AttachFile&do=get&target=RPM-GPG-KEY-rpmfusion-free-el-7',
-      }
-
-      package { ['mpd']:
-        ensure  => installed,
-        require => Yumrepo['rpm-fusion'],
-      }
-    }
-    'Debian': {
-      package { ['mpd']:
-        ensure => installed,
-      }
-    }
-    default: {
-      fail('OS Not supported')
-    }
-  }
 }
diff --git a/manifests/init.pp b/manifests/init.pp
new file mode 100644
index 0000000..e69de29
diff --git a/manifests/output.pp b/manifests/output.pp
index e94cb66..6e90581 100644
--- a/manifests/output.pp
+++ b/manifests/output.pp
@@ -13,8 +13,6 @@ define mpd::output (
   String $output_name = $name,
   Hash $parameters = {},
 ) {
-  require mpd::server
-
   $base = {
     type => $type,
     name => $output_name,
diff --git a/manifests/package.pp b/manifests/package.pp
new file mode 100644
index 0000000..a73756c
--- /dev/null
+++ b/manifests/package.pp
@@ -0,0 +1,30 @@
+# @summary Manages mpd system package
+#
+# @param package_name
+#   Name of the package to install
+class mpd::package (
+  String $package_name = 'mpd',
+) {
+  case $facts['os']['family'] {
+    'RedHat': {
+      yumrepo { 'rpm-fusion':
+        ensure   => present,
+        baseurl  => 'https://download1.rpmfusion.org/free/el/updates/7/x86_64',
+        enabled  => true,
+        gpgcheck => true,
+        gpgkey   => 'https://rpmfusion.org/keys?action=AttachFile&do=get&target=RPM-GPG-KEY-rpmfusion-free-el-7',
+        before   => Package['mpd'],
+      }
+    }
+    'Debian': {}
+    'Archlinux': {}
+    default: {
+      fail('OS Not supported')
+    }
+  }
+
+  package { 'mpd':
+    ensure => installed,
+    name   => $package_name,
+  }
+}
diff --git a/manifests/system_instance.pp b/manifests/system_instance.pp
new file mode 100644
index 0000000..00eec80
--- /dev/null
+++ b/manifests/system_instance.pp
@@ -0,0 +1,19 @@
+# @summary Configures global instance of mpd
+#
+# @param service_name
+#   Name of the mpd service
+# @param mpd_conf
+#   Parameters forwarded to mpd::config
+class mpd::system_instance (
+  String $service_name = 'mpd',
+  Hash[String, Any] $mpd_conf = {},
+) {
+  mpd::config { 'System wide configuration':
+    * => $mpd_conf,
+  } ~> Service[$service_name]
+
+  service { $service_name:
+    ensure => 'running',
+    enable => true,
+  }
+}
diff --git a/manifests/user_instance.pp b/manifests/user_instance.pp
new file mode 100644
index 0000000..50845f3
--- /dev/null
+++ b/manifests/user_instance.pp
@@ -0,0 +1,65 @@
+# @summary Configures an mpd instance for the specified user
+#
+# Only works on systems which uses systemd, and already has a user
+# systemd unit for mpd.
+#
+# @param user
+#   User to run as, as well as owner of files
+# @param group
+#   Group for created files
+# @param user_home
+#   Home directory for user. Used in figuring out where to put
+#   configuation files.
+# @param xdg_config_home
+#   XDG configuration directory for user, used in figuring out where
+#   to put configuration files.
+# @param config_dir
+#   User specific configuration directory for mpd.
+# @param config_file
+#   User specific configuration file for mpd.
+# @param manage_dir
+#   Should the config_dir be managed by us.
+# @param mpd_conf
+#   Settings passed along to mpd::conf
+define mpd::user_instance (
+  String $user = $name,
+  String $group = $user,
+  String $user_home = "/home/${user}",
+  String $xdg_config_home = "${user_home}/.config",
+  String $config_dir = "${xdg_config_home}/mpd",
+  String $config_file = "${config_dir}/mpd.conf",
+  Boolean $manage_dir = true,
+  Hash[String, Any] $mpd_conf = {},
+) {
+  if $manage_dir {
+    file { $config_dir:
+      ensure => directory,
+      owner  => $user,
+      group  => $group,
+    }
+  }
+
+  $conf_defaults = {
+    'mpd_home' => $config_dir,
+  }
+
+  mpd::config { "MPD instance for ${user}":
+    config_file => $config_file,
+    owner       => $user,
+    group       => $group,
+    *           => $conf_defaults + $mpd_conf,
+  } ~> Exec["Restart user ${user} mpd instance"]
+
+  exec { "Enable user ${user} mpd instance":
+    command => 'systemctl enable --now --user mpd.service'.split(' '),
+    creates => "${xdg_config_home}/systemd/user/default.target.wants/mpd.service",
+    user    => $user,
+    path    => ['/bin', '/usr/bin'],
+  }
+
+  exec { "Restart user ${user} mpd instance":
+    command     => 'systemctl restart --user mpd.service'.split(' '),
+    refreshonly => true,
+    path        => ['/bin', '/usr/bin'],
+  }
+}
-- 
GitLab