feat: reuse gamemanager update logic and enforce monthly billing pricing
Agent-Logs-Url: https://github.com/GameServerPanel/GSP/sessions/f982c3a1-c9ae-4c5b-9fb6-2941d0e5b7c1 Co-authored-by: iaretechnician <2749183+iaretechnician@users.noreply.github.com>
This commit is contained in:
parent
daae48d9de
commit
5fae4a2dd5
10 changed files with 212 additions and 355 deletions
131
modules/gamemanager/update_actions.php
Normal file
131
modules/gamemanager/update_actions.php
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
<?php
|
||||
|
||||
if (!function_exists('gamemanager_choose_mod_id')) {
|
||||
function gamemanager_choose_mod_id(array $home_info, int $preferred_mod_id = 0): int
|
||||
{
|
||||
$mods = $home_info['mods'] ?? array();
|
||||
if (!is_array($mods) || empty($mods)) {
|
||||
return 0;
|
||||
}
|
||||
if ($preferred_mod_id > 0 && isset($mods[$preferred_mod_id])) {
|
||||
return $preferred_mod_id;
|
||||
}
|
||||
$keys = array_keys($mods);
|
||||
return intval(reset($keys));
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('gamemanager_trigger_update_install')) {
|
||||
function gamemanager_trigger_update_install($db, array $home_info, int $mod_id, array $options = array()): array
|
||||
{
|
||||
$home_id = intval($home_info['home_id'] ?? 0);
|
||||
$mod_id = gamemanager_choose_mod_id($home_info, $mod_id);
|
||||
if ($home_id <= 0) {
|
||||
return array('ok' => false, 'pending' => true, 'message' => 'Invalid home_id.', 'mod_id' => $mod_id);
|
||||
}
|
||||
if ($mod_id <= 0 || empty($home_info['mods'][$mod_id])) {
|
||||
return array('ok' => false, 'pending' => true, 'message' => "No mod profile configured for home #{$home_id}.", 'mod_id' => $mod_id);
|
||||
}
|
||||
|
||||
$server_xml = read_server_config(SERVER_CONFIG_LOCATION . "/" . $home_info['home_cfg_file']);
|
||||
if (!$server_xml) {
|
||||
return array('ok' => false, 'pending' => true, 'message' => "Could not read server config XML for home #{$home_id}.", 'mod_id' => $mod_id);
|
||||
}
|
||||
|
||||
$remote = new OGPRemoteLibrary($home_info['agent_ip'], $home_info['agent_port'], $home_info['encryption_key'], $home_info['timeout']);
|
||||
if ($remote->status_chk() === 0) {
|
||||
return array('ok' => false, 'pending' => true, 'message' => 'Agent is offline.', 'mod_id' => $mod_id);
|
||||
}
|
||||
if ($remote->is_screen_running(OGP_SCREEN_TYPE_HOME, $home_id) == 1) {
|
||||
return array('ok' => false, 'pending' => false, 'message' => 'Server is running and cannot be updated.', 'mod_id' => $mod_id);
|
||||
}
|
||||
|
||||
$log_txt = '';
|
||||
$update_active = $remote->get_log(OGP_SCREEN_TYPE_UPDATE, $home_id, clean_path($home_info['home_path']), $log_txt);
|
||||
if ($update_active == 1) {
|
||||
return array('ok' => true, 'started' => true, 'already_running' => true, 'message' => 'Update already in progress.', 'mod_id' => $mod_id);
|
||||
}
|
||||
|
||||
$modkey = $home_info['mods'][$mod_id]['mod_key'] ?? '';
|
||||
$mod_xml = xml_get_mod($server_xml, $modkey);
|
||||
if (!$mod_xml) {
|
||||
return array('ok' => false, 'pending' => true, 'message' => "Mod key '{$modkey}' not found in XML.", 'mod_id' => $mod_id);
|
||||
}
|
||||
|
||||
$installer_name = isset($mod_xml->installer_name) ? (string)$mod_xml->installer_name : (string)$modkey;
|
||||
$precmd = $home_info['mods'][$mod_id]['precmd'] == ""
|
||||
? ($home_info['mods'][$mod_id]['def_precmd'] == "" ? $server_xml->pre_install : $home_info['mods'][$mod_id]['def_precmd'])
|
||||
: $home_info['mods'][$mod_id]['precmd'];
|
||||
$postcmd = $home_info['mods'][$mod_id]['postcmd'] == ""
|
||||
? ($home_info['mods'][$mod_id]['def_postcmd'] == "" ? $server_xml->post_install : $home_info['mods'][$mod_id]['def_precmd'])
|
||||
: $home_info['mods'][$mod_id]['postcmd'];
|
||||
$exec_folder_path = clean_path($home_info['home_path'] . "/" . $server_xml->exe_location);
|
||||
$exec_path = clean_path($exec_folder_path . "/" . $server_xml->server_exec_name);
|
||||
|
||||
$master_server_home_id = intval($options['master_server_home_id'] ?? 0);
|
||||
if ($master_server_home_id > 0) {
|
||||
if ($db->getMasterServer($home_info['remote_server_id'], $home_info['home_cfg_id']) != $master_server_home_id) {
|
||||
return array('ok' => false, 'pending' => false, 'message' => 'Attempting update from non-master server.', 'mod_id' => $mod_id);
|
||||
}
|
||||
if ($master_server_home_id == $home_id) {
|
||||
return array('ok' => false, 'pending' => false, 'message' => 'Cannot update from own self.', 'mod_id' => $mod_id);
|
||||
}
|
||||
$ms_info = $db->getGameHome($master_server_home_id);
|
||||
$steam_out = $remote->masterServerUpdate($home_id, $home_info['home_path'], $master_server_home_id, $ms_info['home_path'], $exec_folder_path, $exec_path, $precmd, $postcmd);
|
||||
if ($steam_out === 0) {
|
||||
return array('ok' => false, 'pending' => true, 'message' => 'Failed to start master update.', 'mod_id' => $mod_id);
|
||||
}
|
||||
return array('ok' => true, 'started' => true, 'message' => 'Update started.', 'mod_id' => $mod_id);
|
||||
}
|
||||
|
||||
$use_steamcmd = ((string)$server_xml->installer === "steamcmd");
|
||||
if ($use_steamcmd && !empty((string)$installer_name)) {
|
||||
$cfg_os = '';
|
||||
if (preg_match("/win32/", $server_xml->game_key) || preg_match("/win64/", $server_xml->game_key)) {
|
||||
$cfg_os = "windows";
|
||||
} elseif (preg_match("/linux/", $server_xml->game_key)) {
|
||||
$cfg_os = "linux";
|
||||
}
|
||||
|
||||
$settings = is_array($options['settings'] ?? null) ? $options['settings'] : $db->getSettings();
|
||||
if (!empty($mod_xml->installer_login)) {
|
||||
$login = (string)$mod_xml->installer_login;
|
||||
$pass = '';
|
||||
} else {
|
||||
$login = (string)($settings['steam_user'] ?? '');
|
||||
$pass = (string)($settings['steam_pass'] ?? '');
|
||||
}
|
||||
|
||||
$modname = ($installer_name == '90') ? $modkey : '';
|
||||
$betaname = isset($mod_xml->betaname) ? (string)$mod_xml->betaname : '';
|
||||
$betapwd = isset($mod_xml->betapwd) ? (string)$mod_xml->betapwd : '';
|
||||
$arch = isset($mod_xml->steam_bitness) ? (string)$mod_xml->steam_bitness : '';
|
||||
$lockFiles = (isset($server_xml->lock_files) && !empty($server_xml->lock_files)) ? trim((string)$server_xml->lock_files) : "";
|
||||
$steam_out = $remote->steam_cmd($home_id, $home_info['home_path'], $installer_name, $modname,
|
||||
$betaname, $betapwd, $login, $pass, $settings['steam_guard'] ?? '',
|
||||
$exec_folder_path, $exec_path, $precmd, $postcmd, $cfg_os, $lockFiles, $arch);
|
||||
if ($steam_out === 0) {
|
||||
return array('ok' => false, 'pending' => true, 'message' => 'Failed to start SteamCMD update.', 'mod_id' => $mod_id);
|
||||
}
|
||||
return array('ok' => true, 'started' => true, 'message' => 'Update started.', 'mod_id' => $mod_id);
|
||||
}
|
||||
|
||||
$ran_scripts = false;
|
||||
if (!empty((string)$precmd)) {
|
||||
$remote->exec((string)$precmd);
|
||||
$ran_scripts = true;
|
||||
}
|
||||
if (!empty((string)$postcmd)) {
|
||||
$remote->exec((string)$postcmd);
|
||||
$ran_scripts = true;
|
||||
}
|
||||
return array(
|
||||
'ok' => true,
|
||||
'started' => $ran_scripts,
|
||||
'completed' => !$ran_scripts,
|
||||
'message' => $ran_scripts ? 'Script install started.' : 'No installer command was required.',
|
||||
'mod_id' => $mod_id
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
require_once("includes/lib_remote.php");
|
||||
require_once("modules/config_games/server_config_parser.php");
|
||||
require_once("modules/gamemanager/update_actions.php");
|
||||
|
||||
function exec_ogp_module() {
|
||||
|
||||
|
|
@ -90,111 +91,27 @@ function exec_ogp_module() {
|
|||
// Start update.
|
||||
else if ($_GET['update'] == 'update' && $update_active != 1)
|
||||
{
|
||||
$installer_name = $modkey;
|
||||
|
||||
if ( isset( $mod_xml->installer_name ) )
|
||||
{
|
||||
$installer_name = $mod_xml->installer_name;
|
||||
$start_result = gamemanager_trigger_update_install(
|
||||
$db,
|
||||
$home_info,
|
||||
intval($mod_id),
|
||||
array(
|
||||
'master_server_home_id' => isset($_REQUEST['master_server_home_id']) ? intval($_REQUEST['master_server_home_id']) : 0,
|
||||
'settings' => $db->getSettings(),
|
||||
)
|
||||
);
|
||||
$mod_id = intval($start_result['mod_id'] ?? $mod_id);
|
||||
if (empty($start_result['ok'])) {
|
||||
print_failure(!empty($start_result['message']) ? $start_result['message'] : get_lang("failed_to_start_steam_update"));
|
||||
return;
|
||||
}
|
||||
|
||||
$precmd = $home_info['mods'][$mod_id]['precmd'] == "" ?
|
||||
( $home_info['mods'][$mod_id]['def_precmd'] == "" ? $server_xml->pre_install :
|
||||
$home_info['mods'][$mod_id]['def_precmd'] ) : $home_info['mods'][$mod_id]['precmd'];
|
||||
|
||||
$postcmd = $home_info['mods'][$mod_id]['postcmd'] == "" ?
|
||||
( $home_info['mods'][$mod_id]['def_postcmd'] == "" ? $server_xml->post_install :
|
||||
$home_info['mods'][$mod_id]['def_precmd'] ) : $home_info['mods'][$mod_id]['postcmd'];
|
||||
|
||||
$exec_folder_path = clean_path($home_info['home_path'] . "/" . $server_xml->exe_location );
|
||||
$exec_path = clean_path($exec_folder_path . "/" . $server_xml->server_exec_name );
|
||||
|
||||
if( isset( $_REQUEST['master_server_home_id'] ) )
|
||||
{
|
||||
$ms_home_id = $_REQUEST['master_server_home_id'];
|
||||
|
||||
if ($db->getMasterServer($home_info['remote_server_id'], $home_info['home_cfg_id']) == $ms_home_id) {
|
||||
if ($ms_home_id !== $home_id) {
|
||||
$ms_info = $db->getGameHome($ms_home_id);
|
||||
$steam_out = $remote->masterServerUpdate( $home_id,$home_info['home_path'],$ms_home_id,$ms_info['home_path'],$exec_folder_path,$exec_path,$precmd,$postcmd );
|
||||
} else {
|
||||
print_failure(get_lang('cannot_update_from_own_self'));
|
||||
$view->refresh('?m=gamemanager&p=game_monitor', 2);
|
||||
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
$db->logger(get_lang_f('update_attempt_from_nonmaster_server', $_SESSION['users_login'], $home_id, $ms_home_id));
|
||||
print_failure(get_lang('attempting_nonmaster_update'));
|
||||
$view->refresh('?m=gamemanager&p=game_monitor', 2);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
elseif ($use_steamcmd && !empty((string)$installer_name))
|
||||
{
|
||||
if( preg_match("/win32/", $server_xml->game_key) OR preg_match("/win64/", $server_xml->game_key) )
|
||||
$cfg_os = "windows";
|
||||
elseif( preg_match("/linux/", $server_xml->game_key) )
|
||||
$cfg_os = "linux";
|
||||
|
||||
$settings = $db->getSettings();
|
||||
|
||||
// Some games like L4D2 require anonymous login
|
||||
if($mod_xml->installer_login){
|
||||
$login = $mod_xml->installer_login;
|
||||
$pass = '';
|
||||
}else{
|
||||
$login = $settings['steam_user'];
|
||||
$pass = $settings['steam_pass'];
|
||||
}
|
||||
|
||||
$modname = ( $installer_name == '90' ) ? $modkey : '';
|
||||
$betaname = isset($mod_xml->betaname) ? $mod_xml->betaname : '';
|
||||
$betapwd = isset($mod_xml->betapwd) ? $mod_xml->betapwd : '';
|
||||
$arch = isset($mod_xml->steam_bitness) ? $mod_xml->steam_bitness : '';
|
||||
|
||||
// Additional files to lock
|
||||
if(isset($server_xml->lock_files) && !empty($server_xml->lock_files)){
|
||||
$lockFiles = trim($server_xml->lock_files);
|
||||
}else{
|
||||
$lockFiles = "";
|
||||
}
|
||||
|
||||
$steam_out = $remote->steam_cmd( $home_id,$home_info['home_path'],$installer_name,$modname,
|
||||
$betaname,$betapwd,$login,$pass,$settings['steam_guard'],
|
||||
$exec_folder_path,$exec_path,$precmd,$postcmd,$cfg_os,$lockFiles,$arch);
|
||||
}
|
||||
else
|
||||
{
|
||||
// No SteamCMD installer — run pre/post install scripts only.
|
||||
$ran_scripts = false;
|
||||
if (!empty((string)$precmd)) {
|
||||
$remote->exec((string)$precmd);
|
||||
$ran_scripts = true;
|
||||
}
|
||||
if (!empty((string)$postcmd)) {
|
||||
$remote->exec((string)$postcmd);
|
||||
$ran_scripts = true;
|
||||
}
|
||||
if ($ran_scripts) {
|
||||
print_success( get_lang("update_started") );
|
||||
} else {
|
||||
print_success( get_lang("update_completed") );
|
||||
}
|
||||
if (!empty($start_result['started'])) {
|
||||
print_success(get_lang("update_started"));
|
||||
} else {
|
||||
print_success(get_lang("update_completed"));
|
||||
$view->refresh("?m=gamemanager&p=game_monitor&home_id=$home_id", 3);
|
||||
return;
|
||||
}
|
||||
|
||||
if( $steam_out === 0 )
|
||||
{
|
||||
print_failure( get_lang("failed_to_start_steam_update") );
|
||||
return;
|
||||
}
|
||||
else if ( $steam_out === 1 )
|
||||
{
|
||||
print_success( get_lang("update_started") );
|
||||
}
|
||||
}
|
||||
// Refresh update page.
|
||||
else
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue