From 9be4e55696948e9dec547a4e2bebdd99a0f74642 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Wed, 20 Nov 2024 01:09:29 +0100 Subject: [PATCH] Migrate to nfpm --- .gitignore | 2 +- Makefile | 93 +++++++++++++++++++++++------------------------------- nfpm.yaml | 34 ++++++++++++++++++++ 3 files changed, 75 insertions(+), 54 deletions(-) create mode 100644 nfpm.yaml diff --git a/.gitignore b/.gitignore index 55af516..36a336d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ graphite-ch-optimizer -build/ +out/ artifact/ diff --git a/Makefile b/Makefile index 140064e..cbd6392 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,23 @@ NAME = graphite-ch-optimizer MODULE = github.com/go-graphite/$(NAME) -VERSION = $(shell git describe --long --tags 2>/dev/null | sed 's/^v//;s/\([^-]*-g\)/c\1/;s/-/./g') -VENDOR = "System Administration " -URL = https://$(MODULE) -define DESC = +VERSION = $(shell git describe --always --tags 2>/dev/null | sed 's:^v::; s/\([^-]*-g\)/c\1/; s|-|.|g') +define DESCRIPTION = 'Service to optimize stale GraphiteMergeTree tables This software looking for tables with GraphiteMergeTree engine and evaluate if some of partitions should be optimized. It could work both as one-shot script and background daemon.' endef -GO_FILES = $(shell find -name '*.go') -PKG_FILES = build/$(NAME)_$(VERSION)_amd64.deb build/$(NAME)-$(VERSION)-1.x86_64.rpm -SUM_FILES = build/sha256sum build/md5sum +PKG_FILES = $(wildcard out/*$(VERSION)*.deb out/*$(VERSION)*.rpm ) +SUM_FILES = out/sha256sum out/md5sum GO ?= go +GO_VERSION = -ldflags "-X 'main.version=$(VERSION)'" ifeq ("$(CGO_ENABLED)", "0") GOFLAGS += -ldflags=-extldflags=-static endif export GOFLAGS += -mod=vendor export GO111MODULE := on +SRCS:=$(shell find . -name '*.go') + .PHONY: all clean docker test version all: build @@ -29,6 +29,7 @@ clean: rm -rf artifact rm -rf build rm -rf $(NAME) + rm -rf out rebuild: clean all @@ -41,11 +42,22 @@ build: | $(NAME) mkdir build docker: - docker build -t innogames/$(NAME):builder -f docker/builder/Dockerfile . - docker build -t innogames/$(NAME):latest -f docker/$(NAME)/Dockerfile . + docker build -t ghcr.io/go-graphite/$(NAME):latest . + +$(NAME): $(SRCS) + $(GO) build $(GO_VERSION) -o $@ . + +# we need it static +.PHONY: gox-build +gox-build: + @CGO_ENABLED=0 $(MAKE) out/$(NAME)-linux-amd64 out/$(NAME)-linux-arm64 -$(NAME): $(NAME).go - $(GO) build -ldflags "-X 'main.version=$(VERSION)'" -o $@ . +out/$(NAME)-linux-%: $(SRCS) | out + GOOS=linux GOARCH=$* $(GO) build $(GO_VERSION) -o $@ $(MODULE) + +out: out/done +out/done: + mkdir -p out/done ######################################################### # Prepare artifact directory and set outputs for upload # @@ -58,7 +70,7 @@ artifact: # Link artifact to directory with setting step output to filename artifact/%: ART=$(notdir $@) artifact/%: TYPE=$(lastword $(subst ., ,$(ART))) -artifact/%: build/% | artifact +artifact/%: out/% | artifact cp -l $< $@ @echo '::set-output name=$(TYPE)::$(ART)' @@ -71,54 +83,29 @@ artifact/%: build/% | artifact ############# # Prepare everything for packaging -.ONESHELL: -build/pkg: build/$(NAME) build/config.toml.example - cd build - mkdir -p pkg/etc/$(NAME) - mkdir -p pkg/usr/bin - cp -l $(NAME) pkg/usr/bin/ - cp -l config.toml.example pkg/etc/$(NAME) +out/config.toml.example: $(NAME) | out + ./$(NAME) --print-defaults > $@ -build/$(NAME): $(NAME).go - GOOS=linux GOARCH=amd64 $(GO) build -ldflags "-X 'main.version=$(VERSION)'" -o $@ . +nfpm: nfpm-deb nfpm-rpm -build/config.toml.example: build/$(NAME) - ./build/$(NAME) --print-defaults > $@ +PKG_ARCH = amd64 arm64 -packages: $(PKG_FILES) $(SUM_FILES) +nfpm-%: out/config.toml.example + $(MAKE) out/done/$(NAME)-$(VERSION)-amd64-$* ARCH=amd64 PACKAGER=$* + $(MAKE) out/done/$(NAME)-$(VERSION)-arm64-$* ARCH=arm64 PACKAGER=$* + +.ONESHELL: +out/done/$(NAME)-$(VERSION)%: nfpm.yaml | out/done gox-build + @NAME=$(NAME) DESCRIPTION=$(DESCRIPTION) ARCH=$(ARCH) VERSION_STRING=$(VERSION) nfpm package --packager $(PACKAGER) --target out/ + @touch $@ + +packages: nfpm $(SUM_FILES) # md5 and sha256 sum-files for packages $(SUM_FILES): COMMAND = $(notdir $@) $(SUM_FILES): PKG_FILES_NAME = $(notdir $(PKG_FILES)) -.ONESHELL: -$(SUM_FILES): $(PKG_FILES) - cd build - $(COMMAND) $(PKG_FILES_NAME) > $(COMMAND) - -deb: $(word 1, $(PKG_FILES)) - -rpm: $(word 2, $(PKG_FILES)) - -# Set TYPE to package suffix w/o dot -$(PKG_FILES): TYPE = $(subst .,,$(suffix $@)) -$(PKG_FILES): build/pkg - fpm --verbose \ - -s dir \ - -a x86_64 \ - -t $(TYPE) \ - --vendor $(VENDOR) \ - -m $(VENDOR) \ - --url $(URL) \ - --description $(DESC) \ - --license MIT \ - -n $(NAME) \ - -v $(VERSION) \ - --after-install packaging/postinst \ - --before-remove packaging/prerm \ - -p build \ - build/pkg/=/ \ - packaging/$(NAME).service=/lib/systemd/system/$(NAME).service - +$(SUM_FILES): nfpm + cd out && $(COMMAND) $(PKG_FILES_NAME) > $(COMMAND) ####### # END # ####### diff --git a/nfpm.yaml b/nfpm.yaml new file mode 100644 index 0000000..34393a0 --- /dev/null +++ b/nfpm.yaml @@ -0,0 +1,34 @@ +--- +name: ${NAME} +description: ${DESCRIPTION} + +# Common packages config +arch: "${ARCH}" # amd64, arm64 +platform: "linux" +version: "${VERSION_STRING}" +maintainer: &m "Mikhail f. Shiryaev " +vendor: *m +homepage: "https://github.com/go-graphite/${NAME}" +license: "MIT" +section: "admin" +priority: "optional" + +contents: + - src: "packaging/${NAME}.service" + dst: "/lib/systemd/system/${NAME}.service" + expand: true + - src: out/config.toml.example + dst: /etc/${NAME}/config.toml.example + type: config|noreplace + expand: true + - src: "out/${NAME}-linux-${ARCH}" + dst: /usr/bin/${NAME} + expand: true + # docs + - src: LICENSE + dst: /usr/share/doc/${NAME}/LICENSE + expand: true + +scripts: + postinstall: packaging/postinst + preremove: packaging/prerm