Upgrade from frontend

This commit is contained in:
OpenXE 2022-12-05 17:11:32 +00:00
parent c248cb16fb
commit e4773da6f0
4 changed files with 416 additions and 187 deletions

View File

@ -7,25 +7,20 @@
*
*/
class DatabaseConnectionInfo {
function __construct() {
require('../conf/user.inc.php');
}
function echo_output(array $output) {
echo(implode("\n",$output)."\n");
}
$dbci = new DatabaseConnectionInfo;
function abort(string $message) {
echo($message."\n");
echo("--------------- Aborted! ---------------\n");
}
$host = $dbci->WFdbhost;
$user = $dbci->WFdbuser;
$passwd = $dbci->WFdbpass;
$schema = $dbci->WFdbname;
require('../tools/database_compare/mustal_mysql_upgrade_tool.php');
$lockfile_name = "data/.in_progress.flag";
$remote_file_name = "data/remote.json";
$datafolder = "data";
$schema_file_name = "db_schema.json";
class DatabaseConnectionInfo {
function __construct() {
require_once('../conf/user.inc.php');
}
}
function git(string $command, &$output, bool $verbose, string $error_text) : int {
$output = array();
@ -41,201 +36,274 @@ function git(string $command, &$output, bool $verbose, string $error_text) : int
return($retval);
}
function echo_output(array $output) {
echo("\n".implode("\n",$output)."\n");
}
function abort(string $message) {
echo($message."\n");
echo("--------------- Aborted! ---------------\n");
exit(-1);
}
// -------------------------------- START
if (in_array('-v', $argv)) {
$verbose = true;
// Check for correct call method
$directory = "";
if (php_sapi_name() == "cli") {
$cli = true;
$directory = ".";
} else if (basename(getcwd()) != 'upgrade') {
// Started from "www"
$directory = "../upgrade";
} else {
$verbose = false;
}
if (in_array('-f', $argv)) {
$force = true;
} else {
$force = false;
}
echo("--------------- OpenXE upgrade ---------------\n");
if (basename(getcwd()) != 'upgrade') {
abort("Must be executed from 'upgrade' directory.");
return(-1);
}
$remote_info_contents = file_get_contents($remote_file_name);
if (!$remote_info_contents) {
abort("Unable to load $remote_file_name");
if ($cli) {
if (in_array('-v', $argv)) {
$verbose = true;
} else {
$verbose = false;
}
if (in_array('-f', $argv)) {
$force = true;
} else {
$force = false;
}
if (in_array('-do', $argv)) {
$do_upgrade = true;
} else {
$do_upgrade = false;
}
upgrade_main($directory, $verbose, $do_upgrade, $force);
}
$remote_info = json_decode($remote_info_contents, true);
// -------------------------------- END
// Get changed files on system -> Should be empty
$output = array();
$retval = git("ls-files -m ..", $output,$verbose,"Git not initialized.");
function upgrade_main(string $directory,bool $verbose, bool $do_upgrade, bool $force) {
// Not a git repository -> Create it and then go ahead
if ($retval == 128) {
echo("Setting up git...");
$retval = git("init ..", $output,$verbose,"Error while initializing git!");
if ($retval != 0) {
abort("");
}
$retval = git("add ../.", $output,$verbose,"Error while initializing git!");
if ($retval != 0) {
abort("");
}
$retval = git("fetch ".$remote_info['host']." ".$remote_info['branch'], $output,$verbose,"Error while initializing git!");
if ($retval != 0) {
abort("");
}
$retval = git("checkout FETCH_HEAD -f", $output,$verbose,"Error while initializing git!");
if ($retval != 0) {
abort("");
}
}
$dbci = new DatabaseConnectionInfo;
if ($retval != 0) {
abort("Error while executing git!");
}
$host = $dbci->WFdbhost;
$user = $dbci->WFdbuser;
$passwd = $dbci->WFdbpass;
$schema = $dbci->WFdbname;
if (!empty($output)) {
echo("There are modified files:");
echo_output($output);
if (!$force) {
abort("Clear modified files or use -f");
}
}
require_once($directory.'/../tools/database_compare/mustal_mysql_upgrade_tool.php');
echo("--------------- Locking system ---------------\n");
if (file_exists($lockfile_name)) {
echo("System is already locked.\n");
} else {
file_put_contents($lockfile_name," ");
}
$datafolder = $directory."/data";
$lockfile_name = $datafolder."/.in_progress.flag";
$remote_file_name = $datafolder."/remote.json";
$schema_file_name = "db_schema.json";
echo("--------------- Pulling files... ---------------\n");
echo("--------------- OpenXE upgrade ---------------\n");
if ($force) {
$retval = git("reset --hard",$output,$verbose,"Error while resetting modified files!");
if ($retval != 0) {
echo_output($output);
abort("");
}
}
require_once($directory.'/../cronjobs/githash.php');
$retval = git("pull ".$remote_info['host']." ".$remote_info['branch'],$output,$verbose,"Error while pulling files!");
if ($retval != 0) {
echo_output($output);
abort("");
}
$remote_info_contents = file_get_contents($remote_file_name);
if (!$remote_info_contents) {
abort("Unable to load $remote_file_name");
return(-1);
}
$remote_info = json_decode($remote_info_contents, true);
$retval = git("reset --hard",$output,$verbose,"Error while applying files!");
if ($retval != 0) {
echo_output($output);
abort("");
}
// Get changed files on system -> Should be empty
$output = array();
$retval = git("ls-files -m ..", $output,$verbose,"Git not initialized.");
echo("--------------- Files upgrade completed ---------------\n");
$retval = git("log -1 ",$output,$verbose,"Error while checking files!");
if ($retval != 0) {
echo_output($output);
abort("");
}
echo_output($output);
echo("--------------- Loading from database '$schema@$host'... ---------------\n");
$db_def = mustal_load_tables_from_db($host, $schema, $user, $passwd, $mustal_replacers);
if (empty($db_def)) {
echo ("Could not load from $schema@$host\n");
exit;
}
$compare_differences = array();
echo("--------------- Loading from JSON... ---------------\n");
$compare_def = mustal_load_tables_from_json($datafolder, $schema_file_name);
if (empty($compare_def)) {
echo ("Could not load from JSON $schema_file_name\n");
exit;
}
echo("--------------- Comparing database '$schema@$host' vs. JSON '".$compare_def['database']."@".$compare_def['host']."' ---------------\n");
$compare_differences = mustal_compare_table_array($compare_def,"in JSON",$db_def,"in DB",true);
echo((empty($compare_differences)?0:count($compare_differences))." differences.\n");
echo("--------------- Calculating database upgrade for '$schema@$host'... ---------------\n");
$upgrade_sql = array();
$result = mustal_calculate_db_upgrade($compare_def, $db_def, $upgrade_sql, $mustal_replacers);
if ($result != 0) {
echo("Error: $result\n");
exit;
}
echo(count($upgrade_sql)." upgrade statements\n");
echo("--------------- Executing database upgrade for '$schema@$host' database... ---------------\n");
// First get the contents of the database table structure
$mysqli = mysqli_connect($host, $user, $passwd, $schema);
/* Check if the connection succeeded */
if (!$mysqli) {
echo ("Failed to connect!\n");
} else {
$counter = 0;
$error_counter = 0;
$number_of_statements = count($upgrade_sql);
foreach ($upgrade_sql as $sql) {
$counter++;
echo("\rUpgrade step $counter of $number_of_statements... ");
$query_result = mysqli_query($mysqli, $sql);
if (!$query_result) {
$error = " not ok: ". mysqli_error($mysqli);
echo($error);
echo("\n");
file_put_contents("./errors.txt",date()." ".$error.$sql."\n",FILE_APPEND);
$error_counter++;
} else {
echo("ok.\r");
// Not a git repository -> Create it and then go ahead
if ($retval == 128) {
echo("Setting up git...");
$retval = git("init ..", $output,$verbose,"Error while initializing git!");
if ($retval != 0) {
abort("");
return(-1);
}
$retval = git("add ../.", $output,$verbose,"Error while initializing git!");
if ($retval != 0) {
abort("");
return(-1);
}
$retval = git("fetch ".$remote_info['host']." ".$remote_info['branch'], $output,$verbose,"Error while initializing git!");
if ($retval != 0) {
abort("");
return(-1);
}
$retval = git("checkout FETCH_HEAD -f", $output,$verbose,"Error while initializing git!");
if ($retval != 0) {
abort("");
return(-1);
}
}
echo("\n");
echo("$error_counter errors.\n");
if ($error_counter > 0) {
echo("See 'errors.txt'\n");
if ($retval != 0) {
abort("Error while executing git!");
return(-1);
}
echo("--------------- Checking database upgrade for '$schema@$host'... ---------------\n");
$db_def = mustal_load_tables_from_db($host, $schema, $user, $passwd, $mustal_replacers);
echo("--------------- Comparing database '$schema@$host' vs. JSON '".$compare_def['database']."@".$compare_def['host']."' ---------------\n");
$compare_differences = mustal_compare_table_array($compare_def,"in JSON",$db_def,"in DB",true);
echo((empty($compare_differences)?0:count($compare_differences))." differences.\n");
if ($verbose) {
echo("--------------- Update history ---------------\n");
$retval = git("log --date=short-local --pretty=\"%cd (%h): %s\" HEAD --not HEAD~4",$output,$verbose,"Error while showing history!");
if ($retval != 0) {
abort("");
return(-1);
}
}
if ($do_upgrade) {
if (!empty($output)) {
echo("There are modified files:");
echo_output($output);
if (!$force) {
abort("Clear modified files or use -f");
return(-1);
}
}
echo("--------------- Locking system ---------------\n");
if (file_exists($lockfile_name)) {
echo("System is already locked.\n");
} else {
file_put_contents($lockfile_name," ");
}
echo("--------------- Pulling files... ---------------\n");
if ($force) {
$retval = git("reset --hard",$output,$verbose,"Error while resetting modified files!");
if ($retval != 0) {
echo_output($output);
abort("");
return(-1);
}
}
$retval = git("pull ".$remote_info['host']." ".$remote_info['branch'],$output,$verbose,"Error while pulling files!");
if ($retval != 0) {
echo_output($output);
abort("");
return(-1);
}
$retval = git("reset --hard",$output,$verbose,"Error while applying files!");
if ($retval != 0) {
echo_output($output);
abort("");
return(-1);
}
echo("--------------- Files upgrade completed ---------------\n");
$retval = git("log -1 ",$output,$verbose,"Error while checking files!");
if ($retval != 0) {
echo_output($output);
abort("");
return(-1);
}
echo_output($output);
echo("--------------- Loading from database '$schema@$host'... ---------------\n");
$db_def = mustal_load_tables_from_db($host, $schema, $user, $passwd, $mustal_replacers);
if (empty($db_def)) {
echo ("Could not load from $schema@$host\n");
exit;
}
$compare_differences = array();
echo("--------------- Loading from JSON... ---------------\n");
$compare_def = mustal_load_tables_from_json($datafolder, $schema_file_name);
if (empty($compare_def)) {
abort("Could not load from JSON $schema_file_name\n");
return(-1);
}
echo("--------------- Comparing database '$schema@$host' vs. JSON '".$compare_def['database']."@".$compare_def['host']."' ---------------\n");
$compare_differences = mustal_compare_table_array($compare_def,"in JSON",$db_def,"in DB",true);
echo((empty($compare_differences)?0:count($compare_differences))." differences.\n");
echo("--------------- Calculating database upgrade for '$schema@$host'... ---------------\n");
$upgrade_sql = array();
$result = mustal_calculate_db_upgrade($compare_def, $db_def, $upgrade_sql, $mustal_replacers);
if ($result != 0) {
abort("Error: $result\n");
return(-1);
}
echo(count($upgrade_sql)." upgrade statements\n");
echo("--------------- Executing database upgrade for '$schema@$host' database... ---------------\n");
// First get the contents of the database table structure
$mysqli = mysqli_connect($host, $user, $passwd, $schema);
/* Check if the connection succeeded */
if (!$mysqli) {
echo ("Failed to connect!\n");
} else {
$counter = 0;
$error_counter = 0;
$number_of_statements = count($upgrade_sql);
foreach ($upgrade_sql as $sql) {
$counter++;
echo("\rUpgrade step $counter of $number_of_statements... ");
$query_result = mysqli_query($mysqli, $sql);
if (!$query_result) {
$error = " not ok: ". mysqli_error($mysqli);
echo($error);
echo("\n");
file_put_contents("./errors.txt",date()." ".$error.$sql."\n",FILE_APPEND);
$error_counter++;
} else {
echo("ok.\r");
}
}
echo("\n");
echo("$error_counter errors.\n");
if ($error_counter > 0) {
echo("See 'errors.txt'\n");
}
echo("--------------- Checking database upgrade for '$schema@$host'... ---------------\n");
$db_def = mustal_load_tables_from_db($host, $schema, $user, $passwd, $mustal_replacers);
echo("--------------- Comparing database '$schema@$host' vs. JSON '".$compare_def['database']."@".$compare_def['host']."' ---------------\n");
$compare_differences = mustal_compare_table_array($compare_def,"in JSON",$db_def,"in DB",true);
echo((empty($compare_differences)?0:count($compare_differences))." differences.\n");
}
echo("--------------- Unlocking system ---------------\n");
unlink($lockfile_name);
}
else { // Dry run
echo("--------------- Dry run, use -do to upgrade ---------------\n");
echo("--------------- Fetching files... ---------------\n");
$retval = git("fetch ".$remote_info['host']." ".$remote_info['branch'],$output,$verbose,"Error while fetching files!");
if ($retval != 0) {
echo_output($output);
abort("");
}
echo("--------------- Pending upgrades: ---------------\n");
$retval = git("log --date=short-local --pretty=\"%cd (%h): %s\" FETCH_HEAD --not HEAD",$output,$verbose,"Error while fetching files!");
if (!$verbose) {
echo_output($output);
}
if (empty($output)) {
echo("No upgrades pending.\n");
}
if ($retval != 0) {
abort("");
}
}
echo("--------------- Done! ---------------\n");
return(0);
}
echo("--------------- Unlocking system ---------------\n");
unlink($lockfile_name);
echo("--------------- Done! ---------------\n");
exit(0);

View File

@ -7107,6 +7107,7 @@ title: 'Abschicken',
$navarray['menu']['admin'][$menu]['sec'][] = array('Einstellungen','einstellungen','list');
$navarray['menu']['admin'][$menu]['sec'][] = array('Online-Shops / Marktplätze','onlineshops','list');
$navarray['menu']['admin'][$menu]['sec'][] = array('Backup','backup','list','recover','delete','reset');
$navarray['menu']['admin'][$menu]['sec'][] = array('Upgrade','upgrade','list','recover','delete','reset');
//$navarray['menu']['admin'][$menu]['sec'][] = array('AppStore','appstore','list');
$navarray['menu']['admin'][++$menu]['first'] = array('Mein Bereich','welcome','main');

View File

@ -0,0 +1,108 @@
<div id="tabs">
<ul>
<li><a href="#tabs-1"></a></li>
</ul>
<!-- Example for multiple tabs
<ul hidden">
<li><a href="#tabs-1">First Tab</a></li>
<li><a href="#tabs-2">Second Tab</a></li>
</ul>
-->
<div id="tabs-1">
[MESSAGE]
<form action="" method="post">
[FORMHANDLEREVENT]
<div class="row">
<div class="row-height">
<div class="col-xs-14 col-md-12 col-md-height">
<div class="inside inside-full-height">
<div class="row">
<div class="row-height">
<div class="col-xs-14 col-md-12 col-md-height">
<div class="inside inside-full-height">
<fieldset>
<legend>{|Aktuelle Version|}</legend>
<table width="100%" border="0" class="mkTableFormular">
<b>OpenXE [CURRENT]</b>
</table>
</fieldset>
</div>
</div>
</div>
</div>
<div [PENDING_VISIBLE] class="row">
<div class="row-height">
<div class="col-xs-14 col-md-12 col-md-height">
<div class="inside inside-full-height">
<fieldset>
<legend>{|Verf&uuml;gbare Upgrades|}</legend>
<table width="100%" border="0" class="mkTableFormular">
<pre style="white-space:pre-line;">
[OUTPUT_FROM_CLI]
</pre>
</table>
</fieldset>
</div>
</div>
</div>
</div>
<div [PROGRESS_VISIBLE] class="row">
<div class="row-height">
<div class="col-xs-12 col-md-12 col-full-height">
<div class="inside inside-full-height">
<fieldset>
<legend>{|Upgrade-Fortschritt|}</legend>
<table width="100%" border="0" class="mkTableFormular">
<pre style="white-space:pre-line;">
[OUTPUT_FROM_CLI]
</pre>
</table>
</fieldset>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-xs-14 col-md-2 col-md-height">
<div class="inside inside-full-height">
<fieldset>
<legend>{|Aktionen|}</legend>
<table width="100%" border="0" class="mkTableFormular">
<tr><td>{|Details anzeigen|}:</td><td><input type="checkbox" name="details_anzeigen" value=1 size="20"></td></tr>
<tr><td colspan=2><button name="submit" value="refresh" class="ui-button-icon" style="width:100%;">Auffrischen</button></td></tr>
<tr><td>{|Erzwingen (-f)|}:</td><td><input type="checkbox" name="erzwingen" value=1 size="20"></td></tr>
<tr><td colspan=2><button name="submit" value="do_upgrade" class="ui-button-icon" style="width:100%;">UPGRADE!</button></td></tr>
</table>
</fieldset>
</div>
</div>
</div>
</div>
</form>
</div>
<!-- Example for 2nd tab
<div id="tabs-2">
[MESSAGE]
<form action="" method="post">
[FORMHANDLEREVENT]
<div class="row">
<div class="row-height">
<div class="col-xs-12 col-md-12 col-md-height">
<div class="inside inside-full-height">
<fieldset>
<legend>{|...|}</legend>
<table width="100%" border="0" class="mkTableFormular">
...
</table>
</fieldset>
</div>
</div>
</div>
</div>
<input type="submit" name="submit" value="Speichern" style="float:right"/>
</form>
</div>
-->
</div>

52
www/pages/upgrade.php Normal file
View File

@ -0,0 +1,52 @@
<?php
/*
* Copyright (c) 2022 OpenXE project
*/
use Xentral\Components\Database\Exception\QueryFailureException;
class upgrade {
function __construct($app, $intern = false) {
$this->app = $app;
if ($intern)
return;
$this->app->ActionHandlerInit($this);
$this->app->ActionHandler("list", "upgrade_overview");
$this->app->DefaultActionHandler("list");
$this->app->ActionHandlerListen($app);
}
public function Install() {
/* Fill out manually later */
}
function upgrade_overview() {
$submit = $this->app->Secure->GetPOST('submit');
$verbose = $this->app->Secure->GetPOST('details_anzeigen') === '1';
$force = $this->app->Secure->GetPOST('erzwingen') === '1';
ob_start();
include("../upgrade/upgrade.php");
if ($submit == 'do_upgrade') {
$do_upgrade = true;
$this->app->Tpl->Set('PENDING_VISIBLE', "hidden");
} else {
$do_upgrade = false;
$this->app->Tpl->Set('PROGRESS_VISIBLE', "hidden");
}
upgrade_main("../upgrade",$verbose,$do_upgrade,$force);
$result = ob_get_contents();
ob_end_clean();
$this->app->Tpl->Set('CURRENT', $this->app->erp->Revision());
$this->app->Tpl->Set('OUTPUT_FROM_CLI',$result);
$this->app->Tpl->Parse('PAGE', "upgrade.tpl");
}
}