Update src/main.rs
This commit is contained in:
parent
2786815330
commit
04e7082d93
102
src/main.rs
102
src/main.rs
@ -6,10 +6,28 @@ const BIN_PATH: &str = "/home/grigoriy/futriix/";
|
||||
const CLUSTER_HOST: &str = "127.0.0.1";
|
||||
const TIMEOUT: &str = "2000";
|
||||
const NODES: usize = 6;
|
||||
//const REPLICAS: usize = 1;
|
||||
const PROTECTED_MODE: &str = "yes";
|
||||
const CONFIG_PATH: &str = "futriix.conf";
|
||||
|
||||
|
||||
fn main() {
|
||||
let mut port = 7000; // Начальный порт
|
||||
let command = env::args().nth(1).unwrap_or_else(|| "start".to_string());
|
||||
|
||||
match command.as_str() {
|
||||
"start" => start_nodes(&mut port),
|
||||
"create-cluster" => create_cluster(&mut port, false),
|
||||
"force-cluster" => create_cluster(&mut port, true),
|
||||
"stop" => stop_nodes(&mut port),
|
||||
"watch" => watch_nodes(&mut port),
|
||||
"clean" => clean(),
|
||||
_ => {
|
||||
eprintln!("Неизвестная команда: {}", command);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn start_nodes(port: &mut u16) {
|
||||
let end_port = *port + NODES as u16;
|
||||
while *port < end_port {
|
||||
@ -42,13 +60,17 @@ fn start_nodes(port: &mut u16) {
|
||||
}
|
||||
*port += 1;
|
||||
}
|
||||
|
||||
// После запуска всех узлов, создаем кластер
|
||||
let force = true; // Устанавливаем параметр force в true
|
||||
create_cluster(&mut *port, force);
|
||||
}
|
||||
|
||||
fn create_cluster(port: &mut u16, force: bool) {
|
||||
let hosts: Vec<String> = (0..NODES).map(|i| format!("{}:{}", CLUSTER_HOST, *port + i as u16)).collect();
|
||||
let hosts: Vec<String> = (0..NODES).map(|i| format!("{}:{}", CLUSTER_HOST, *port + i as u16 - NODES as u16)).collect();
|
||||
|
||||
let command_path = format!("{}/futriix-cli", BIN_PATH); // Создаем переменную для пути
|
||||
let mut command = Command::new(command_path); // Используем переменную
|
||||
let command_path = format!("{}/futriix-cli", BIN_PATH);
|
||||
let mut command = Command::new(command_path);
|
||||
|
||||
command.arg("--cluster")
|
||||
.arg("create")
|
||||
@ -64,7 +86,6 @@ fn create_cluster(port: &mut u16, force: bool) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn stop_nodes(port: &mut u16) {
|
||||
let end_port = *port + NODES as u16;
|
||||
while *port < end_port {
|
||||
@ -105,36 +126,65 @@ fn watch_nodes(port: &mut u16) {
|
||||
|
||||
fn clean() {
|
||||
println!("Очистка логов и данных...");
|
||||
fs::remove_dir_all("appendonlydir-*").ok();
|
||||
fs::remove_dir_all("nodes-*.conf").ok();
|
||||
|
||||
// Удаляем все файлы appendonly.aof
|
||||
for entry in fs::read_dir(".").unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path();
|
||||
if path.is_file() {
|
||||
if let Some(name) = path.file_name() {
|
||||
if name.to_string_lossy() == "appendonly.aof" {
|
||||
if let Err(e) = fs::remove_file(&path) {
|
||||
eprintln!("Не удалось удалить файл {}: {}", name.to_string_lossy(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Удаляем все файлы nodes-*.conf
|
||||
for entry in fs::read_dir(".").unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path();
|
||||
if path.is_file() {
|
||||
if let Some(name) = path.file_name() {
|
||||
if name.to_string_lossy().starts_with("nodes-") && name.to_string_lossy().ends_with(".conf") {
|
||||
if let Err(e) = fs::remove_file(&path) {
|
||||
eprintln!("Не удалось удалить файл {}: {}", name.to_string_lossy(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Удаляем все файлы .aof
|
||||
for entry in fs::read_dir(".").unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path();
|
||||
if path.is_file() {
|
||||
if let Some(name) = path.file_name() {
|
||||
if name.to_string_lossy().ends_with(".aof") {
|
||||
if let Err(e) = fs::remove_file(&path) {
|
||||
eprintln!("Не удалось удалить файл {}: {}", name.to_string_lossy(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Удаляем все файлы .log
|
||||
for entry in fs::read_dir(".").unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path();
|
||||
if path.is_file() {
|
||||
if let Some(name) = path.file_name() {
|
||||
if name.to_string_lossy().ends_with(".log") {
|
||||
fs::remove_file(&path).ok(); // Добавлена точка с запятой
|
||||
if let Err(e) = fs::remove_file(&path) {
|
||||
eprintln!("Не удалось удалить файл {}: {}", name.to_string_lossy(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut port = 7000; // Начальный порт
|
||||
let command = env::args().nth(1).unwrap_or_else(|| "start".to_string());
|
||||
|
||||
match command.as_str() {
|
||||
"start" => start_nodes(&mut port),
|
||||
"create-cluster" => create_cluster(&mut port, false),
|
||||
"force-cluster" => create_cluster(&mut port, true),
|
||||
"stop" => stop_nodes(&mut port),
|
||||
"watch" => watch_nodes(&mut port),
|
||||
"clean" => clean(),
|
||||
_ => {
|
||||
eprintln!("Неизвестная команда: {}", command);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user