use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
pub fn prepare_health_check_command(cmd: &str, container_name: &str) -> String {
if cmd.contains("\n") {
let dir_path = "/root/hero/var/containers";
if let Err(_) = fs::create_dir_all(dir_path) {
return cmd.to_string();
}
let script_path = format!("{}/healthcheck_{}.sh", dir_path, container_name);
if let Err(_) = fs::write(&script_path, cmd) {
return cmd.to_string();
}
if let Ok(metadata) = fs::metadata(&script_path) {
let mut perms = metadata.permissions();
perms.set_mode(0o755);
if let Err(_) = fs::set_permissions(&script_path, perms) {
return format!("sh {}", script_path);
}
} else {
return format!("sh {}", script_path);
}
script_path
} else {
cmd.to_string()
}
}
pub fn cleanup_health_check_scripts(container_name: &str) {
let dir_path = "/root/hero/var/containers";
let script_path = format!("{}/healthcheck_{}.sh", dir_path, container_name);
if Path::new(&script_path).exists() {
let _ = fs::remove_file(script_path);
}
}