add log function, and join the new object to replaces all thoses stupids functions. The arn't removed yet.

This commit is contained in:
primardj 2024-04-11 13:30:59 +01:00
parent a6debb3e51
commit 940f2957e3
5 changed files with 173 additions and 104 deletions

View file

@ -2,8 +2,7 @@
pub mod sent_signal;
use std::path::Path;
use std::env::set_current_dir;
use crate::status_obj;
pub fn execute(svdir_var: String, svwait_var: i32, verbose: i8, command: &str, services: Vec<String>) -> i32{
@ -31,19 +30,33 @@ pub fn execute(svdir_var: String, svwait_var: i32, verbose: i8, command: &str, s
path = svdir_var.clone() + "/" + &sv;
}
let status_option = status_obj::StatusObj::new(path, sv);
match status_option {
Some(status) => {
exit_code += sent_signal::sent_signal(svwait_var, verbose, command, status);
},
None => {
if exit_code < 99 {
exit_code += 1;
}
},
};
// 1. Check if path exists.
//
let path_to_sv = Path::new(&path);
//let path_to_sv = Path::new(&path);
if set_current_dir(path_to_sv).is_ok() {
// Call the change in the service.
exit_code = sent_signal::sent_signal(exit_code, svwait_var, verbose, command, sv);
} else {
println!("fail: {}: unable to change to service directory: file does not exist", sv);
if exit_code < 99 {
exit_code += 1;
}
}
//if set_current_dir(path_to_sv).is_ok() {
// // Call the change in the service.
// exit_code = sent_signal::sent_signal(exit_code, svwait_var, verbose, command, sv);
//} else {
// println!("fail: {}: unable to change to service directory: file does not exist", sv);
// if exit_code < 99 {
// exit_code += 1;
// }
//}
}

View file

