Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,11 @@ Error: Service deployment failed with following error; ChainTimeoutError(Timed o

Error: Service terminatation failed with following error; ChainInteractionError({'code': -32010, 'message': 'AlreadyKnown'})
```

## Build deployments without executing the service

The script builds both a Docker Compose deployment (on `./trader/trader_service/abci_build`) and a Kubernetes deployment (on `./trader/trader_service/abci_build_k8s`). Then, by default, the script will launch the local Docker Compose deployment. If you just want to build the deployment without executing the service (for example, if you are deploying to a custom Kubernetes cluster), then execute the script as

```bash
./run_service.sh --build-only
```
97 changes: 88 additions & 9 deletions run_service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ warm_start() {
}

# Function to add a volume to a service in a Docker Compose file
add_volume_to_service() {
add_volume_to_service_docker_compose() {
local compose_file="$1"
local service_name="$2"
local volume_name="$3"
Expand Down Expand Up @@ -265,6 +265,57 @@ add_volume_to_service() {
mv temp_compose_file "$compose_file"
}

# Function to add a volume to a service in a Kubernetes deployment file
add_volume_to_service_k8s() {
local deployment_file="$1"

# Define the PVC YAML content
local pvc_yaml="
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: trader-data
spec:
storageClassName: nfs-ephemeral
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1000M
"

# Append the PVC YAML to the deployment file
echo "$pvc_yaml" >> "$deployment_file"

# Add the new volume to the volumes section
sed -i '0,/^[[:space:]]*volumes:/s//&\
\ - name: trader-data\
\ persistentVolumeClaim:\
\ claimName: trader-data/' "$deployment_file"

# Find the line number where the container named 'aea' is defined
container_line=$(awk '/containers:/ {flag=1} flag && /name: aea/ {print NR; exit}' "$deployment_file")

if [ -z "$container_line" ]; then
echo "Error: Container named 'aea' not found in $deployment_file."
exit 1
fi

# Check if the container 'aea' already has a volumeMounts section
volume_mounts_line=$(awk -v start="$container_line" 'NR>start && /^[[:space:]]*volumeMounts:/ {print NR; exit}' "$deployment_file")

if [ -z "$volume_mounts_line" ]; then
# No volumeMounts section; add it
sed -i "$((container_line+1)) i \ volumeMounts:\n\ - name: trader-data\n\ mountPath: /data/" "$deployment_file"
else
# volumeMounts section exists; append to it
sed -i "$((volume_mounts_line+1)) i \ - name: trader-data\n\ mountPath: /data/" "$deployment_file"
fi

}


# Function to retrieve on-chain service state (requires env variables set to use --use-custom-chain)
get_on_chain_service_state() {
local service_id="$1"
Expand Down Expand Up @@ -680,6 +731,7 @@ echo "Current branch: $current_branch"
echo "Commit hash: $latest_commit_hash"

# Check the command-line arguments
build_only=false
while [[ "$#" -gt 0 ]]; do
case $1 in
--with-staking)
Expand All @@ -690,6 +742,10 @@ while [[ "$#" -gt 0 ]]; do
echo ""
echo ""
;;
--build-only)
echo "Build-only flag selected."
build_only=true
;;
*) echo "Unknown parameter: $1" ;;
esac
shift
Expand Down Expand Up @@ -1173,14 +1229,9 @@ fi

service_dir="trader_service"
build_dir="abci_build"
build_dir_k8s="abci_build_k8s"
directory="$service_dir/$build_dir"

suggested_amount=$suggested_top_up_default
ensure_minimum_balance "$agent_address" $suggested_amount "agent instance's address"

suggested_amount=$suggested_safe_top_up_default
ensure_minimum_balance "$SAFE_CONTRACT_ADDRESS" $suggested_amount "service Safe's address" $WXDAI_ADDRESS

if [ -d $directory ]
then
echo "Detected an existing build. Using this one..."
Expand All @@ -1207,16 +1258,44 @@ else
poetry run autonomy build-image
fi

# Build the deployment with a single agent
# Build the deployment with a single agent (Docker Compose and Kubernetes)
if [[ -d "$build_dir" ]]; then
echo "You may need to provide sudo password in order for the script to delete part of the build artifacts."
sudo rm -rf "$build_dir"
echo "Directory removed: $build_dir"
fi
if [[ -d "$build_dir_k8s" ]]; then
echo "You may need to provide sudo password in order for the script to delete part of the build artifacts."
sudo rm -rf "$build_dir_k8s"
echo "Directory removed: $build_dir"
fi
export OPEN_AUTONOMY_PRIVATE_KEY_PASSWORD="$password" && poetry run autonomy deploy build --kubernetes "../../$keys_json_path" --n $n_agents -ltm
mv $build_dir $build_dir_k8s
echo "Kubernetes deployment built on ./trader/$service_dir/$build_dir_k8s"

export OPEN_AUTONOMY_PRIVATE_KEY_PASSWORD="$password" && poetry run autonomy deploy build "../../$keys_json_path" --n $n_agents -ltm
echo "Docker Compose deployment built on ./trader/$service_dir/$build_dir"

cd ..

# warm start is disabled as no global weights are provided to calibrate the tools' weights
# warm_start

add_volume_to_service "$PWD/trader_service/abci_build/docker-compose.yaml" "trader_abci_0" "/data" "$path_to_store"
add_volume_to_service_docker_compose "$PWD/trader_service/abci_build/docker-compose.yaml" "trader_abci_0" "/data" "$path_to_store"
add_volume_to_service_k8s "$PWD/trader_service/abci_build_k8s/build.yaml"
sudo chown -R $(whoami) "$path_to_store"

if [[ "$build_only" == true ]]; then
echo ""
echo "Build-only done."
exit 0
fi

# Run the deployment
suggested_amount=$suggested_top_up_default
ensure_minimum_balance "$agent_address" $suggested_amount "agent instance's address"

suggested_amount=$suggested_safe_top_up_default
ensure_minimum_balance "$SAFE_CONTRACT_ADDRESS" $suggested_amount "service Safe's address" $WXDAI_ADDRESS

export OPEN_AUTONOMY_PRIVATE_KEY_PASSWORD="$password" && poetry run autonomy deploy run --build-dir "$directory" --detach