Skip to content

Commit 0cb30c8

Browse files
Add docker image for example MorpheusVM (#1915)
1 parent 9d4618a commit 0cb30c8

File tree

6 files changed

+187
-13
lines changed

6 files changed

+187
-13
lines changed

.dockerignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.ci
2+
.github
3+
.gitignore
4+
.golangci.yml
5+
6+
.idea
7+
.vscode
8+
9+
LICENSE
10+
*.md
11+
12+
# Ignore all dockerfiles not just the main one
13+
Dockerfile*

.github/workflows/hypersdk-ci.yml

+13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ jobs:
2626
- shell: bash
2727
run: scripts/tests.clean.sh
2828

29+
build_image:
30+
name: Image build
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/checkout@v4
34+
- name: Install qemu (required for cross-platform builds)
35+
run: |
36+
sudo apt update
37+
sudo apt -y install qemu-system qemu-user-static
38+
- name: Build MorpheusVM Docker Image
39+
shell: bash
40+
run: bash -x scripts/build_docker_image.sh
41+
2942
hypersdk-lint:
3043
runs-on: ubuntu-latest
3144
steps:

Dockerfile

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# syntax=docker/dockerfile:experimental
2+
3+
# ============= Setting up base Stage ================
4+
# AVALANCHEGO_NODE_IMAGE needs to identify an existing node image and should include the tag
5+
ARG AVALANCHEGO_NODE_IMAGE
6+
ARG VM_ID
7+
ARG VM_COMMIT
8+
ARG CURRENT_BRANCH
9+
10+
# ============= Compilation Stage ================
11+
FROM --platform=$BUILDPLATFORM golang:1.22.8-bullseye AS builder
12+
13+
WORKDIR /build
14+
15+
# Copy the code into the container
16+
COPY . .
17+
18+
ARG VM_NAME
19+
20+
WORKDIR /build/examples/$VM_NAME/
21+
22+
# Ensure pre-existing builds are not available for inclusion in the final image
23+
RUN [ -d ./build ] && rm -rf ./build/* || true
24+
25+
RUN export VM_COMMIT=$VM_COMMIT && export CURRENT_BRANCH=$CURRENT_BRANCH && ./scripts/build.sh /build/build/vm
26+
27+
# ============= Cleanup Stage ================
28+
FROM $AVALANCHEGO_NODE_IMAGE AS builtImage
29+
30+
# Copy the evm binary into the correct location in the container
31+
ARG VM_ID
32+
COPY --from=builder /build/build/vm /avalanchego/build/plugins/$VM_ID

examples/morpheusvm/scripts/run.sh

+8-11
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,34 @@ source ../../scripts/constants.sh
1717
# shellcheck source=/scripts/common/utils.sh
1818
source ../../scripts/common/utils.sh
1919

20-
VERSION=13c08681c17d0790a94ed8c8ef8a3c88f8bb196d
21-
22-
############################
2320
# build avalanchego
2421
# https://github.com/ava-labs/avalanchego/releases
2522
HYPERSDK_DIR=$HOME/.hypersdk
2623

2724
echo "working directory: $HYPERSDK_DIR"
2825

29-
AVALANCHEGO_PATH=${HYPERSDK_DIR}/avalanchego-${VERSION}/avalanchego
30-
AVALANCHEGO_PLUGIN_DIR=${HYPERSDK_DIR}/avalanchego-${VERSION}/plugins
26+
AVALANCHEGO_PATH=${HYPERSDK_DIR}/avalanchego-${AVALANCHE_VERSION}/avalanchego
27+
AVALANCHEGO_PLUGIN_DIR=${HYPERSDK_DIR}/avalanchego-${AVALANCHE_VERSION}/plugins
3128

3229
if [ ! -f "$AVALANCHEGO_PATH" ]; then
3330
echo "building avalanchego"
3431
CWD=$(pwd)
3532

3633
# Clear old folders
37-
rm -rf "${HYPERSDK_DIR}"/avalanchego-"${VERSION}"
38-
mkdir -p "${HYPERSDK_DIR}"/avalanchego-"${VERSION}"
34+
rm -rf "${HYPERSDK_DIR}"/avalanchego-"${AVALANCHE_VERSION}"
35+
mkdir -p "${HYPERSDK_DIR}"/avalanchego-"${AVALANCHE_VERSION}"
3936
rm -rf "${HYPERSDK_DIR}"/avalanchego-src
4037
mkdir -p "${HYPERSDK_DIR}"/avalanchego-src
4138

4239
# Download src
4340
cd "${HYPERSDK_DIR}"/avalanchego-src
4441
git clone https://github.com/ava-labs/avalanchego.git
4542
cd avalanchego
46-
git checkout "${VERSION}"
43+
git checkout "${AVALANCHE_VERSION}"
4744

4845
# Build avalanchego
4946
./scripts/build.sh
50-
mv build/avalanchego "${HYPERSDK_DIR}"/avalanchego-"${VERSION}"
47+
mv build/avalanchego "${HYPERSDK_DIR}"/avalanchego-"${AVALANCHE_VERSION}"
5148

5249
cd "${CWD}"
5350

@@ -62,11 +59,11 @@ fi
6259
echo "building morpheusvm"
6360

6461
# delete previous (if exists)
65-
rm -f "${HYPERSDK_DIR}"/avalanchego-"${VERSION}"/plugins/qCNyZHrs3rZX458wPJXPJJypPf6w423A84jnfbdP2TPEmEE9u
62+
rm -f "${HYPERSDK_DIR}"/avalanchego-"${AVALANCHE_VERSION}"/plugins/qCNyZHrs3rZX458wPJXPJJypPf6w423A84jnfbdP2TPEmEE9u
6663

6764
# rebuild with latest code
6865
go build \
69-
-o "${HYPERSDK_DIR}"/avalanchego-"${VERSION}"/plugins/qCNyZHrs3rZX458wPJXPJJypPf6w423A84jnfbdP2TPEmEE9u \
66+
-o "${HYPERSDK_DIR}"/avalanchego-"${AVALANCHE_VERSION}"/plugins/qCNyZHrs3rZX458wPJXPJJypPf6w423A84jnfbdP2TPEmEE9u \
7067
./cmd/morpheusvm
7168

7269
############################

scripts/build_docker_image.sh

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env bash
2+
# Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
3+
# See the file LICENSE for licensing terms.
4+
5+
set -euo pipefail
6+
7+
# If set to non-empty, prompts the building of a multi-arch image when the image
8+
# name indicates use of a registry.
9+
#
10+
# A registry is required to build a multi-arch image since a multi-arch image is
11+
# not really an image at all. A multi-arch image (also called a manifest) is
12+
# basically a list of arch-specific images available from the same registry that
13+
# hosts the manifest. Manifests are not supported for local images.
14+
#
15+
# Reference: https://docs.docker.com/build/building/multi-platform/
16+
PLATFORMS="${PLATFORMS:-}"
17+
18+
# If set to non-empty, the image will be published to the registry.
19+
PUBLISH="${PUBLISH:-}"
20+
21+
# The name of the VM to build. Defaults to build morpheusvm in examples/morpheusvm/
22+
VM_NAME=${VM_NAME:-"morpheusvm"}
23+
24+
# Directory above this script
25+
HYPERSDK_PATH=$(
26+
cd "$(dirname "${BASH_SOURCE[0]}")"
27+
cd .. && pwd
28+
)
29+
VM_PATH=${VM_PATH:-"${HYPERSDK_PATH}/examples/${VM_NAME}"}
30+
31+
# Load the constants
32+
source "$HYPERSDK_PATH"/scripts/constants.sh
33+
34+
# WARNING: this will use the most recent commit even if there are un-committed changes present
35+
BUILD_IMAGE_ID=${BUILD_IMAGE_ID:-"${CURRENT_BRANCH}"}
36+
37+
# buildx (BuildKit) improves the speed and UI of builds over the legacy builder and
38+
# simplifies creation of multi-arch images.
39+
#
40+
# Reference: https://docs.docker.com/build/buildkit/
41+
DOCKER_CMD="docker buildx build"
42+
43+
if [[ -n "${PUBLISH}" ]]; then
44+
DOCKER_CMD="${DOCKER_CMD} --push"
45+
46+
echo "Pushing $DOCKERHUB_REPO:$BUILD_IMAGE_ID"
47+
48+
# A populated DOCKER_USERNAME env var triggers login
49+
if [[ -n "${DOCKER_USERNAME:-}" ]]; then
50+
echo "$DOCKER_PASS" | docker login --username "$DOCKER_USERNAME" --password-stdin
51+
fi
52+
fi
53+
54+
# Build a multi-arch image if requested
55+
if [[ -n "${PLATFORMS}" ]]; then
56+
DOCKER_CMD="${DOCKER_CMD} --platform=${PLATFORMS}"
57+
fi
58+
59+
VM_ID=${VM_ID:-"${DEFAULT_VM_ID}"}
60+
if [[ "${VM_ID}" != "${DEFAULT_VM_ID}" ]]; then
61+
DOCKERHUB_TAG="${VM_ID}-${DOCKERHUB_TAG}"
62+
fi
63+
64+
# Default to the release image. Will need to be overridden when testing against unreleased versions.
65+
AVALANCHEGO_NODE_IMAGE="${AVALANCHEGO_NODE_IMAGE:-${AVALANCHEGO_IMAGE_NAME}:${AVALANCHE_DOCKER_VERSION}}"
66+
67+
echo "Building Docker Image: $DOCKERHUB_REPO:$BUILD_IMAGE_ID based off AvalancheGo@$AVALANCHE_DOCKER_VERSION"
68+
${DOCKER_CMD} -t "$DOCKERHUB_REPO:$BUILD_IMAGE_ID" \
69+
"$HYPERSDK_PATH" -f "$HYPERSDK_PATH/Dockerfile" \
70+
--build-arg AVALANCHEGO_NODE_IMAGE="$AVALANCHEGO_NODE_IMAGE" \
71+
--build-arg VM_COMMIT="$VM_COMMIT" \
72+
--build-arg CURRENT_BRANCH="$CURRENT_BRANCH" \
73+
--build-arg VM_ID="$VM_ID" \
74+
--build-arg VM_NAME="$VM_NAME"
75+
76+
if [[ -n "${PUBLISH}" && $CURRENT_BRANCH == "master" ]]; then
77+
echo "Tagging current image as $DOCKERHUB_REPO:latest"
78+
docker buildx imagetools create -t "$DOCKERHUB_REPO:latest" "$DOCKERHUB_REPO:$BUILD_IMAGE_ID"
79+
fi

scripts/constants.sh

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
11
#!/usr/bin/env bash
22
# Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
33
# See the file LICENSE for licensing terms.
4+
# Ignore warnings about variables appearing unused since this file is not the consumer of the variables it defines.
5+
# shellcheck disable=SC2034
6+
7+
set -euo pipefail
8+
9+
AVALANCHE_VERSION=${AVALANCHE_VERSION:-'13c08681c17d0790a94ed8c8ef8a3c88f8bb196d'}
10+
# Optionally specify a separate version of AvalancheGo for building docker images
11+
# Added to support the case there's no such docker image for the specified commit of AvalancheGo
12+
AVALANCHE_DOCKER_VERSION=${AVALANCHE_DOCKER_VERSION:-'v1.12.2'}
13+
14+
# Set the PATHS
15+
GOPATH="$(go env GOPATH)"
16+
DEFAULT_VM_ID="pkEmJQuTUic3dxzg8EYnktwn4W7uCHofNcwiYo458vodAUbY7"
17+
18+
# Avalabs docker hub
19+
# avaplatform/avalanchego - defaults to local as to avoid unintentional pushes
20+
# You should probably set it - export DOCKER_REPO='avaplatform/subnet-evm'
21+
DOCKERHUB_REPO=${DOCKER_REPO:-"morpheusvm"}
22+
23+
# Shared between ./scripts/build_docker_image.sh
24+
AVALANCHEGO_IMAGE_NAME="${AVALANCHEGO_IMAGE_NAME:-avaplatform/avalanchego}"
25+
26+
# set git constants if available, otherwise set to empty string (say building from a release)
27+
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
28+
# Current branch
29+
CURRENT_BRANCH=${CURRENT_BRANCH:-$(git describe --tags --exact-match 2>/dev/null || git symbolic-ref -q --short HEAD || git rev-parse --short HEAD || :)}
30+
31+
# Image build id
32+
#
33+
# Use an abbreviated version of the full commit to tag the image.
34+
# WARNING: this will use the most recent commit even if there are un-committed changes present
35+
VM_COMMIT="$(git rev-parse HEAD || :)"
36+
else
37+
CURRENT_BRANCH=""
38+
VM_COMMIT=""
39+
fi
40+
41+
# Shared between ./scripts/build_docker_image.sh
42+
DOCKERHUB_TAG=${VM_COMMIT::8}
43+
44+
echo "Using branch: ${CURRENT_BRANCH}"
445

546
# Set the CGO flags to use the portable version of BLST
647
#
748
# We use "export" here instead of just setting a bash variable because we need
849
# to pass this flag to all child processes spawned by the shell.
9-
10-
export CGO_CFLAGS="-O -D__BLST_PORTABLE__"
50+
export CGO_CFLAGS="-O2 -D__BLST_PORTABLE__"

0 commit comments

Comments
 (0)