@ -1,25 +1,42 @@
pub mod exec_status;
pub mod make_exit_code;
pub mod return_u8_in_u32;
pub mod return_u8_in_u64;
//pub mod exec_status;
//pub mod make_exit_code;
//pub mod return_u8_in_u32;
//pub mod return_u8_in_u64;
use std::path::Path;
use crate::status_obj;
pub fn sent_signal(exit_code: i32, svwait_var: i32, verbose: i8, command: &str, sv: String) -> i32 {
//use std::path::Path;
pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_obj::StatusObj) -> i32 {
// Return 0 in case everything worked fine, return 1 if timeout or error.
// Check if supervise/ok exist.
let supervise_ok = Path::new("supervise/ok");
if ! supervise_ok.exists() {
println!("warning: {}: unable to open supervise/ok: file does not exist", sv.clone());
return make_exit_code::make_exit_code(exit_code);
}
//let supervise_ok = Path::new("supervise/ok");
//if ! supervise_ok.exists() {
// println!("warning: {}: unable to open supervise/ok: file does not exist", sv.clone());
// return make_exit_code::make_exit_code(exit_code);
//}
// execute command.
if command == "s" {
// case command is status
return exec_status::exec_status(exit_code, sv);
let mut status_sv = sv.get_status_string();
let is_log = sv.get_log();
if is_log {
let log_option = sv.get_obj_log();
match log_option {
Some(mut log) => {
status_sv = status_sv + "; " + &log.get_status_string();
},
None => {// TODO: Do nothing for now, eventually, increase by one exit code.
},
};
}
println!("{}", status_sv);
return 0;
}
//let service = fs::OpenOptions::new().append(true).open("supervise/control");
@ -38,5 +55,6 @@ pub fn sent_signal(exit_code: i32, svwait_var: i32, verbose: i8, command: &str,
// sent the proper signal to supervise/control
//
// if verbose = 1, wait for the service to reach the proper state or timeout.
return exit_code;
println!("Error, command not implemented.");
return 1;
}

View file

@ -1,6 +1,7 @@
mod parser;
mod executor;
pub mod status_obj;
use std::env;
use std::process::exit;

View file

@ -1,9 +1,14 @@
mod misc;
use std::path::Path;
use std::env::set_current_dir;
use std::fs;
use std::io::prelude::*;
pub struct Status_obj {
pub struct StatusObj {
svname: String, // Service name
svpath: Path, // Service path
svpath: String, // Service path
time: u64, // 0 -> 7
nano_seconds: u32, // 8 -> 11
pid: u32, // 12 -> 15
@ -15,16 +20,25 @@ pub struct Status_obj {
log: bool, // is log/supervise/ok exists ?
}
impl Status_obj {
pub fn new(path: Path, sv: String ) -> Option<Status_obj> {
impl StatusObj {
pub fn new(path_string: String, sv: String ) -> Option<StatusObj> {
// if error when making the status_obj, return None,
// Else, return the status_obj.
//
let path = Path::new(&path_string);
if ! set_current_dir(path).is_ok() {
println!("fail: {}: unable to change to service directory: file does not exist", sv);
return None
}
let ok_option = Path::new("supervise/ok");
if ! ok_option.exists() {
println!("warning: {}: unable to open supervise/ok: file does not exist", sv);
return None;
}
let status_option = fs::OpenOptions::new().read(true).open("supervise/status");
let mut status match status_option {
let mut status = match status_option {
Ok(file) => file,
Err(..) => {
println!("warning: {}: unable to open supervise/status: file does not exist", sv);
@ -65,18 +79,18 @@ impl Status_obj {
let nano_buf: Vec<u8> = contents[8..12].to_vec();
let pid_buf: Vec<u8> = contents[12..16].to_vec();
let seconds = return_u8_in_u64(time_buf);
let nano = return_u8_in_u32(nano_buf);
let pid_found = return_u8_in_u32(pid_buf);
let seconds = misc::return_u8_in_u64(time_buf);
let nano = misc::return_u8_in_u32(nano_buf);
let pid_found = misc::return_u8_in_u32(pid_buf);
let pause_run_raw = make_pause_run(contents[16]); // done
let up_down_raw = make_up_down(contents[17]); // done
let term_sig_raw = make_term_sig(contents[18]); // done
let run_finish_raw = make_run_finish(contents[19]); // done
let pause_run_raw = misc::make_pause_run(contents[16]); // done
let up_down_raw = misc::make_up_down(contents[17]); // done
let term_sig_raw = misc::make_term_sig(contents[18]); // done
let run_finish_raw = misc::make_run_finish(contents[19]); // done
Status_obj {
Some(StatusObj {
svname: sv,
svpath: path,
svpath: path_string,
time: seconds,
nano_seconds: nano,
pid: pid_found,
@ -86,72 +100,33 @@ impl Status_obj {
run_finish: run_finish_raw,
down: is_down,
log: log_exists,
}
})
}
pub fn print_status(&mut self) {
println!("Ta gueule.");
pub fn get_status_string(&mut self) -> String {
dbg!(&self.svname);
dbg!(&self.svpath);
dbg!(self.time);
dbg!(self.nano_seconds);
dbg!(self.pid);
dbg!(self.pause_run);
dbg!(self.up_down);
dbg!(self.term_sig);
dbg!(self.run_finish);
dbg!(self.down);
dbg!(self.log);
// TODO
return "Ta gueule".to_string();
}
pub fn get_log(&mut self) -> bool {
return self.log;
}
pub fn get_obj_log(&mut self) -> Option<StatusObj> {
return StatusObj::new(self.svpath.clone() + "/log", "log".to_string());
}
}
fn make_pause_run(pr_int: u8) -> bool {
if pr_int == 0 {
return false // run
} else {
return true // pause.
}
}
fn make_up_down(ud_int: u8) -> char {
if ud_int == 117 {
return 'u'
} else {
return 'd'
}
}
fn make_term_sig(ts_int: u8) -> bool{
if ts_int == 0 {
return false // Normal
} else {
return true // Got Term
}
}
fn make_run_finish(rf_int: u8) -> bool {
if rf_int == 0 {
return false // service finish.
} else {
return true // service run
}
}
fn return_u8_in_u32(mut table: Vec<u8>) -> u32 {
// Table make 4 values u8 in one u32
if table.len() != 4 {
return 0
}
let mut r: u32 = 0;
table.reverse();
for i in table.iter() {
//println!("{}",i);
r = r * 256;
r += <u8 as Into<u32>>::into(*i);
}
return r
}
fn return_u8_in_u64(mut table: Vec<u8>) -> u64 {
// Table make 8 values u8 in one u64
if table.len() != 8 {
return 0
}
let mut r: u64 = 0;
table.reverse();
for i in table.iter() {
//println!("{}",i);
r = r * 256;
r += <u8 as Into<u64>>::into(*i);
}
return r
}

View file

@ -0,0 +1,62 @@
pub fn make_pause_run(pr_int: u8) -> bool {
if pr_int == 0 {
return false // run
} else {
return true // pause.
}
}
pub fn make_up_down(ud_int: u8) -> char {
if ud_int == 117 {
return 'u'
} else {
return 'd'
}
}
pub fn make_term_sig(ts_int: u8) -> bool{
if ts_int == 0 {
return false // Normal
} else {
return true // Got Term
}
}
pub fn make_run_finish(rf_int: u8) -> bool {
if rf_int == 0 {
return false // service finish.
} else {
return true // service run
}
}
pub fn return_u8_in_u32(mut table: Vec<u8>) -> u32 {
// Table make 4 values u8 in one u32
if table.len() != 4 {
return 0
}
let mut r: u32 = 0;
table.reverse();
for i in table.iter() {
//println!("{}",i);
r = r * 256;
r += <u8 as Into<u32>>::into(*i);
}
return r
}
pub fn return_u8_in_u64(mut table: Vec<u8>) -> u64 {
// Table make 8 values u8 in one u64
if table.len() != 8 {
return 0
}
let mut r: u64 = 0;
table.reverse();
for i in table.iter() {
//println!("{}",i);
r = r * 256;
r += <u8 as Into<u64>>::into(*i);
}
return r
}