split executor file in a lot of smaller files, make basic status function.

This commit is contained in:
primardj 2024-04-09 00:40:01 +01:00
parent 6e497b3164
commit 84622ce986
11 changed files with 233 additions and 115 deletions

View file

@ -1,122 +1,10 @@
use std::fs; pub mod sent_signal;
use std::path::Path; use std::path::Path;
use std::env::set_current_dir; use std::env::set_current_dir;
use std::io::prelude::*;
fn make_exit_code(exit_code: i32) -> i32 {
if exit_code < 99 {
return exit_code + 1;
} else {
return exit_code;
}
}
fn return_u8_in_u32(mut table: Vec<u8>) -> u32 {
// Table make 4 vector 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 exec_status(exit_code: i32, sv: String) -> i32 {
let status_option = fs::OpenOptions::new().read(true).open("supervise/status");
// works this way :
// 20 chars,
// TODO 0 -> 11; time since I don't know when. It is time_sv = 4611686018427387914 + time_unix
// 12 -> 15; PID
// 16; PAUSE/RUN
// 17; UP/DOWN
// 18; TERM SIG
// 19; RUN/FINISH
//
// In this version, don't take in account the time. (char 0 -> 11)
if ! status_option.is_ok() {
println!("ERROR, Unable to get supervise/status");
return make_exit_code(exit_code);
}
let mut status = match status_option {
Ok(file) => file,
Err(..) => {
println!("warning: {}: unable to open supervise/status: file does not exist", sv.clone());
return make_exit_code(exit_code);
},
};
let mut contents: Vec<u8> = Vec::new();
let option_size = status.read_to_end(&mut contents);
let size = match option_size {
Ok(size) => size,
Err(..) => {
println!("warning: {}: couldn't read supervise/status", sv.clone());
return make_exit_code(exit_code);
},
};
let time_buf: Vec<u8> = contents[0..12].to_vec();
let pid_buf: Vec<u8> = contents[12..16].to_vec();
let pid: u32 = return_u8_in_u32(pid_buf.clone()); // PID OK
let pause_run: u8 = contents[16]; // TODO function truc(contents) -> String
let up_down: u8 = contents[17]; // TODO function truc(contents) -> String
let term_sig: u8 = contents[18]; // TODO function truc(contents) -> String
let run_finish: u8 = contents[19]; // TODO function truc(contents) -> String
dbg!(contents);
dbg!(time_buf);
dbg!(pid_buf);
dbg!(pid);
return exit_code;
}
fn sent_signal(exit_code: i32, svwait_var: i32, verbose: i8, command: &str, sv: String) -> i32 {
// 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(exit_code);
}
// execute command.
if command == "s" {
// case command is status
return exec_status(exit_code, sv);
}
//let service = fs::OpenOptions::new().append(true).open("supervise/control");
//if ! service.is_ok() {
// println!("warning: {}: unable to append to supervise/control: file does not exist", sv.clone());
// return make_exit_code(exit_code);
//}
//let mut control = match service {
// Ok(file) => file,
// Err(..) => panic!("Shouln't goes there, the programmer is trash."),
//};
// TODO
// sent the proper signal to supervise/control
//
// if verbose = 1, wait for the service to reach the proper state or timeout.
return exit_code;
}
pub fn execute(svdir_var: String, svwait_var: i32, verbose: i8, command: &str, services: Vec<String>) -> i32{ pub fn execute(svdir_var: String, svwait_var: i32, verbose: i8, command: &str, services: Vec<String>) -> i32{
println!("execute in exec"); println!("execute in exec");
@ -149,7 +37,7 @@ pub fn execute(svdir_var: String, svwait_var: i32, verbose: i8, command: &str, s
if set_current_dir(path_to_sv).is_ok() { if set_current_dir(path_to_sv).is_ok() {
// Call the change in the service. // Call the change in the service.
exit_code = sent_signal(exit_code, svwait_var, verbose, command, sv); exit_code = sent_signal::sent_signal(exit_code, svwait_var, verbose, command, sv);
} else { } else {
println!("fail: {}: unable to change to service directory: file does not exist", sv); println!("fail: {}: unable to change to service directory: file does not exist", sv);
if exit_code < 99 { if exit_code < 99 {

View file

@ -0,0 +1,42 @@
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;
pub fn sent_signal(exit_code: i32, svwait_var: i32, verbose: i8, command: &str, sv: String) -> i32 {
// 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);
}
// execute command.
if command == "s" {
// case command is status
return exec_status::exec_status(exit_code, sv);
}
//let service = fs::OpenOptions::new().append(true).open("supervise/control");
//if ! service.is_ok() {
// println!("warning: {}: unable to append to supervise/control: file does not exist", sv.clone());
// return make_exit_code(exit_code);
//}
//let mut control = match service {
// Ok(file) => file,
// Err(..) => panic!("Shouln't goes there, the programmer is trash."),
//};
// TODO
// sent the proper signal to supervise/control
//
// if verbose = 1, wait for the service to reach the proper state or timeout.
return exit_code;
}

View file

@ -0,0 +1,84 @@
mod make_pause_run;
mod make_run_finish;
mod make_term_sig;
mod make_up_down;
mod make_normal_state;
use crate::executor::sent_signal::make_exit_code::make_exit_code;
use crate::executor::sent_signal::return_u8_in_u32::return_u8_in_u32;
use crate::executor::sent_signal::return_u8_in_u64::return_u8_in_u64;
use std::fs;
use std::io::prelude::*;
pub fn exec_status(exit_code: i32, sv: String) -> i32 {
let status_option = fs::OpenOptions::new().read(true).open("supervise/status");
// works this way :
// 20 chars,
// TODO 0 -> 11; time since I don't know when. It is time_sv = 4611686018427387914 + time_unix
// TODO
// TODO seams to be 0 -> 7 time, 7 -> 11 uknown.
// 12 -> 15; PID
// 16; PAUSE/RUN
// 17; UP/DOWN
// 18; TERM SIG
// 19; RUN/FINISH
//
// In this version, don't take in account the time. (char 0 -> 11)
if ! status_option.is_ok() {
println!("ERROR, Unable to get supervise/status");
return make_exit_code(exit_code);
}
let mut status = match status_option {
Ok(file) => file,
Err(..) => {
println!("warning: {}: unable to open supervise/status: file does not exist", sv.clone());
return make_exit_code(exit_code);
},
};
let mut contents: Vec<u8> = Vec::new();
let option_size = status.read_to_end(&mut contents);
let size = match option_size {
Ok(size) => size,
Err(..) => {
println!("warning: {}: couldn't read supervise/status", sv.clone());
return make_exit_code(exit_code);
},
};
let time_buf: Vec<u8> = contents[0..8].to_vec();
let uknown_buf: Vec<u8> = contents[8..12].to_vec(); // TODO
let pid_buf: Vec<u8> = contents[12..16].to_vec();
let time = return_u8_in_u64(time_buf); // TODO: correct the unsigned/signed bug.
let pid: u32 = return_u8_in_u32(pid_buf.clone());
let pause_run = make_pause_run::mpr(contents[16]);
let up_down = make_up_down::mud(contents[17]);
let term_sig = make_term_sig::mts(contents[18]);
let run_finish = make_run_finish::mrf(contents[19]);
let normal_state = make_normal_state::mns(contents[19]);
dbg!(contents);
//dbg!(time_buf);
dbg!(pause_run);
dbg!(up_down);
dbg!(term_sig);
if pid != 0 {
println!("{run_finish}{sv}: (pid {pid}) {time}s{normal_state}");
} else {
println!("{run_finish}{sv}: {time}s{normal_state}");
}
return exit_code;
}

View file

@ -0,0 +1,20 @@
use std::path::Path;
pub fn mns(actual_state: u8) -> &'static str {
let down_option = Path::new("down");
if down_option.exists() {
if actual_state == 0 {
return "";
} else {
return ", normally down";
}
} else {
if actual_state == 0 {
return ", normally up";
} else {
return "";
}
}
}

View file

@ -0,0 +1,9 @@
pub fn mpr(pause_run_int: u8) -> &'static str {
if pause_run_int == 0 {
return ""
} else {
return ", paused"
}
}

View file

@ -0,0 +1,10 @@
pub fn mrf(run_finish_int: u8) -> &'static str {
if run_finish_int == 0 {
return "down: "
} else {
return "run: "
}
}

View file

@ -0,0 +1,10 @@
pub fn mts(term_sig_int: u8) -> &'static str {
if term_sig_int == 0 {
return ""
} else {
return ", got TERM"
}
}

View file

@ -0,0 +1,11 @@
pub fn mud(up_down_int: u8) -> &'static str {
if up_down_int == 117 {
return ", want up"
} else {
return ", want down"
}
}

View file

@ -0,0 +1,9 @@
pub fn make_exit_code(exit_code: i32) -> i32 {
if exit_code < 99 {
return exit_code + 1;
} else {
return exit_code;
}
}

View file

@ -0,0 +1,18 @@
pub fn return_u8_in_u32(mut table: Vec<u8>) -> u32 {
// Table make 4 vector 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
}

View file

@ -0,0 +1,17 @@
pub fn return_u8_in_u64(mut table: Vec<u8>) -> u64 {
// Table make 4 vector u8 in one u32
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
}