Several improvements to the installer (show correct status, keep a log file and stop on database errors).

This commit is contained in:
Alexandre Rosenfeld
2011-02-22 22:09:00 +00:00
parent a6070e2883
commit ad4a86c055
3 changed files with 162 additions and 117 deletions

View File

@@ -169,72 +169,31 @@ class Installer
if($this->options['advanced']['ao_db_drop']===true) {
/* Drop databases */
$q = "DROP DATABASE IF EXISTS ".$wf;
$ac = @mysql_query($q,$this->connection_database);
$this->log($q.": => ".((!$ac)?mysql_error():"OK")."\n");
$q = "DROP DATABASE IF EXISTS ".$rb;
$ac = @mysql_query($q,$this->connection_database);
$this->log($q.": => ".((!$ac)?mysql_error():"OK")."\n");
$q = "DROP DATABASE IF EXISTS ".$rp;
$ac = @mysql_query($q,$this->connection_database);
$this->log($q.": => ".((!$ac)?mysql_error():"OK")."\n");
$this->run_query("DROP DATABASE IF EXISTS ".$wf, "Drop database $wf");
$this->run_query("DROP DATABASE IF EXISTS ".$rb, "Drop database $rb");
$this->run_query("DROP DATABASE IF EXISTS ".$rp, "Drop database $rp");
}
$q = "CREATE DATABASE IF NOT EXISTS ".$wf." DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
$ac = @mysql_query($q,$this->connection_database);
$this->log($q.": => ".((!$ac)?mysql_error():"OK")."\n");
$q = "CREATE DATABASE IF NOT EXISTS ".$rb." DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
$ac = @mysql_query($q,$this->connection_database);
$this->log($q.": => ".((!$ac)?mysql_error():"OK")."\n");
/* report DB begin */
$q = "CREATE DATABASE IF NOT EXISTS ".$rp." DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
$ac = @mysql_query($q,$this->connection_database);
$this->log($q.": => ".((!$ac)?mysql_error():"OK")."\n");
/* report DB end */
$this->run_query("CREATE DATABASE IF NOT EXISTS ".$wf." DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci",
"Create database $wf");
$this->run_query("CREATE DATABASE IF NOT EXISTS ".$rb." DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci",
"Create database $rb");
$this->run_query("CREATE DATABASE IF NOT EXISTS ".$rp." DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci",
"Create database $rp");
if($this->cc_status==1) {
if($islocal) {
$priv_wf = "GRANT ALL PRIVILEGES ON `".$wf."`.* TO ".$wf."@'localhost' IDENTIFIED BY '".$this->options['password']."' WITH GRANT OPTION";
}
else {
$priv_wf = "GRANT ALL PRIVILEGES ON `".$wf."`.* TO ".$wf."@'%' IDENTIFIED BY '".$this->options['password']."' WITH GRANT OPTION";
}
$ac = @mysql_query($priv_wf,$this->connection_database);
$this->log($priv_wf.": => ".((!$ac)?mysql_error():"OK")."\n");
if($islocal) {
$priv_rb = "GRANT ALL PRIVILEGES ON `".$rb."`.* TO ".$rb."@'localhost' IDENTIFIED BY '".$this->options['password']."' WITH GRANT OPTION";
}
else {
$priv_rb = "GRANT ALL PRIVILEGES ON `".$rb."`.* TO ".$rb."@'%' IDENTIFIED BY '".$this->options['password']."' WITH GRANT OPTION";
}
$ac = @mysql_query($priv_rb,$this->connection_database);
$this->log($priv_rb.": => ".((!$ac)?mysql_error():"OK")."\n");
/* report DB begin */
if($islocal) {
$priv_rp = "GRANT ALL PRIVILEGES ON `".$rp."`.* TO ".$rp."@'localhost' IDENTIFIED BY '".$this->options['password']."' WITH GRANT OPTION";
}
else {
$priv_rp = "GRANT ALL PRIVILEGES ON `".$rp."`.* TO ".$rp."@'%' IDENTIFIED BY '".$this->options['password']."' WITH GRANT OPTION";
}
$ac = @mysql_query($priv_rp,$this->connection_database);
$this->log($priv_rp.": => ".((!$ac)?mysql_error():"OK")."\n");
/* report DB end */
$host = ($islocal) ? "localhost" : "%";
$this->run_query("GRANT ALL PRIVILEGES ON `$wf`.* TO $wf@'$host' IDENTIFIED BY '{$this->options['password']}' WITH GRANT OPTION",
"Grant privileges for user $wf on database $wf");
$this->run_query("GRANT ALL PRIVILEGES ON `$rb`.* TO $rb@'$host' IDENTIFIED BY '{$this->options['password']}' WITH GRANT OPTION",
"Grant privileges for user $rb on database $rb");
$this->run_query("GRANT ALL PRIVILEGES ON `$rp`.* TO $rp@'$host' IDENTIFIED BY '{$this->options['password']}' WITH GRANT OPTION",
"Grant privileges for user $rp on database $rp");
}
/* Dump schema workflow && data */
$this->log("Importing database schema\n");
$this->log("Importing database schema:\n");
$myPortA = explode(":",$this->options['database']['hostname']);
if(count($myPortA)<2) {
$myPortA[1]="3306";
@@ -245,18 +204,18 @@ class Installer
mysql_select_db($wf,$this->connection_database);
$pws = PATH_WORKFLOW_MYSQL_DATA.$schema;
$qws = $this->query_sql_file(PATH_WORKFLOW_MYSQL_DATA.$schema,$this->connection_database);
$this->log($qws);
$this->log($qws, isset($qws['errors']));
$qwv = $this->query_sql_file(PATH_WORKFLOW_MYSQL_DATA.$values,$this->connection_database);
$this->log($qwv);
$this->log($qwv, isset($qwv['errors']));
/* Dump schema rbac && data */
$pws = PATH_RBAC_MYSQL_DATA.$schema;
mysql_select_db($rb,$this->connection_database);
$qrs = $this->query_sql_file(PATH_RBAC_MYSQL_DATA.$schema,$this->connection_database);
$this->log($qrs);
$this->log($qrs, isset($qrs['errors']));
$qrv = $this->query_sql_file(PATH_RBAC_MYSQL_DATA.$values,$this->connection_database);
$this->log($qrv);
$this->log($qrv, isset($qrv['errors']));
$path_site = $this->options['path_data']."/sites/".$this->options['name']."/";
$db_file = $path_site."db.php";
@@ -284,9 +243,9 @@ class Installer
"define ('DB_REPORT_PASS', '". (($this->cc_status==1)?$this->options['password']:$this->options['database']['password']) . "' );\n" .
"?>";
$fp = @fopen($db_file, "w");
$this->log("Creating: ".$db_file." => ".((!$fp)?$fp:"OK")."\n");
$this->log("Create: ".$db_file." => ".((!$fp)?$fp:"OK")."\n", $fp === FALSE);
$ff = @fputs( $fp, $db_text, strlen($db_text));
$this->log("Write: ".$db_file." => ".((!$ff)?$ff:"OK")."\n");
$this->log("Write: ".$db_file." => ".((!$ff)?$ff:"OK")."\n", $ff === FALSE);
fclose( $fp );
$this->set_admin();
@@ -302,14 +261,26 @@ class Installer
public function set_admin()
{
mysql_select_db($this->wf_site_name,$this->connection_database);
$q = 'UPDATE USERS SET USR_USERNAME = \''.mysql_escape_string($this->options['admin']['username']).'\', `USR_PASSWORD` = \''.md5($this->options['admin']['password']).'\' WHERE `USR_UID` = \'00000000000000000000000000000001\' LIMIT 1';
$ac = @mysql_query($q,$this->connection_database);
$this->log("Set workflow USERNAME:PASSWORD => ".((!$ac)?mysql_error():"OK")."\n");
$this->run_query('UPDATE USERS SET USR_USERNAME = \''.mysql_escape_string($this->options['admin']['username']).'\', `USR_PASSWORD` = \''.md5($this->options['admin']['password']).'\' WHERE `USR_UID` = \'00000000000000000000000000000001\' LIMIT 1',
"Add 'admin' user in ProcessMaker (wf)");
mysql_select_db($this->rbac_site_name,$this->connection_database);
$q = 'UPDATE USERS SET USR_USERNAME = \''.mysql_escape_string($this->options['admin']['username']).'\', `USR_PASSWORD` = \''.md5($this->options['admin']['password']).'\' WHERE `USR_UID` = \'00000000000000000000000000000001\' LIMIT 1';
$ac = @mysql_query($q,$this->connection_database);
$this->log("Set rbac USERNAME:PASSWORD => ".((!$ac)?mysql_error():"OK")."\n");
$this->run_query('UPDATE USERS SET USR_USERNAME = \''.mysql_escape_string($this->options['admin']['username']).'\', `USR_PASSWORD` = \''.md5($this->options['admin']['password']).'\' WHERE `USR_UID` = \'00000000000000000000000000000001\' LIMIT 1',
"Add 'admin' user in ProcessMaker (rb)");
}
/**
* Run a mysql query on the current database and take care of logging and
* error handling.
*
* @param string $query SQL command
* @param string $description Description to log instead of $query
*/
private function run_query($query, $description = NULL) {
$result = @mysql_query($query, $this->connection_database);
$error = ($result) ? false : mysql_error();
$this->log(($description ? $description : $query) . " => " . (($error) ? $error : "OK") . "\n",
$error);
}
/**
@@ -323,7 +294,6 @@ class Installer
{
$report = array(
'SQL_FILE' => $file,
'errors' => array(),
'queries' => 0
);
@@ -342,6 +312,8 @@ class Installer
/* Make sure we have a query to execute, then execute it */
if (trim($qr) != "") {
if(!@mysql_query($qr, $connection)) {
if (!array_key_exists('errors', $report))
$report['errors'] = array();
$report['errors'][] = "Error in query ".$i.": ".mysql_error();
} else {
$report['queries'] += 1;
@@ -570,9 +542,11 @@ class Installer
* @param string $text
* @return void
*/
public function log($text)
public function log($text, $failed = NULL)
{
array_push($this->report,$text);
if ($failed)
throw new Exception($text);
}
}
?>

View File

@@ -578,27 +578,21 @@ var installer=function()
this.table = document.createElement("table");
this.table.className="inst_table";
var success = (rpc.xmlhttp.status == 200);
var tr = this.table.insertRow(-1);
var tdtitle = tr.insertCell(0);
tdtitle.innerHTML="Directories";
tdtitle.innerHTML="Status";
tdtitle.className="app_grid_title___gray title";
var tr = this.table.insertRow(-1);
var td0 = tr.insertCell(0);
td0.innerHTML="SUCCESS";
td0.className="tdOk";
var tr = this.table.insertRow(-1);
var tdtitle = tr.insertCell(0);
tdtitle.innerHTML="New Workspace";
tdtitle.className="app_grid_title___gray title";
var tr = this.table.insertRow(-1);
var td0 = tr.insertCell(0);
td0.innerHTML="SUCCESS";
td0.className="tdOk";
td0.innerHTML=(success) ? "Success" : "Failed (Check log below)";
td0.className=(success) ? "tdOk" : "tdFailed";
this.options.target.appendChild(this.table);
if (success) {
var tr = this.table.insertRow(-1);
var tdS = tr.insertCell(0);
tdS.colSpan = 2;
@@ -617,6 +611,8 @@ var installer=function()
this.buttonFun(this.options.buttong);
tdS.appendChild(document.createElement("br"));
tdS.appendChild(document.createElement("br"));
}
var tr = this.table.insertRow(-1);
var tdtitle = tr.insertCell(0);

View File

@@ -200,7 +200,9 @@ else if($action==="install")
*5.1 Install processes
*5.2 Install plugins
* */
require_once 'Log.php';
$sp = "/";
$dir_data = $dataClient->path_data;
//$dir_compiled = $dataClient->path_compiled;
@@ -212,6 +214,15 @@ else if($action==="install")
@mkdir($dir_data."sites",0777,true);
@mkdir($dir_compiled,0777,true);
$logFile = "{$dir_data}install.log";
$display = Log::singleton('display', '', 'INSTALLER', array('lineFormat' => "%{message}"));
$file = Log::singleton('file', $logFile, 'INSTALLER');
global $logger;
$logger = Log::singleton('composite');
$logger->addChild($display);
$logger->addChild($file);
$create_db ="create-db.sql";
$schema ="schema.sql";
@@ -222,42 +233,61 @@ else if($action==="install")
$siteName="workflow";
$p1 = (isset($dataClient->ao_admin_pass1))?$dataClient->ao_admin_pass1:'admin';
$p2 = (isset($dataClient->ao_admin_pass2))?$dataClient->ao_admin_pass2:'admin';
$s = $inst->create_site(Array(
'name' =>'workflow',
'path_data'=>$dataClient->path_data,
'path_compiled'=>$dataClient->path_compiled,
'admin'=>Array('username'=>(isset($dataClient->ao_admin))?$dataClient->ao_admin:'admin','password'=>$p1),
'advanced'=>Array(
'ao_db'=>(isset($dataClient->ao_db) && $dataClient->ao_db===2)?false:true,
'ao_db_drop'=>(isset($dataClient->ao_db_drop) && $dataClient->ao_db_drop===true)?true:false,
'ao_db_wf'=>(isset($dataClient->ao_db_wf))?$dataClient->ao_db_wf:'wf_'.$siteName,
'ao_db_rb'=>(isset($dataClient->ao_db_rb))?$dataClient->ao_db_rb:'rb_'.$siteName,
'ao_db_rp'=>(isset($dataClient->ao_db_rp))?$dataClient->ao_db_rp:'rp_'.$siteName
),
'database'=>Array(
'hostname'=>$dataClient->mysqlH,
'username'=>$dataClient->mysqlU,
'password'=>$dataClient->mysqlP
)
),true);
try {
$s = $inst->create_site(Array(
'name' =>'workflow',
'path_data'=>$dataClient->path_data,
'path_compiled'=>$dataClient->path_compiled,
'admin'=>Array('username'=>(isset($dataClient->ao_admin))?$dataClient->ao_admin:'admin','password'=>$p1),
'advanced'=>Array(
'ao_db'=>(isset($dataClient->ao_db) && $dataClient->ao_db===2)?false:true,
'ao_db_drop'=>(isset($dataClient->ao_db_drop) && $dataClient->ao_db_drop===true)?true:false,
'ao_db_wf'=>(isset($dataClient->ao_db_wf))?$dataClient->ao_db_wf:'wf_'.$siteName,
'ao_db_rb'=>(isset($dataClient->ao_db_rb))?$dataClient->ao_db_rb:'rb_'.$siteName,
'ao_db_rp'=>(isset($dataClient->ao_db_rp))?$dataClient->ao_db_rp:'rp_'.$siteName
),
'database'=>Array(
'hostname'=>$dataClient->mysqlH,
'username'=>$dataClient->mysqlU,
'password'=>$dataClient->mysqlP
)
),true);
$installError = (!$s['created']);
} catch (Exception $e) {
$installError = ($e->getMessage() ? $e->getMessage() : true);
}
if ($installError)
header('HTTP', true, 500);
/* Status is used in the Windows installer, do not change this */
print_r("Status: ".($s['created'] ? 'SUCCESS':'FAILED')."\n\n");
print_r("Installation arguments:\n");
print_r($dataClient);
print_r("\n");
if (!$s['created']) {
print_r("Status: ".(($installError) ? 'FAILED':'SUCCESS')."\n\n");
$file->log("Status: ".(($installError) ? 'FAILED':'SUCCESS'));
$display->log("This log is also avaliable in $logFile");
$installArgs = (array)$dataClient;
$hiddenFields = array('mysqlP', 'ao_admin_pass1', 'ao_admin_pass2');
foreach ($installArgs as $arg => $param)
if (in_array($arg, $hiddenFields))
$installArgs[$arg] = "********";
$logger->log("Installation arguments\n" . neat_r(array($installArgs)));
if ($installError !== false && isset($s) && !$s['created']) {
/* On a failed install, $inst->report is blank because the
* installation didnt occured at all. So we use the test report
* instead.
*/
print_r("Installation report:\n");
print_r($s['result']);
$logger->log("Installation report\n" . neat_r(array($s['result'])));
die();
}
print_r("Installation report:\n");
print_r($inst->report);
$logger->log("Installation report\n" . neat_r(array($inst->report)));
if ($installError)
die();
$sh=md5(filemtime(PATH_GULLIVER."/class.g.php"));
$h=G::encrypt($dataClient->mysqlH.$sh.$dataClient->mysqlU.$sh.$dataClient->mysqlP.$sh.$inst->cc_status,$sh);
@@ -273,23 +303,68 @@ else if($action==="install")
/* Update languages */
$update = file_get_contents("http://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT']."/sysworkflow/en/green/tools/updateTranslation");
print_r("Update language => ".((!$update)?$update:"OK")."\n");
$logger->log("Update language => ".((!$update)?$update:"OK"));
/* Heartbeat Enable/Disable */
if(!isset($dataClient->heartbeatEnabled)) $dataClient->heartbeatEnabled=true;
$update = file_get_contents("http://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT']."/sysworkflow/en/green/install/heartbeatStatus?status=".$dataClient->heartbeatEnabled);
print_r("Heartbeat Status => ".str_replace("<br>","\n",$update)."\n");
$logger->log("Heartbeat Status => ".str_replace("<br>","\n",$update));
/* Autoinstall Process */
$update = file_get_contents("http://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT']."/sysworkflow/en/green/install/autoinstallProcesses");
if (trim(str_replace("<br>","",$update)) == "")
$update = "Nothing to do.";
print_r("Process AutoInstall => ".str_replace("<br>","\n",$update)."\n");
$logger->log("Process AutoInstall => ".str_replace("<br>","\n",$update));
/* Autoinstall Plugins */
$update = file_get_contents("http://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT']."/sysworkflow/en/green/install/autoinstallPlugins");
if (trim(str_replace("<br>","",$update)) == "")
$update = "Nothing to do.";
print_r("Plugin AutoInstall => ".str_replace("<br>","\n",$update)."\n");
$logger->log("Plugin AutoInstall => ".str_replace("<br>","\n",$update));
}
/*
neat_r works like print_r but with much less visual clutter.
By Jake Lodwick. Copy freely.
*/
function neat_r($arr, $return = false) {
$out = array();
$oldtab = " ";
$newtab = " ";
$lines = explode("\n", print_r($arr, true));
foreach ($lines as $line) {
//remove numeric indexes like "[0] =>" unless the value is an array
//if (substr($line, -5) != "Array") {
$line = preg_replace("/^(\s*)\[[0-9]+\] => /", "$1", $line, 1);
//}
//garbage symbols
foreach (array(
"Array" => "",
"[" => "",
"]" => "",
//" =>" => ":",
) as $old => $new) {
$out = str_replace($old, $new, $out);
}
//garbage lines
if (in_array(trim($line), array("Array", "(", ")", ""))) continue;
//indents
$indent = "";
$indents = floor((substr_count($line, $oldtab) - 1) / 2);
if ($indents > 0) { for ($i = 0; $i < $indents; $i++) { $indent .= $newtab; } }
$out[] = $indent . trim($line);
}
$out = implode("\n", $out);
if ($return == true) return $out;
return $out;
}
?>