From 9d62278c7285d94956efe3359bd0b64e8d6addad Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Wed, 19 Jun 2024 16:58:40 +0200 Subject: [PATCH 1/4] Add Makefile It's a very basic Makefile providing just the all/test/install/clean targets intended to avoid duplication of the build and installation steps in the Dockerfile and the rpm/deb packaging repos. /usr/sbin was chosen for the installation path because that's where the Icinga DB packages place the binary. By using the Makefile, the Docker image will now also include the default config and the schema files. --- .gitignore | 1 + Dockerfile | 13 ++++--------- Makefile | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..84c048a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/Dockerfile b/Dockerfile index c04d8807..c90f9385 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,20 +5,15 @@ ENV CGO_ENABLED 0 COPY . /src/icinga-notifications WORKDIR /src/icinga-notifications -RUN mkdir bin RUN --mount=type=cache,target=/go/pkg/mod \ --mount=type=cache,target=/root/.cache/go-build \ - go build -o bin/ ./cmd/icinga-notifications + make all -RUN mkdir bin/channel -RUN --mount=type=cache,target=/go/pkg/mod \ - --mount=type=cache,target=/root/.cache/go-build \ - go build -o bin/channel/ ./cmd/channel/... +RUN make DESTDIR=/target install FROM docker.io/library/alpine -COPY --from=build /src/icinga-notifications/bin/icinga-notifications /usr/bin/icinga-notifications -COPY --from=build /src/icinga-notifications/bin/channel /usr/libexec/icinga-notifications/channel +COPY --from=build /target / RUN apk add tzdata @@ -28,4 +23,4 @@ RUN adduser -u 1000 -H -D -G $username $username USER $username EXPOSE 5680 -CMD ["/usr/bin/icinga-notifications", "--config", "/etc/icinga-notifications/config.yml"] +CMD ["/usr/sbin/icinga-notifications", "--config", "/etc/icinga-notifications/config.yml"] diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..908b65c3 --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +# These variables follow the naming convention from the GNU Make documentation +# but their defaults correspond to the rest of the code (note that changing +# libexecdir here wouldn't affect the default path for the channel plugin +# directory used by the dameon for example). +# +# https://www.gnu.org/software/make/manual/html_node/Directory-Variables.html +prefix ?= /usr +sbindir ?= $(prefix)/sbin +libexecdir ?= $(prefix)/libexec +datadir ?= $(prefix)/share +sysconfdir ?= /etc + +all: + mkdir -p build + go build -o build/ ./cmd/icinga-notifications + go build -o build/channel/ ./cmd/channel/... + +install: + @# config + install -d $(DESTDIR)$(sysconfdir)/icinga-notifications + install -m644 config.example.yml $(DESTDIR)$(sysconfdir)/icinga-notifications/config.yml + @# dameon + install -D build/icinga-notifications $(DESTDIR)$(sbindir)/icinga-notifications + @# channel plugins + install -d $(DESTDIR)$(libexecdir)/icinga-notifications/channel + install build/channel/* $(DESTDIR)$(libexecdir)/icinga-notifications/channel/ + @# database schema + install -d $(DESTDIR)$(datadir)/icinga-notifications + cp -rv --no-dereference schema $(DESTDIR)$(datadir)/icinga-notifications + @# chmod ensures consistent permissions when cp is called with umask != 022 + chmod -R u=rwX,go=rX $(DESTDIR)$(datadir)/icinga-notifications/schema + + +clean: + rm -rf build + +.PHONY: all install clean From 1900a36766860942a458b8debb4a537f46031c9b Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Thu, 20 Jun 2024 14:36:48 +0200 Subject: [PATCH 2/4] add make test --- Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 908b65c3..aefb4599 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,9 @@ all: go build -o build/ ./cmd/icinga-notifications go build -o build/channel/ ./cmd/channel/... +test: + go test ./... + install: @# config install -d $(DESTDIR)$(sysconfdir)/icinga-notifications @@ -30,8 +33,7 @@ install: @# chmod ensures consistent permissions when cp is called with umask != 022 chmod -R u=rwX,go=rX $(DESTDIR)$(datadir)/icinga-notifications/schema - clean: rm -rf build -.PHONY: all install clean +.PHONY: all test install clean From 1bf100a2ac0ea9427380d29e7de026f16ca7c872 Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Thu, 20 Jun 2024 13:29:02 +0200 Subject: [PATCH 3/4] 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" +) From 9ad4cbcc5cc4964663e573d7dedee4d57a6a059b Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Thu, 20 Jun 2024 13:32:01 +0200 Subject: [PATCH 4/4] Add default path for config file Previously, the path to the config file had to be passed on the command line. This commit adds a sensible default so that this is no longer necessary. As the mechanism for setting the channel plugin dir based on libexecdir from the Makefile is already there, the same was used for sysconfdir, even though that should always be /etc for our packaging needs. --- Dockerfile | 2 +- Makefile | 2 +- cmd/icinga-notifications/main.go | 7 +------ internal/paths.go | 3 ++- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index c90f9385..086897bb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,4 +23,4 @@ RUN adduser -u 1000 -H -D -G $username $username USER $username EXPOSE 5680 -CMD ["/usr/sbin/icinga-notifications", "--config", "/etc/icinga-notifications/config.yml"] +CMD ["/usr/sbin/icinga-notifications"] diff --git a/Makefile b/Makefile index 3e7201c6..e41bbe51 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ all: mkdir -p build go build \ -o build/ \ - -ldflags "-X '$(pkg).LibExecDir=$(libexecdir)'" \ + -ldflags "-X '$(pkg).LibExecDir=$(libexecdir)' -X '$(pkg).SysConfDir=$(sysconfdir)'" \ ./cmd/icinga-notifications go build -o build/channel/ ./cmd/channel/... diff --git a/cmd/icinga-notifications/main.go b/cmd/icinga-notifications/main.go index 23d0bfa7..3dea365f 100644 --- a/cmd/icinga-notifications/main.go +++ b/cmd/icinga-notifications/main.go @@ -26,7 +26,7 @@ func main() { var configPath string var showVersion bool - flag.StringVar(&configPath, "config", "", "path to config file") + flag.StringVar(&configPath, "config", internal.SysConfDir+"/icinga-notifications/config.yml", "path to config file") flag.BoolVar(&showVersion, "version", false, "print version") flag.Parse() @@ -43,11 +43,6 @@ func main() { return } - if configPath == "" { - _, _ = fmt.Fprintln(os.Stderr, "missing -config flag") - os.Exit(1) - } - err := daemon.LoadConfig(configPath) if err != nil { _, _ = fmt.Fprintln(os.Stderr, "cannot load config:", err) diff --git a/internal/paths.go b/internal/paths.go index d7076f71..01f2681c 100644 --- a/internal/paths.go +++ b/internal/paths.go @@ -1,6 +1,7 @@ package internal -// This variable exists to allow overwriting the path using `go build -ldflags "-X ...", see Makefile. +// These variables exist to allow overwriting the paths using `go build -ldflags "-X ...", see Makefile. var ( LibExecDir = "/usr/libexec" + SysConfDir = "/etc" )