Skip to content

Commit df2e2b4

Browse files
committed
add script and optimize dockerfile for referencing local hummingbot
1 parent c5463a4 commit df2e2b4

File tree

3 files changed

+198
-4
lines changed

3 files changed

+198
-4
lines changed

Dockerfile

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# syntax=docker/dockerfile:1.7-labs
12
# Start from a base image with Miniconda installed
23
FROM continuumio/miniconda3
34

@@ -9,14 +10,36 @@ RUN apt-get update && \
910
# Set the working directory in the container
1011
WORKDIR /backend-api
1112

12-
# Copy the current directory contents and the Conda environment file into the container
13-
COPY . .
14-
1513
# Create the environment from the environment.yml file
14+
COPY environment.yml ./
1615
RUN conda env create -f environment.yml
1716

1817
# Make RUN commands use the new environment
1918
SHELL ["conda", "run", "-n", "backend-api", "/bin/bash", "-c"]
2019

20+
# Copy Optional wheels directory and handle wheel installation (for utilizing local hummingbot version)
21+
COPY --parents wheels* .
22+
23+
# Optionally install local Hummingbot wheel if present - use the latest wheel only
24+
RUN if [ -n "$(find wheels/ -name 'hummingbot-*.whl' 2>/dev/null)" ]; then \
25+
echo "Installing local Hummingbot wheel..." && \
26+
LATEST_WHEEL=$(find wheels/ -name 'hummingbot-*.whl' | sort -r | head -n1) && \
27+
echo "Using wheel: $LATEST_WHEEL" && \
28+
pip install --force-reinstall $LATEST_WHEEL && \
29+
echo "Local Hummingbot wheel installed successfully"; \
30+
else \
31+
echo "No local Hummingbot wheel found, using version from environment.yml"; \
32+
fi
33+
34+
# Copy the current directory contents and the Conda environment file into the container
35+
COPY main.py models.py config.py LICENSE README.md ./
36+
COPY utils/ utils/
37+
COPY routers/ routers/
38+
COPY services/ services/
39+
40+
COPY bots/controllers bots/credentials bots/scripts bots/__init__.py bots/
41+
42+
# Add any other specific directories or files needed
43+
2144
# The code to run when container is started
2245
ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "backend-api", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
.PHONY: install-pre-commit
88
.PHONY: docker_build
99
.PHONY: docker_run
10+
.PHONY: reference-local-hummingbot
1011

1112

