archive server

This commit is contained in:
2026-06-12 18:27:55 +02:00
parent 6aa0ab473d
commit 97f3e4c139
11 changed files with 138 additions and 14 deletions

69
src/cli/archive.rs Normal file
View File

@ -0,0 +1,69 @@
use bollard::Docker;
use clap::Arg;
use clap::{Command, ArgMatches};
use diesel::{SelectableHelper};
use diesel::result::Error::NotFound;
use crate::models::Servers;
use crate::status::ServerStatus;
use crate::{DbPool, schema, models};
use diesel::prelude::*;
use crate::dockermgr;
use async_process;
pub fn create_subcommand() -> Command {
Command::new("archive")
.about("archive a server")
.arg(Arg::new("NAME").required(true))
}
pub async fn subcommand(arguments: &ArgMatches, pool: &DbPool, docker: &Docker) -> Result<(), String> {
use schema::servers::dsl::*;
let server_name = arguments.get_one::<String>("NAME").unwrap();
let conn = &mut pool.get().unwrap();
let server = match servers.select(models::Servers::as_select()).filter(name.eq(server_name)).first(conn) {
Ok(server) => {server},
Err(NotFound) => { return Err("server not found".to_string()); },
Err(e) => { return Err(format!("Failed to fetch server from db : {}", e)); }
};
if server.status != ServerStatus::Running && server.status != ServerStatus::Stopped {
return Err(format!("invalid current server state : {}", server.status));
}
if server.status == ServerStatus::Running {
println!("Stopping container");
dockermgr::stop_server(&docker, pool, server_name).await?;
println!("container stopped");
}
archive_server(&server, pool).await?;
Ok(())
}
pub async fn archive_server(server: &Servers, pool: &DbPool) -> Result<(), String> {
use schema::servers::dsl::*;
let conn = &mut pool.get().unwrap();
let config = crate::Config::load();
diesel::update(servers).filter(name.eq(&server.name)).set(status.eq(ServerStatus::Archiving)).execute(conn).map_err(|e| format!("Failed to set the server as archiving :{}", e))?;
println!("Starting compression (this might take a while)");
let cmd = format!("cd {} && tar cf /dev/stdout {} | lrzip - -Q -o {}.tar.lrz", config.config_path, server.name, server.name);
let _ = async_process::Command::new("sh").arg("-c").arg(cmd).output().await.map_err(|e| format!("compression failed : {}", e))?;
std::fs::remove_dir_all(config.config_path.to_owned() + "/" + &server.name).map_err(|e| format!("Failed to delete server directory: {}", e))?;
diesel::update(servers).filter(name.eq(&server.name)).set(status.eq(ServerStatus::Archived)).execute(conn).map_err(|e| format!("Failed to set the server as achived :{}", e))?;
Ok(())
}

View File

@ -5,6 +5,7 @@ pub mod ls;
pub mod start;
pub mod stop;
pub mod set_state;
pub mod archive;
pub mod env_settings;