Skip to content

Commit

Permalink
Migrate to nfpm
Browse files Browse the repository at this point in the history
  • Loading branch information
Felixoid committed Nov 20, 2024
1 parent e993bb9 commit 9be4e55
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
graphite-ch-optimizer
build/
out/
artifact/
93 changes: 40 additions & 53 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"
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
Expand All @@ -29,6 +29,7 @@ clean:
rm -rf artifact
rm -rf build
rm -rf $(NAME)
rm -rf out

rebuild: clean all

Expand 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 #
Expand All @@ -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)'

Expand All @@ -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 #
#######
34 changes: 34 additions & 0 deletions nfpm.yaml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"
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

0 comments on commit 9be4e55

Please sign in to comment.