1213
detect_conda_bin := $(shell bash -c 'if [ "${CONDA_EXE} " == " " ]; then \
@@ -44,7 +45,11 @@ install-pre-commit:
4445
fi && pre-commit install'
4546

4647
docker_build:
47-
docker build -t hummingbot/backend-api:latest .
48+
docker build -t hummingbot/backend-api$(TAG) .
4849

4950
docker_run:
5051
docker compose up -d
52+
53+
# See reference_local_hummingbot.sh for available options
54+
reference-local-hummingbot:
55+
bash ./scripts/reference_local_hummingbot.sh $(if $(force-repackage),--force-repackage,) $(if $(build-env),--build-env=$(build-env),) $(ARGS)

scripts/reference_local_hummingbot.sh

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/bin/bash
2+
3+
# Script to build, copy and install a Python wheel package from a source directory to a destination directory
4+
5+
print_usage() {
6+
echo "Usage: $0 [OPTIONS]"
7+
echo "Options:"
8+
echo " --force-repackage Force rebuilding the wheel package"
9+
echo " --source-dir=DIR Source directory containing the Python package (default: ../hummingbot)"
10+
echo " --dest-dir=DIR Destination directory for the wheel file (default: ./wheels)"
11+
echo " --package-name=NAME Name of the package to build (default: hummingbot)"
12+
echo " --conda-env=NAME Conda environment to install into (required if not in environment.yml or .env)"
13+
echo " --build-env=NAME Conda environment to build the package in (default: hummingbot)"
14+
echo " --help Show this help message"
15+
echo ""
16+
echo "When called from Makefile, you can use this format for arguments not in the makefile:"
17+
echo "make reference-local-hummingbot ARGS=\"--force-repackage --source-dir=DIR ...\""
18+
}
19+
20+
# Default values
21+
FORCE_REPACKAGE=false
22+
SOURCE_DIR=""
23+
DEST_DIR=""
24+
PACKAGE_NAME="hummingbot"
25+
CONDA_ENV=""
26+
BUILD_ENV="hummingbot"
27+
28+
# Get current working directory
29+
CWD="$(pwd)"
30+
31+
# Parse arguments
32+
while [[ $# -gt 0 ]]; do
33+
case $1 in
34+
--force-repackage)
35+
FORCE_REPACKAGE=true
36+
shift
37+
;;
38+
--source-dir=*)
39+
SOURCE_DIR="${1#*=}"
40+
shift
41+
;;
42+
--dest-dir=*)
43+
DEST_DIR="${1#*=}"
44+
shift
45+
;;
46+
--package-name=*)
47+
PACKAGE_NAME="${1#*=}"
48+
shift
49+
;;
50+
--conda-env=*)
51+
CONDA_ENV="${1#*=}"
52+
shift
53+
;;
54+
--build-env=*)
55+
BUILD_ENV="${1#*=}"
56+
shift
57+
;;
58+
--help)
59+
print_usage
60+
exit 0
61+
;;
62+
*)
63+
echo "Unknown option: $1"
64+
print_usage
65+
exit 1
66+
;;
67+
esac
68+
done
69+
70+
# Extract conda environment name from environment.yml
71+
if [ -z "$CONDA_ENV" ] && [ -f "$CWD/environment.yml" ]; then
72+
CONDA_ENV=$(grep "^name:" "$CWD/environment.yml" | cut -d ":" -f2 | tr -d " ")
73+
fi
74+
75+
# If not found in environment.yml, try .env file
76+
if [ -z "$CONDA_ENV" ] && [ -f "$CWD/.env" ]; then
77+
CONDA_ENV=$(grep "^CONDA_ENV=" "$CWD/.env" | cut -d "=" -f2 | tr -d '"' | tr -d "'" | tr -d " ")
78+
fi
79+
80+
# If still not found, throw an error
81+
if [ -z "$CONDA_ENV" ]; then
82+
echo "Error: Conda environment name not found in environment.yml or .env"
83+
echo "Please specify with --conda-env option or add CONDA_ENV to .env file or name to environment.yml"
84+
exit 1
85+
fi
86+
87+
# Set default paths if not provided
88+
if [ -z "$SOURCE_DIR" ]; then
89+
SOURCE_DIR="../$PACKAGE_NAME"
90+
fi
91+
92+
if [ -z "$DEST_DIR" ]; then
93+
DEST_DIR="./wheels"
94+
fi
95+
96+
# Convert to absolute paths if relative
97+
if [[ ! "$SOURCE_DIR" = /* ]]; then
98+
SOURCE_DIR="$CWD/$SOURCE_DIR"
99+
fi
100+
101+
if [[ ! "$DEST_DIR" = /* ]]; then
102+
DEST_DIR="$CWD/$DEST_DIR"
103+
fi
104+
105+
# Create wheels directory if it doesn't exist
106+
mkdir -p "$DEST_DIR"
107+
108+
# Check for existing wheel in source directory
109+
cd "$SOURCE_DIR" || { echo "Error: Could not change to source directory: $SOURCE_DIR"; exit 1; }
110+
EXISTING_WHEEL=$(ls "dist/$PACKAGE_NAME-"*.whl 2>/dev/null)
111+
if [ -n "$EXISTING_WHEEL" ] && [ "$FORCE_REPACKAGE" = false ]; then
112+
echo "Found existing wheel: $EXISTING_WHEEL"
113+
WHEEL_FILE="$EXISTING_WHEEL"
114+
else
115+
if [ -n "$EXISTING_WHEEL" ]; then
116+
echo "Force repackage requested, rebuilding wheel..."
117+
rm -f "$EXISTING_WHEEL"
118+
echo "Removed existing wheel: $EXISTING_WHEEL"
119+
fi
120+
121+
# Build wheel
122+
echo "Building $PACKAGE_NAME wheel in conda environment '$BUILD_ENV'..."
123+
conda run -n $BUILD_ENV python setup.py sdist bdist_wheel
124+
125+
# Find the wheel file
126+
WHEEL_FILE=$(ls "dist/$PACKAGE_NAME-"*.whl 2>/dev/null)
127+
if [ -z "$WHEEL_FILE" ]; then
128+
echo "Error: No wheel file found in dist directory"
129+
exit 1
130+
fi
131+
fi
132+
133+
# Get the wheel filename without path
134+
WHEEL_FILENAME=$(basename "$WHEEL_FILE")
135+
136+
# Check if there's already a matching wheel file in the destination directory
137+
LOCAL_WHEEL_FILE="$DEST_DIR/$WHEEL_FILENAME"
138+
if [ -f "$LOCAL_WHEEL_FILE" ]; then
139+
# Calculate hashes for both files
140+
SOURCE_HASH=$(sha256sum "$WHEEL_FILE" | cut -d ' ' -f 1)
141+
LOCAL_HASH=$(sha256sum "$LOCAL_WHEEL_FILE" | cut -d ' ' -f 1)
142+
143+
# Compare hashes
144+
if [ "$SOURCE_HASH" = "$LOCAL_HASH" ]; then
145+
if [ -z "$EXISTING_WHEEL" ] || [ "$FORCE_REPACKAGE" = true ]; then
146+
echo "Existing local $LOCAL_WHEEL_FILE already matches generated $WHEEL_FILE"
147+
else
148+
echo "Existing local $LOCAL_WHEEL_FILE already matches existing $WHEEL_FILE"
149+
fi
150+
echo "Skipping installation as files are identical."
151+
exit 0
152+
else
153+
echo "Local wheel exists but has different hash, will update..."
154+
fi
155+
fi
156+
157+
# Copy wheel to destination directory
158+
echo "Copying $(basename "$WHEEL_FILE") to wheels directory..."
159+
cp "$WHEEL_FILE" "$DEST_DIR/"
160+
WHEEL_FILE="$DEST_DIR/$(basename "$WHEEL_FILE")"
161+
162+
# Install new wheel
163+
echo "Installing $(basename "$WHEEL_FILE") into conda environment '$CONDA_ENV'..."
164+
conda run -n $CONDA_ENV pip install --force-reinstall "$WHEEL_FILE"
165+
166+
echo -e "\nSuccessfully installed $(basename "$WHEEL_FILE") into conda environment '$CONDA_ENV'"

0 commit comments

Comments
 (0)