embed migrations, fix start, add stop

This commit is contained in:
2026-06-09 17:09:07 +02:00
parent 5136e0bdc3
commit 408530db00
16 changed files with 212 additions and 26 deletions

View File

@ -1,3 +1,4 @@
use chrono::Utc;
use clap::{ArgMatches, Command, arg};
use crate::cli::env_settings;
use crate::status::ServerStatus;
@ -9,7 +10,7 @@ use crate::{DbPool, models}; use crate::schema;
use tokio::{fs::{File, create_dir}, io::AsyncWriteExt};
use std::path::Path;
use std::time::SystemTime;
pub fn create_subcommand() -> Command {
let mut arguments = vec![
@ -56,15 +57,13 @@ pub async fn subcommand(arg: &ArgMatches, pool: DbPool) -> Result<(), String>{
std::fs::remove_dir_all(path.to_string() + "tmp").map_err(|e| format!("failed to remove the tmp dir, reason : {}", e))?;
}
let _new_server = models::CreateServer {
let new_server = models::CreateServer {
name: server_name,
last_login: Some(SystemTime::now()),
container_id: None,
last_login: Some(Utc::now().naive_utc()),
status: ServerStatus::Stopped,
redirect_ip: None
};
// diesel::insert_into(schema::servers::dsl::servers).values(&new_server).execute(conn).map_err(|e| format!("Failed to insert in db, error : {:?}", e))?;
// TODO: fix weird error
diesel::insert_into(servers).values(&new_server).execute(conn).map_err(|e| format!("Failed to insert in db, error : {:?}", e))?;
Ok(())
}

View File

@ -3,6 +3,7 @@ pub mod edit;
pub mod remove;
pub mod ls;
pub mod start;
pub mod stop;
pub mod env_settings;

View File

@ -1,11 +1,14 @@
use bollard::Docker;
use chrono::Utc;
use clap::Arg;
use clap::{Command, ArgMatches};
use crate::DbPool;
use crate::{ DbPool, schema };
use crate::dockermgr;
use diesel::prelude::*;
pub fn create_subcommand() -> Command {
Command::new("start")
.about("manually start a server (and set last login datetime to now)")
@ -13,8 +16,11 @@ pub fn create_subcommand() -> Command {
}
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();
dockermgr::start_server(&docker, pool, server_name).await?;
diesel::update(servers).filter(name.eq(server_name)).set(last_login.eq(Utc::now().naive_utc())).execute(&mut pool.get().unwrap()).map_err(|e| format!("Failed to set the last login date: {}", e))?;
Ok(())
}

22
src/cli/stop.rs Normal file
View File

@ -0,0 +1,22 @@
use bollard::Docker;
use clap::Arg;
use clap::{Command, ArgMatches};
use crate::DbPool;
use crate::dockermgr;
pub fn create_subcommand() -> Command {
Command::new("stop")
.about("manually stop a server")
.arg(Arg::new("NAME").required(true))
}
pub async fn subcommand(arguments: &ArgMatches, pool: &DbPool, docker: Docker) -> Result<(), String> {
let server_name = arguments.get_one::<String>("NAME").unwrap();
println!("Stopping container");
dockermgr::stop_server(&docker, pool, server_name).await?;
println!("container stopped");
Ok(())
}