Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kvinwang committed Dec 9, 2024
1 parent 933bb61 commit 26fcc4d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
7 changes: 5 additions & 2 deletions teepod/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,14 @@ impl App {

pub async fn remove_vm(&self, id: &str) -> Result<()> {
let info = self.supervisor.info(id).await?;
let is_running = info.map_or(false, |i| i.state.status.is_running());
let is_running = info.as_ref().map_or(false, |i| i.state.status.is_running());
if is_running {
bail!("VM is running, stop it first");
}
self.supervisor.remove(id).await?;

if info.is_some() {
self.supervisor.remove(id).await?;
}

{
let mut state = self.lock();
Expand Down
11 changes: 11 additions & 0 deletions teepod/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{net::IpAddr, path::PathBuf, str::FromStr};

use anyhow::{bail, Context, Result};
use fs_err as fs;
use rocket::figment::{
providers::{Format, Toml},
Figment,
Expand Down Expand Up @@ -149,6 +150,16 @@ pub struct Config {
pub supervisor: SupervisorConfig,
}

impl Config {
pub fn abs_path(self) -> Result<Self> {
Ok(Self {
image_path: fs::canonicalize(&self.image_path)?,
run_path: fs::canonicalize(&self.run_path)?,
..self
})
}
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(tag = "mode", rename_all = "lowercase")]
pub enum Networking {
Expand Down
2 changes: 1 addition & 1 deletion teepod/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async fn main() -> Result<()> {

let args = Args::parse();
let figment = config::load_config_figment(args.config.as_deref());
let config = Config::extract_or_default(&figment)?;
let config = Config::extract_or_default(&figment)?.abs_path()?;
let api_auth = ApiToken::new(config.auth.tokens.clone(), config.auth.enabled);
let supervisor = {
let cfg = &config.supervisor;
Expand Down
47 changes: 25 additions & 22 deletions teepod/src/main_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,18 @@ pub struct RpcHandler {

impl RpcHandler {
fn compose_file_path(&self, id: &str) -> PathBuf {
self.shared_dir(id)
.join("app-compose.json")
self.shared_dir(id).join("app-compose.json")
}

fn encrypted_env_path(&self, id: &str) -> PathBuf {
self.shared_dir(id)
.join("encrypted-env")
self.shared_dir(id).join("encrypted-env")
}

fn shared_dir(&self, id: &str) -> PathBuf {
self.app
.config
.run_path
.join(id)
.join("shared")
self.app.config.run_path.join(id).join("shared")
}

fn prepare_work_dir(
&self,
id: &str,
req: &VmConfiguration,
) -> Result<VmWorkDir> {
fn prepare_work_dir(&self, id: &str, req: &VmConfiguration) -> Result<VmWorkDir> {
let work_dir = self.app.work_dir(id);
if work_dir.exists() {
anyhow::bail!("The instance is already exists at {}", work_dir.display());
Expand Down Expand Up @@ -94,8 +84,11 @@ impl RpcHandler {
let instance_info = serde_json::json!({
"app_id": app_id,
});
fs::write(shared_dir.join(".instance_info"), serde_json::to_string(&instance_info)?)
.context("Failed to write vm config")?;
fs::write(
shared_dir.join(".instance_info"),
serde_json::to_string(&instance_info)?,
)
.context("Failed to write vm config")?;
}

Ok(work_dir)
Expand Down Expand Up @@ -160,7 +153,10 @@ impl TeepodRpc for RpcHandler {
})
.collect::<Result<Vec<_>>>()?;

let app_id = app_id_of(&request.compose_file);
let app_id = match &request.app_id {
Some(id) => id.clone(),
None => app_id_of(&request.compose_file),
};
let id = uuid::Uuid::new_v4().to_string();
let work_dir = self.prepare_work_dir(&id, &request)?;
let now = SystemTime::now()
Expand All @@ -187,10 +183,17 @@ impl TeepodRpc for RpcHandler {
warn!("Failed to set started: {}", err);
}

self.app
let result = self
.app
.load_vm(&work_dir, &Default::default())
.await
.context("Failed to load VM")?;
.context("Failed to load VM");
if let Err(err) = result {
if let Err(err) = fs::remove_dir_all(&work_dir) {
warn!("Failed to remove work dir: {}", err);
}
return Err(err);
}

Ok(Id { id })
}
Expand Down Expand Up @@ -255,8 +258,8 @@ impl TeepodRpc for RpcHandler {
runner: String,
docker_compose_file: Option<String>,
}
let app_compose: AppCompose = serde_json::from_str(&request.compose_file)
.context("Invalid compose file")?;
let app_compose: AppCompose =
serde_json::from_str(&request.compose_file).context("Invalid compose file")?;
if app_compose.docker_compose_file.is_none() {
anyhow::bail!("Docker compose file cannot be empty");
}
Expand All @@ -267,7 +270,7 @@ impl TeepodRpc for RpcHandler {
}
fs::write(compose_file_path, &request.compose_file)
.context("Failed to write compose file")?;

app_id_of(&request.compose_file)
} else {
Default::default()
Expand Down

0 comments on commit 26fcc4d

Please sign in to comment.