|
@@ -228,7 +228,7 @@ elsif ($no_startups != 1)
|
|
|
my (
|
|
my (
|
|
|
$home_id, $home_path, $server_exe,
|
|
$home_id, $home_path, $server_exe,
|
|
|
$run_dir, $startup_cmd, $server_port,
|
|
$run_dir, $startup_cmd, $server_port,
|
|
|
- $server_ip, $cpu, $nice
|
|
|
|
|
|
|
+ $server_ip, $cpu, $nice, $preStart, $envVars
|
|
|
) = split(',', $_);
|
|
) = split(',', $_);
|
|
|
|
|
|
|
|
if (is_screen_running_without_decrypt(SCREEN_TYPE_HOME, $home_id) ==
|
|
if (is_screen_running_without_decrypt(SCREEN_TYPE_HOME, $home_id) ==
|
|
@@ -243,7 +243,7 @@ elsif ($no_startups != 1)
|
|
|
universal_start_without_decrypt(
|
|
universal_start_without_decrypt(
|
|
|
$home_id, $home_path, $server_exe,
|
|
$home_id, $home_path, $server_exe,
|
|
|
$run_dir, $startup_cmd, $server_port,
|
|
$run_dir, $startup_cmd, $server_port,
|
|
|
- $server_ip, $cpu, $nice
|
|
|
|
|
|
|
+ $server_ip, $cpu, $nice, $preStart, $envVars
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
close(STARTFILE);
|
|
close(STARTFILE);
|
|
@@ -463,7 +463,7 @@ sub create_screen_cmd
|
|
|
|
|
|
|
|
sub create_screen_cmd_loop
|
|
sub create_screen_cmd_loop
|
|
|
{
|
|
{
|
|
|
- my ($screen_id, $exec_cmd) = @_;
|
|
|
|
|
|
|
+ my ($screen_id, $exec_cmd, $envVars) = @_;
|
|
|
my $server_start_bashfile = $screen_id . "_startup_scr.sh";
|
|
my $server_start_bashfile = $screen_id . "_startup_scr.sh";
|
|
|
|
|
|
|
|
$exec_cmd = replace_OGP_Vars($screen_id, $exec_cmd);
|
|
$exec_cmd = replace_OGP_Vars($screen_id, $exec_cmd);
|
|
@@ -476,9 +476,15 @@ sub create_screen_cmd_loop
|
|
|
# Create bash file that screen will run which spawns the server
|
|
# Create bash file that screen will run which spawns the server
|
|
|
# If it crashes without user intervention, it will restart
|
|
# If it crashes without user intervention, it will restart
|
|
|
open (SERV_START_SCRIPT, '>', $server_start_bashfile);
|
|
open (SERV_START_SCRIPT, '>', $server_start_bashfile);
|
|
|
|
|
+
|
|
|
my $respawn_server_command = "#!/bin/bash" . "\n"
|
|
my $respawn_server_command = "#!/bin/bash" . "\n"
|
|
|
- . "function startServer(){" . "\n"
|
|
|
|
|
- . "NUMSECONDS=`expr \$(date +%s)`" . "\n"
|
|
|
|
|
|
|
+ . "function startServer(){" . "\n" ;
|
|
|
|
|
+
|
|
|
|
|
+ if(defined $envVars && $envVars ne ""){
|
|
|
|
|
+ $respawn_server_command .= $envVars;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $respawn_server_command .= "NUMSECONDS=`expr \$(date +%s)`" . "\n"
|
|
|
. "until " . $exec_cmd . "; do" . "\n"
|
|
. "until " . $exec_cmd . "; do" . "\n"
|
|
|
. "let DIFF=(`date +%s` - \"\$NUMSECONDS\")" . "\n"
|
|
. "let DIFF=(`date +%s` - \"\$NUMSECONDS\")" . "\n"
|
|
|
. "if [ \"\$DIFF\" -gt 15 ]; then" . "\n"
|
|
. "if [ \"\$DIFF\" -gt 15 ]; then" . "\n"
|
|
@@ -494,6 +500,7 @@ sub create_screen_cmd_loop
|
|
|
. "fi" . "\n"
|
|
. "fi" . "\n"
|
|
|
. "}" . "\n"
|
|
. "}" . "\n"
|
|
|
. "startServer" . "\n";
|
|
. "startServer" . "\n";
|
|
|
|
|
+
|
|
|
print SERV_START_SCRIPT $respawn_server_command;
|
|
print SERV_START_SCRIPT $respawn_server_command;
|
|
|
close (SERV_START_SCRIPT);
|
|
close (SERV_START_SCRIPT);
|
|
|
|
|
|
|
@@ -526,6 +533,15 @@ sub replace_OGP_Vars{
|
|
|
return $exec_cmd;
|
|
return $exec_cmd;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+sub replace_OGP_Env_Vars{
|
|
|
|
|
+ # This function replaces constants from environment variables set in the XML
|
|
|
|
|
+ my ($homeid, $homepath, $strToReplace) = @_;
|
|
|
|
|
+
|
|
|
|
|
+ $strToReplace =~ s/{OGP_HOME_DIR}/$homepath/g;
|
|
|
|
|
+
|
|
|
|
|
+ return $strToReplace;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
sub encode_list
|
|
sub encode_list
|
|
|
{
|
|
{
|
|
|
my $encoded_content = '';
|
|
my $encoded_content = '';
|
|
@@ -670,7 +686,7 @@ sub universal_start_without_decrypt
|
|
|
{
|
|
{
|
|
|
my (
|
|
my (
|
|
|
$home_id, $home_path, $server_exe, $run_dir,
|
|
$home_id, $home_path, $server_exe, $run_dir,
|
|
|
- $startup_cmd, $server_port, $server_ip, $cpu, $nice
|
|
|
|
|
|
|
+ $startup_cmd, $server_port, $server_ip, $cpu, $nice, $preStart, $envVars
|
|
|
) = @_;
|
|
) = @_;
|
|
|
|
|
|
|
|
if (is_screen_running_without_decrypt(SCREEN_TYPE_HOME, $home_id) == 1)
|
|
if (is_screen_running_without_decrypt(SCREEN_TYPE_HOME, $home_id) == 1)
|
|
@@ -712,6 +728,35 @@ sub universal_start_without_decrypt
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if(defined $preStart && $preStart ne ""){
|
|
|
|
|
+ # Get it in the format that the startup file can use
|
|
|
|
|
+ $preStart = multiline_to_startup_comma_format($preStart);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ $preStart = "";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(defined $envVars && $envVars ne ""){
|
|
|
|
|
+ # Replace variables in the envvars if they exist
|
|
|
|
|
+ my @prestartenvvars = split /[\r\n]+/, $envVars;
|
|
|
|
|
+ my $envVarStr = "";
|
|
|
|
|
+ foreach my $line (@prestartenvvars) {
|
|
|
|
|
+ $line = replace_OGP_Env_Vars($home_id, $home_path, $line);
|
|
|
|
|
+ if($line ne ""){
|
|
|
|
|
+ logger "Configuring environment variable: $line";
|
|
|
|
|
+ $envVarStr .= "$line\n";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(defined $envVarStr && $envVarStr ne ""){
|
|
|
|
|
+ $envVars = $envVarStr;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ # Get it in the format that the startup file can use
|
|
|
|
|
+ $envVars = multiline_to_startup_comma_format($envVars);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ $envVars = "";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
secure_path_without_decrypt('chattr+i', $server_exe);
|
|
secure_path_without_decrypt('chattr+i', $server_exe);
|
|
|
|
|
|
|
|
# Create startup file for the server.
|
|
# Create startup file for the server.
|
|
@@ -720,7 +765,7 @@ sub universal_start_without_decrypt
|
|
|
if (open(STARTUP, '>', $startup_file))
|
|
if (open(STARTUP, '>', $startup_file))
|
|
|
{
|
|
{
|
|
|
print STARTUP
|
|
print STARTUP
|
|
|
- "$home_id,$home_path,$server_exe,$run_dir,$startup_cmd,$server_port,$server_ip,$cpu,$nice";
|
|
|
|
|
|
|
+ "$home_id,$home_path,$server_exe,$run_dir,$startup_cmd,$server_port,$server_ip,$cpu,$nice,$preStart,$envVars";
|
|
|
logger "Created startup flag for $server_ip-$server_port";
|
|
logger "Created startup flag for $server_ip-$server_port";
|
|
|
close(STARTUP);
|
|
close(STARTUP);
|
|
|
}
|
|
}
|
|
@@ -729,11 +774,26 @@ sub universal_start_without_decrypt
|
|
|
logger "Cannot create file in " . $startup_file . " : $!";
|
|
logger "Cannot create file in " . $startup_file . " : $!";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if(defined $preStart && $preStart ne ""){
|
|
|
|
|
+ # Get it in the format that the startup file can use
|
|
|
|
|
+ $preStart = startup_comma_format_to_multiline($preStart);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ $preStart = "";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(defined $envVars && $envVars ne ""){
|
|
|
|
|
+ # Get it in the format that the startup file can use
|
|
|
|
|
+ $envVars = startup_comma_format_to_multiline($envVars);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ $envVars = "";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
# Create the startup string.
|
|
# Create the startup string.
|
|
|
my $screen_id = create_screen_id(SCREEN_TYPE_HOME, $home_id);
|
|
my $screen_id = create_screen_id(SCREEN_TYPE_HOME, $home_id);
|
|
|
my $file_extension = substr $server_exe, -4;
|
|
my $file_extension = substr $server_exe, -4;
|
|
|
my $cli_bin;
|
|
my $cli_bin;
|
|
|
my $command;
|
|
my $command;
|
|
|
|
|
+ my $run_before_start;
|
|
|
|
|
|
|
|
if($file_extension eq ".exe" or $file_extension eq ".bat")
|
|
if($file_extension eq ".exe" or $file_extension eq ".bat")
|
|
|
{
|
|
{
|
|
@@ -746,7 +806,7 @@ sub universal_start_without_decrypt
|
|
|
|
|
|
|
|
if(defined($Cfg::Preferences{ogp_autorestart_server}) && $Cfg::Preferences{ogp_autorestart_server} eq "1"){
|
|
if(defined($Cfg::Preferences{ogp_autorestart_server}) && $Cfg::Preferences{ogp_autorestart_server} eq "1"){
|
|
|
deleteStoppedStatFile($home_path);
|
|
deleteStoppedStatFile($home_path);
|
|
|
- $cli_bin = create_screen_cmd_loop($screen_id, $command);
|
|
|
|
|
|
|
+ $cli_bin = create_screen_cmd_loop($screen_id, $command, $envVars);
|
|
|
}else{
|
|
}else{
|
|
|
$cli_bin = create_screen_cmd($screen_id, $command);
|
|
$cli_bin = create_screen_cmd($screen_id, $command);
|
|
|
}
|
|
}
|
|
@@ -762,7 +822,7 @@ sub universal_start_without_decrypt
|
|
|
|
|
|
|
|
if(defined($Cfg::Preferences{ogp_autorestart_server}) && $Cfg::Preferences{ogp_autorestart_server} eq "1"){
|
|
if(defined($Cfg::Preferences{ogp_autorestart_server}) && $Cfg::Preferences{ogp_autorestart_server} eq "1"){
|
|
|
deleteStoppedStatFile($home_path);
|
|
deleteStoppedStatFile($home_path);
|
|
|
- $cli_bin = create_screen_cmd_loop($screen_id, $command);
|
|
|
|
|
|
|
+ $cli_bin = create_screen_cmd_loop($screen_id, $command, $envVars);
|
|
|
}else{
|
|
}else{
|
|
|
$cli_bin = create_screen_cmd($screen_id, $command);
|
|
$cli_bin = create_screen_cmd($screen_id, $command);
|
|
|
}
|
|
}
|
|
@@ -778,7 +838,7 @@ sub universal_start_without_decrypt
|
|
|
|
|
|
|
|
if(defined($Cfg::Preferences{ogp_autorestart_server}) && $Cfg::Preferences{ogp_autorestart_server} eq "1"){
|
|
if(defined($Cfg::Preferences{ogp_autorestart_server}) && $Cfg::Preferences{ogp_autorestart_server} eq "1"){
|
|
|
deleteStoppedStatFile($home_path);
|
|
deleteStoppedStatFile($home_path);
|
|
|
- $cli_bin = create_screen_cmd_loop($screen_id, $command);
|
|
|
|
|
|
|
+ $cli_bin = create_screen_cmd_loop($screen_id, $command, $envVars);
|
|
|
}else{
|
|
}else{
|
|
|
$cli_bin = create_screen_cmd($screen_id, $command);
|
|
$cli_bin = create_screen_cmd($screen_id, $command);
|
|
|
}
|
|
}
|
|
@@ -790,6 +850,9 @@ sub universal_start_without_decrypt
|
|
|
logger
|
|
logger
|
|
|
"Startup command [ $cli_bin ] will be executed in dir $game_binary_dir.";
|
|
"Startup command [ $cli_bin ] will be executed in dir $game_binary_dir.";
|
|
|
|
|
|
|
|
|
|
+ # Run before start script and set environment variables which will affect create_screen_cmd only... loop already has the envvars as well
|
|
|
|
|
+ $run_before_start = run_before_start_commands($home_id, $home_path, $preStart, $envVars);
|
|
|
|
|
+
|
|
|
system($cli_bin);
|
|
system($cli_bin);
|
|
|
|
|
|
|
|
sleep(1);
|
|
sleep(1);
|
|
@@ -1616,6 +1679,64 @@ sub start_file_download
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+sub run_before_start_commands
|
|
|
|
|
+{
|
|
|
|
|
+ #return "Bad Encryption Key" unless(decrypt_param(pop(@_)) eq "Encryption checking OK");
|
|
|
|
|
+ my ($server_id, $homedir, $beforestartcmd, $envVars) = @_;
|
|
|
|
|
+
|
|
|
|
|
+ if ($homedir ne "" && $server_id ne ""){
|
|
|
|
|
+ # Run any prestart scripts
|
|
|
|
|
+ if (defined $beforestartcmd && $beforestartcmd ne "")
|
|
|
|
|
+ {
|
|
|
|
|
+ logger "Running pre-start XML commands before starting server ID $server_id with a home directory of $homedir.";
|
|
|
|
|
+ my @prestartcmdlines = split /[\r\n]+/, $beforestartcmd;
|
|
|
|
|
+ my $prestartcmdfile = $homedir."/".'prestart_ogp.sh';
|
|
|
|
|
+ open FILE, '>', $prestartcmdfile;
|
|
|
|
|
+ print FILE "#!/bin/bash" . "\n";
|
|
|
|
|
+ print FILE "cd $homedir\n";
|
|
|
|
|
+ foreach my $line (@prestartcmdlines) {
|
|
|
|
|
+ print FILE "$line\n";
|
|
|
|
|
+ }
|
|
|
|
|
+ print FILE "rm -f $prestartcmdfile\n";
|
|
|
|
|
+ close FILE;
|
|
|
|
|
+ chmod 0755, $prestartcmdfile;
|
|
|
|
|
+ system("bash $prestartcmdfile");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ # Set and export any environment variables for game server developers unwilling to properly learn Linux
|
|
|
|
|
+ if (defined $envVars && $envVars ne ""){
|
|
|
|
|
+ my @prestartenvvars = split /[\r\n]+/, $envVars;
|
|
|
|
|
+ foreach my $line (@prestartenvvars) {
|
|
|
|
|
+ $line = replace_OGP_Env_Vars($server_id, $homedir, $line);
|
|
|
|
|
+ if($line ne ""){
|
|
|
|
|
+ logger "Configuring environment variable: $line";
|
|
|
|
|
+ system($line);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }else{
|
|
|
|
|
+ return -2;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return 1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+sub multiline_to_startup_comma_format{
|
|
|
|
|
+ my ($multiLineVar) = @_;
|
|
|
|
|
+ $multiLineVar =~ s/,//g; # commas are invalid anyways in bash
|
|
|
|
|
+ $multiLineVar =~ s/[\r]+//g;
|
|
|
|
|
+ $multiLineVar =~ s/[\n]+/{OGPNEWLINE}/g;
|
|
|
|
|
+ return $multiLineVar;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+sub startup_comma_format_to_multiline{
|
|
|
|
|
+ my ($multiLineVar) = @_;
|
|
|
|
|
+ $multiLineVar =~ s/{OGPNEWLINE}/\n/g;
|
|
|
|
|
+ return $multiLineVar;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
sub create_secure_script
|
|
sub create_secure_script
|
|
|
{
|
|
{
|
|
|
my ($home_path, $exec_folder_path, $exec_path) = @_;
|
|
my ($home_path, $exec_folder_path, $exec_path) = @_;
|
|
@@ -2321,14 +2442,14 @@ sub restart_server_without_decrypt
|
|
|
{
|
|
{
|
|
|
my ($home_id, $server_ip, $server_port, $control_protocol,
|
|
my ($home_id, $server_ip, $server_port, $control_protocol,
|
|
|
$control_password, $control_type, $home_path, $server_exe, $run_dir,
|
|
$control_password, $control_type, $home_path, $server_exe, $run_dir,
|
|
|
- $cmd, $cpu, $nice) = @_;
|
|
|
|
|
|
|
+ $cmd, $cpu, $nice, $preStart, $envVars) = @_;
|
|
|
|
|
|
|
|
if (stop_server_without_decrypt($home_id, $server_ip,
|
|
if (stop_server_without_decrypt($home_id, $server_ip,
|
|
|
$server_port, $control_protocol,
|
|
$server_port, $control_protocol,
|
|
|
$control_password, $control_type, $home_path) == 0)
|
|
$control_password, $control_type, $home_path) == 0)
|
|
|
{
|
|
{
|
|
|
if (universal_start_without_decrypt($home_id, $home_path, $server_exe, $run_dir,
|
|
if (universal_start_without_decrypt($home_id, $home_path, $server_exe, $run_dir,
|
|
|
- $cmd, $server_port, $server_ip, $cpu, $nice) == 1)
|
|
|
|
|
|
|
+ $cmd, $server_port, $server_ip, $cpu, $nice, $preStart, $envVars) == 1)
|
|
|
{
|
|
{
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|