Skip to content

Commit cceabb9

Browse files
committed
chore: create consistent compose configs
1 parent a71659a commit cceabb9

11 files changed

+106
-242
lines changed

Cargo.lock

+1-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ rust-version = "1.81"
1515
[dependencies]
1616
blueprint-sdk = { git = "https://github.com/tangle-network/gadget", features = ["tangle", "macros"] }
1717
phala-tee-deploy-rs = { git = "https://github.com/tangle-network/phala-tee-deploy-rs" }
18-
dockworker = { git = "https://github.com/tangle-network/dockworker" }
1918
serde = { version = "1.0", features = ["derive"] }
2019
serde_json = "1.0"
2120
tokio = { version = "1.25", features = ["rt", "macros", "process", "fs", "time", "net"] }

src/create_agent.rs

+23-33
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ use crate::docker;
22
use crate::types::{AgentCreationResult, CreateAgentParams};
33
use crate::{AgentPortConfig, ServiceContext};
44
use blueprint_sdk::logging;
5-
use dockworker::ComposeConfig;
6-
use phala_tee_deploy_rs::PubkeyResponse;
7-
use std::collections::HashMap;
85
use std::fs;
96
use std::path::{Path, PathBuf};
107
use uuid::Uuid;
@@ -59,21 +56,16 @@ pub async fn handle_create_agent(
5956
logging::warn!("No agent_ports map available in context");
6057
}
6158

62-
// Create Docker Compose file
63-
let env_vars = HashMap::new(); // No additional env vars at creation time
64-
let compose_path = docker::write_docker_compose_file(
65-
&agent_dir,
66-
&agent_id,
67-
Some(http_port),
68-
Some(websocket_port),
69-
env_vars,
70-
)?;
59+
let compose_path = docker::write_docker_compose_file(&agent_dir)?;
7160

7261
// Prepare TEE config if enabled
73-
let pubkey_response = if params.deployment_config.tee_enabled {
74-
get_tee_public_key(&agent_dir, context).await?
62+
let (tee_pubkey, tee_app_id) = if params.deployment_config.tee_enabled {
63+
match get_tee_public_key(&agent_dir, context).await? {
64+
Some((pubkey, app_id)) => (Some(pubkey), Some(app_id)),
65+
None => (None, None),
66+
}
7567
} else {
76-
None
68+
(None, None)
7769
};
7870

7971
// Return the result
@@ -84,7 +76,8 @@ pub async fn handle_create_agent(
8476
agent_dir.join("package.json").to_string_lossy().to_string(),
8577
compose_path.to_string_lossy().to_string(),
8678
],
87-
pubkey_response,
79+
tee_pubkey,
80+
tee_app_id,
8881
};
8982

