feat: move running server to subcommand

incremental-backups
Jef Roosens 2023-06-14 22:16:54 +02:00
parent fcc111b4ef
commit a9e7b215d1
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 119 additions and 81 deletions

View File

@ -1,3 +1,3 @@
[alias] [alias]
runs = "run -- paper 1.19.4-545 --config data/config --backup data/backups --world data/worlds --jar data/paper-1.19.4-525.jar" runs = "run -- run paper 1.19.4-550 --config data/config --backup data/backups --world data/worlds --jar paper-1.19.4-550.jar"
runrs = "run --release -- paper 1.19.4-545 --config data/config --backup data/backups --world data/worlds --jar data/paper-1.19.4-525.jar" runrs = "run --release -- paper 1.19.4-545 --config data/config --backup data/backups --world data/worlds --jar data/paper-1.19.4-525.jar"

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://git.rustybever.be/Chewing_Bever/alex/src/branch/dev) ## [Unreleased](https://git.rustybever.be/Chewing_Bever/alex/src/branch/dev)
### Changed
* Running the server now uses the `run` CLI subcommand
## [0.2.2](https://git.rustybever.be/Chewing_Bever/alex/src/tag/0.2.2) ## [0.2.2](https://git.rustybever.be/Chewing_Bever/alex/src/tag/0.2.2)
### Fixed ### Fixed

91
src/cli.rs 100644
View File

@ -0,0 +1,91 @@
use crate::server::ServerType;
use clap::{Args, Parser, Subcommand};
use std::path::PathBuf;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
/// Directory where configs are stored, and where the server will run
#[arg(
long,
value_name = "CONFIG_DIR",
default_value = ".",
env = "ALEX_CONFIG_DIR",
global = true
)]
pub config: PathBuf,
/// Directory where world files will be saved
#[arg(
long,
value_name = "WORLD_DIR",
default_value = "../worlds",
env = "ALEX_WORLD_DIR",
global = true
)]
pub world: PathBuf,
/// Directory where backups will be stored
#[arg(
long,
value_name = "BACKUP_DIR",
default_value = "../backups",
env = "ALEX_BACKUP_DIR",
global = true
)]
pub backup: PathBuf,
/// How many backups to keep
#[arg(
short = 'n',
long,
default_value_t = 7,
env = "ALEX_MAX_BACKUPS",
global = true
)]
pub max_backups: u64,
}
#[derive(Subcommand)]
pub enum Commands {
/// Run the server
Run(RunArgs),
}
#[derive(Args)]
pub struct RunArgs {
/// Type of server
pub type_: ServerType,
/// Version string for the server, e.g. 1.19.4-545
#[arg(env = "ALEX_SERVER_VERSION")]
pub server_version: String,
/// Server jar to execute
#[arg(
long,
value_name = "JAR_PATH",
default_value = "server.jar",
env = "ALEX_JAR"
)]
pub jar: PathBuf,
/// Java command to run the server jar with
#[arg(long, value_name = "JAVA_CMD", default_value_t = String::from("java"), env = "ALEX_JAVA")]
pub java: String,
/// XMS value in megabytes for the server instance
#[arg(long, default_value_t = 1024, env = "ALEX_XMS")]
pub xms: u64,
/// XMX value in megabytes for the server instance
#[arg(long, default_value_t = 2048, env = "ALEX_XMX")]
pub xmx: u64,
/// How frequently to perform a backup, in minutes; 0 to disable.
#[arg(short = 't', long, default_value_t = 0, env = "ALEX_FREQUENCY")]
pub frequency: u64,
/// Don't actually run the server, but simply output the server configuration that would have
/// been ran
#[arg(short, long, default_value_t = false)]
pub dry: bool,
}

View File

@ -1,78 +1,13 @@
mod cli;
mod server; mod server;
mod signals; mod signals;
mod stdin; mod stdin;
use clap::Parser; use clap::Parser;
use server::ServerType; use cli::{Cli, Commands, RunArgs};
use std::io; use std::io;
use std::path::PathBuf;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
/// Type of server
type_: ServerType,
/// Version string for the server, e.g. 1.19.4-545
#[arg(env = "ALEX_SERVER_VERSION")]
server_version: String,
/// Server jar to execute
#[arg(
long,
value_name = "JAR_PATH",
default_value = "server.jar",
env = "ALEX_JAR"
)]
jar: PathBuf,
/// Directory where configs are stored, and where the server will run
#[arg(
long,
value_name = "CONFIG_DIR",
default_value = ".",
env = "ALEX_CONFIG_DIR"
)]
config: PathBuf,
/// Directory where world files will be saved
#[arg(
long,
value_name = "WORLD_DIR",
default_value = "../worlds",
env = "ALEX_WORLD_DIR"
)]
world: PathBuf,
/// Directory where backups will be stored
#[arg(
long,
value_name = "BACKUP_DIR",
default_value = "../backups",
env = "ALEX_BACKUP_DIR"
)]
backup: PathBuf,
/// Java command to run the server jar with
#[arg(long, value_name = "JAVA_CMD", default_value_t = String::from("java"), env = "ALEX_JAVA")]
java: String,
/// XMS value in megabytes for the server instance
#[arg(long, default_value_t = 1024, env = "ALEX_XMS")]
xms: u64,
/// XMX value in megabytes for the server instance
#[arg(long, default_value_t = 2048, env = "ALEX_XMX")]
xmx: u64,
/// How many backups to keep
#[arg(short = 'n', long, default_value_t = 7, env = "ALEX_MAX_BACKUPS")]
max_backups: u64,
/// How frequently to perform a backup, in minutes; 0 to disable.
#[arg(short = 't', long, default_value_t = 0, env = "ALEX_FREQUENCY")]
frequency: u64,
/// Don't actually run the server, but simply output the server configuration that would have
/// been ran
#[arg(short, long, default_value_t = false)]
dry: bool,
}
fn backups_thread(counter: Arc<Mutex<server::ServerProcess>>, frequency: u64) { fn backups_thread(counter: Arc<Mutex<server::ServerProcess>>, frequency: u64) {
loop { loop {
std::thread::sleep(std::time::Duration::from_secs(frequency * 60)); std::thread::sleep(std::time::Duration::from_secs(frequency * 60));
@ -86,22 +21,21 @@ fn backups_thread(counter: Arc<Mutex<server::ServerProcess>>, frequency: u64) {
} }
} }
fn main() -> io::Result<()> { fn command_run(cli: &Cli, args: &RunArgs) -> io::Result<()> {
let (_, mut signals) = signals::install_signal_handlers()?; let (_, mut signals) = signals::install_signal_handlers()?;
let cli = Cli::parse();
let mut cmd = server::ServerCommand::new(cli.type_, &cli.server_version) let mut cmd = server::ServerCommand::new(args.type_, &args.server_version)
.java(&cli.java) .java(&args.java)
.jar(cli.jar) .jar(args.jar.clone())
.config(cli.config) .config(cli.config.clone())
.world(cli.world) .world(cli.world.clone())
.backup(cli.backup) .backup(cli.backup.clone())
.xms(cli.xms) .xms(args.xms)
.xmx(cli.xmx) .xmx(args.xmx)
.max_backups(cli.max_backups); .max_backups(cli.max_backups);
cmd.canonicalize()?; cmd.canonicalize()?;
if cli.dry { if args.dry {
print!("{}", cmd); print!("{}", cmd);
return Ok(()); return Ok(());
@ -109,9 +43,10 @@ fn main() -> io::Result<()> {
let counter = Arc::new(Mutex::new(cmd.spawn()?)); let counter = Arc::new(Mutex::new(cmd.spawn()?));
if cli.frequency > 0 { if args.frequency > 0 {
let clone = Arc::clone(&counter); let clone = Arc::clone(&counter);
std::thread::spawn(move || backups_thread(clone, cli.frequency)); let frequency = args.frequency;
std::thread::spawn(move || backups_thread(clone, frequency));
} }
// Spawn thread that handles the main stdin loop // Spawn thread that handles the main stdin loop
@ -121,3 +56,11 @@ fn main() -> io::Result<()> {
// Signal handler loop exits the process when necessary // Signal handler loop exits the process when necessary
signals::handle_signals(&mut signals, counter) signals::handle_signals(&mut signals, counter)
} }
fn main() -> io::Result<()> {
let cli = Cli::parse();
match &cli.command {
Commands::Run(args) => command_run(&cli, args),
}
}