From 85eecfe470274e746df7d776f357fdba2c5de44f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 13 Jul 2021 19:26:14 +0200 Subject: Add way to manage pacman hooks. --- modules/pacman/manifests/hook.pp | 68 +++++++++++++++++++++++++++++++++++++++ modules/pacman/manifests/init.pp | 20 ++++++++++++ modules/pacman/templates/hook.epp | 31 ++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 modules/pacman/manifests/hook.pp create mode 100644 modules/pacman/manifests/init.pp create mode 100644 modules/pacman/templates/hook.epp diff --git a/modules/pacman/manifests/hook.pp b/modules/pacman/manifests/hook.pp new file mode 100644 index 0000000..940ae5f --- /dev/null +++ b/modules/pacman/manifests/hook.pp @@ -0,0 +1,68 @@ + +type Pacman::Operation = Enum['Install', 'Upgrade', 'Remove'] +# type Variant[Type, Array[Type, 1]] = Variant[Type, Array[Type, 1]] + +type Pacman::Trigger = Struct[{ + type => Enum['Path', 'Package'], + operation => Variant[Pacman::Operation, Array[Pacman::Operation, 1]], + target => Variant[String, Array[String, 1]], +}] + +define pacman::hook ( + Integer $priority = 50, + Optional[String] $description = undef, + Enum['PreTransation', 'PostTransaction'] $when, + String $exec, + Optional[Variant[String, Array[String, 1]]] $depends = undef, + Boolean $abortOnFail = false, # only for PreTransation + Boolean $needsTargets = false, + Variant[Pacman::Trigger, Array[Pacman::Trigger, 1]] $trigger, +) { + + require ::pacman + + if ($abortOnFail and $when != 'PreTransation') { + fail('abortOnFail only valid when "when" => "PreTransation"') + } + + $triggers = $trigger ? { + Array => $trigger, + default => [$trigger], + } + + $str = epp('pacman/hook.epp', { + description => $description, + depends => $depends ? { + Optional => [], + Array => $depends, + default => [$depends], + }, + triggers => $triggers.map |$trigger| { + { + type => $trigger['type'], + operation => $trigger['operation'] ? { + Array => $trigger['operation'], + default => [$trigger['operation']], + }, + target => $trigger['target'] ? { + Array => $trigger['target'], + default => [$trigger['target']], + } + } + }, + exec => $exec, + when => $when, + abortOnFail => $abortOnFail, + needsTargets => $needsTargets, + }) + + $chksum = $str.md5() + + file { $chksum: + ensure => 'present', + content => $str, + path => "${pacman::hooks_path}/${priority}-${name}.hook", + checksum => 'md5', + checksum_value => $chksum, + } +} diff --git a/modules/pacman/manifests/init.pp b/modules/pacman/manifests/init.pp new file mode 100644 index 0000000..eadc1c2 --- /dev/null +++ b/modules/pacman/manifests/init.pp @@ -0,0 +1,20 @@ +class pacman ( + String $hooks_path = '/etc/pacman.d/hooks-puppet', + String $conf_path = '/etc/pacman.conf', +) { + + # TODO ability to set multiple settings + ini_setting { 'Pacman HookDir': + path => $conf_path, + section => 'options', + setting => 'HookDir', + value => $hooks_path, + + } + + file { $hooks_path: + ensure => directory, + recurse => true, + purge => true, + } +} diff --git a/modules/pacman/templates/hook.epp b/modules/pacman/templates/hook.epp new file mode 100644 index 0000000..08377d9 --- /dev/null +++ b/modules/pacman/templates/hook.epp @@ -0,0 +1,31 @@ +<%- | Array[Pacman::Trigger] $triggers, + Optional[String] $description, + String $exec, + Enum['PreTransation', 'PostTransaction'] $when, + Array[String] $depends, + Boolean $abortOnFail, + Boolean $needsTargets, + +| -%> +# Managed by Puppet + +[Trigger] +<%- $triggers.each |$trigger| { -%> +Type = <%= $trigger['type'] %> +<%- $trigger['operation'].each |$op| { -%> +Operation = <%= $op %> +<%- } -%> +<% $trigger['target'].each |$target| { -%> +Target = <%= $target %> +<%- } -%> +<%- } %> + +[Action] +<%- if ($description) { -%>Description = <%= $description %><% } %> +Exec = <%= $exec %> +When = <%= $when %> +<%- $depends.each |$depend| { -%> +Depends = <%= $depend %> +<%- } -%> +<%- if ($abortOnFail) { -%>AbortOnFail<% } %> +<%- if ($needsTargets) { -%>NeedsTargets<% } %> -- cgit v1.2.3