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

Add support for wormchain and update the docker files to better support a custom build dir #134

Merged
merged 7 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
63 changes: 51 additions & 12 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,22 @@ func rawDockerfile(
}

// baseImageForGoVersion will determine the go version in go.mod and return the base image
func baseImageForGoVersion(
func baseImageForGoVersion(modFile *modfile.File) GoVersion {
goVersion := modFile.Go.Version
baseImageVersion := GetImageAndVersionForGoVersion(goVersion)
fmt.Printf("Go version from go.mod: %s, will build with version: %s image: %s\n", goVersion, baseImageVersion.Version, baseImageVersion.Image)

return baseImageVersion
}

func getModFile(
repoHost string,
organization string,
repoName string,
ref string,
buildDir string,
local bool,
) (GoVersion, error) {
) (*modfile.File, error) {
var goModBz []byte
var err error

Expand All @@ -165,7 +173,7 @@ func baseImageForGoVersion(
if local {
goModBz, err = os.ReadFile(goModPath)
if err != nil {
return GoVersion{}, fmt.Errorf("failed to read %s for local build: %w", goModPath, err)
return nil, fmt.Errorf("failed to read %s for local build: %w", goModPath, err)
}
} else {
// single branch depth 1 clone to only fetch most recent state of files
Expand All @@ -187,33 +195,57 @@ func baseImageForGoVersion(

_, err := git.Clone(memory.NewStorage(), fs, cloneOpts)
if err != nil {
return GoVersion{}, fmt.Errorf("failed to clone go.mod file to determine go version: %w", err)
return nil, fmt.Errorf("failed to clone go.mod file to determine go version: %w", err)
}
}

goModFile, err := fs.Open(goModPath)
if err != nil {
return GoVersion{}, fmt.Errorf("failed to open go.mod file: %w", err)
return nil, fmt.Errorf("failed to open go.mod file: %w", err)
}

goModBz, err = io.ReadAll(goModFile)
if err != nil {
return GoVersion{}, fmt.Errorf("failed to read go.mod file: %w", err)
return nil, fmt.Errorf("failed to read go.mod file: %w", err)
}
}

goMod, err := modfile.Parse("go.mod", goModBz, nil)
if err != nil {
return GoVersion{}, fmt.Errorf("failed to parse go.mod file: %w", err)
return nil, fmt.Errorf("failed to parse go.mod file: %w", err)
}

goVersion := goMod.Go.Version
baseImageVersion := GetImageAndVersionForGoVersion(goVersion)
fmt.Printf("Go version from go.mod: %s, will build with version: %s image: %s\n", goVersion, baseImageVersion.Version, baseImageVersion.Image)
return goMod, nil
}

return baseImageVersion, nil

// getWasmvmVersion will get the wasmvm version from the mod file
func getWasmvmVersion(modFile *modfile.File) string {
wasmvmRepo := "github.com/CosmWasm/wasmvm"
wasmvmVersion := ""

// First check all the "requires"
for _, item := range modFile.Require {
// Must have 2 tokens, repo & version
if (len(item.Syntax.Token) == 2) && (strings.Contains(item.Syntax.Token[0], wasmvmRepo)) {
wasmvmVersion = item.Syntax.Token[1]
}
}

// Then, check all the "replaces"
for _, item := range modFile.Replace {
// Must have 3 or more tokens
if (len(item.Syntax.Token) > 2) && (strings.Contains(item.Syntax.Token[0], wasmvmRepo)) {
wasmvmVersion = item.Syntax.Token[len(item.Syntax.Token)-1]
}
}

fmt.Println("WasmVM version from go.mod: ", wasmvmVersion)

return wasmvmVersion
}


// buildChainNodeDockerImage builds the requested chain node docker image
// based on the input configuration.
func (h *HeighlinerBuilder) buildChainNodeDockerImage(
Expand Down Expand Up @@ -319,10 +351,16 @@ func (h *HeighlinerBuilder) buildChainNodeDockerImage(

var baseVersion string

baseVer, err := baseImageForGoVersion(
modFile, err := getModFile(
repoHost, chainConfig.Build.GithubOrganization, chainConfig.Build.GithubRepo,
chainConfig.Ref, chainConfig.Build.BuildDir, h.local,
)
if err != nil {
return fmt.Errorf("error getting mod file: %w", err)
}

baseVer := baseImageForGoVersion(modFile)
wasmvmVersion := getWasmvmVersion(modFile)

race := ""

Expand Down Expand Up @@ -367,6 +405,7 @@ func (h *HeighlinerBuilder) buildChainNodeDockerImage(
"BUILD_DIR": chainConfig.Build.BuildDir,
"BUILD_TIMESTAMP": buildTimestamp,
"GO_VERSION": baseVer.Version,
"WASMVM_VERSION": wasmvmVersion,
"RACE": race,
}

Expand Down
13 changes: 13 additions & 0 deletions chains.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,19 @@
build-env:
- BUILD_TAGS=muslc

# Wormchain (Wormhole Gateway)
- name: wormchain
github-organization: wormhole-foundation
github-repo: wormhole
dockerfile: cosmos
build-target: |
BUILD_TAGS=netgo,muslc
LD_FLAGS="-s -w -X github.com/cosmos/cosmos-sdk/version.Name=wormchain -X github.com/cosmos/cosmos-sdk/version.Version=$(echo $(git describe --tags) | sed 's/^v//') -X github.com/cosmos/cosmos-sdk/version.Commit=$(git log -1 --format='%H') -X github.com/cosmos/cosmos-sdk/version.BuildTags=\"${BUILD_TAGS}\" -X github.com/cosmos/cosmos-sdk/version.ServerName=wormchaind"
go build -mod=readonly -tags="${BUILD_TAGS}" -ldflags="$LDFLAGS ${LD_FLAGS}" -o build/wormchaind cmd/wormchaind/main.go
build-dir: wormchain
binaries:
- wormchain/build/wormchaind

# Xion
- name: xion
github-organization: burnt-labs
Expand Down
6 changes: 3 additions & 3 deletions dockerfile/cosmos/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ARG BUILD_ENV
ARG BUILD_TAGS
ARG PRE_BUILD
ARG BUILD_DIR
ARG WASMVM_VERSION

RUN set -eux;\
LIBDIR=/lib;\
Expand All @@ -48,9 +49,8 @@ RUN set -eux;\
export CC=x86_64-linux-musl-gcc CXX=x86_64-linux-musl-g++;\
fi;\
fi;\
WASM_VERSION=$(go list -m all | grep github.com/CosmWasm/wasmvm | awk '{print $NF}');\
if [ ! -z "${WASM_VERSION}" ]; then\
wget -O $LIBDIR/libwasmvm_muslc.a https://github.com/CosmWasm/wasmvm/releases/download/${WASM_VERSION}/libwasmvm_muslc.$ARCH.a;\
if [ ! -z "${WASMVM_VERSION}" ]; then\
wget -O $LIBDIR/libwasmvm_muslc.a https://github.com/CosmWasm/wasmvm/releases/download/${WASMVM_VERSION}/libwasmvm_muslc.$ARCH.a;\
fi;\
export GOOS=linux GOARCH=$TARGETARCH CGO_ENABLED=1 LDFLAGS='-linkmode external -extldflags "-static"';\
if [ ! -z "$PRE_BUILD" ]; then sh -c "${PRE_BUILD}"; fi;\
Expand Down
12 changes: 5 additions & 7 deletions dockerfile/cosmos/local.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ ARG BUILDARCH
ARG GITHUB_ORGANIZATION
ARG REPO_HOST
ARG GITHUB_REPO
ARG WASMVM_VERSION

WORKDIR /go/src/${REPO_HOST}/${GITHUB_ORGANIZATION}/${GITHUB_REPO}

# Download dependencies and CosmWasm libwasmvm if found.
ADD go.mod go.sum ./
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this and L22 removed? We should be able to cache deps still, but it could be done as a phase after the libwasmvm download since that no longer depends on the go.mod being present.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulling those files does not work when there is a custom build directory

# Download CosmWasm libwasmvm if found.
RUN set -eux; \
export ARCH=$(uname -m); \
WASM_VERSION=$(go list -m all | grep github.com/CosmWasm/wasmvm | awk '{print $NF}'); \
if [ ! -z "${WASM_VERSION}" ]; then \
wget -O /lib/libwasmvm_muslc.a https://github.com/CosmWasm/wasmvm/releases/download/${WASM_VERSION}/libwasmvm_muslc.$(uname -m).a; \
fi; \
go mod download;
if [ ! -z "${WASMVM_VERSION}" ]; then \
wget -O /lib/libwasmvm_muslc.a https://github.com/CosmWasm/wasmvm/releases/download/${WASMVM_VERSION}/libwasmvm_muslc.$(uname -m).a; \
fi;

# Use minimal busybox from infra-toolkit image for final scratch image
FROM ghcr.io/strangelove-ventures/infra-toolkit:v0.0.7 AS infra-toolkit
Expand Down
8 changes: 3 additions & 5 deletions dockerfile/cosmos/native.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ RUN apk add --update --no-cache curl make git libc-dev bash gcc linux-headers eu

ARG TARGETARCH
ARG BUILDARCH


ARG GITHUB_ORGANIZATION
ARG REPO_HOST

Expand All @@ -25,12 +23,12 @@ ARG BUILD_ENV
ARG BUILD_TAGS
ARG PRE_BUILD
ARG BUILD_DIR
ARG WASMVM_VERSION

RUN set -eux;\
export ARCH=$(uname -m);\
WASM_VERSION=$(go list -m all | grep github.com/CosmWasm/wasmvm | awk '{print $NF}');\
if [ ! -z "${WASM_VERSION}" ]; then\
wget -O /lib/libwasmvm_muslc.a https://github.com/CosmWasm/wasmvm/releases/download/${WASM_VERSION}/libwasmvm_muslc.$(uname -m).a;\
if [ ! -z "${WASMVM_VERSION}" ]; then\
wget -O /lib/libwasmvm_muslc.a https://github.com/CosmWasm/wasmvm/releases/download/${WASMVM_VERSION}/libwasmvm_muslc.$(uname -m).a;\
fi;\
export CGO_ENABLED=1 LDFLAGS='-linkmode external -extldflags "-static"';\
if [ ! -z "$PRE_BUILD" ]; then sh -c "${PRE_BUILD}"; fi;\
Expand Down
Loading