diff --git a/Cargo.toml b/Cargo.toml index 79b3e0b..80dbc54 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,5 @@ hyperlocal = "0.7" serde = {version = "1.0.116", features = ["derive"]} serde_json = "1.0.57" tokio = {version = "0.2.22", features = ["full"]} +tracing = "0.1" +tracing-subscriber = "0.2" diff --git a/src/api/create.rs b/src/api/create.rs index 72e3aa7..d6f2d15 100644 --- a/src/api/create.rs +++ b/src/api/create.rs @@ -1,4 +1,5 @@ use hyper::{Body, Request, Response, StatusCode}; +use tracing::error; use super::VmInput; use crate::{vm, State}; @@ -9,7 +10,7 @@ pub async fn handler(req: Request, state: State) -> Result, let body: VmInput = match serde_json::from_slice(body_bytes) { Ok(j) => j, Err(e) => { - eprintln!("{}", e); + error!("{}", e); let mut error = Response::default(); *error.status_mut() = StatusCode::BAD_REQUEST; @@ -21,7 +22,7 @@ pub async fn handler(req: Request, state: State) -> Result, match vm::spawn(&body.vm_name, state).await { Ok(_) => {} Err(e) => { - eprintln!("{}", e); + error!("{}", e); let mut error = Response::default(); *error.status_mut() = StatusCode::BAD_REQUEST; diff --git a/src/api/delete.rs b/src/api/delete.rs index 2a16b6a..fa4517e 100644 --- a/src/api/delete.rs +++ b/src/api/delete.rs @@ -1,4 +1,5 @@ use hyper::{Body, Request, Response, StatusCode}; +use tracing::error; use super::VmInput; use crate::{vm, State}; @@ -9,7 +10,7 @@ pub async fn handler(req: Request, state: State) -> Result, let body: VmInput = match serde_json::from_slice(body_bytes) { Ok(j) => j, Err(e) => { - eprintln!("{}", e); + error!("{}", e); let mut error = Response::default(); *error.status_mut() = StatusCode::BAD_REQUEST; diff --git a/src/api/kill.rs b/src/api/kill.rs index 5509bc4..2cea430 100644 --- a/src/api/kill.rs +++ b/src/api/kill.rs @@ -1,4 +1,5 @@ use hyper::{Body, Request, Response, StatusCode}; +use tracing::error; use super::VmInput; use crate::State; @@ -9,7 +10,7 @@ pub async fn handler(req: Request, state: State) -> Result, let body: VmInput = match serde_json::from_slice(body_bytes) { Ok(j) => j, Err(e) => { - eprintln!("{}", e); + error!("{}", e); let mut error = Response::default(); *error.status_mut() = StatusCode::BAD_REQUEST; diff --git a/src/io.rs b/src/io.rs index 5df0381..6421435 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,12 +1,13 @@ use std::fs; use std::io::{Error, ErrorKind}; use std::path::PathBuf; +use tracing::{error, info, warn}; pub fn create_folder(path: PathBuf) -> Result<(), std::io::Error> { validate_path(&path)?; if let Ok(_) = check_exists(&path) { - println!("Path create successfully [{}]", path.display()); + info!("Path create successfully [{}]", path.display()); fs::create_dir(path)?; return Ok(()); } @@ -20,12 +21,12 @@ pub fn create_folder(path: PathBuf) -> Result<(), std::io::Error> { match fs::read_dir(&path) { Ok(entries) => { - println!("Using folder [{}]", path.display()); + info!("Using folder [{}]", path.display()); if entries.peekable().peek().is_some() { - println!("Warning: tmp dir not empty [{}]", path.display()); + warn!("Warning: tmp dir not empty [{}]", path.display()); } } - Err(e) => println!("Cannot read dir: {}", e), + Err(e) => error!("Cannot read dir [{}]", e), }; Ok(()) @@ -38,9 +39,6 @@ pub fn copy_file(src: PathBuf, dest: PathBuf) -> Result<(), std::io::Error> { validate_path(&dest)?; check_exists(&dest)?; - println!("src: {:?}", src); - println!("dest: {:?}", dest); - match fs::copy(src, dest) { Ok(_) => Ok(()), Err(e) => Err(e), @@ -87,6 +85,7 @@ fn check_exists(path: &PathBuf) -> Result<(), std::io::Error> { Ok(()) } +//TODO: figure out testing // #[cfg(test)] // mod test { // #[test] diff --git a/src/main.rs b/src/main.rs index 6818639..33abed9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,9 @@ use clap::{load_yaml, App}; use hyper::service::{make_service_fn, service_fn}; use hyper::Server; use serde::{Deserialize, Serialize}; +use std::env; use std::sync::{Arc, Mutex}; +use tracing::info; mod api; mod error; @@ -38,6 +40,12 @@ impl Clone for State { #[tokio::main] async fn main() -> Result<(), Box> { + if let Err(_) = env::var("RUST_LOG") { + env::set_var("RUST_LOG", "info"); + } + + tracing_subscriber::fmt::init(); + let yaml = load_yaml!("cli.yml"); let matches = App::from_yaml(yaml).get_matches(); @@ -68,7 +76,7 @@ async fn main() -> Result<(), Box> { let server = Server::bind(&addr).serve(service); - println!("Listening on http://{}", addr); + info!("Listening on http://{}", addr); server.await?; diff --git a/src/tmp.rs b/src/tmp.rs index 79501d8..7124c4e 100644 --- a/src/tmp.rs +++ b/src/tmp.rs @@ -1,8 +1,10 @@ use std::io::ErrorKind; use std::path::PathBuf; +use tracing::error; use crate::io; +//TODO: better error handling pub fn init(path: &str) -> Result<(), std::io::Error> { let path = PathBuf::from(path); @@ -11,7 +13,7 @@ pub fn init(path: &str) -> Result<(), std::io::Error> { Err(e) => match e.kind() { ErrorKind::AlreadyExists => Ok(()), _ => { - println!("{:?}", e.kind()); + error!("{:?}", e.kind()); Err(e) } }, diff --git a/src/vm/child.rs b/src/vm/child.rs index 993dfb1..49b1d56 100644 --- a/src/vm/child.rs +++ b/src/vm/child.rs @@ -1,6 +1,7 @@ use std::process::Stdio; use tokio::process::Command; use tokio::time::{delay_for, Duration}; +use tracing::info; use crate::error::RuntimeError; use crate::vm::http; @@ -22,13 +23,8 @@ pub async fn spawn_process( delay_for(Duration::from_millis(10)).await; set_kernel(vm_name, &state.assets_dir).await?; - println!("kernel"); - set_drive(vm_name, &state.tmp_dir).await?; - println!("drive"); - start_machine(vm_name).await?; - println!("start"); Ok(child) } @@ -41,8 +37,7 @@ pub async fn set_kernel(vm_name: &str, assets_dir: &str) -> Result<(), RuntimeEr "boot_args": "console=ttyS0 reboot=k panic=1 pci=off" }); - println!("{}", url); - println!("{}", body); + info!("Set Kernel [{}]", vm_name); http::send_request(vm_name, url, &body.to_string()).await } @@ -57,8 +52,7 @@ pub async fn set_drive(vm_name: &str, tmp_dir: &str) -> Result<(), RuntimeError> "is_read_only": false }); - println!("{}", url); - println!("{}", body); + info!("Set Drive [{}]", vm_name); http::send_request(vm_name, url, &body.to_string()).await } @@ -69,8 +63,7 @@ pub async fn start_machine(vm_name: &str) -> Result<(), RuntimeError> { "action_type": "InstanceStart", }); - println!("{}", url); - println!("{}", body); + info!("Starting [{}]", vm_name); http::send_request(vm_name, url, &body.to_string()).await } @@ -81,8 +74,7 @@ pub async fn stop_machine(vm_name: &str) -> Result<(), RuntimeError> { "action_type": "SendCtrlAltDel", }); - println!("{}", url); - println!("{}", body); + info!("Stopping [{}]", vm_name); http::send_request(vm_name, url, &body.to_string()).await } diff --git a/src/vm/http.rs b/src/vm/http.rs index c565b9f..e6e4c02 100644 --- a/src/vm/http.rs +++ b/src/vm/http.rs @@ -1,6 +1,7 @@ use hyper::{Body, Client, Method, Request}; use hyperlocal::{UnixClientExt, Uri}; use std::path::Path; +use tracing::info; use crate::error::RuntimeError; @@ -28,7 +29,7 @@ pub async fn send_request(vm_name: &str, url: &str, body: &str) -> Result<(), Ru Err(_) => return Err(RuntimeError::new("error getting response")), }; - println!("Response: {}", res.status()); + info!("{} {}", path.display(), res.status()); Ok(()) } diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 18335ab..c83afc0 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -4,6 +4,7 @@ mod http; mod socket; use tokio::task; +use tracing::{error, info}; use crate::error::RuntimeError; use crate::State; @@ -41,7 +42,10 @@ pub async fn spawn(name: &str, state: State) -> Result<(), RuntimeError> { } if let Err(_) = child.await { - println!("ok") + error!( + "Failed to start machine, proceeding to teardown [{}]", + &name + ); }; drive::delete_drive(&name, &state.tmp_dir).unwrap(); @@ -52,7 +56,7 @@ pub async fn spawn(name: &str, state: State) -> Result<(), RuntimeError> { index => vms.remove(index), }; - println!("{} terminated", name); + info!("Terminated [{}]", name); }); Ok(())