From b2e7a2cb61efb8c69cf5742bea2f4e974a1d51bc Mon Sep 17 00:00:00 2001 From: Stanislav Bogatyrev Date: Fri, 9 Apr 2021 18:29:18 +0300 Subject: [PATCH] Clean up Makefile and image build Signed-off-by: Stanislav Bogatyrev --- .gitignore | 4 +- .golangci.yml | 47 +++++++++++++++ Dockerfile | 28 +++------ Dockerfile.dirty | 8 +++ Makefile | 145 +++++++++++++++++++++++++++++++---------------- README.md | 27 ++++----- help.mk | 11 ---- misc.go | 1 + 8 files changed, 173 insertions(+), 98 deletions(-) create mode 100644 .golangci.yml create mode 100644 Dockerfile.dirty delete mode 100644 help.mk diff --git a/.gitignore b/.gitignore index b3199e5..405b1dd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ +.idea bin temp -cmd/test /plugins/ /vendor/ @@ -10,3 +10,5 @@ test.sh testfile .blast.yml .neofs-cli.yml + +.cache diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..ba892e7 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,47 @@ +# This file contains all available configuration options +# with their default values. + +# options for analysis running +run: + # timeout for analysis, e.g. 30s, 5m, default is 1m + timeout: 5m + + # include test files or not, default is true + tests: false + +# output configuration options +output: + # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" + format: tab + +# all available settings of specific linters +linters-settings: + exhaustive: + # indicates that switch statements are to be considered exhaustive if a + # 'default' case is present, even if all enum members aren't listed in the + # switch + default-signifies-exhaustive: true + govet: + # report about shadowed variables + check-shadowing: false + +linters: + enable: + # mandatory linters + - govet + - golint + + # some default golangci-lint linters + - errcheck + - gosimple + - ineffassign + - staticcheck + - typecheck + + # extra linters + - exhaustive + - gofmt + - whitespace + - goimports + disable-all: true + fast: false diff --git a/Dockerfile b/Dockerfile index ce850dc..187993e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,33 +1,23 @@ -FROM golang:1 as builder - +FROM golang:1.16-alpine as basebuilder +RUN apk add --update make bash ca-certificates +FROM basebuilder as builder ENV GOGC off ENV CGO_ENABLED 0 - -RUN set -x \ - && apt update \ - && apt install -y upx-ucl - +ARG BUILD=now +ARG VERSION=dev +ARG REPO=repository WORKDIR /src COPY . /src -ARG VERSION=dev -ENV LDFLAGS "-w -s -X main.Version=${VERSION}" -RUN set -x \ - && go build \ - -v \ - -mod=vendor \ - -trimpath \ - -ldflags "${LDFLAGS} -X main.Build=$(date -u +%s%N)" \ - -o /go/bin/neofs-gw ./ \ - && upx -3 /go/bin/neofs-gw +RUN make # Executable image FROM scratch WORKDIR / -COPY --from=builder /go/bin/neofs-gw /bin/neofs-gw COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=builder /src/bin/neofs-http-gw /bin/neofs-http-gw -ENTRYPOINT ["/bin/neofs-gw"] +ENTRYPOINT ["/bin/neofs-http-gw"] diff --git a/Dockerfile.dirty b/Dockerfile.dirty new file mode 100644 index 0000000..c17734b --- /dev/null +++ b/Dockerfile.dirty @@ -0,0 +1,8 @@ +FROM alpine +RUN apk add --update --no-cache bash ca-certificates + +WORKDIR / + +COPY bin/neofs-http-gw /bin/neofs-http-gw + +CMD ["neofs-http-gw"] diff --git a/Makefile b/Makefile index 0ec592e..77c7436 100644 --- a/Makefile +++ b/Makefile @@ -1,63 +1,108 @@ --include .env --include help.mk +#!/usr/bin/make -f -VERSION ?= "$(shell git describe --tags 2>/dev/null | sed 's/^v//')" +REPO ?= $(shell go list -m) +VERSION ?= $(shell git describe --tags --dirty --always) +BUILD ?= $(shell date -u --iso=seconds) +DEBUG ?= false -GRPC_VERSION=$(shell go list -m google.golang.org/grpc | cut -d " " -f 2) +HUB_IMAGE ?= nspccdev/neofs-http-gw +HUB_TAG ?= "$(shell echo ${VERSION} | sed 's/^v//')" -HUB_IMAGE=nspccdev/neofs +# List of binaries to build. For now just one. +BINDIR = bin +DIRS = $(BINDIR) +BINS = "$(BINDIR)/neofs-http-gw" -B=\033[0;1m -G=\033[0;92m -R=\033[0m +.PHONY: help all dep clean fmts fmt imports test lint docker/lint -.PHONY: version deps image publish +# Make all binaries +all: $(BINS) -# Show current version -version: - @echo "Current version: $(VERSION)-$(GRPC_VERSION)" +$(BINS): $(DIRS) dep + @echo "⇒ Build $@" + CGO_ENABLED=0 \ + GO111MODULE=on \ + go build -v -trimpath \ + -ldflags "-X main.Version=$(VERSION) \ + -X main.Build=$(BUILD) \ + -X main.Debug=$(DEBUG)" \ + -o $@ ./ -# Check and ensure dependencies -deps: - @printf "${B}${G}⇒ Ensure vendor${R}: " - @go mod tidy -v && echo OK || (echo fail && exit 2) - @printf "${B}${G}⇒ Download requirements${R}: " - @go mod download && echo OK || (echo fail && exit 2) - @printf "${B}${G}⇒ Store vendor localy${R}: " - @go mod vendor && echo OK || (echo fail && exit 2) +$(DIRS): + @echo "⇒ Ensure dir: $@" + @mkdir -p $@ -# Build docker image -image: VERSION?= -image: deps - @echo "${B}${G}⇒ Build GW docker-image with $(GRPC_VERSION) ${R}" +# Pull go dependencies +dep: + @printf "⇒ Download requirements: " + @CGO_ENABLED=0 \ + GO111MODULE=on \ + go mod download && echo OK + @printf "⇒ Tidy requirements: " + @CGO_ENABLED=0 \ + GO111MODULE=on \ + go mod tidy -v && echo OK + +# Run all code formatters +fmts: fmt imports + +# Reformat code +fmt: + @echo "⇒ Processing gofmt check" + @GO111MODULE=on gofmt -s -w ./ + +# Reformat imports +imports: + @echo "⇒ Processing goimports check" + @GO111MODULE=on goimports -w ./ + +# Build clean Docker image +image: + @echo "⇒ Build NeoFS HTTP Gateway docker image " @docker build \ + --build-arg REPO=$(REPO) \ --build-arg VERSION=$(VERSION) \ - -f Dockerfile \ - -t $(HUB_IMAGE)-http-gate:$(VERSION) . + --rm \ + -f Dockerfile \ + -t $(HUB_IMAGE):$(HUB_TAG) . -# Publish docker image -publish: - @echo "${B}${G}⇒ publish docker image ${R}" - @docker push $(HUB_IMAGE)-http-gate:$(VERSION) +# Build dirty Docker image +dirty-image: + @echo "⇒ Build NeoFS HTTP Gateway dirty docker image " + @docker build \ + --build-arg REPO=$(REPO) \ + --build-arg VERSION=$(VERSION) \ + --rm \ + -f Dockerfile.dirty \ + -t $(HUB_IMAGE)-dirty:$(HUB_TAG) . -.PHONY: dev +# Run linters +lint: + @golangci-lint --timeout=5m run -# Build development docker images -dev: VERSIONS?=$(GRPC_VERSION) -dev: - @echo "=> Build multiple images for $(VERSIONS)"; \ - git checkout go.{sum,mod}; \ - for v in $(VERSIONS); do \ - curdir=$$(pwd); \ - echo "=> Checkout gRPC to $${v}"; \ - cd ../grpc-go; \ - git checkout $${v} &> /dev/null || (echo "Release $${v} not found" && exit 2); \ - cd ../neofs-api; \ - git checkout go.{sum,mod}; \ - go get google.golang.org/grpc@$${v}; \ - cd $${curdir}; \ - cp go_dev.mod go.mod; \ - go get google.golang.org/grpc@$${v}; \ - make image VERSION=$(VERSION)-$${v}; \ - git checkout go.{sum,mod}; \ - done \ No newline at end of file +# Run linters in Docker +docker/lint: + docker run --rm -it \ + -v `pwd`:/src \ + -u `stat -c "%u:%g" .` \ + --env HOME=/src \ + golangci/golangci-lint:v1.39 bash -c 'cd /src/ && make lint' + +# Print version +version: + @echo $(VERSION) + +# Show this help prompt +help: + @echo ' Usage:' + @echo '' + @echo ' make ' + @echo '' + @echo ' Targets:' + @echo '' + @awk '/^#/{ comment = substr($$0,3) } comment && /^[a-zA-Z][a-zA-Z0-9_-]+ ?:/{ print " ", $$1, comment }' $(MAKEFILE_LIST) | column -t -s ':' | grep -v 'IGNORE' | sort -u + +# Clean up +clean: + rm -rf vendor + rm -rf $(BINDIR) diff --git a/README.md b/README.md index c28063a..537aeb3 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,18 @@ -# NeoFS HTTP Gate +# NeoFS HTTP Protocol Gateway -NeoFS HTTP Gate is example of tool that provides basic interactions with NeoFS. - -- you can download one file per request from NeoFS Network using NeoFS Gate -- you can upload one file per request into NeoFS Network using NeoFS Gate +NeoFS HTTP Protocol Gateway bridges NeoFS internal protocol and HTTP standard. +- you can download one file per request from NeoFS Network +- you can upload one file per request into the NeoFS Network ## Notable make targets ``` - Usage: - - make - - Targets: - - deps Check and ensure dependencies - dev Build development docker images - help Show this help prompt - image Build docker image - publish Publish docker image - version Show current version +dep Check and ensure dependencies +image Build clean docker image +dirty-image Build diry docker image with host-built binaries +fmts Run all code formatters +lint Run linters +version Show current version ``` ## Install diff --git a/help.mk b/help.mk deleted file mode 100644 index c065ec8..0000000 --- a/help.mk +++ /dev/null @@ -1,11 +0,0 @@ -.PHONY: help - -# Show this help prompt -help: - @echo ' Usage:' - @echo '' - @echo ' make ' - @echo '' - @echo ' Targets:' - @echo '' - @awk '/^#/{ comment = substr($$0,3) } comment && /^[a-zA-Z][a-zA-Z0-9_-]+ ?:/{ print " ", $$1, comment }' $(MAKEFILE_LIST) | column -t -s ':' | grep -v 'IGNORE' | sort | uniq diff --git a/misc.go b/misc.go index 1541334..50b8cb0 100644 --- a/misc.go +++ b/misc.go @@ -5,4 +5,5 @@ const Prefix = "HTTP_GW" var ( Build = "now" Version = "dev" + Debug = "false" )