Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPM target + check working on CentOS #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.17

- name: Run tests
run: |
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor
.idea
.vscode
bin
bin
deploy/rpm/artifacts
102 changes: 88 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ $(value $(shell [ ! -d "$(CURDIR)/bin" ] && mkdir -p "$(CURDIR)/bin"))
export GOBIN=$(CURDIR)/bin
GOLANGCI_BIN:=$(GOBIN)/golangci-lint
GOLANGCI_REPO=https://github.com/golangci/golangci-lint
GOLANGCI_LATEST_VERSION:= $(shell git ls-remote --tags --refs --sort='v:refname' $(GOLANGCI_REPO)|tail -1|egrep -E -o "v\d+\.\d+\..*")
GOLANGCI_LATEST_VERSION:= $(shell git ls-remote --tags --refs --sort='v:refname' $(GOLANGCI_REPO)|tail -1|egrep -o "v[0-9]+.*")
NFPM_BIN:=$(GOBIN)/nfpm
DEPLOY:=$(CURDIR)/deploy


GIT_TAG:=$(shell git describe --exact-match --abbrev=0 --tags 2> /dev/null)
GIT_HASH:=$(shell git log --format="%h" -n 1 2> /dev/null)
GIT_BRANCH:=$(shell git branch 2> /dev/null | grep '*' | cut -f2 -d' ')
GO_VERSION:=$(shell go version | sed -E 's/.* go(.*) .*/\1/g')
BUILD_TS:=$(shell date +%FT%T%z)
VERSION:=$(shell cat ./VERSION 2> /dev/null | sed -n "1p")
APP_NAME:=crispy/dummy

APP:=dummy
PROJECT:=crispy
APP_NAME=$(PROJECT)-$(APP)
APP_VERSION:=$(if $(VERSION),$(VERSION),$(if $(GIT_TAG),$(GIT_TAG),$(GIT_BRANCH)))
APP_MAIN:=$(CURDIR)/cmd/$(APP)
APP_BIN?=$(CURDIR)/bin/$(APP)
APP_VERSION:=$(if $(VERSION),$(VERSION),$(if $(GIT_TAG),$(GIT_TAG),$(GIT_BRANCH)))


Expand Down Expand Up @@ -55,8 +64,8 @@ lint: install-linter
# install project dependencies
.PHONY: go-deps
go-deps:
$(info Install dependencies...)
@go mod tidy && go mod vendor && go mod verify
$(info Check go modules dependencies...)
@go mod tidy && go mod vendor && go mod verify && echo "success"

.PHONY: bin-tools
bin-tools:
Expand Down Expand Up @@ -86,6 +95,7 @@ generate: bin-tools
@PATH=$(PATH):$(GOBIN) && \
protoc -I $(CURDIR)/vendor/github.com/grpc-ecosystem/grpc-gateway/v2/ \
-I $(CURDIR)/3d-party \
-I /usr/local/include \
--go_opt=paths=source_relative \
--go-grpc_opt=paths=source_relative \
--go_out $(CURDIR)/pkg \
Expand All @@ -97,6 +107,7 @@ generate: bin-tools
dummy/dummy.proto && \
protoc -I $(CURDIR)/vendor/github.com/grpc-ecosystem/grpc-gateway/v2/ \
-I $(CURDIR)/3d-party \
-I /usr/local/include \
--proto_path=$(CURDIR)/api \
--openapiv2_out $(CURDIR)/internal/api \
--openapiv2_opt logtostderr=true \
Expand All @@ -108,18 +119,81 @@ test:
@go clean -testcache && go test -v ./...


DUMMY-MAIN:=$(CURDIR)/cmd/dummy
DUMMY-BIN:=$(CURDIR)/bin/dummy
.PHONY: $(APP)
$(APP): go-deps
$(info ENV:[$(BUILD_ENV)] GC_FLAGS:[$(GC_FLAGS)] OUT:"$(APP_BIN)")
@echo "building '$(APP)'..." && \
$(BUILD_ENV) go build -ldflags="$(LDFLAGS)" $(GC_FLAGS) -o $(APP_BIN) $(APP_MAIN) && \
echo "success"


.PHONY: $(APP)-dbg
$(APP)-dbg: GC_FLAGS:=-gcflags="all=-N -l"
$(APP)-dbg: APP_BIN=$(CURDIR)/bin/$(APP)-dbg
$(APP)-dbg: $(APP)
@echo "" > /dev/null

