refactor: split process into own module
parent
c177c04a74
commit
d176fac420
|
@ -16,3 +16,7 @@ target/
|
||||||
# Added by cargo
|
# Added by cargo
|
||||||
|
|
||||||
/target
|
/target
|
||||||
|
|
||||||
|
# testing files
|
||||||
|
*.jar
|
||||||
|
data/
|
||||||
|
|
|
@ -8,6 +8,15 @@ version = "1.0.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "alex"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"chrono",
|
||||||
|
"flate2",
|
||||||
|
"tar",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "android-tzdata"
|
name = "android-tzdata"
|
||||||
version = "0.1.1"
|
version = "0.1.1"
|
||||||
|
@ -149,15 +158,6 @@ version = "0.4.18"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "518ef76f2f87365916b142844c16d8fefd85039bc5699050210a7778ee1cd1de"
|
checksum = "518ef76f2f87365916b142844c16d8fefd85039bc5699050210a7778ee1cd1de"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "mc-wrapper"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"chrono",
|
|
||||||
"flate2",
|
|
||||||
"tar",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "miniz_oxide"
|
name = "miniz_oxide"
|
||||||
version = "0.7.1"
|
version = "0.7.1"
|
||||||
|
|
|
@ -3,7 +3,11 @@ use std::io;
|
||||||
mod server;
|
mod server;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut server = server::Server::spawn();
|
let mut server =
|
||||||
|
server::ServerCommand::new("paper-1.19.4-545.jar", "data/config", "data/worlds")
|
||||||
|
.mem(512, 1024)
|
||||||
|
.spawn()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let stdin = io::stdin();
|
let stdin = io::stdin();
|
||||||
let input = &mut String::new();
|
let input = &mut String::new();
|
||||||
|
|
|
@ -0,0 +1,70 @@
|
||||||
|
use crate::server::ServerProcess;
|
||||||
|
use flate2::write::GzEncoder;
|
||||||
|
use flate2::Compression;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::process::{Child, Command, Stdio};
|
||||||
|
|
||||||
|
pub struct ServerCommand {
|
||||||
|
java: String,
|
||||||
|
jar: PathBuf,
|
||||||
|
config_dir: PathBuf,
|
||||||
|
world_dir: PathBuf,
|
||||||
|
xms: u32,
|
||||||
|
xmx: u32,
|
||||||
|
child: Option<Child>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ServerCommand {
|
||||||
|
pub fn new(jar: &str, config_dir: &str, world_dir: &str) -> Self {
|
||||||
|
ServerCommand {
|
||||||
|
java: String::from("java"),
|
||||||
|
jar: Path::new(jar).canonicalize().unwrap(),
|
||||||
|
config_dir: Path::new(config_dir).canonicalize().unwrap(),
|
||||||
|
world_dir: Path::new(world_dir).canonicalize().unwrap(),
|
||||||
|
xms: 1024,
|
||||||
|
xmx: 2048,
|
||||||
|
child: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn java(mut self, java: &str) -> Self {
|
||||||
|
self.java = String::from(java);
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mem(mut self, xms: u32, xmx: u32) -> Self {
|
||||||
|
self.xms = xms;
|
||||||
|
self.xmx = xmx;
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn accept_eula(&self) -> std::io::Result<()> {
|
||||||
|
let mut eula_path = self.config_dir.clone();
|
||||||
|
eula_path.push("eula.txt");
|
||||||
|
let mut eula_file = File::create(eula_path)?;
|
||||||
|
eula_file.write(b"eula=true")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spawn(&mut self) -> std::io::Result<ServerProcess> {
|
||||||
|
self.accept_eula()?;
|
||||||
|
|
||||||
|
let child = Command::new(&self.java)
|
||||||
|
.current_dir(&self.config_dir)
|
||||||
|
.arg("-jar")
|
||||||
|
.arg(&self.jar)
|
||||||
|
.arg("--universe")
|
||||||
|
.arg(&self.world_dir)
|
||||||
|
.arg("--nogui")
|
||||||
|
.stdin(Stdio::piped())
|
||||||
|
.spawn()?;
|
||||||
|
|
||||||
|
Ok(ServerProcess::new(child))
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
mod command;
|
||||||
|
mod process;
|
||||||
|
|
||||||
|
pub use command::ServerCommand;
|
||||||
|
pub use process::ServerProcess;
|
|
@ -1,27 +1,19 @@
|
||||||
|
use flate2::write::GzEncoder;
|
||||||
|
use flate2::Compression;
|
||||||
|
use std::fs::File;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::process::{Child, Command, Stdio};
|
use std::path::{Path, PathBuf};
|
||||||
use std::fs::File;
|
use std::process::Child;
|
||||||
use flate2::Compression;
|
use std::process::{Command, Stdio};
|
||||||
use flate2::write::GzEncoder;
|
|
||||||
|
|
||||||
pub struct Server {
|
pub struct ServerProcess {
|
||||||
child: Child,
|
child: Child,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Server {
|
impl ServerProcess {
|
||||||
pub fn spawn() -> Server {
|
pub fn new(child: Child) -> ServerProcess {
|
||||||
let child = Command::new("/usr/lib/jvm/java-16-openjdk/bin/java")
|
ServerProcess { child }
|
||||||
.arg("-jar")
|
|
||||||
.arg("paper-1.17.1-259.jar")
|
|
||||||
.arg("--universe")
|
|
||||||
.arg("worlds")
|
|
||||||
.arg("--nogui")
|
|
||||||
.stdin(Stdio::piped())
|
|
||||||
.spawn()
|
|
||||||
.expect("Couldn't start child process");
|
|
||||||
|
|
||||||
Server { child }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_command(&mut self, cmd: &str) {
|
pub fn send_command(&mut self, cmd: &str) {
|
||||||
|
@ -33,8 +25,10 @@ impl Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn custom(&mut self, cmd: &str) {
|
fn custom(&mut self, cmd: &str) {
|
||||||
let mut stdin = self.child.stdin.as_ref().unwrap();
|
let mut stdin = self.child.stdin.take().unwrap();
|
||||||
stdin.write_all(format!("{}\n", cmd.trim()).as_bytes()).unwrap();
|
stdin
|
||||||
|
.write_all(format!("{}\n", cmd.trim()).as_bytes())
|
||||||
|
.unwrap();
|
||||||
stdin.flush().unwrap();
|
stdin.flush().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +43,10 @@ impl Server {
|
||||||
self.custom("save-all");
|
self.custom("save-all");
|
||||||
|
|
||||||
// Create a gzip-compressed tarball of the worlds folder
|
// Create a gzip-compressed tarball of the worlds folder
|
||||||
let filename = format!("{}", chrono::offset::Local::now().format("backups/%Y-%m-%d_%H-%M-%S.tar.gz"));
|
let filename = format!(
|
||||||
|
"{}",
|
||||||
|
chrono::offset::Local::now().format("backups/%Y-%m-%d_%H-%M-%S.tar.gz")
|
||||||
|
);
|
||||||
let tar_gz = std::fs::File::create(filename).unwrap();
|
let tar_gz = std::fs::File::create(filename).unwrap();
|
||||||
let enc = GzEncoder::new(tar_gz, Compression::default());
|
let enc = GzEncoder::new(tar_gz, Compression::default());
|
||||||
let mut tar = tar::Builder::new(enc);
|
let mut tar = tar::Builder::new(enc);
|
Loading…
Reference in New Issue