From 40bf96766d6ef56caa8f97ee74d17d92d4589a73 Mon Sep 17 00:00:00 2001 From: jmoreira-valory Date: Thu, 3 Oct 2024 17:05:43 +0200 Subject: [PATCH 1/4] feat: k8s deployments --- README.md | 4 ++++ run_service.sh | 41 ++++++++++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 261b9234..a06e774e 100644 --- a/README.md +++ b/README.md @@ -343,3 +343,7 @@ 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 use the flag `--build-only`. \ No newline at end of file diff --git a/run_service.sh b/run_service.sh index 1505e047..05664d65 100755 --- a/run_service.sh +++ b/run_service.sh @@ -680,6 +680,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) @@ -690,6 +691,10 @@ while [[ "$#" -gt 0 ]]; do echo "" echo "" ;; + --build-only) + echo "Build-only flag selected." + build_only=true + ;; *) echo "Unknown parameter: $1" ;; esac shift @@ -1162,14 +1167,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..." @@ -1196,8 +1196,23 @@ 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 .. @@ -1207,5 +1222,17 @@ cd .. add_volume_to_service "$PWD/trader_service/abci_build/docker-compose.yaml" "trader_abci_0" "/data" "$path_to_store" 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 From 271415c5f8da59877d8da751f5ceb168aa7038ab Mon Sep 17 00:00:00 2001 From: jmoreira-valory Date: Thu, 3 Oct 2024 17:09:57 +0200 Subject: [PATCH 2/4] chore: docs --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a06e774e..75021d63 100644 --- a/README.md +++ b/README.md @@ -346,4 +346,8 @@ Error: Service terminatation failed with following error; ChainInteractionError( ## 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 use the flag `--build-only`. \ No newline at end of file +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 +``` From cacd2da103d507b9d63f970bed24a777bc400f30 Mon Sep 17 00:00:00 2001 From: Ardian Date: Thu, 17 Oct 2024 16:47:03 +0200 Subject: [PATCH 3/4] feat: add `/data/` volume --- run_service.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/run_service.sh b/run_service.sh index f7fc7384..afd1577e 100755 --- a/run_service.sh +++ b/run_service.sh @@ -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" @@ -265,6 +265,57 @@ add_volume_to_service() { mv temp_compose_file "$compose_file" } +# add a volume to a service in a Docker Compose 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" @@ -1230,7 +1281,8 @@ 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 From 5c26dc414e3638c4112aa569344a02e5fc04dc62 Mon Sep 17 00:00:00 2001 From: jmoreira-valory Date: Thu, 17 Oct 2024 17:33:13 +0200 Subject: [PATCH 4/4] fix: comment --- run_service.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run_service.sh b/run_service.sh index afd1577e..433b3db8 100755 --- a/run_service.sh +++ b/run_service.sh @@ -265,7 +265,7 @@ add_volume_to_service_docker_compose() { mv temp_compose_file "$compose_file" } -# add a volume to a service in a Docker Compose file +# Function to add a volume to a service in a Kubernetes deployment file add_volume_to_service_k8s() { local deployment_file="$1"