.PHONY: build-dummy
build-dummy: go-deps
$(info building 'dummy' server...)
@go build -ldflags="$(LDFLAGS)" -o $(DUMMY-BIN) $(DUMMY-MAIN)

.PHONY: build-dummy-d
build-dummy-d:
$(info building 'dummy-debug' server...)
@go build -ldflags="$(LDFLAGS)" -gcflags="all=-N -l" -o $(DUMMY-BIN)-dbg $(DUMMY-MAIN)
.PHONY: $(APP)-linux
$(APP)-linux: BUILD_ENV=env GOOS=linux GOARCH=amd64
$(APP)-linux: $(APP)
@echo "" > /dev/null


.PHONY: install-npfm
install-npfm:
ifeq ($(wildcard $(NFPM_BIN)),)
$(info install 'npfm' tool...)
@go install github.com/goreleaser/nfpm/v2/cmd/nfpm@latest && echo "success"
endif
@$(NFPM_BIN) >/dev/null


.PHONY: rpm
rpm: NPFM-CONF:=$(shell mktemp -u nfmp-XXXXXXXXXX).yaml
rpm: RPM=$(DEPLOY)/rpm
rpm: ARTIFACTS=$(RPM)/artifacts
rpm: APP_BIN=$(ARTIFACTS)/$(APP_NAME)
rpm: install-npfm
@rm -rf $(ARTIFACTS) 2>/dev/null && mkdir -p $(ARTIFACTS) && \
cat $(RPM)/.service-config.yaml | \
sed -e 's/<service>/$(APP_NAME)/' \
> $(ARTIFACTS)/$(APP_NAME).yaml && \
cat $(RPM)/.service-unit.service | \
sed -e 's/<service>/$(APP_NAME)/g' \
-e 's/<app>/$(APP)/g' \
-e 's/<project>/$(PROJECT)/g' \
> $(ARTIFACTS)/$(APP_NAME).service && \
cat $(RPM)/.postinstall.sh | \
sed -e 's/<service>/$(APP_NAME)/g' \
-e 's/<app>/$(APP)/g' \
-e 's/<project>/$(PROJECT)/g' \
> $(ARTIFACTS)/postinstall.sh && \
cat $(RPM)/.preinstall.sh | \
sed -e 's/<service>/$(APP_NAME)/g' \
-e 's/<app>/$(APP)/g' \
-e 's/<project>/$(PROJECT)/g' \
> $(ARTIFACTS)/preinstall.sh && \
cat $(RPM)/.postremove.sh | \
sed -e 's/<service>/$(APP_NAME)/g' \
-e 's/<app>/$(APP)/g' \
-e 's/<project>/$(PROJECT)/g' \
> $(ARTIFACTS)/postremove.sh && \
cat $(RPM)/.preremove.sh | \
sed -e 's/<service>/$(APP_NAME)/g' \
-e 's/<app>/$(APP)/g' \
-e 's/<project>/$(PROJECT)/g' \
> $(ARTIFACTS)/preremove.sh && \
cat $(DEPLOY)/.packager-config.yaml | \
sed -e 's;<name>;$(APP_NAME);g' \
-e 's/<version>/$(VERSION)/g' \
-e 's;<artifacts>;$(ARTIFACTS);g' \
-e 's/<service>/$(APP_NAME)/g' \
-e 's/<app>/$(APP)/g' \
-e 's/<project>/$(PROJECT)/g' \
> $(ARTIFACTS)/$(NPFM-CONF) && \
env APP_BIN=$(APP_BIN) $(MAKE) $(APP)-linux && \
echo "building '$@'..." && \
$(NFPM_BIN) pkg --config="$(ARTIFACTS)/$(NPFM-CONF)" --packager=rpm --target="$(ARTIFACTS)" && \
echo "success"

2 changes: 1 addition & 1 deletion api/dummy/dummy.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
version: "2.0";
contact: {
name: "Pavel Fiskovich";
url: "https://blog.bullgare.com/2020/07/complete-list-of-swagger-options-to-protobuf-file";
url: "https://khannz.medium.com/about";
};
};
schemes: [HTTP];
Expand Down
13 changes: 11 additions & 2 deletions cmd/dummy/dummy-service-main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"runtime"

