From f982c6d34f6d93a23b9180e9396e4de7e9620fe8 Mon Sep 17 00:00:00 2001 From: Norman Meier Date: Thu, 11 Jan 2024 14:05:42 +0100 Subject: [PATCH] fix: docker build Signed-off-by: Norman Meier --- .github/workflows/docker.yml | 30 +++++ .github/workflows/go.yml | 6 +- Dockerfile | 96 ++++++------- Makefile | 255 +++++++++++++---------------------- 4 files changed, 167 insertions(+), 220 deletions(-) create mode 100644 .github/workflows/docker.yml diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..b0e96e5 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,30 @@ +name: Docker + +on: + push: + branches: + - main + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Gather info + id: info + run: echo "tags=$(git describe --tags || git rev-parse HEAD)" >> "$GITHUB_OUTPUT" + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Build image + uses: docker/build-push-action@v5 + with: + context: . + push: false + platforms: linux/amd64 + tags: rg.nl-ams.scw.cloud/teritori/teritorid:${{ steps.info.outputs.tags }} diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 33675bd..53085fd 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -7,7 +7,7 @@ on: pull_request: jobs: - go: + test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 @@ -23,10 +23,10 @@ jobs: run: git diff --exit-code - name: Build - run: go build ./... + run: make build - name: Test - run: go test ./... + run: make test - name: Check diff run: git diff --exit-code diff --git a/Dockerfile b/Dockerfile index a2f922e..7a80979 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,66 +1,48 @@ -# syntax=docker/dockerfile:1 - -ARG GO_VERSION="1.19" -ARG RUNNER_IMAGE="gcr.io/distroless/static" - -# -------------------------------------------------------- -# Builder -# -------------------------------------------------------- - -FROM golang:${GO_VERSION}-alpine as builder - -RUN set -eux; apk add --no-cache ca-certificates build-base; apk add git linux-headers - -# Download go dependencies -WORKDIR /teritori -COPY go.* ./ -RUN --mount=type=cache,target=/root/.cache/go-build \ - --mount=type=cache,target=/root/go/pkg/mod \ - go mod download - -# Cosmwasm - download correct libwasmvm version -RUN WASMVM_VERSION=$(go list -m github.com/CosmWasm/wasmvm | cut -d ' ' -f 2) && \ - wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/libwasmvm_muslc.$(uname -m).a \ - -O /lib/libwasmvm_muslc.a - -# Cosmwasm - verify checksum -RUN wget https://github.com/CosmWasm/wasmvm/releases/download/v1.0.0/checksums.txt -O /tmp/checksums.txt && \ - sha256sum /lib/libwasmvm_muslc.a | grep $(cat /tmp/checksums.txt | grep $(uname -m) | cut -d ' ' -f 1) - -# Copy the remaining files -COPY . . - -# Build teritorid binary -RUN --mount=type=cache,target=/root/.cache/go-build \ - --mount=type=cache,target=/root/go/pkg/mod \ - VERSION=$(echo $(git describe --tags) | sed 's/^v//') && \ - COMMIT=$(git log -1 --format='%H') && \ - go build \ - -mod=readonly \ - -tags "netgo,ledger,muslc" \ - -ldflags "-X github.com/cosmos/cosmos-sdk/version.Name="teritori" \ - -X github.com/cosmos/cosmos-sdk/version.AppName="teritorid" \ - -X github.com/cosmos/cosmos-sdk/version.Version=$VERSION \ - -X github.com/cosmos/cosmos-sdk/version.Commit=$COMMIT \ - -X github.com/cosmos/cosmos-sdk/version.BuildTags='netgo,ledger,muslc' \ - -w -s -linkmode=external -extldflags '-Wl,-z,muldefs -static'" \ - -trimpath \ - -o /teritori/build/ \ - ./... +# docker build . -t rg.nl-ams.scw.cloud/teritori/teritorid:latest +# docker run --rm -it rg.nl-ams.scw.cloud/teritori/teritorid:latest /bin/sh +FROM golang:1.21-alpine3.17 AS go-builder +ARG arch=x86_64 + +# this comes from standard alpine nightly file +# https://github.com/rust-lang/docker-rust-nightly/blob/master/alpine3.12/Dockerfile +# with some changes to support our toolchain, etc +RUN set -eux; apk add --no-cache ca-certificates build-base; + +RUN apk add git +# NOTE: add these to run with LEDGER_ENABLED=true +# RUN apk add libusb-dev linux-headers + +WORKDIR /code +COPY . /code/ +# See https://github.com/CosmWasm/wasmvm/releases +ADD https://github.com/CosmWasm/wasmvm/releases/download/v1.5.1/libwasmvm_muslc.aarch64.a /lib/libwasmvm_muslc.aarch64.a +ADD https://github.com/CosmWasm/wasmvm/releases/download/v1.5.1/libwasmvm_muslc.x86_64.a /lib/libwasmvm_muslc.x86_64.a +RUN sha256sum /lib/libwasmvm_muslc.aarch64.a | grep b89c242ffe2c867267621a6469f07ab70fc204091809d9c6f482c3fdf9293830 +RUN sha256sum /lib/libwasmvm_muslc.x86_64.a | grep c0f4614d0835be78ac8f3d647a70ccd7ed9f48632bc1374db04e4df2245cb467 + +# Copy the library you want to the final location that will be found by the linker flag `-lwasmvm_muslc` +RUN cp /lib/libwasmvm_muslc.${arch}.a /lib/libwasmvm_muslc.a + +# force it to use static lib (from above) not standard libgo_cosmwasm.so file +RUN LEDGER_ENABLED=false BUILD_TAGS=muslc LINK_STATICALLY=true make build +RUN echo "Ensuring binary is statically linked ..." \ + && (file /code/build/teritorid | grep "statically linked") # -------------------------------------------------------- -# Runner -# -------------------------------------------------------- +FROM alpine:3.17 -FROM ${RUNNER_IMAGE} +COPY --from=go-builder /code/build/teritorid /usr/bin/teritorid -COPY --from=builder /teritori/build/teritorid /bin/teritorid +#COPY docker/* /opt/ +#RUN chmod +x /opt/*.sh -ENV HOME /teritori -WORKDIR $HOME +WORKDIR /opt +# rest server +EXPOSE 1317 +# tendermint p2p EXPOSE 26656 +# tendermint rpc EXPOSE 26657 -EXPOSE 1317 -ENTRYPOINT ["teritorid"] +CMD ["/usr/bin/teritorid", "version"] diff --git a/Makefile b/Makefile index 9efdf36..b6732fa 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,21 @@ #!/usr/bin/make -f -BRANCH := $(shell git rev-parse --abbrev-ref HEAD) -COMMIT := $(shell git log -1 --format='%H') - -# don't override user values -ifeq (,$(VERSION)) - VERSION := $(shell git describe --tags) - # if VERSION is empty, then populate it with branch's name and raw commit hash - ifeq (,$(VERSION)) - VERSION := $(BRANCH)-$(COMMIT) - endif -endif +#!/usr/bin/make -f PACKAGES_SIMTEST=$(shell go list ./... | grep '/simulation') +VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//') +COMMIT := $(shell git log -1 --format='%H') LEDGER_ENABLED ?= true SDK_PACK := $(shell go list -m github.com/cosmos/cosmos-sdk | sed 's/ /\@/g') -TM_VERSION := $(shell go list -m github.com/cometbft/cometbft | sed 's:.* ::') # grab everything after the space in "github.com/cometbft/cometbft v0.34.7" +BINDIR ?= $(GOPATH)/bin +SIMAPP = ./app +IMAGE_TAG=rg.nl-ams.scw.cloud/teritori/teritorid:${shell git describe --tags 2>/dev/null || echo dev-$(git rev-parse HEAD)} + +# for dockerized protobuf tools DOCKER := $(shell which docker) -BUILDDIR ?= $(CURDIR)/build -TEST_DOCKER_REPO=jackzampolin/teritoritest +BUF_IMAGE=bufbuild/buf@sha256:3cb1f8a4b48bd5ad8f09168f10f607ddc318af202f5c057d52a45216793d85e5 #v1.4.0 +DOCKER_BUF := $(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace $(BUF_IMAGE) +HTTPS_GIT := https://github.com/TERITORI/teritorid.git export GO111MODULE = on @@ -48,16 +45,16 @@ ifeq ($(LEDGER_ENABLED),true) endif endif -ifeq (cleveldb,$(findstring cleveldb,$(NXTPOP_BUILD_OPTIONS))) - build_tags += gcc cleveldb +ifeq ($(WITH_CLEVELDB),yes) + build_tags += gcc endif build_tags += $(BUILD_TAGS) build_tags := $(strip $(build_tags)) whitespace := -whitespace += $(whitespace) +empty = $(whitespace) $(whitespace) comma := , -build_tags_comma_sep := $(subst $(whitespace),$(comma),$(build_tags)) +build_tags_comma_sep := $(subst $(empty),$(comma),$(build_tags)) # process linker flags @@ -65,61 +62,49 @@ ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=teritori \ -X github.com/cosmos/cosmos-sdk/version.AppName=teritorid \ -X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \ -X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \ + -X github.com/TERITORI/teritorid/app.Bech32Prefix=tori \ -X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags_comma_sep)" \ - -X github.com/cometbft/cometbft/version.TMCoreSemVer=$(TM_VERSION) + -X github.com/cometbft/cometbft/version.TMCoreSemVer=$(TM_VERSION) -ifeq (cleveldb,$(findstring cleveldb,$(NXTPOP_BUILD_OPTIONS))) +ifeq ($(WITH_CLEVELDB),yes) ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=cleveldb endif -ifeq (,$(findstring nostrip,$(NXTPOP_BUILD_OPTIONS))) - ldflags += -w -s +ifeq ($(LINK_STATICALLY),true) + ldflags += -linkmode=external -extldflags "-Wl,-z,muldefs -static" endif ldflags += $(LDFLAGS) ldflags := $(strip $(ldflags)) -BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags)' -# check for nostrip option -ifeq (,$(findstring nostrip,$(NXTPOP_BUILD_OPTIONS))) - BUILD_FLAGS += -trimpath -endif - -#$(info $$BUILD_FLAGS is [$(BUILD_FLAGS)]) - -# The below include contains the tools target. +BUILD_FLAGS := -tags "$(build_tags_comma_sep)" -ldflags '$(ldflags)' -trimpath -############################################################################### -### Documentation ### -############################################################################### +# The below include contains the tools and runsim targets. +# include contrib/devtools/Makefile all: install lint test -BUILD_TARGETS := build install - -build: BUILD_ARGS=-o $(BUILDDIR)/ - -$(BUILD_TARGETS): go.sum $(BUILDDIR)/ - go $@ -mod=readonly $(BUILD_FLAGS) $(BUILD_ARGS) ./... +build: go.sum +ifeq ($(OS),Windows_NT) + $(error teritorid server not supported. Use "make build-windows-client" for client) + exit 1 +else + go build -mod=readonly $(BUILD_FLAGS) -o build/teritorid ./cmd/teritorid +endif -$(BUILDDIR)/: - mkdir -p $(BUILDDIR)/ +build-windows-client: go.sum + GOOS=windows GOARCH=amd64 go build -mod=readonly $(BUILD_FLAGS) -o build/teritorid.exe ./cmd/teritorid -build-reproducible: go.sum - $(DOCKER) rm latest-build || true - $(DOCKER) run --volume=$(CURDIR):/sources:ro \ - --env TARGET_PLATFORMS='linux/amd64 darwin/amd64 linux/arm64 windows/amd64' \ - --env APP=teritorid \ - --env VERSION=$(VERSION) \ - --env COMMIT=$(COMMIT) \ - --env LEDGER_ENABLED=$(LEDGER_ENABLED) \ - --name latest-build cosmossdk/rbuilder:latest - $(DOCKER) cp -a latest-build:/home/builder/artifacts/ $(CURDIR)/ +build-contract-tests-hooks: +ifeq ($(OS),Windows_NT) + go build -mod=readonly $(BUILD_FLAGS) -o build/contract_tests.exe ./cmd/contract_tests +else + go build -mod=readonly $(BUILD_FLAGS) -o build/contract_tests ./cmd/contract_tests +endif -build-linux: go.sum - LEDGER_ENABLED=false GOOS=linux GOARCH=amd64 $(MAKE) build +install: go.sum + go install -mod=readonly $(BUILD_FLAGS) ./cmd/teritorid -build-contract-tests-hooks: - mkdir -p $(BUILDDIR) - go build -mod=readonly $(BUILD_FLAGS) -o $(BUILDDIR)/ ./cmd/contract_tests +######################################## +### Tools & dependencies go-mod-cache: go.sum @echo "--> Download go modules to local cache" @@ -131,47 +116,20 @@ go.sum: go.mod draw-deps: @# requires brew install graphviz or apt-get install graphviz - go get github.com/RobotsAndPencils/goviz + go install github.com/RobotsAndPencils/goviz@latest @goviz -i ./cmd/teritorid -d 2 | dot -Tpng -o dependency-graph.png clean: - rm -rf $(BUILDDIR)/ artifacts/ + rm -rf snapcraft-local.yaml build/ distclean: clean rm -rf vendor/ -############################################################################### -### Devdoc ### -############################################################################### - -build-docs: - @cd docs && \ - while read p; do \ - (git checkout $${p} && npm install && VUEPRESS_BASE="/$${p}/" npm run build) ; \ - mkdir -p ~/output/$${p} ; \ - cp -r .vuepress/dist/* ~/output/$${p}/ ; \ - cp ~/output/$${p}/index.html ~/output ; \ - done < versions ; -.PHONY: build-docs - -sync-docs: - cd ~/output && \ - echo "role_arn = ${DEPLOYMENT_ROLE_ARN}" >> /root/.aws/config ; \ - echo "CI job = ${CIRCLE_BUILD_URL}" >> version.html ; \ - aws s3 sync . s3://${WEBSITE_BUCKET} --profile terraform --delete ; \ - aws cloudfront create-invalidation --distribution-id ${CF_DISTRIBUTION_ID} --profile terraform --path "/*" ; -.PHONY: sync-docs +######################################## +### Testing - -############################################################################### -### Tests & Simulation ### -############################################################################### - -include sims.mk - -test: test-unit test-build - -test-all: check test-race test-cover +test: test-unit +test-all: test-race test-cover test-system test-unit: @VERSION=$(VERSION) go test -mod=readonly -tags='ledger test_ledger_mock' ./... @@ -185,94 +143,71 @@ test-cover: benchmark: @go test -mod=readonly -bench=. ./... +test-sim-import-export: runsim + @echo "Running application import/export simulation. This may take several minutes..." + @$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 50 5 TestAppImportExport -############################################################################### -### Linting ### -############################################################################### +test-sim-multi-seed-short: runsim + @echo "Running short multi-seed application simulation. This may take awhile!" + @$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 50 5 TestFullAppSimulation -lint: - golangci-lint run - find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs gofmt -d -s +test-sim-deterministic: runsim + @echo "Running application deterministic simulation. This may take awhile!" + @$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 1 1 TestAppStateDeterminism -format: - find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./client/lcd/statik/statik.go" | xargs gofmt -w -s - find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./client/lcd/statik/statik.go" | xargs misspell -w - find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./client/lcd/statik/statik.go" | xargs goimports -w -local github.com/cosmos/cosmos-sdk +test-system: install + $(MAKE) -C tests/system/ test ############################################################################### -### Localnet ### +### Linting ### ############################################################################### -build-docker-teritoridnode: - $(MAKE) -C networks/local - -# Run a 4-node testnet locally -localnet-start: build-linux localnet-stop - @if ! [ -f build/node0/teritorid/config/genesis.json ]; then docker run --rm -v $(CURDIR)/build:/teritorid:Z tendermint/teritoridnode testnet --v 4 -o . --starting-ip-address 192.168.10.2 --keyring-backend=test ; fi - docker-compose up -d +format-tools: + go install mvdan.cc/gofumpt@v0.4.0 + go install github.com/client9/misspell/cmd/misspell@v0.3.4 + go install github.com/daixiang0/gci@v0.11.2 -# Stop testnet -localnet-stop: - docker-compose down +lint: format-tools + golangci-lint run --tests=false + find . -name '*.go' -type f -not -path "./vendor*" -not -path "./tests/system/vendor*" -not -path "*.git*" -not -path "*_test.go" | xargs gofumpt -d -test-docker: - @docker build -f contrib/Dockerfile.test -t ${TEST_DOCKER_REPO}:$(shell git rev-parse --short HEAD) . - @docker tag ${TEST_DOCKER_REPO}:$(shell git rev-parse --short HEAD) ${TEST_DOCKER_REPO}:$(shell git rev-parse --abbrev-ref HEAD | sed 's#/#_#g') - @docker tag ${TEST_DOCKER_REPO}:$(shell git rev-parse --short HEAD) ${TEST_DOCKER_REPO}:latest +format: format-tools + find . -name '*.go' -type f -not -path "./vendor*" -not -path "./tests/system/vendor*" -not -path "*.git*" -not -path "./client/lcd/statik/statik.go" | xargs gofumpt -w + find . -name '*.go' -type f -not -path "./vendor*" -not -path "./tests/system/vendor*" -not -path "*.git*" -not -path "./client/lcd/statik/statik.go" | xargs misspell -w + find . -name '*.go' -type f -not -path "./vendor*" -not -path "./tests/system/vendor*" -not -path "*.git*" -not -path "./client/lcd/statik/statik.go" | xargs gci write --skip-generated -s standard -s default -s "prefix(cosmossdk.io)" -s "prefix(github.com/cosmos/cosmos-sdk)" -s "prefix(github.com/TERITORI/teritorid)" --custom-order -test-docker-push: test-docker - @docker push ${TEST_DOCKER_REPO}:$(shell git rev-parse --short HEAD) - @docker push ${TEST_DOCKER_REPO}:$(shell git rev-parse --abbrev-ref HEAD | sed 's#/#_#g') - @docker push ${TEST_DOCKER_REPO}:latest -.PHONY: all build-linux install format lint \ - go-mod-cache draw-deps clean build \ - setup-transactions setup-contract-tests-data start-teritori run-lcd-contract-tests contract-tests \ - test test-all test-build test-cover test-unit test-race \ - benchmark \ - build-docker-teritoridnode localnet-start localnet-stop \ - docker-single-node - -protoVer=0.11.6 +############################################################################### +### Protobuf ### +############################################################################### +protoVer=0.14.0 protoImageName=ghcr.io/cosmos/proto-builder:$(protoVer) protoImage=$(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace $(protoImageName) +proto-all: proto-format proto-lint proto-gen format + proto-gen: @echo "Generating Protobuf files" @$(protoImage) sh ./scripts/protocgen.sh -init-hermes: kill-dev install - @echo "Initializing both blockchains..." - ./network/init.sh - ./network/start.sh - @echo "Initializing relayer..." - ./network/hermes/restore-keys.sh - ./network/hermes/create-conn.sh - -init-golang-rly: kill-dev install - @echo "Initializing both blockchains..." - ./network/init.sh - ./network/start.sh - @echo "Initializing relayer..." - ./network/relayer/interchain-acc-config/rly.sh - -start: - @echo "Starting up test network" - ./network/start.sh - -start-hermes: - ./network/hermes/start.sh - -start-rly: - ./network/hermes/start.sh - -kill-dev: - @echo "Killing icad and removing previous data" - -@rm -rf ./data - -@killall icad 2>/dev/null - -@killall teritorid 2>/dev/null - -IMAGE_TAG=rg.nl-ams.scw.cloud/teritori/teritorid:${shell git describe --tags 2>/dev/null || echo dev-$(git rev-parse HEAD)} +proto-format: + @echo "Formatting Protobuf files" + @$(protoImage) find ./ -name "*.proto" -exec clang-format -i {} \; + +proto-swagger-gen: + @./scripts/protoc-swagger-gen.sh + +proto-lint: + @$(DOCKER_BUF) lint --error-format=json + +proto-check-breaking: + @$(DOCKER_BUF) breaking --against $(HTTPS_GIT)#branch=main + +.PHONY: all install install-debug \ + go-mod-cache draw-deps clean build format \ + test test-all test-build test-cover test-unit test-race \ + test-sim-import-export build-windows-client \ + test-system .PHONY: docker.publish docker.publish: