277 lines
7.8 KiB
PHP
277 lines
7.8 KiB
PHP
<?PHP
|
|
|
|
|
|
|
|
|
|
require __DIR__.'/scripts/func.downloadFile.php';
|
|
require __DIR__.'/scripts/class.FactorioVersion.php';
|
|
require __DIR__.'/scripts/class.Systemctl.php';
|
|
require __DIR__.'/scripts/class.ValveRcon.php';
|
|
|
|
|
|
|
|
// Welches Build ist gewünscht?
|
|
$target_build = "stable";
|
|
$target_edition = "headless";
|
|
|
|
// Wo wird der Server dieses Builds heruntergeladen?
|
|
$download_source = "https://factorio.com/get-download/stable/headless/linux64";
|
|
|
|
// In welcher Datei soll der Download gespeichert werden?
|
|
// Wichtig da Factorio eine .tar.gz datei als linux64 Datei anbietet.
|
|
$download_output = __DIR__."/factorio.tar";
|
|
|
|
// Welche Dateien oder Ordner sollen vom alten Server übernommen werden?
|
|
$copy_paths = [
|
|
"server-adminlist.json",
|
|
"player-data.json",
|
|
"config",
|
|
"config-path.cfg"
|
|
];
|
|
|
|
|
|
|
|
|
|
// In welcher Datei steht die aktuelle Versionsnummer?
|
|
$current_version_str = (function(){
|
|
|
|
$env = file_get_contents(__DIR__."/factorio-settings.env");
|
|
|
|
$version = preg_replace("/.*FACTORIO_VERSION\=\"((\d+\.)+\d+)\".*/su","$1",$env);
|
|
|
|
return $version;
|
|
|
|
})();
|
|
$current_version = new \NAE\Factorio\FactorioVersion($current_version_str);
|
|
echo "CURRENT_VERSION: ".$current_version->getVersion().PHP_EOL;
|
|
|
|
|
|
|
|
// Aktuelle Versionsnummern aus der Factorio-API abrufen.
|
|
$live_versions = json_decode(file_get_contents("https://factorio.com/api/latest-releases"), true);
|
|
|
|
// Anhand von Build und Edition die richtige Version auswählen.
|
|
$latest_version = new \NAE\Factorio\FactorioVersion($live_versions[$target_build][$target_edition]);
|
|
echo "LATEST_VERSION: ".$latest_version->getVersion().PHP_EOL.PHP_EOL;
|
|
|
|
|
|
|
|
|
|
/**
|
|
* Schreibt die neue Versionsnummer in die environment (env) datei des servers
|
|
* @return bool Schreiben erfolgreich?
|
|
*/
|
|
function writeBackVersion() : bool {
|
|
|
|
global $latest_version;
|
|
|
|
$env_file = __DIR__."/factorio-settings.env";
|
|
$env = file_get_contents($env_file);
|
|
$new_env = preg_replace("/FACTORIO_VERSION\=\"((\d+\.)+\d+)\"/su", "FACTORIO_VERSION=\"".$latest_version->getVersion()."\"", $env);
|
|
|
|
return ( file_put_contents($env_file, $new_env) !== false );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Variablen zum speichern des Fortschrittes bzw. Erfolgs.
|
|
$download_success = false;
|
|
$extract_success = false;
|
|
|
|
echo "# [ PRÜFE AUF FACTORIO UPDATES ]".PHP_EOL;
|
|
|
|
if($latest_version->isNewer($current_version)) { // neue Version verfügbar?
|
|
|
|
echo "## [ NEUE VERSION GEFUNDEN : UPDATE STARTET ]".PHP_EOL;
|
|
|
|
if(file_exists($download_output)) { // liegt noch ein altes Server Archiv im Vertzeichnis?
|
|
|
|
echo PHP_EOL."[ ALTE SERVER FILES GEFUNDEN : STARTE LÖSCHVORGANG ]".PHP_EOL;
|
|
|
|
if( unlink($download_output) ) { // altes Archiv löschen
|
|
|
|
echo "-> ALTE SERVER FILES ERFOLGREICH GELÖSCHT!".PHP_EOL;
|
|
|
|
} else {
|
|
|
|
echo "-> ERR : ALTE SERVER FILES ERFOLGREICH GELÖSCHT!".PHP_EOL;
|
|
die(1);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
echo PHP_EOL."[ STARTE DOWNLOAD DER SERVER FILES ]".PHP_EOL;
|
|
|
|
if(\NAE\Functions\Curl\downloadFile($download_output, $download_source)) { // Download der neuen Server Files
|
|
|
|
echo "-> DOWNLOAD SUCCESS!".PHP_EOL;
|
|
$download_success = true;
|
|
|
|
} else {
|
|
|
|
echo "-> ERR : DOWNLOAD FEHLGESCHLAGEN!".PHP_EOL;
|
|
die(1);
|
|
|
|
}
|
|
|
|
|
|
if($download_success) {
|
|
|
|
echo PHP_EOL."[ STARTE DAS ENTPACKEN DER NEUEN SERVER FILES ]".PHP_EOL;
|
|
|
|
exec("tar -xvf \"$download_output\" > /dev/null", $output, $return); // Entpacken
|
|
|
|
if($return === 0) {
|
|
|
|
if( rename(__DIR__."/factorio", __DIR__."/factorio-".$latest_version->getVersion()) ) { // Umbenennen mit Versionsnummer
|
|
|
|
echo "-> Neue Server Files wurden Erfolgreich entpackt!".PHP_EOL;
|
|
$extract_success = true;
|
|
|
|
} else {
|
|
|
|
echo "-> ERR : Umbenennen des Factorio Ordners mit Versionsnummer Fehlgeschlagen!".PHP_EOL;
|
|
die(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
echo "-> ERR : Entpacken der Server Files ist Fehlgeschlagen!".PHP_EOL;
|
|
die(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($extract_success) {
|
|
|
|
// RCON Kommunikation zum Server starten
|
|
$rcon_data = (function() : ?array {
|
|
|
|
global $current_version;
|
|
|
|
$cfg = parse_ini_file(__DIR__."/factorio-".$current_version->getVersion()."/config/config.ini", true);
|
|
if(!$cfg) return null;
|
|
|
|
$cd = [];
|
|
|
|
if(isset($cfg["other"])) {
|
|
|
|
$parts = explode(":", $cfg["other"]["local-rcon-socket"]??"127.0.0.1:27015");
|
|
$cd["ip"] = $parts[0];
|
|
$cd["port"] = $parts[1];
|
|
|
|
if(isset($cfg["other"]["local-rcon-socket"])) {
|
|
$cd["password"] = $cfg["other"]["local-rcon-password"]??"";
|
|
}
|
|
|
|
}
|
|
|
|
return $cd;
|
|
|
|
})();
|
|
|
|
$rcon = null;
|
|
if($rcon_data) {
|
|
try {
|
|
|
|
$rcon = new \NAE\Valve\Rcon\ValveRcon($rcon_data["ip"], $rcon_data["port"], $rcon_data["password"]);
|
|
$rcon->sendCommand("ACHTUNG: Server wird in kürze für ein Update gestoppt, bitte trennen Sie Ihre Verbindung zum Server. Der Server wird in kürze wieder verfügbar sein. Zeit bis zum Stopp: 60 Sekunden.", false);
|
|
|
|
if($rcon->getError() !== null) {
|
|
echo "[!] ERR : RCON Verbindung konnte nicht aufgebaut werden!".PHP_EOL;
|
|
$rcon = null;
|
|
}
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
echo "[!] ERR : RCON Verbindung konnte nicht aufgebaut werden!".PHP_EOL;
|
|
$rcon = null;
|
|
|
|
}
|
|
}
|
|
|
|
if($rcon) sleep(60); // Falls die Meldung geklappt hat 60s warten, damit Spieler den Server verlassen können.
|
|
|
|
|
|
// inititialize Systemctl Service interface
|
|
$service = new \NAE\Service\Systemctl("factorio");
|
|
|
|
// Server Stoppen
|
|
if($service->stop()) {
|
|
echo "[+] FACTORIO SERVER WURDE GESTOPPT!".PHP_EOL;
|
|
}
|
|
|
|
// neue Versionsnummer in .env Datei übernehmen
|
|
if(writeBackVersion()) {
|
|
echo "[+] Version in Environment File ist aktualisiert".PHP_EOL;
|
|
}
|
|
|
|
// Dateien aus alten Server Files kopieren..
|
|
if( is_dir(__DIR__."/factorio-".$current_version->getVersion()) && is_dir(__DIR__."/factorio-".$latest_version->getVersion()) ) {
|
|
|
|
echo PHP_EOL."[ Kopiere Konfigurations-Dateien in den neuen Server Pfad ]".PHP_EOL;
|
|
|
|
foreach($copy_paths as $copy_id => $copy_name) {
|
|
|
|
$copy_source = __DIR__."factorio-/".$current_version->getVersion().$copy_name;
|
|
$copy_dest = __DIR__."factorio-/".$latest_version->getVersion().$copy_name;
|
|
|
|
if(file_exists($copy_source)) {
|
|
|
|
echo "[ Kopierenvorgang Nr. ".($copy_id+1)." von ".count($copy_paths)." - $copy_name ] ";
|
|
if(copy($copy_source, $copy_dest)) {
|
|
|
|
echo "-> Kopiervorgang Erfolgreich!".PHP_EOL;
|
|
|
|
} else {
|
|
|
|
echo "-> ERR : Kopiervorgang Fehlgeschlagen!".PHP_EOL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
echo " - Quelle nicht vorhanden!".PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Fix file permissions..
|
|
(function(){
|
|
|
|
global $latest_version;
|
|
|
|
exec("chown -R factorio:factorio \"".__DIR__."/factorio-".$latest_version->getVersion()."\"", $output, $return);
|
|
if($return === 0) {
|
|
echo "[+] Rechte im neuen Server verzeichnis korrigiert.".PHP_EOL;
|
|
} else {
|
|
echo "[!] ERR : Rechte im neuen Server verzeichnis konnten nicht korrigiert werden.".PHP_EOL;
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
if($service->start()) {
|
|
echo "[+] FACTORIO SERVER WURDE WIEDER GESTARTET!".PHP_EOL;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Keine aktuellere Version verfügbar!
|
|
echo "-> Kein Update verfügbar.".PHP_EOL;
|
|
|
|
}
|