|
| 1 | +//! Provision and execute parsec. |
| 2 | +
|
| 3 | +use anyhow::anyhow; |
| 4 | +use log::{error, info}; |
| 5 | +use utils::attestation; |
| 6 | +use transport::{messages::{Request, Response, Status}, session::Session}; |
| 7 | + |
| 8 | +use std::fs; |
| 9 | +use std::fs::File; |
| 10 | +use std::io::Read; |
| 11 | + |
| 12 | +const DPU_SERVER_URL: &str = "127.0.0.1:6666"; |
| 13 | +const ATTESTATION_SERVER_URL: &str = "127.0.0.1:3010"; |
| 14 | + |
| 15 | +const PARSEC_APP_NAME: &str = "/tmp/dpu/parsec_app"; |
| 16 | +const PARSEC_PERM_CMD: &str = "chmod +x /tmp/dpu/parsec_app"; |
| 17 | +const PARSEC_EXECUTE_CMD: &str = "PARSEC_SERVICE_CONN_TYPE=tcp PARSEC_SERVICE_CONN_IP_ADDR=172.28.97.222 PARSEC_SERVICE_CONN_PORT_NO=8002 /tmp/dpu/parsec_app ping"; |
| 18 | + |
| 19 | +// Read program file |
| 20 | +fn get_file_data(filename: &String) -> anyhow::Result<Vec<u8>> { |
| 21 | + |
| 22 | + let mut file = File::open(&filename).unwrap(); |
| 23 | + let metadata = fs::metadata(&filename).unwrap(); |
| 24 | + let mut file_content = vec![0; metadata.len() as usize]; |
| 25 | + file.read(&mut file_content).unwrap(); |
| 26 | + |
| 27 | + Ok(file_content) |
| 28 | +} |
| 29 | + |
| 30 | +fn main() -> anyhow::Result<()> { |
| 31 | + env_logger::init(); |
| 32 | + |
| 33 | + // TODO: Parse arguments with clap |
| 34 | + let dpu_server_url = DPU_SERVER_URL; |
| 35 | + let proxy_attestation_server_url = ATTESTATION_SERVER_URL; |
| 36 | + |
| 37 | + let dpu_session_id = Session::from_url(dpu_server_url)?; |
| 38 | + |
| 39 | + attestation::request_attestation(dpu_session_id, proxy_attestation_server_url)?; |
| 40 | + info!("Successfully attested DPU."); |
| 41 | + |
| 42 | + info!("Preparing parsec-tool executable data..."); |
| 43 | + let filename = PARSEC_APP_NAME.to_owned(); |
| 44 | + let data = get_file_data(&filename).unwrap(); |
| 45 | + |
| 46 | + info!("Provisioning parsec-tool to DPU..."); |
| 47 | + Session::send_message(dpu_session_id, &Request::UploadFile(filename, data)) |
| 48 | + .map_err(|e| { |
| 49 | + error!("Failed to send provisioning message to DPU. Error returned: {:?}.", e); |
| 50 | + e |
| 51 | + })?; |
| 52 | + let response = Session::receive_message(dpu_session_id).map_err(|e| { |
| 53 | + error!("Failed to receive response to provisioning message. Error received: {:?}.", e); |
| 54 | + e |
| 55 | + })?; |
| 56 | + match response { |
| 57 | + Response::Status(Status::Success(_)) => { |
| 58 | + info!("Successfully provisioned DPU."); |
| 59 | + }, |
| 60 | + _ => { |
| 61 | + error!("Error provisioning DPU."); |
| 62 | + return Err(anyhow!("Provisioning failure.")); |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + info!("Requesting permission change..."); |
| 67 | + Session::send_message(dpu_session_id, &Request::Execute(PARSEC_PERM_CMD.to_owned())) |
| 68 | + .map_err(|e| { |
| 69 | + error!("Failed to send execution message to DPU. Error returned: {:?}.", e); |
| 70 | + e |
| 71 | + })?; |
| 72 | + let response = Session::receive_message(dpu_session_id).map_err(|e| { |
| 73 | + error!("Failed to receive response to execution message. Error received: {:?}.", e); |
| 74 | + e |
| 75 | + })?; |
| 76 | + match response { |
| 77 | + Response::Status(Status::Success(output)) => { |
| 78 | + info!("Successfully executed code remotely. Output: \n{}", output); |
| 79 | + }, |
| 80 | + _ => { |
| 81 | + error!("Error executing code remotely."); |
| 82 | + return Err(anyhow!("Remote execution failure.")); |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + info!("Requesting parsec-tool execution.."); |
| 87 | + Session::send_message(dpu_session_id, &Request::Execute(PARSEC_EXECUTE_CMD.to_owned())) |
| 88 | + .map_err(|e| { |
| 89 | + error!("Failed to send execution message to DPU. Error returned: {:?}.", e); |
| 90 | + e |
| 91 | + })?; |
| 92 | + let response = Session::receive_message(dpu_session_id).map_err(|e| { |
| 93 | + error!("Failed to receive response to execution message. Error received: {:?}.", e); |
| 94 | + e |
| 95 | + })?; |
| 96 | + match response { |
| 97 | + Response::Status(Status::Success(output)) => { |
| 98 | + info!("Successfully executed code remotely. Output: \n{}", output); |
| 99 | + }, |
| 100 | + _ => { |
| 101 | + error!("Error executing code remotely."); |
| 102 | + return Err(anyhow!("Remote execution failure.")); |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + Ok(()) |
| 107 | +} |
0 commit comments