You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ya...@apache.org on 2025/04/08 02:29:53 UTC

(airavata) branch cybershuttle-staging updated: fix issues in agent/sdk for creating custom environments. update agent deployment scripts

This is an automated email from the ASF dual-hosted git repository.

yasith pushed a commit to branch cybershuttle-staging
in repository https://gitbox.apache.org/repos/asf/airavata.git


The following commit(s) were added to refs/heads/cybershuttle-staging by this push:
     new e3d14df161 fix issues in agent/sdk for creating custom environments. update agent deployment scripts
e3d14df161 is described below

commit e3d14df1612b052a32cbdcc2b39a7a0f5372b63c
Author: yasith <ya...@gatech.edu>
AuthorDate: Mon Apr 7 22:29:40 2025 -0400

    fix issues in agent/sdk for creating custom environments. update agent deployment scripts
---
 .../airavata_experiments/airavata.py               | 12 +++----
 .../airavata_jupyter_magic/__init__.py             |  2 +-
 .../airavata-python-sdk/pyproject.toml             |  2 +-
 modules/agent-framework/airavata-agent/Makefile    | 40 +++++++++++-----------
 modules/agent-framework/airavata-agent/agent.go    | 34 ++++++++++--------
 modules/agent-framework/airavata-agent/agent.sh    | 36 ++++++++++---------
 6 files changed, 67 insertions(+), 59 deletions(-)

diff --git a/airavata-api/airavata-client-sdks/airavata-python-sdk/airavata_experiments/airavata.py b/airavata-api/airavata-client-sdks/airavata-python-sdk/airavata_experiments/airavata.py
index e352d909af..b3308f5995 100644
--- a/airavata-api/airavata-client-sdks/airavata-python-sdk/airavata_experiments/airavata.py
+++ b/airavata-api/airavata-client-sdks/airavata-python-sdk/airavata_experiments/airavata.py
@@ -348,7 +348,7 @@ class AiravataOperator:
       b64data = base64.b64encode(rawdata).decode()
       res = requests.post(f"{self.connection_svc_url()}/agent/execute/shell", json={
           "agentId": agent_ref,
-          "envName": "base",
+          "envName": agent_ref,
           "workingDir": ".",
           "arguments": ["sh", "-c", f"echo {b64data} | base64 -d > {fp}"]
       })
