Skip to content

Commit

Permalink
Updated the way docker container is run
Browse files Browse the repository at this point in the history
  • Loading branch information
kpachhai committed Oct 31, 2024
1 parent e904a4f commit 984b6f0
Show file tree
Hide file tree
Showing 19 changed files with 191 additions and 73 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ compose-dev.yaml
# Custom
test_accounts/
*.pem
*.pub
*.pub
*.key
4 changes: 2 additions & 2 deletions Dockerfile.devnet
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN apt update && DEBIAN_FRONTEND=noninteractive apt install -y \

# Set working directory and Go mod cache path
WORKDIR /app

ENV GOMODCACHE=/go/pkg/mod

# Copy the avalanchego binary from the first stage
Expand Down Expand Up @@ -40,5 +41,4 @@ RUN go mod download && go mod verify
# Uncomment this if you have a main package to build.
# RUN go build -o /app/nuklaivm ./cmd/nuklaivm

# Set entrypoint to a script that will stop and start the devnet
ENTRYPOINT ["bash", "-c", "./scripts/stop.sh; ./scripts/run.sh && echo 'Devnet started' && tail -f /dev/null"]
CMD ["bash", "-c", "./scripts/run.sh \"$INITIAL_OWNER_ADDRESS\" \"$EMISSION_ADDRESS\" && tail -f /dev/null"]
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,30 @@ This also allocates all funds on the network to `created address: 00c4cb545f748a
key for this address is `323b1d8f4eed5f0da9da93071b034f2dce9d2d22692c172f3cb252a64ddfafd01b057de320297c29ad0c1f589ea216869cf1938d88c9fbd70d6748323dbf2fa7`.
For convenience, this key has is also stored at `demo.pk`.\_

Alternatively, you can also run the network via docker:

```bash
./scripts/run_docker.sh start
```

And you can check out the logs at:

```bash
./scripts/run_docker.sh logs
```

To stop the network, run:

```sh
```bash
./scripts/stop.sh
```

Alternatively, with docker:

```bash
./scripts/run_docker.sh stop
```

The run script uses AvalancheGo's [tmpnet](https://github.com/ava-labs/avalanchego/tree/master/tests/fixture/tmpnet) to launch a 2 node network with one node's server running at the hardcoded URI: `http://127.0.0.1:9650/ext/bc/nuklaivm`.

Each default API comes with its own extension. For example, you can get the `networkID`, `subnetID`, and `chainID` by hitting the `/coreapi` extension:
Expand Down
19 changes: 15 additions & 4 deletions scripts/aws/deploy.devnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Copyright (C) 2024, Nuklai. All rights reserved.
# See the file LICENSE for licensing terms.


if [[ $(basename "$PWD") != "nuklaivm" ]]; then
echo "Error: This script must be executed from the repository root (nuklaivm/)."
exit 1
Expand All @@ -17,8 +16,13 @@ USER_DATA_FILE="./scripts/aws/install_docker.sh"
TARBALL="nuklaivm.tar.gz"
AMI_ID="ami-008d05461f83df5b1"

# Accept arguments or use default values
INITIAL_OWNER_ADDRESS=${1:-"002b5d019495996310f81c6a26a4dd9eeb9a3f3be1bac0a9294436713aecc84496"}
EMISSION_ADDRESS=${2:-"00f3b89e583e3944dee8d45ca40ce30829eff47481bc45669d401c2f9cc2bc110d"}

echo "Using AMI ID: $AMI_ID"

# Check if an instance is already running
INSTANCE_ID=$(aws ec2 describe-instances --region $REGION \
--filters "Name=tag:Name,Values=$INSTANCE_NAME" "Name=instance-state-name,Values=running" \
--query "Reservations[0].Instances[0].InstanceId" --output text)
Expand All @@ -30,6 +34,7 @@ if [ "$INSTANCE_ID" != "None" ]; then
echo "Instance terminated."
fi