9083
// Serialize the result
@@ -188,7 +181,7 @@ fn copy_dir_contents(src: &Path, dst: &Path) -> Result<(), String> {
188181
async fn get_tee_public_key(
189182
agent_dir: &Path,
190183
context: &ServiceContext,
191-
) -> Result<Option<PubkeyResponse>, String> {
184+
) -> Result<Option<(String, String)>, String> {
192185
// Get API key directly from context
193186
let tee_api_key = context
194187
.phala_tee_api_key
@@ -218,24 +211,17 @@ async fn get_tee_public_key(
218211
let docker_compose = fs::read_to_string(&docker_compose_path)
219212
.map_err(|e| format!("Failed to read docker-compose.yml: {}", e))?;
220213

221-
// Create VM configuration using TeeDeployer's native method
222-
logging::info!(
223-
"Creating VM configuration from Docker Compose {:#?}",
224-
docker_compose
225-
);
226-
227-
// Parse docker-compose.yml to ComposeConfig using dockworker
228-
let compose_config: ComposeConfig = serde_yaml::from_str(&docker_compose)
229-
.map_err(|e| format!("Failed to parse docker-compose.yml: {}", e))?;
214+
// Normalize the Docker Compose file to ensure consistent ordering
215+
let docker_compose = docker::normalize_docker_compose(&docker_compose)?;
230216

231-
// Use TeeDeployer's built-in create_vm_config method
232217
let app_name = format!(
233218
"coinbase-agent-{}",
234219
agent_dir.file_name().unwrap().to_string_lossy()
235220
);
221+
236222
let vm_config = deployer
237223
.create_vm_config(
238-
&compose_config,
224+
&docker_compose,
239225
&app_name,
240226
Some(2), // vcpu
241227
Some(2048), // memory in MB
@@ -244,20 +230,24 @@ async fn get_tee_public_key(
244230
.map_err(|e| format!("Failed to create VM configuration: {}", e))?;
245231

246232
// Get the public key for this VM configuration
233+
let vm_config_json = serde_json::to_value(vm_config)
234+
.map_err(|e| format!("Failed to serialize VM configuration: {}", e))?;
247235
logging::info!(
248-
"Requesting encryption public key with config {:#?}",
249-
vm_config
236+
"Requesting encryption public key with VM Config: {:#?}",
237+
vm_config_json
250238
);
251-
let vm_config_json = serde_json::to_value(&vm_config).unwrap();
252239
let pubkey_response = deployer
253240
.get_pubkey_for_config(&vm_config_json)
254241
.await
255242
.map_err(|e| format!("Failed to get TEE public key: {}", e))?;
256243

257-
logging::info!("Pubkey response: {:#?}", pubkey_response);
244+
// Extract the pubkey and salt from the response
245+
let pubkey = pubkey_response.app_env_encrypt_pubkey;
246+
let salt = pubkey_response.app_id_salt;
247+
258248
logging::info!("Successfully obtained TEE public key");
259249

260-
Ok(Some(pubkey_response))
250+
Ok(Some((pubkey, salt)))
261251
}
262252

263253
/// Creates a .env file with the necessary environment variables

src/deploy_agent.rs

+18-26
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::types::{AgentDeploymentResult, DeployAgentParams};
44
use crate::ServiceContext;
55
use blueprint_sdk::logging;
66
use dotenv::dotenv;
7-
use serde_json;
87
use std::fs;
98
use std::path::Path;
109
use tokio::process::Command as TokioCommand;
@@ -70,10 +69,11 @@ async fn deploy_to_tee(
7069
let docker_compose = fs::read_to_string(&docker_compose_path)
7170
.map_err(|e| format!("Failed to read docker-compose.yml: {}", e))?;
7271

73-
logging::info!(
74-
"Deploying agent to TEE with Docker compose: {:#?}",
75-
docker_compose
76-
);
72+
// Normalize the Docker Compose file to ensure consistent ordering
73+
let docker_compose = docker::normalize_docker_compose(&docker_compose)?;
74+
75+
// Log for debugging
76+
logging::info!("Deploying agent to TEE with normalized Docker compose YAML");
7777

7878
// Initialize the TeeDeployer
7979
logging::info!("Initializing TeeDeployer for deployment");
@@ -91,43 +91,38 @@ async fn deploy_to_tee(
9191
"No encrypted environment variables provided for TEE deployment".to_string()
9292
})?;
9393

94-
// Create VM configuration using TeeDeployer's native method
94+
// Create VM configuration using our consistent helper function
9595
logging::info!("Creating VM configuration from Docker Compose");
9696
let app_name = format!("coinbase-agent-{}", params.agent_id);
9797
let vm_config = deployer
98-
.create_vm_config_from_string(
98+
.create_vm_config(
9999
&docker_compose,
100100
&app_name,
101-
Some(2), // vcpu
102-
Some(2048), // memory in MB
103-
Some(10), // disk size in GB
101+
Some(2_u64), // vcpu
102+
Some(2048_u64), // memory in MB
103+
Some(10_u64), // disk size in GB
104104
)
105-
.map_err(|e| format!("Failed to create VM configuration: {}", e))?;
106-
105+
.map_err(|e| format!("Failed to deploy with VM configuration: {}", e))?;
106+
let vm_config_json = serde_json::to_value(vm_config)
107+
.map_err(|e| format!("Failed to serialize VM configuration: {}", e))?;
107108
logging::info!(
108109
"Deploying agent to TEE with VM configuration: {:#?}",
109-
vm_config
110+
vm_config_json
110111
);
111112

112113
// Get the public key for this VM configuration
113114
logging::info!("Requesting encryption public key...");
114-
let vm_config_json = serde_json::to_value(&vm_config).unwrap();
115115
let pubkey_response = deployer
116116
.get_pubkey_for_config(&vm_config_json)
117117
.await
118118
.map_err(|e| format!("Failed to get TEE public key: {}", e))?;
119119

120-
logging::info!(
121-
"Deploying agent to TEE with pubkey response: {:#?}",
122-
pubkey_response
123-
);
124-
125120
let pubkey = pubkey_response.app_env_encrypt_pubkey;
126121
let salt = pubkey_response.app_id_salt;
122+
let app_id = pubkey_response.app_id;
127123

128124
// Deploy with the VM configuration and encrypted environment variables
129125
logging::info!("Deploying agent to TEE with encrypted environment variables");
130-
let vm_config_json = serde_json::to_value(&vm_config).unwrap();
131126
let deployment = deployer
132127
.deploy_with_encrypted_env(vm_config_json, encrypted_env.clone(), &pubkey, &salt)
133128
.await
@@ -138,9 +133,8 @@ async fn deploy_to_tee(
138133
// Prepare the deployment result
139134
let result = AgentDeploymentResult {
140135
agent_id: params.agent_id.clone(),
141-
endpoint: None,
142136
tee_pubkey: Some(pubkey),
143-
tee_salt: Some(salt),
137+
tee_app_id: Some(app_id),
144138
};
145139

146140
// Serialize the result
@@ -231,9 +225,8 @@ async fn deploy_locally(
231225
// Prepare the deployment result
232226
let result = AgentDeploymentResult {
233227
agent_id: params.agent_id.clone(),
234-
endpoint: Some(endpoint),
235228
tee_pubkey: None,
236-
tee_salt: None,
229+
tee_app_id: None,
237230
};
238231

239232
// Serialize the result
@@ -313,8 +306,7 @@ fn create_env_content(
313306
WEBSOCKET_URL=ws://localhost:{websocket_port}\n\
314307
OPENAI_API_KEY={openai_api_key}\n\
315308
CDP_API_KEY_NAME={cdp_api_key_name}\n\
316-
CDP_API_KEY_PRIVATE_KEY={cdp_api_key_private_key}\n\
317-
RUN_TESTS=false\n"
309+
CDP_API_KEY_PRIVATE_KEY={cdp_api_key_private_key}\n"
318310
);
319311

320312
Ok(env_content)

0 commit comments

Comments
 (0)