"github.com/gradusp/crispy-dummy/internal/app"
"github.com/gradusp/crispy-dummy/internal/config"
Expand All @@ -17,15 +18,23 @@ func main() {
ctx := app.Context()
logger.SetLevel(zap.InfoLevel)
logger.Info(ctx, "--== HELLO ==--")
err := config.InitGlobalConfig(

confOpts := []config.Option{
config.WithAcceptEnvironment{EnvPrefix: "DUMMY"},
config.WithSourceFile{FileName: app.ConfigFile},
config.WithDefValue{Key: app.LoggerLevel, Val: "INFO"},
config.WithDefValue{Key: app.MetricsEnable, Val: false},
config.WithDefValue{Key: app.TraceEnable, Val: false},
config.WithDefValue{Key: app.ServerGracefulShutdown, Val: "10s"},
config.WithDefValue{Key: app.ServerEndpoint, Val: "tcp://127.0.0.1:9001"},
)
}
switch runtime.GOOS {
case "linux":
confOpts = append(confOpts, config.WithDefValue{Key: app.AnnounceInterfaceName, Val: "lo"})
case "darwin":
confOpts = append(confOpts, config.WithDefValue{Key: app.AnnounceInterfaceName, Val: "lo0"})
}
err := config.InitGlobalConfig(confOpts...)
if err != nil {
logger.Fatal(ctx, err)
}
Expand Down
60 changes: 60 additions & 0 deletions deploy/.packager-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# nfpm example config file
#
# check https://nfpm.goreleaser.com/configuration for detailed usage
#
name: "<name>"
arch: amd64
platform: linux
version: "<version>"
##section: "default"
#priority: "extra"
#replaces:
#- foobar
#provides:
#- sber
#depends:
#- foo
#- bar
#recommends:
#- whatever
#suggests:
#- something-else
#conflicts:
#- not-foo
#- not-bar
maintainer: "Pavel Fiskovich"
description: <project> <app> service
vendor: Sber
#homepage: "http://example.com"
license: "MIT?"
#changelog: "changelog.yaml"
#depends:
#- ipvsadm
rpm:
arch: "x86_64"
overrides:
rpm:
contents:
- dst: /opt/<project>
type: dir

- src: "<artifacts>/<service>"
dst: /opt/<project>/<service>

- src: "<artifacts>/<service>.yaml"
dst: /opt/<project>/<service>.yaml
type: config

- src: "<artifacts>/<service>.service"
dst: /lib/systemd/system/<service>.service

- dst: /var/run/<service>.sock
type: ghost
file_info:
mode: 776

scripts:
preinstall: "<artifacts>/preinstall.sh"
postinstall: "<artifacts>/postinstall.sh"
preremove: "<artifacts>/preremove.sh"
postremove: "<artifacts>/postremove.sh"
2 changes: 2 additions & 0 deletions deploy/rpm/.postinstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
systemctl daemon-reload
systemctl enable --now <service>.service
8 changes: 8 additions & 0 deletions deploy/rpm/.postremove.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if [ "$1" -ge 1 ]; then
echo "$1"
fi
if [ "$1" = 0 ]; then
systemctl daemon-reload
[ -e "/var/run/<service>.sock" ] && rm "/var/run/<service>.sock"
fi
exit 0
1 change: 1 addition & 0 deletions deploy/rpm/.preinstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit 0
6 changes: 6 additions & 0 deletions deploy/rpm/.preremove.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if [ "$1" -ge 1 ]; then
systemctl stop <service>.service
fi
if [ "$1" = 0 ]; then
systemctl disable --now <service>.service
fi
14 changes: 14 additions & 0 deletions deploy/rpm/.service-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ---

trace:
enable: false

metrics:
enable: true

services:
announce:
interface-name: dummy0

server:
endpoint: unix:///var/run/<service>.sock
20 changes: 20 additions & 0 deletions deploy/rpm/.service-unit.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[Unit]
Description=<project> <app> service
ConditionPathExists=/opt/<project>/<service>
After=network.target

[Service]
Type=simple
User=root
Group=root
LimitNOFILE=1024
Restart=on-failure
RestartSec=10
WorkingDirectory=/opt/<project>
ExecStart=/opt/<project>/<service> -config=<service>.yaml
KillSignal=SIGINT
SendSIGKILL=no
PermissionsStartOnly=true

[Install]
WantedBy=multi-user.target
Loading