Просмотр исходного кода

Changes to Support Pre Start Commands and Environment Variables

own3mall 9 лет назад
Родитель
Сommit
190b92a80c
9 измененных файлов с 146 добавлено и 57 удалено
  1. 0 5
      EHCP/addAccount.php
  2. 6 6
      EHCP/config.php
  3. 7 13
      EHCP/delAccount.php
  4. 0 4
      EHCP/listAllUsers.php
  5. 0 6
      EHCP/showAccount.php
  6. 0 3
      EHCP/syncftp.php
  7. 0 2
      EHCP/updateInfo.php
  8. 0 6
      EHCP/updatePass.php
  9. 133 12
      ogp_agent.pl

+ 0 - 5
EHCP/addAccount.php

@@ -1,9 +1,6 @@
 <?php
-
 // Adds users to the database
-
 // Variables
-
 $success = 0;
 
 if (isset($_GET['username'])) {
@@ -128,7 +125,5 @@ if ($errorCount > 0) {
 }
 
 // Return value:
-
 echo $success;
-
 ?>

+ 6 - 6
EHCP/config.php

@@ -1,5 +1,4 @@
 <?php
-
 /*
 This FTP addon works with EHCP (www.ehcp.net)
 It allows OGP - the open game panel - to manage custom FTP user accounts
@@ -22,6 +21,7 @@ if(!isset($dbpass) || empty($dbpass)){
 }
 
 $dbName = 'ehcp';
+$debug=false;
 
 /**********************************
 *          END DB Creds           *
@@ -31,7 +31,7 @@ $dbName = 'ehcp';
 $logFile = 'ehcp_ftp_log.txt';
 
 function addToLog($errors) {
-    global $logFile;
+    global $logFile, $debug;
     
     if (!file_exists($logFile)) {
         $createLog = fopen($logFile, 'a+');
@@ -53,8 +53,10 @@ function addToLog($errors) {
     
     foreach ($errors as $err) {
         $logContents.= $err . "\n";
-        trigger_error($err, E_USER_NOTICE);
-        echo $err . "\n";
+        if($debug){
+			trigger_error($err, E_USER_NOTICE);
+			echo $err . "\n";
+		}
     }
     $updateLog = file_put_contents($logFile, $logContents);
     
@@ -78,6 +80,4 @@ if(!$connection){
     addToLog($errToLog);
     die('Unable to connect to the EHCP MySQL database using provided credentials! Please update your config.php settings!');
 }
-
 ?>
-

+ 7 - 13
EHCP/delAccount.php

@@ -1,5 +1,4 @@
 <?php
-
 if (file_exists("config.php")) {
     include 'config.php';
 } else {
@@ -28,23 +27,19 @@ if (!isset($userToDelete)) {
     $SQL = "SELECT ftpusername FROM ftpaccounts WHERE ftpusername = '$userToDelete'";
 	$Result = execSQL($SQL, $connection);
     
-    if ($Result !== FALSE) {
+    if ($Result !== FALSE && countSQLResult($Result) == 1) {
 		$row = getSQLRowArray($Result);
         $unameDeleted = $row[0];
-    }
+    }else{
+		$errorCount++;
+        $errors[] = "The specified user $userToDelete does not exist within the databse. No actions were taken!";
+	}
     
     if (isset($unameDeleted)) {
         $SQL = "DELETE FROM ftpaccounts WHERE ftpusername = '$userToDelete'";
-		$Result = execSQL($SQL, $connection);
-        
+		$Result = execSQL($SQL, $connection); 
         if ($Result !== FALSE) {
-            
-            if ($unameDeleted === "none") {
-                $errorCount++;
-                $errors[] = "The specified user $userToDelete does not exist within the databse. No actions were taken!";
-            } else {
-                $success = 1;
-            }
+			$success = 1;
         } else {
             $errorCount++;
 			$errors[] = getSQLError($connection);			
@@ -61,5 +56,4 @@ if ($errorCount > 0) {
 
 // Return value:
 echo $success;
-
 ?>

+ 0 - 4
EHCP/listAllUsers.php

@@ -1,9 +1,6 @@
 <?php
-
 // Returns a list of all custom FTP users
-
 // Only custom users are setup when tying into the EHCP FTP API
-
 $countNotNull = 0;
 $users_list = "";
 $success = 0;
@@ -66,5 +63,4 @@ if (!isset($connection)) {
 
 // Return the user list
 echo $users_list;
-
 ?>

+ 0 - 6
EHCP/showAccount.php

@@ -1,9 +1,4 @@
 <?php
-
-// Returns the information of ONE custom ftpuser
-
-// Only custom users are setup when tying into the EHCP FTP API
-
 $countNotNull = 0;
 $user_details = "";
 $success = 0;
@@ -68,5 +63,4 @@ if (isset($ftp_account)) {
 
 // Return the user list
 echo $user_details;
-
 ?>

+ 0 - 3
EHCP/syncftp.php

@@ -1,6 +1,5 @@
 <?php
 $curDir = getcwd();
-
 if(chdir("/var/www/new/ehcp/")){
 	require ("classapp.php");
 	$app = new Application();
@@ -8,7 +7,5 @@ if(chdir("/var/www/new/ehcp/")){
 
 	$app->addDaemonOp('syncftp', '', '', '', 'sync ftp for nonstandard homes');
 }
-
 chdir($curDir);
-
 ?>

+ 0 - 2
EHCP/updateInfo.php

@@ -1,5 +1,4 @@
 <?php
-
 if (file_exists("config.php")) {
     include 'config.php';
 } else {
@@ -142,5 +141,4 @@ if ($errorCount > 0) {
 
 // Return value:
 echo $success;
-
 ?>

+ 0 - 6
EHCP/updatePass.php

@@ -1,5 +1,4 @@
 <?php
-
 if (file_exists("config.php")) {
     include 'config.php';
 } else {
@@ -67,16 +66,11 @@ if (!isset($ftp_username) || !isset($ftp_pass)) {
     }
 }
 
-
-
 // Log errors
-
 if ($errorCount > 0) {
     addToLog($errors);
 }
 
 // Return value:
-
 echo $success;
-
 ?>

+ 133 - 12
ogp_agent.pl

@@ -228,7 +228,7 @@ elsif ($no_startups != 1)
 			my (
 				$home_id,   $home_path,   $server_exe,
 				$run_dir,   $startup_cmd, $server_port,
-				$server_ip, $cpu,		 $nice
+				$server_ip, $cpu, $nice, $preStart, $envVars
 			   ) = split(',', $_);
 
 			if (is_screen_running_without_decrypt(SCREEN_TYPE_HOME, $home_id) ==
@@ -243,7 +243,7 @@ elsif ($no_startups != 1)
 			universal_start_without_decrypt(
 										 $home_id,   $home_path,   $server_exe,
 										 $run_dir,   $startup_cmd, $server_port,
-										 $server_ip, $cpu,		 $nice
+										 $server_ip, $cpu,	$nice, $preStart, $envVars
 										   );
 		}
 		close(STARTFILE);
@@ -463,7 +463,7 @@ sub create_screen_cmd
 
 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";
 	
 	$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
 	# If it crashes without user intervention, it will restart
 	open (SERV_START_SCRIPT, '>', $server_start_bashfile);
+	
 	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" 
 	. "let DIFF=(`date +%s` - \"\$NUMSECONDS\")" . "\n"
 	. "if [ \"\$DIFF\" -gt 15 ]; then" . "\n" 
@@ -494,6 +500,7 @@ sub create_screen_cmd_loop
 	. "fi" . "\n"
 	. "}" . "\n"
 	. "startServer" . "\n";
+	
 	print SERV_START_SCRIPT $respawn_server_command;
 	close (SERV_START_SCRIPT);
 	
@@ -526,6 +533,15 @@ sub replace_OGP_Vars{
 	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
 {
 	my $encoded_content = '';
@@ -670,7 +686,7 @@ sub universal_start_without_decrypt
 {
 	my (
 		$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)
@@ -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);
 	
 	# Create startup file for the server.
@@ -720,7 +765,7 @@ sub universal_start_without_decrypt
 	if (open(STARTUP, '>', $startup_file))
 	{
 		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";
 		close(STARTUP);
 	}
@@ -729,11 +774,26 @@ sub universal_start_without_decrypt
 		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.
 	my $screen_id = create_screen_id(SCREEN_TYPE_HOME, $home_id);
 	my $file_extension = substr $server_exe, -4;
 	my $cli_bin;
 	my $command;
+	my $run_before_start;
 	
 	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"){
 			deleteStoppedStatFile($home_path);
-			$cli_bin = create_screen_cmd_loop($screen_id, $command);
+			$cli_bin = create_screen_cmd_loop($screen_id, $command, $envVars);
 		}else{
 			$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"){
 			deleteStoppedStatFile($home_path);
-			$cli_bin = create_screen_cmd_loop($screen_id, $command);
+			$cli_bin = create_screen_cmd_loop($screen_id, $command, $envVars);
 		}else{
 			$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"){
 			deleteStoppedStatFile($home_path);
-			$cli_bin = create_screen_cmd_loop($screen_id, $command);
+			$cli_bin = create_screen_cmd_loop($screen_id, $command, $envVars);
 		}else{
 			$cli_bin = create_screen_cmd($screen_id, $command);
 		}
@@ -790,6 +850,9 @@ sub universal_start_without_decrypt
 	logger
 	  "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);
 	
 	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
 {	
 	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,
 		$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, 
 									$server_port, $control_protocol,
 									$control_password, $control_type, $home_path) == 0)
 	{
 		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;
 		}