From 7229ee22b48e6e065a572d31caeb447f78c16734 Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Thu, 20 Jun 2024 13:29:02 +0200 Subject: [PATCH] Allow setting libexecdir (/usr/libexec) from build flags This allows building packages with different values of libexecdir with the daemon binary automatically adapting the channel plugin dir to it, i.e. so there is no need to specify it in the config file. This is relevant for Suse where %{_libexecdir} expands to /usr/lib instead. --- Makefile | 6 +++++- config.example.yml | 2 +- internal/daemon/config.go | 13 ++++++++++++- internal/paths.go | 6 ++++++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 internal/paths.go diff --git a/Makefile b/Makefile index aefb4599..3e7201c6 100644 --- a/Makefile +++ b/Makefile @@ -10,9 +10,13 @@ libexecdir ?= $(prefix)/libexec datadir ?= $(prefix)/share sysconfdir ?= /etc +all: pkg = github.com/icinga/icinga-notifications/internal all: mkdir -p build - go build -o build/ ./cmd/icinga-notifications + go build \ + -o build/ \ + -ldflags "-X '$(pkg).LibExecDir=$(libexecdir)'" \ + ./cmd/icinga-notifications go build -o build/channel/ ./cmd/channel/... test: diff --git a/config.example.yml b/config.example.yml index 8b800d05..3d126a63 100644 --- a/config.example.yml +++ b/config.example.yml @@ -7,7 +7,7 @@ #debug-password: "put-something-secret-here" icingaweb2-url: http://localhost/icingaweb2/ -channel-plugin-dir: /usr/libexec/icinga-notifications/channel +#channel-plugin-dir: /usr/libexec/icinga-notifications/channel api-timeout: 1m database: diff --git a/internal/daemon/config.go b/internal/daemon/config.go index 309f53b5..e612cc6a 100644 --- a/internal/daemon/config.go +++ b/internal/daemon/config.go @@ -6,6 +6,7 @@ import ( "github.com/goccy/go-yaml" "github.com/icinga/icinga-go-library/database" "github.com/icinga/icinga-go-library/logging" + "github.com/icinga/icinga-notifications/internal" "os" "time" ) @@ -13,13 +14,23 @@ import ( type ConfigFile struct { Listen string `yaml:"listen" default:"localhost:5680"` DebugPassword string `yaml:"debug-password"` - ChannelPluginDir string `yaml:"channel-plugin-dir" default:"/usr/libexec/icinga-notifications/channel"` + ChannelPluginDir string `yaml:"channel-plugin-dir"` ApiTimeout time.Duration `yaml:"api-timeout" default:"1m"` Icingaweb2URL string `yaml:"icingaweb2-url"` Database database.Config `yaml:"database"` Logging logging.Config `yaml:"logging"` } +// SetDefaults implements the defaults.Setter interface. +func (c *ConfigFile) SetDefaults() { + if defaults.CanUpdate(c.ChannelPluginDir) { + c.ChannelPluginDir = internal.LibExecDir + "/icinga-notifications/channel" + } +} + +// Assert interface compliance. +var _ defaults.Setter = (*ConfigFile)(nil) + // config holds the configuration state as a singleton. It is used from LoadConfig and Config var config *ConfigFile diff --git a/internal/paths.go b/internal/paths.go new file mode 100644 index 00000000..d7076f71 --- /dev/null +++ b/internal/paths.go @@ -0,0 +1,6 @@ +package internal + +// This variable exists to allow overwriting the path using `go build -ldflags "-X ...", see Makefile. +var ( + LibExecDir = "/usr/libexec" +)