@@ -390,7 +390,7 @@ class AiravataOperator:
     """
     res = requests.post(f"{self.connection_svc_url()}/agent/execute/shell", json={
         "agentId": agent_ref,
-        "envName": "base",
+        "envName": agent_ref,
         "workingDir": ".",
         "arguments": ["sh", "-c", r"find /data -type d -name 'venv' -prune -o -type f -printf '%P\n' | sort"]
     })
@@ -428,7 +428,7 @@ class AiravataOperator:
     fp = os.path.join("/data", remote_file)
     res = requests.post(f"{self.connection_svc_url()}/agent/execute/shell", json={
         "agentId": agent_ref,
-        "envName": "base",
+        "envName": agent_ref,
         "workingDir": ".",
         "arguments": ["sh", "-c", f"cat {fp} | base64 -w0"]
     })
@@ -474,7 +474,7 @@ class AiravataOperator:
     fp = os.path.join("/data", remote_file)
     res = requests.post(f"{self.connection_svc_url()}/agent/execute/shell", json={
         "agentId": agent_ref,
-        "envName": "base",
+        "envName": agent_ref,
         "workingDir": ".",
         "arguments": ["sh", "-c", f"cat {fp} | base64 -w0"]
     })
@@ -752,7 +752,7 @@ class AiravataOperator:
       print(f"[av] Agent {agent_id} found! creating environment...")
       res = requests.post(f"{self.connection_svc_url()}/setup/env", json={
         "agentId": agent_id,
-        "envName": "base",
+        "envName": agent_id,
         "libraries": ["python=3.10", "pip"],
         "pip": libraries,
       })
@@ -770,7 +770,7 @@ class AiravataOperator:
       print(f"[av] Agent {agent_id} env created! executing code...")
       res = requests.post(f"{self.connection_svc_url()}/agent/execute/python", json={
         "agentId": agent_id,
-        "envName": "base",
+        "envName": agent_id,
         "workingDir": ".",
         "code": code,
       })
diff --git a/airavata-api/airavata-client-sdks/airavata-python-sdk/airavata_jupyter_magic/__init__.py b/airavata-api/airavata-client-sdks/airavata-python-sdk/airavata_jupyter_magic/__init__.py
index 839fda942d..04b1fbd491 100644
--- a/airavata-api/airavata-client-sdks/airavata-python-sdk/airavata_jupyter_magic/__init__.py
+++ b/airavata-api/airavata-client-sdks/airavata-python-sdk/airavata_jupyter_magic/__init__.py
@@ -474,7 +474,7 @@ def run_on_runtime(rt_name: str, code_obj: str, result: ExecutionResult) -> bool
     url = api_base_url + '/api/v1/agent/execute/jupyter'
     data = {
         "agentId": rt.agentId,
-        "envName": "base",
+        "envName": rt.agentId,
         "code": code_obj,
     }
     json_data = json.dumps(data)
diff --git a/airavata-api/airavata-client-sdks/airavata-python-sdk/pyproject.toml b/airavata-api/airavata-client-sdks/airavata-python-sdk/pyproject.toml
index 58c49a9e4e..9c3df6fe11 100644
--- a/airavata-api/airavata-client-sdks/airavata-python-sdk/pyproject.toml
+++ b/airavata-api/airavata-client-sdks/airavata-python-sdk/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
 
 [project]
 name = "airavata-python-sdk"
-version = "2.0.6"
+version = "2.0.7"
 description = "Apache Airavata Python SDK"
 readme = "README.md"
 license = "Apache-2.0"
diff --git a/modules/agent-framework/airavata-agent/Makefile b/modules/agent-framework/airavata-agent/Makefile
index 8fd4614dad..193ed7b11d 100644
--- a/modules/agent-framework/airavata-agent/Makefile
+++ b/modules/agent-framework/airavata-agent/Makefile
@@ -13,41 +13,41 @@ run-container:
 deploy-anvil: deploy-anvil-scigap deploy-anvil-waterhub deploy-anvil-gcommunityus
 
 deploy-anvil-scigap:
-	ssh x-scigap@anvil srun -p shared --mem 4G -t 30 singularity pull \
-	--disable-cache \
-	--force \
+	ssh x-scigap@anvil \
+	"srun -N1 -n1 -p shared --mem 4G -t 30 \
+	singularity pull --disable-cache --force \
 	cybershuttle/container/remote-agent-base.sif \
-	docker://$(IMAGE_NAME)
+	docker://$(IMAGE_NAME)"
 
 deploy-anvil-waterhub:
-	ssh x-waterhub@anvil srun -p shared --mem 4G -t 30 singularity pull \
-	--disable-cache \
-	--force \
+	ssh x-waterhub@anvil \
+	"srun -N1 -n1 -p shared --mem 4G -t 30 \
+	singularity pull --disable-cache --force \
 	cybershuttle/container/remote-agent-base.sif \
-	docker://$(IMAGE_NAME)
+	docker://$(IMAGE_NAME)"
 
 deploy-anvil-gcommunityus:
-	ssh x-gcommunityus@anvil srun -p shared --mem 4G -t 30 singularity pull \
-	--disable-cache \
-	--force \
+	ssh x-gcommunityus@anvil \
+	"srun -N1 -n1 -p shared --mem 4G -t 30 \
+	singularity pull --disable-cache --force \
 	cybershuttle/container/remote-agent-base.sif \
-	docker://$(IMAGE_NAME)
+	docker://$(IMAGE_NAME)"
 
 deploy-expanse: deploy-expanse-scigap deploy-expanse-gridchem
 
 deploy-expanse-scigap:
-	ssh scigap@expanse srun -p shared --mem 4G -t 30 singularity pull \
-	--disable-cache \
-	--force \
+	ssh scigap@expanse \
+	"module load singularitypro && srun -N1 -n1 -A uic151 -p shared --mem 4G -t 30 \
+	singularity pull --disable-cache --force \
 	cybershuttle/container/remote-agent-base.sif \
-	docker://$(IMAGE_NAME)
+	docker://$(IMAGE_NAME)"
 
 deploy-expanse-gridchem:
-	ssh gridchem@expanse srun -p shared --mem 4G -t 30 singularity pull \
-	--disable-cache \
-	--force \
+	ssh gridchem@expanse \
+	"module load singularitypro && srun -N1 -n1 -A uic151 -p shared --mem 4G -t 30 \
+	singularity pull --disable-cache --force \
 	cybershuttle/container/remote-agent-base.sif \
-	docker://$(IMAGE_NAME)
+	docker://$(IMAGE_NAME)"
 
 deploy-bootstrap:
 	scp agent.sh x-scigap@anvil:~/cybershuttle/agent.sh && \
diff --git a/modules/agent-framework/airavata-agent/agent.go b/modules/agent-framework/airavata-agent/agent.go
index 6814328326..c5be62443c 100644
--- a/modules/agent-framework/airavata-agent/agent.go
+++ b/modules/agent-framework/airavata-agent/agent.go
@@ -25,8 +25,8 @@ import (
 
 type Stream = grpc.BidiStreamingClient[protos.AgentMessage, protos.ServerMessage]
 
-var envName = "base"
 var pidMap = make(map[string]int)
+var defaultLibs = []string{"python<3.12", "pip", "ipykernel", "git", "flask", "jupyter_client"}
 
 func main() {
 
@@ -38,6 +38,10 @@ func main() {
 
 	// Parse the flags provided by the user.
 	flag.Parse()
+	log.Printf("[agent.go] main() --server=%s\n", *serverUrl)
+	log.Printf("[agent.go] main() --agent=%s\n", *agentId)
+	log.Printf("[agent.go] main() --lib=%s\n", *lib)
+	log.Printf("[agent.go] main() --pip=%s\n", *pip)
 
 	// Validate required flags
 	if *serverUrl == "" {
@@ -63,12 +67,14 @@ func main() {
 
 	var libList []string
 	if strings.TrimSpace(*lib) != "" {
-		libList = strings.Split(*lib, ",")
+		libList = append(strings.Split(*lib, ","), defaultLibs...)
 		log.Printf("[agent.go] main() Installing --lib: %v\n", libList)
-		libCmd := exec.Command("micromamba", "install", "-n", envName, "--yes")
+		libCmd := exec.Command("micromamba", "install", "-n", *agentId, "--yes")
 		libCmd.Args = append(libCmd.Args, libList...)
+		libCmd.Stdout = os.Stdout
+		libCmd.Stderr = os.Stderr
 		if err := libCmd.Run(); err != nil {
-			log.Fatalf("[agent.go] main() Error Installing --lib: %v\n%s\n", err, libCmd.Stderr)
+			log.Fatalf("[agent.go] main() Error Installing --lib: %v\n", err)
 		}
 		log.Printf("[agent.go] main() Installed --lib: %v\n", libList)
 	} else {
@@ -79,13 +85,14 @@ func main() {
 	if strings.TrimSpace(*pip) != "" {
 		pipList = strings.Split(*pip, ",")
 		log.Printf("[agent.go] main() Installing --pip: %v\n", pipList)
-		pipCmd := exec.Command("micromamba", "run", "-n", envName, "pip", "install")
+		pipCmd := exec.Command("micromamba", "run", "-n", *agentId, "pip", "install")
 		pipCmd.Args = append(pipCmd.Args, pipList...)
+		pipCmd.Stdout = os.Stdout
+		pipCmd.Stderr = os.Stderr
 		if err := pipCmd.Run(); err != nil {
-			log.Fatalf("[agent.go] main() Error Installing --pip: %v\n%s\n", err, pipCmd.Stderr)
+			log.Fatalf("[agent.go] main() Error Installing --pip: %v\n", err)
 		}
 		log.Printf("[agent.go] main() Installed --pip: %v\n", pipList)
-
 	} else {
 		log.Printf("[agent.go] main() No --pip to install.\n")
 	}
@@ -132,15 +139,15 @@ func startInterceptor(stream Stream, grpcStreamChannel chan struct{}) {
 		case *protos.ServerMessage_EnvSetupRequest:
 			log.Printf("[agent.go] Recived a env setup request\n")
 			executionId := x.EnvSetupRequest.ExecutionId
-			envName = x.EnvSetupRequest.EnvName
+			envName := x.EnvSetupRequest.EnvName
 			envLibs := x.EnvSetupRequest.Libraries
 			envPip := x.EnvSetupRequest.Pip
-			go createEnv(stream, executionId, envName, envLibs, envPip)
+			go createEnv(stream, executionId, envName, append(envLibs, defaultLibs...), envPip)
 
 		case *protos.ServerMessage_PythonExecutionRequest:
 			log.Printf("[agent.go] Recived a python execution request\n")
 			executionId := x.PythonExecutionRequest.ExecutionId
-			envName = x.PythonExecutionRequest.EnvName
+			envName := x.PythonExecutionRequest.EnvName
 			workingDir := x.PythonExecutionRequest.WorkingDir
 			code := x.PythonExecutionRequest.Code
 			go executePython(stream, executionId, envName, workingDir, code)
@@ -148,7 +155,7 @@ func startInterceptor(stream Stream, grpcStreamChannel chan struct{}) {
 		case *protos.ServerMessage_CommandExecutionRequest:
 			log.Printf("[agent.go] Recived a shell execution request\n")
 			executionId := x.CommandExecutionRequest.ExecutionId
-			envName = x.CommandExecutionRequest.EnvName
+			envName := x.CommandExecutionRequest.EnvName
 			workingDir := x.CommandExecutionRequest.WorkingDir
 			execArgs := x.CommandExecutionRequest.Arguments
 			go executeShell(stream, executionId, envName, workingDir, execArgs)
@@ -156,14 +163,14 @@ func startInterceptor(stream Stream, grpcStreamChannel chan struct{}) {
 		case *protos.ServerMessage_JupyterExecutionRequest:
 			log.Printf("[agent.go] Recived a jupyter execution request\n")
 			executionId := x.JupyterExecutionRequest.ExecutionId
-			envName = x.JupyterExecutionRequest.EnvName
+			envName := x.JupyterExecutionRequest.EnvName
 			code := x.JupyterExecutionRequest.Code
 			go executeJupyter(stream, executionId, envName, code)
 
 		case *protos.ServerMessage_KernelRestartRequest:
 			log.Printf("[agent.go] Recived a kernel restart request\n")
 			executionId := x.KernelRestartRequest.ExecutionId
-			envName = x.KernelRestartRequest.EnvName
+			envName := x.KernelRestartRequest.EnvName
 			go restartKernel(stream, executionId, envName)
 
 		case *protos.ServerMessage_TunnelCreationRequest:
@@ -203,7 +210,6 @@ func createEnv(stream Stream, executionId string, envName string, envLibs []stri
 		}
 		log.Printf("[agent.go] createEnv() Environment created: %s\n", envName)
 	}
-	envLibs = append(envLibs, "python<3.12", "pip", "ipykernel", "git", "flask", "jupyter_client")
 	installDepsCmd := exec.Command("micromamba", "install", "-n", envName, "--yes")
 	installDepsCmd.Args = append(installDepsCmd.Args, envLibs...)
 	if err := installDepsCmd.Run(); err != nil {
diff --git a/modules/agent-framework/airavata-agent/agent.sh b/modules/agent-framework/airavata-agent/agent.sh
index 9059ae09cf..3e2c18f794 100755
--- a/modules/agent-framework/airavata-agent/agent.sh
+++ b/modules/agent-framework/airavata-agent/agent.sh
@@ -49,7 +49,9 @@ while true; do
         --mounts)    
             IFS=',' read -ra MOUNTS <<< "$2"
             for MOUNT in "${MOUNTS[@]}"; do
-              BIND_OPTS+=("--bind $(eval echo $MOUNT):ro")
+              IFS=':' read -r SRC DEST <<< "$MOUNT"
+              SRC=$(eval echo "$SRC")
+              BIND_OPTS+=("--bind $SRC:$DEST:ro")
             done
             shift 2 ;;
         --bind)      BIND_OPTS+=("--bind $2:ro"); shift 2 ;;
@@ -65,22 +67,22 @@ echo "SERVER=$SERVER"
 echo "CONTAINER=$CONTAINER"
 echo "LIBRARIES=$LIBRARIES"
 echo "PIP=$PIP"
-echo "BIND_OPTS=$BIND_OPTS"
+echo "BIND_OPTS=${BIND_OPTS[@]}"
 
 # ----------------------------------------------------------------------
-# STEP 2 - RUN AGENT
+# STEP 2 - RUN USING AGENT
 # ----------------------------------------------------------------------
-# Create overlay image (4GB) if it doesn't exist
-if [ ! -f workspace.img ]; then
-  singularity overlay create --size 4096 workspace.img
-fi
-# Start agent with the given params and overlay
-KERNEL_SOCK=$(mktemp) singularity exec \
-  --overlay workspace.img \
-  ${BIND_OPTS[@]} \
-  $CS_HOME/container/$CONTAINER /opt/airavata-agent \
-    --server $SERVER:19900 \
-    --agent $AGENT \
-    --lib $LIBRARIES \
-    --pip $PIP \
-    $@
+
+mkdir -p $CS_HOME/scratch/tmp
+
+# Run container in environment
+singularity exec \
+  --bind $CS_HOME/scratch:/scratch ${BIND_OPTS[@]} \
+  --env MAMBA_ROOT_PREFIX=/scratch \
+  --env TMPDIR=/scratch/tmp \
+  --env KERNEL_SOCK=$(mktemp) \
+  $CS_HOME/container/$CONTAINER \
+  bash -c "\
+  micromamba create -n $AGENT && \
+  /opt/airavata-agent --server $SERVER:19900 --agent $AGENT --lib $LIBRARIES --pip $PIP $@\
+  "