Skip to content

Commit

Permalink
Merge pull request #3 from Smehnov/feature/config-update
Browse files Browse the repository at this point in the history
Config updates + signed messages
  • Loading branch information
Smehnov authored Aug 14, 2024
2 parents b7ee30f + 6b1efa5 commit affb428
Show file tree
Hide file tree
Showing 8 changed files with 547 additions and 338 deletions.
36 changes: 0 additions & 36 deletions src/agent.rs

This file was deleted.

26 changes: 13 additions & 13 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ use clap::Parser;
#[derive(Parser, Debug, Clone)]
#[command(author, version, about, long_about = None)]
pub struct Args {
#[arg(short, long)]
pub api_key: String,

#[arg(short, long, default_value = "https://robots.merklebot.com")]
pub robot_server_url: String,

#[arg(short, long, default_value = "normal")]
#[arg(short = 'm', long, default_value = "normal")]
pub mode: String,

#[arg(short, long, default_value = "merklebot.socket")]
#[arg(short = 's', long, default_value = "rn.socket")]
pub socket_filename: String,

#[arg(short, long, default_value = "merklebot.key")]
#[arg(short = 'f', long, default_value = "rn.key")]
pub key_filename: String,

#[arg(short, long)]
#[arg(short = 'b', long)]
pub bootstrap_addr: Option<String>,

#[arg(short, long, default_value = "8765")]
#[arg(short = 'l', long, default_value = "8765")]
pub port_libp2p: String,

#[arg(short, long)]
pub owner: Option<String>,
#[arg(short = 'c', long, default_value = "rn.json")]
pub config_path: String,

#[arg(short = 'k', long)]
pub secret_key: Option<String>,

#[arg(short = 'o', long)]
pub owner: String,
}

pub fn get_args() -> Args {
Expand Down
101 changes: 50 additions & 51 deletions src/commands/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use serde::{Deserialize, Serialize};

use base64::{engine::general_purpose, Engine as _};

use crate::agent;
use crate::{
commands::{RobotJob, RobotJobResult},
store::Jobs,
Expand All @@ -21,37 +20,38 @@ use crate::{
},
};

pub async fn execute_launch(
socket: Option<Client>,
robot_job: RobotJob,
agent: agent::Agent,
jobs: Jobs,
) {
pub async fn execute_launch(socket: Option<Client>, robot_job: RobotJob, jobs: Jobs) {
let args = serde_json::from_str::<DockerLaunchArgs>(&robot_job.args).unwrap();
info!("launching docker job {:?}", args);
let docker_launch = DockerLaunch { args };
let robot_job_result = match docker_launch.execute(robot_job.clone(), agent, jobs).await {
Ok(result) => {
info!("job successfully executed");
result
}
Err(error) => {
error!("error {:?}", error);
RobotJobResult {
job_id: robot_job.id,
status: String::from("error"),
logs: error.to_string(),
{
let exec_jobs = Arc::clone(&jobs);
let robot_job_result = match docker_launch.execute(robot_job.clone(), exec_jobs).await {
Ok(result) => {
info!("job successfully executed");
result
}
}
};
match socket {
Some(socket) => {
let _ = socket
.emit("job_done", serde_json::json!(robot_job_result))
.await;
}
None => {}
Err(error) => {
error!("error {:?}", error);
RobotJobResult {
job_id: robot_job.id,
status: String::from("error"),
logs: error.to_string(),
}
}
};
let mut job_manager = jobs.lock().unwrap();
job_manager.set_job_result(robot_job_result);
}

// match socket {
// Some(socket) => {
// let _ = socket
// .emit("job_done", serde_json::json!(robot_job_result))
// .await;
// }
// None => {}
// }
}

#[derive(Clone, Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -81,7 +81,6 @@ impl DockerLaunch {
pub async fn execute(
&self,
robot_job: RobotJob,
agent: agent::Agent,
jobs: Jobs,
) -> Result<RobotJobResult, bollard::errors::Error> {
info!("launching docker with image {}", self.args.image);
Expand Down Expand Up @@ -302,29 +301,29 @@ impl DockerLaunch {
};
let job_data_path = get_job_data_path(&robot_job.id);

if let Some(true) = &self.args.store_data {
match get_files_in_directory_recursively(&job_data_path) {
//TODO: change to path
Ok(paths) => {
info!("{:?}", paths);
for path in paths {
let path_str = path.as_path().display().to_string();
let key = path_str.replace(&get_merklebot_data_path(), "");
upload_content(
agent.robot_server_url.clone(),
path,
key,
robot_job.id.clone(),
agent.api_key.clone(),
)
.await;
}
}
_ => {
error!("Can't get resulting paths");
}
}
}
// if let Some(true) = &self.args.store_data {
// match get_files_in_directory_recursively(&job_data_path) {
// //TODO: change to path
// Ok(paths) => {
// info!("{:?}", paths);
// for path in paths {
// let path_str = path.as_path().display().to_string();
// let key = path_str.replace(&get_merklebot_data_path(), "");
// upload_content(
// agent.robot_server_url.clone(),
// path,
// key,
// robot_job.id.clone(),
// agent.api_key.clone(),
// )
// .await;
// }
// }
// _ => {
// error!("Can't get resulting paths");
// }
// }
// }
Ok(robot_job_result)
}
}
28 changes: 9 additions & 19 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use serde_json::json;
use tokio::sync::broadcast::Sender;
use tracing::{error, info};

use crate::agent;
use crate::store;
use crate::store::ChannelMessageFromJob;
use crate::store::Message;
Expand Down Expand Up @@ -69,30 +68,21 @@ pub enum TunnnelClient {
},
}

pub async fn launch_new_job(
robot_job: RobotJob,
socket: Option<Client>,
agent: agent::Agent,
jobs: store::Jobs,
) {
pub async fn launch_new_job(robot_job: RobotJob, socket: Option<Client>, jobs: store::Jobs) {
info!("{:?}", robot_job);
let mut job_manager = jobs.lock().unwrap();
job_manager.new_job(
robot_job.id.clone(),
robot_job.job_type.clone(),
robot_job.status.clone(),
);
job_manager.set_job_status(robot_job.id.clone(), "processing".to_string());

match robot_job.job_type.as_str() {
"docker-container-launch" => {
info!("container launch");
let mut job_manager = jobs.lock().unwrap();
job_manager.new_job(
robot_job.id.clone(),
robot_job.job_type.clone(),
robot_job.status.clone(),
);
let shared_jobs = Arc::clone(&jobs);
tokio::spawn(docker::execute_launch(
socket,
robot_job,
agent,
shared_jobs,
));
tokio::spawn(docker::execute_launch(socket, robot_job, shared_jobs));
}
_ => {}
}
Expand Down
Loading

0 comments on commit affb428

Please sign in to comment.