-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into mbround18-patch-1
- Loading branch information
Showing
12 changed files
with
258 additions
and
107 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// pub fn invoke(args: Option<&ArgMatches>) { | ||
// let option | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod install; | ||
pub mod start; | ||
pub mod stop; | ||
pub mod initialize; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,92 @@ | ||
use crate::executable::{create_execution}; | ||
use std::process::{Stdio}; | ||
use clap::{ArgMatches}; | ||
use std::{process}; | ||
use crate::utils::{get_working_dir, get_variable}; | ||
use std::path::Path; | ||
use crate::utils::{get_variable, server_installed, get_working_dir}; | ||
use std::fs::{File}; | ||
use std::io::Write; | ||
use log::{info, error}; | ||
use tinytemplate::TinyTemplate; | ||
use serde::Serialize; | ||
|
||
#[derive(Serialize)] | ||
struct Context { | ||
command: String, | ||
arguments: String | ||
} | ||
|
||
fn create_start_server_script(command: String, arguments: String) { | ||
let source = &[ | ||
"#!/usr/bin/env bash", | ||
format!("{} {} &", command.as_str(), arguments.as_str()).as_str(), | ||
"disown" | ||
].join("\n"); | ||
static TEMPLATE: &'static &str = &r#" | ||
#!/usr/bin/env bash | ||
cd "$(dirname "$0")" | ||
# This script will be overwritten at each start! | ||
match File::create("./start_server_rusty.sh") { | ||
Ok(mut file) => { | ||
match file.write_all(source.as_bytes()) { | ||
Ok(_) => println!("Successfully written script file."), | ||
_ => println!("Failed to write script file.") | ||
}; | ||
{command} {arguments} 2>&1 | tee ./output.log & | ||
disown | ||
match create_execution("chmod").args(&["+x", "./start_server_rusty.sh"]).output() { | ||
Ok(_) =>println!("Success changing permission"), | ||
_ => println!("Unable to change permissions") | ||
}; | ||
} | ||
_ => println!("Failed to write script file.") | ||
}; | ||
} | ||
"#; | ||
|
||
pub fn invoke(args: Option<&ArgMatches>) { | ||
let paths = &[get_working_dir(), "valheim_server.x86_64".to_string()]; | ||
let script_path = &paths.join("/"); | ||
let script_file = Path::new(script_path); | ||
if script_file.exists() { | ||
let mut command = create_execution("bash"); | ||
let mut command_arguments: Vec<String> = Vec::new(); | ||
fn parse_to_script(context: Context) -> String{ | ||
let mut tt = TinyTemplate::new(); | ||
tt.add_template( | ||
"hello", &TEMPLATE).unwrap(); | ||
tt.render("hello", &context).unwrap().replace(""", "\"") | ||
} | ||
|
||
if let Some(port) = get_variable("PORT", args, "2456".to_string()) { | ||
println!("Found Port Argument: {}", port); | ||
command_arguments.push(format!("-port {}", port)); | ||
} | ||
if let Some(name) = get_variable("NAME", args, "Valheim Docker".to_string()) { | ||
println!("Adding Name Argument: {}", name); | ||
command_arguments.push(format!("-name \"{}\"", name)); | ||
} | ||
if let Some(world) = get_variable("WORLD", args, "Dedicated".to_string()) { | ||
println!("Adding World Argument: {}", world); | ||
command_arguments.push(format!("-world \"{}\"", world)); | ||
} | ||
if let Some(password) = get_variable("PASSWORD", args, "".to_string()) { | ||
if password.len() > 0 { | ||
println!("Adding Password Argument"); | ||
command_arguments.push(format!("-password \"{}\"", password)); | ||
fn create_start_server_script(command: String, arguments: String, dry_run: bool) { | ||
let context = Context { | ||
command, | ||
arguments | ||
}; | ||
let source = parse_to_script(context); | ||
if dry_run { | ||
info!("This would have written a file to ./start_server_rusty.sh with content: \n {}", source); | ||
} else { | ||
match File::create("./start_server_rusty.sh") { | ||
Ok(mut file) => { | ||
match file.write_all(source.as_bytes()) { | ||
Ok(_) => println!("Successfully written script file."), | ||
_ => println!("Failed to write script file.") | ||
}; | ||
match create_execution("chmod").args(&["+x", "./start_server_rusty.sh"]).output() { | ||
Ok(_) => info!("Success changing permission"), | ||
_ => error!("Unable to change permissions") | ||
}; | ||
} | ||
} | ||
|
||
create_start_server_script(script_path.to_string(), command_arguments.join(" ")); | ||
|
||
let updated_command = command | ||
.stdout(Stdio::inherit()) | ||
.stderr(Stdio::inherit()) | ||
.arg("-c") | ||
.arg("./start_server_rusty.sh") | ||
.env("LD_LIBRARY_PATH", "${PWD}/linux64:${LD_LIBRARY_PATH}"); | ||
_ => error!("Failed to write script file.") | ||
}; | ||
} | ||
} | ||
|
||
fn parse_arg(args: &ArgMatches, name: &str, default: &str) -> String { | ||
format!("-{} \"{}\"", name, get_variable(args, name,default.to_string())) | ||
} | ||
|
||
match updated_command.output() { | ||
Ok(output) => print!("Exit with code {}", output.status), | ||
_ => { | ||
print!("An error has occurred!") | ||
pub fn invoke(args: &ArgMatches) { | ||
let mut command = create_execution("bash"); | ||
let command_args: &str = &[ | ||
parse_arg(args, "port", "2456"), | ||
parse_arg(args, "name", "Valheim Docker"), | ||
parse_arg(args, "world", "Dedicated"), | ||
parse_arg(args, "password", "12345"), | ||
].join(" "); | ||
let dry_run: bool = args.is_present("dry_run"); | ||
let server_executable = &[get_working_dir(), "valheim_server.x86_64".to_string()].join("/"); | ||
create_start_server_script(server_executable.to_string(), command_args.to_string(), dry_run); | ||
if !dry_run { | ||
if server_installed() { | ||
let updated_command = command | ||
.stdout(Stdio::inherit()) | ||
.stderr(Stdio::inherit()) | ||
.arg("-c") | ||
.arg("./start_server_rusty.sh") | ||
.env("LD_LIBRARY_PATH", "${PWD}/linux64:${LD_LIBRARY_PATH}"); | ||
match updated_command.output() { | ||
Ok(output) => print!("Exit with code {}", output.status), | ||
_ => { | ||
error!("An error has occurred!") | ||
} | ||
} | ||
} else { | ||
error!("Could not find server executable! Please install the server!") | ||
} | ||
// updated_command.exec(); | ||
// let result = execute_mut(updated_command); | ||
// handle_exit_status(result, "Server Started Successfully!".to_string()); | ||
|
||
} else { | ||
println!("Cannot start server! valheim_server.x86_64 not found in current directory!"); | ||
process::exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,29 @@ | ||
use crate::utils::get_working_dir; | ||
use crate::executable::{create_execution, execute_mut, handle_exit_status}; | ||
use std::process::Stdio; | ||
use crate::utils::{get_working_dir, server_installed}; | ||
use log::{info, error}; | ||
use clap::ArgMatches; | ||
use std::fs::{File, remove_file}; | ||
use std::io::Write; | ||
use std::thread::sleep; | ||
use std::time::Duration; | ||
|
||
pub fn invoke() { | ||
pub fn invoke(args: &ArgMatches) { | ||
let paths = &[get_working_dir(), "server_exit.drp".to_string()]; | ||
let script_path = &paths.join("/"); | ||
let mut command = create_execution(format!("echo 1 > {}", script_path).as_str()); | ||
let updated_command = command | ||
.stdout(Stdio::inherit()) | ||
.stderr(Stdio::inherit()); | ||
|
||
let result = execute_mut(updated_command); | ||
handle_exit_status(result, "Server Stopped Successfully!".to_string()) | ||
|
||
info!("Stopping server {}", get_working_dir()); | ||
let command_arguments = format!("> {}", script_path); | ||
if args.is_present("dry_run") { | ||
info!("This command would have run: "); | ||
info!("echo {}", command_arguments) | ||
} else { | ||
if !server_installed() { | ||
error!("Failed to find server executable!"); | ||
return; | ||
} | ||
let mut file = File::create(script_path).unwrap(); | ||
file.write_all(b"1").unwrap(); | ||
info!("Stop file created, waiting for server to stop!"); | ||
sleep(Duration::from_secs(5)); | ||
remove_file(script_path).unwrap(); | ||
info!("Server has been halted!"); | ||
} | ||
} |
Oops, something went wrong.