# Launch a new EC2 instance
echo "Launching a new EC2 instance..."
INSTANCE_ID=$(aws ec2 run-instances --region $REGION \
--image-id $AMI_ID --count 1 --instance-type $INSTANCE_TYPE \
Expand All @@ -56,19 +61,25 @@ EXCLUDES=$(cat .gitignore .dockerignore 2>/dev/null | grep -v '^#' | sed '/^$/d'
EXCLUDES+=" --exclude='./web_wallet' --exclude=$TARBALL"
eval "tar -czf $TARBALL $EXCLUDES -C . ."

echo "Transferring tarball to the EC2 instance..."
echo "Transferring tarball and private key to the EC2 instance..."
scp -o "StrictHostKeyChecking=no" -i $KEY_NAME $TARBALL ec2-user@$PUBLIC_IP:/home/ec2-user/

echo "Connecting to the EC2 instance and deploying devnet..."
ssh -o "StrictHostKeyChecking=no" -i $KEY_NAME ec2-user@$PUBLIC_IP << 'EOF'
ssh -o "StrictHostKeyChecking=no" -i $KEY_NAME ec2-user@$PUBLIC_IP << EOF
docker stop nuklai-devnet || true
docker rm nuklai-devnet || true
cd /home/ec2-user
tar -xzf nuklaivm.tar.gz --strip-components=1
# Build the Docker image
docker build -t nuklai-devnet -f Dockerfile.devnet .
docker run -d --name nuklai-devnet -p 9650:9650 nuklai-devnet
# Run the Docker container with the passed arguments as environment variables
docker run -d --name nuklai-devnet -p 9650:9650 \
-e INITIAL_OWNER_ADDRESS="$INITIAL_OWNER_ADDRESS" \
-e EMISSION_ADDRESS="$EMISSION_ADDRESS" \
nuklai-devnet
echo "Devnet is running on the instance."
EOF
Expand Down
6 changes: 0 additions & 6 deletions scripts/build.release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ set -o errexit
set -o nounset
set -o pipefail

# Set the CGO flags to use the portable version of BLST
#
# We use "export" here instead of just setting a bash variable because we need
# to pass this flag to all child processes spawned by the shell.
export CGO_CFLAGS="-O -D__BLST_PORTABLE__" CGO_ENABLED=1

if ! [[ "$0" =~ scripts/build.release.sh ]]; then
echo "must be run from nuklaivm root"
exit 255
Expand Down
6 changes: 0 additions & 6 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ set -o errexit
set -o nounset
set -o pipefail

# Set the CGO flags to use the portable version of BLST
#
# We use "export" here instead of just setting a bash variable because we need
# to pass this flag to all child processes spawned by the shell.
export CGO_CFLAGS="-O -D__BLST_PORTABLE__" CGO_ENABLED=1

# Get the directory of the script, even if sourced from another directory
SCRIPT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)

Expand Down
7 changes: 2 additions & 5 deletions scripts/deploy.devnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

set -e

# Set the CGO flags to use the portable version of BLST
#
# We use "export" here instead of just setting a bash variable because we need
# to pass this flag to all child processes spawned by the shell.
export CGO_CFLAGS="-O -D__BLST_PORTABLE__" CGO_ENABLED=1
source ./scripts/hypersdk/common/utils.sh
source ./scripts/hypersdk/constants.sh

# Set console colors
RED='\033[1;31m'
Expand Down
2 changes: 1 addition & 1 deletion scripts/hypersdk/constants.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
# We use "export" here instead of just setting a bash variable because we need
# to pass this flag to all child processes spawned by the shell.

export CGO_CFLAGS="-O -D__BLST_PORTABLE__"
export CGO_CFLAGS="-O -D__BLST_PORTABLE__" CGO_ENABLED=1
28 changes: 18 additions & 10 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

set -e

# Set the CGO flags to use the portable version of BLST
#
# We use "export" here instead of just setting a bash variable because we need
# to pass this flag to all child processes spawned by the shell.
export CGO_CFLAGS="-O -D__BLST_PORTABLE__" CGO_ENABLED=1
# Default values
DEFAULT_INITIAL_OWNER_ADDRESS="00c4cb545f748a28770042f893784ce85b107389004d6a0e0d6d7518eeae1292d9"
DEFAULT_EMISSION_ADDRESS="00c4cb545f748a28770042f893784ce85b107389004d6a0e0d6d7518eeae1292d9"

# Read arguments from the command line, or use default values
INITIAL_OWNER_ADDRESS=${1:-$DEFAULT_INITIAL_OWNER_ADDRESS}
EMISSION_ADDRESS=${2:-$DEFAULT_EMISSION_ADDRESS}
# Shift arguments only if they are provided
[[ $# -ge 1 ]] && shift
[[ $# -ge 1 ]] && shift
# Remove these arguments from "$@" so they don’t go into additional_args

# to run E2E tests (terminates cluster afterwards)
# MODE=test ./scripts/run.sh
Expand All @@ -23,6 +29,8 @@ source ./scripts/hypersdk/constants.sh

VERSION=v1.11.12-rc.2

echo "Running script with MODE=${MODE}"

############################
# build avalanchego
# https://github.com/ava-labs/avalanchego/releases
Expand Down Expand Up @@ -75,23 +83,23 @@ go build \

############################
echo "building e2e.test"

rm -f ./tests/e2e/e2e.test
prepare_ginkgo

ACK_GINKGO_RC=true ginkgo build ./tests/e2e
./tests/e2e/e2e.test --help
ACK_GINKGO_RC=true ginkgo build ./tests/e2e || echo "ginkgo build failed"

additional_args=("$@")

if [[ ${MODE} == "run" ]]; then
echo "applying ginkgo.focus=Ping and --reuse-network to setup local network"
echo "applying --ginkgo.focus=Ping and --reuse-network to setup local network"
additional_args+=("--ginkgo.focus=Ping")
additional_args+=("--reuse-network")
fi

echo "running e2e tests"
./tests/e2e/e2e.test \
--ginkgo.v \
--initial-owner-address="$INITIAL_OWNER_ADDRESS" \
--emission-address="$EMISSION_ADDRESS" \
--avalanchego-path="${AVALANCHEGO_PATH}" \
--plugin-dir="${AVALANCHEGO_PLUGIN_DIR}" \
"${additional_args[@]}"
69 changes: 69 additions & 0 deletions scripts/run_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env bash
# Copyright (C) 2024, Nuklai. All rights reserved.
# See the file LICENSE for licensing terms.

set -e

# Variables
IMAGE_NAME="nuklai-devnet"
CONTAINER_NAME="nuklai-devnet"

# Function to stop and remove any existing container
cleanup_docker() {
echo "Stopping and removing any existing Docker container..."
docker stop "$CONTAINER_NAME" || true
docker rm "$CONTAINER_NAME" || true
}

# Function to build the Docker image
build_docker_image() {
echo "Building the Docker image..."
docker build -t "$IMAGE_NAME" -f Dockerfile.devnet .
}

# Function to start the Docker container with environment variables
start_container() {
# Use provided arguments, or default values if not set
local initial_owner_address=${1:-"00c4cb545f748a28770042f893784ce85b107389004d6a0e0d6d7518eeae1292d9"}
local emission_address=${2:-"00c4cb545f748a28770042f893784ce85b107389004d6a0e0d6d7518eeae1292d9"}

echo "Starting the Docker container with passed arguments..."
docker run -d --name "$CONTAINER_NAME" \
-p 9650:9650 \
-e INITIAL_OWNER_ADDRESS="$initial_owner_address" \
-e EMISSION_ADDRESS="$emission_address" \
"$IMAGE_NAME"
echo "Docker container started. Use './scripts/run_docker.sh logs' to view logs."
}

# Function to stop the Docker container
stop_container() {
echo "Stopping the Docker container..."
docker stop "$CONTAINER_NAME" || echo "No running container found."
}

# Function to view logs of the container
view_logs() {
echo "Displaying logs for the container..."
docker logs -f "$CONTAINER_NAME"
}

# Main logic to parse commands
case "$1" in
start)
cleanup_docker
build_docker_image
shift # Shift the first argument ("start") so the next ones are passed correctly
start_container "$@" # Pass remaining arguments to start_container
;;
stop)
stop_container
;;
logs)
view_logs
;;
*)
echo "Usage: $0 {start [INITIAL_OWNER_ADDRESS] [EMISSION_ADDRESS]|stop|logs}"
exit 1
;;
esac
4 changes: 2 additions & 2 deletions scripts/stop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

set -e

VMWITHCONTRACTS_PATH=$(
NUKLAIVM_PATH=$(
cd "$(dirname "${BASH_SOURCE[0]}")"
cd .. && pwd
)

ginkgo -v "$VMWITHCONTRACTS_PATH"/tests/e2e/e2e.test -- --stop-network
ginkgo -v "$NUKLAIVM_PATH"/tests/e2e/e2e.test -- --stop-network
6 changes: 0 additions & 6 deletions scripts/tests.benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

set -e

# Set the CGO flags to use the portable version of BLST
#
# We use "export" here instead of just setting a bash variable because we need
# to pass this flag to all child processes spawned by the shell.
export CGO_CFLAGS="-O -D__BLST_PORTABLE__" CGO_ENABLED=1

if ! [[ "$0" =~ scripts/tests.benchmark.sh ]]; then
echo "must be run from nuklaivm root"
exit 255
Expand Down
6 changes: 0 additions & 6 deletions scripts/tests.integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

set -e

# Set the CGO flags to use the portable version of BLST
#
# We use "export" here instead of just setting a bash variable because we need
# to pass this flag to all child processes spawned by the shell.
export CGO_CFLAGS="-O -D__BLST_PORTABLE__" CGO_ENABLED=1

if ! [[ "$0" =~ scripts/tests.integration.sh ]]; then
echo "must be run from nuklaivm root"
exit 255
Expand Down
6 changes: 0 additions & 6 deletions scripts/tests.unit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

set -e

# Set the CGO flags to use the portable version of BLST
#
# We use "export" here instead of just setting a bash variable because we need
# to pass this flag to all child processes spawned by the shell.
export CGO_CFLAGS="-O -D__BLST_PORTABLE__" CGO_ENABLED=1

if ! [[ "$0" =~ scripts/tests.unit.sh ]]; then
echo "must be run from nuklaivm root"
exit 255
Expand Down
Loading

0 comments on commit 984b6f0

Please sign in to comment.