add 'disabled' state, fix handshake proxy send
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -640,7 +640,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "dockermcmgr"
|
||||
version = "0.0.8"
|
||||
version = "0.0.9"
|
||||
dependencies = [
|
||||
"async-process",
|
||||
"async-std",
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "dockermcmgr"
|
||||
version = "0.0.8"
|
||||
version = "0.0.9"
|
||||
edition = "2024"
|
||||
|
||||
[[bin]]
|
||||
|
||||
@ -38,7 +38,7 @@
|
||||
final:
|
||||
{
|
||||
pname = "dmm";
|
||||
version = "0.0.8";
|
||||
version = "0.0.9";
|
||||
|
||||
inherit src;
|
||||
cargoLock.lockFile = src + "/Cargo.lock";
|
||||
|
||||
@ -36,7 +36,7 @@ pub async fn subcommand(arguments: &ArgMatches, pool: &DbPool, docker: &Docker)
|
||||
|
||||
if server.status == ServerStatus::Running {
|
||||
println!("Stopping container");
|
||||
srvmgr::stop_server(&docker, pool, server_name).await?;
|
||||
srvmgr::stop_server(&docker, pool, server_name, false).await?;
|
||||
println!("container stopped");
|
||||
}
|
||||
|
||||
|
||||
32
src/cli/disable.rs
Normal file
32
src/cli/disable.rs
Normal file
@ -0,0 +1,32 @@
|
||||
use bollard::Docker;
|
||||
use clap::Arg;
|
||||
use clap::{Command, ArgMatches};
|
||||
|
||||
use crate::DbPool;
|
||||
|
||||
use crate::srvmgr;
|
||||
use crate::status::ServerStatus;
|
||||
|
||||
use diesel::prelude::*;
|
||||
|
||||
use crate::schema;
|
||||
|
||||
pub fn create_subcommand() -> Command {
|
||||
Command::new("disable")
|
||||
.about("disable")
|
||||
.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();
|
||||
println!("Stopping container");
|
||||
srvmgr::stop_server(&docker, pool, server_name, true).await?;
|
||||
println!("container stopped");
|
||||
|
||||
let conn = &mut pool.get().unwrap();
|
||||
|
||||
diesel::update(servers).filter(name.eq(server_name)).set(status.eq(ServerStatus::Disabled)).execute(conn).map_err(|e| format!("Failed to disable the server : {}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -6,6 +6,7 @@ pub mod start;
|
||||
pub mod stop;
|
||||
pub mod set_state;
|
||||
pub mod archive;
|
||||
pub mod disable;
|
||||
|
||||
pub mod env_settings;
|
||||
|
||||
|
||||
@ -21,7 +21,8 @@ pub async fn subcommand(arguments: &ArgMatches, pool: &DbPool, docker: &Docker)
|
||||
|
||||
let conn = &mut pool.get().unwrap();
|
||||
|
||||
let _ = srvmgr::stop_server(docker, pool, server_name).await;
|
||||
println!("stopping container if it exists");
|
||||
srvmgr::stop_server(docker, pool, server_name, true).await?;
|
||||
|
||||
let deleted = diesel::delete(servers.filter(name.eq(server_name))).execute(conn).map_err(|_| "failed to delete from db".to_string())?;
|
||||
if deleted != 1 {
|
||||
|
||||
@ -15,7 +15,7 @@ pub fn create_subcommand() -> Command {
|
||||
pub async fn subcommand(arguments: &ArgMatches, pool: &DbPool, docker: &Docker) -> Result<(), String> {
|
||||
let server_name = arguments.get_one::<String>("NAME").unwrap();
|
||||
println!("Stopping container");
|
||||
srvmgr::stop_server(&docker, pool, server_name).await?;
|
||||
srvmgr::stop_server(&docker, pool, server_name, false).await?;
|
||||
println!("container stopped");
|
||||
|
||||
Ok(())
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/05/29 21:22:17 by tomoron #+# #+# */
|
||||
/* Updated: 2026/06/14 16:21:23 by tomoron ### ########.fr */
|
||||
/* Updated: 2026/06/17 17:21:44 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -67,6 +67,7 @@ fn cli() -> Command {
|
||||
.subcommand(start::create_subcommand())
|
||||
.subcommand(stop::create_subcommand())
|
||||
.subcommand(archive::create_subcommand())
|
||||
.subcommand(disable::create_subcommand())
|
||||
}
|
||||
|
||||
#[tokio::main(flavor = "multi_thread")]
|
||||
@ -89,6 +90,7 @@ async fn main() -> Result<(), String> {
|
||||
Some(("start", sub_matches)) => { start::subcommand(sub_matches, &pool, &docker).await?; },
|
||||
Some(("stop", sub_matches)) => { stop::subcommand(sub_matches, &pool, &docker).await?; },
|
||||
Some(("archive", sub_matches)) => { archive::subcommand(sub_matches, &pool, &docker).await? },
|
||||
Some(("disable", sub_matches)) => { disable::subcommand(sub_matches, &pool, &docker).await? },
|
||||
_ => {panic!("subcommand not implemented")}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@ -86,7 +86,8 @@ impl Client {
|
||||
|
||||
let handshake_packet = new_handshake.to_packet();
|
||||
let mut packet = Vec::new();
|
||||
packet.extend(varint_write(handshake_packet.len() as i32));
|
||||
packet.extend(varint_write((handshake_packet.len() + 1) as i32));
|
||||
packet.push(0x00);
|
||||
packet.extend(handshake_packet);
|
||||
|
||||
self.out_stream = Some(stream);
|
||||
|
||||
@ -58,7 +58,7 @@ async fn should_stop(docker: &Docker, pool: &DbPool, diff: &TimeDelta, srv: &Ser
|
||||
|
||||
if diff.num_days() >= 7 {
|
||||
println!("last login time exceeded, stopping {}", srv.name);
|
||||
let _ = srvmgr::stop_server(docker, pool, &srv.name).await;
|
||||
let _ = srvmgr::stop_server(docker, pool, &srv.name, false).await;
|
||||
println!("{} stopped", srv.name);
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,11 +75,14 @@ pub async fn start_server(docker: &Docker, pool: &DbPool, server_name: &String)
|
||||
return Err("Server is starting, please wait".to_string());
|
||||
}
|
||||
|
||||
if server.status == ServerStatus::Disabled {
|
||||
return Err("This server is disabled".to_string());
|
||||
}
|
||||
|
||||
if server.status != ServerStatus::Stopped && server.status != ServerStatus::Archived {
|
||||
return Err(format!("Invalid server state ({}), refusing to start", server.status));
|
||||
}
|
||||
|
||||
|
||||
diesel::update(servers).filter(name.eq(server_name)).set(status.eq(ServerStatus::Starting)).execute(conn).map_err(|e| format!("Failed to set server to starting : {}", e))?;
|
||||
|
||||
if server.status == ServerStatus::Archived {
|
||||
|
||||
@ -17,7 +17,7 @@ async fn stop_container(docker: &Docker, name: &String) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn stop_server(docker: &Docker, pool: &DbPool, server_name: &String) -> Result<(), String> {
|
||||
pub async fn stop_server(docker: &Docker, pool: &DbPool, server_name: &String, ignore_stopped: bool) -> Result<(), String> {
|
||||
use schema::servers::dsl::*;
|
||||
|
||||
let conn = &mut pool.get().unwrap();
|
||||
@ -28,6 +28,10 @@ pub async fn stop_server(docker: &Docker, pool: &DbPool, server_name: &String) -
|
||||
Ok(server) => { server }
|
||||
};
|
||||
|
||||
if ignore_stopped && (server.status == ServerStatus::Stopped || server.status == ServerStatus::Archived) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if server.status != ServerStatus::Running {
|
||||
return Err(format!("Invalid server state ({}), refusing to stop the server", server.status));
|
||||
}
|
||||
|
||||
@ -17,6 +17,7 @@ pub enum ServerStatus {
|
||||
Starting = 3,
|
||||
Running = 4,
|
||||
Archiving = 5,
|
||||
Disabled = 6,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
@ -42,6 +43,7 @@ impl ToSql<SmallInt, Sqlite> for ServerStatus {
|
||||
impl fmt::Display for ServerStatus {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let str_status = match self {
|
||||
ServerStatus::Disabled => "disabled",
|
||||
ServerStatus::Archived => "archived",
|
||||
ServerStatus::Archiving => "archiving",
|
||||
ServerStatus::Stopped => "stopped",
|
||||
|
||||
Reference in New Issue
Block a user