瀏覽代碼

updated Install route

Piotr Labudda 8 年之前
父節點
當前提交
bebec2bc0f
共有 2 個文件被更改,包括 106 次插入162 次删除
  1. 103 160
      SE/se-lib/Route/Install.php
  2. 3 2
      SE/superedit-INSTALL_SES_PROCESY_A.php

+ 103 - 160
SE/se-lib/Route/Install.php

@@ -237,8 +237,22 @@ jQuery(document).ready(function () {
 	public function _encodeSource($appLicenceInfo) {
 		if (empty($appLicenceInfo->ID)) throw new Exception("Nie wybrano serwera/licencji.");
 		if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found");
-		$installPath = $appLicenceInfo->installPath;
+		if (empty($appLicenceInfo->installPath)) throw new Exception("Install path not found");
+		UI::startContainer();
+		$this->encodeSourceFiles($appLicenceInfo->installPath, $appLicenceInfo->domains, $dbg = true);
+		// try {
+		// 	$this->encodeSourceFiles($appLicenceInfo->installPath, $appLicenceInfo->domains, $dbg = false);
+		// 	UI::alert('success', "OK");
+		// } catch (Exception $e) {
+		// 	UI::alert('danger', $e->getMessage());
+		// }
+		UI::endContainer();
+	}
+
+	// @usage: Router::getRoute('Install')->encodeSourceFiles($installPath = '/path_to_git_repo', $domains = [ 'domain.com', 'localhost' ], $dbg = false);
+	public function encodeSourceFiles($installPath, $domains, $dbg = false) {
 		if (empty($installPath)) throw new Exception("Install path not found");
+		if (empty($domains)) throw new Exception("Domains not found");
 
 		$phpVersionsForSgencoder = '--phpversion 5.5 --phpversion 5.6';// encode for PHP 5.x (currently supported PHP 5.0-5.6)
 
@@ -246,151 +260,86 @@ jQuery(document).ready(function () {
 			$cmd = "cd {$installPath}/SE && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder --help ";
 			$out = ''; $ret = '';
 			exec($cmd, $out, $ret);
-			echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
-			exit;
-		}
-		if (0) {//'1' == V::get('DBG_ENCODER_TEST_IP', '', $_REQUEST)) {
-			$phpFiles = array();
-			$phpFiles[] = 't.php';
-			$cmd = "cd {$installPath}/SE && echo '<?php' > t.php ; echo 'echo \"test\\n\";' >> t.php ";
-			//$cmd = "cd {$installPath}/SE && rm t.php ";
-			V::exec($cmd, $out, $ret);
-			DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
-
-			$cmd = "cd {$installPath}/SE && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder {$phpVersionsForSgencoder} -b- ";
-			//$mac = '38:c9:86:31:80:77';
-			//$cmd .= " --mac {$mac}";
-			foreach ($appLicenceInfo->domains as $domain) {
-				$cmd .= " --domain {$domain} ";
-			}
-			$cmd .= " " . implode(" ", $phpFiles);
-			V::exec($cmd, $out, $ret);
-			DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
-			if (0 !== $ret) throw new Exception("Error at encode files");
-			if (empty($out)) throw new Exception("No output for encode files command");
+			DBG::nicePrint($out, "cmd: `{$cmd}` (return:{$ret})");
 			exit;
 		}
 
-		{
-/*
-			$phpFiles = array();
-			$skipPhpFiles = array();
-			$skipPhpFiles[] = 'bash_sync_perms.php';
-			$skipPhpFiles[] = 'bash_install_check.php';
-			$skipPhpFiles[] = '.config.php';
-			$skipPhpFiles[] = '.config_base_structure.php';
-			$skipPhpFiles[] = 'se-lib/V.php';
-			$skipPhpFiles[] = 'se-lib/Lib.php';
-			$skipPhpFiles[] = 'se-lib/DB.php';
-			$skipPhpFiles[] = 'se-lib/DBG.php';
-			$skipPhpFiles[] = 'se-lib/User.php';
-			$skipPhpFiles[] = 'se-lib/UserProfile.php';
-			$skipPhpFiles[] = 'se-lib/Config.php';
-			$skipPhpFiles[] = 'se-lib/Config/INI.php';
-			$skipPhpFiles[] = 'se-lib/FoldersConfig.php';
-			$skipPhpFiles[] = 'se-lib/FileUploader.php';
-			$skipPhpFiles[] = 'se-lib/Route/Budget.php';
-			$skipPhpFiles[] = 'se-lib/Route/FixCrmProcesInitIdx.php';
-			$skipPhpFiles[] = 'se-lib/Route/FixProjectPath.php';
-			$skipPhpFiles[] = 'se-lib/Route/FixZasobPath.php';
-			//$skipPhpFiles[] = 'se-lib/Route/Install.php';
-			$skipPhpFiles[] = 'se-lib/Route/Msgs.php';
-			$skipPhpFiles[] = 'se-lib/Route/UrlAction/WmsGenerate.php';
-			$skipPhpFiles[] = 'se-lib/Route/UrlAction/BiAuditGenerate.php';
-			$skipPhpFiles[] = 'superedit-DB_PROCEDURES_CREATE.php';
-			$skipPhpFiles[] = 'se-lib/Theme.php';
-			//$skipPhpFiles[] = '';
-
-			$skipPhpFiles[] = 'se-lib/DataSourceException.php';
-			$skipPhpFiles[] = 'se-lib/Core/Pdo.php';
-			$skipPhpFiles[] = 'se-lib/Cron.php';
-			$skipPhpFiles[] = 'se-lib/Router.php';
-			$skipPhpFiles[] = 'se-lib/RouteBase.php';
-			$skipPhpFiles[] = 'se-lib/HttpException.php';
-			$skipPhpFiles[] = 'se-lib/DebugExecutionTime.php';
-			$skipPhpFiles[] = 'se-lib/UI.php';
-			$skipPhpFiles[] = 'se-lib/Request.php';
-
-			$cmd = "cd {$installPath}/SE && find . -name '*.php' ";
-			$out = ''; $ret = '';
-			exec($cmd, $out, $ret);
-			echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
-			if (0 !== $ret) throw new Exception("Error at find php files");
-			if (empty($out)) throw new Exception("No php files found");
-			foreach ($out as $phpFilePath) {
-				$phpFilePath = ('./' == substr($phpFilePath, 0, 2))? substr($phpFilePath, 2) : $phpFilePath;
-				if (in_array($phpFilePath, $skipPhpFiles)) continue;
-				if ('schema/' == substr($phpFilePath, 0, 7)) continue;
-				$phpFiles[] = $phpFilePath;
-			}
-
-			DBG::_(true, true, 'phpFiles', $phpFiles, __CLASS__, __FUNCTION__, __LINE__);
-			if (empty($phpFiles)) throw new Exception("No php files to encode");
-*/
-
-			$domainEncodePhpFiles = [
-				'ant.php',
-				'api.php',
-				'budynki.php',
-				'index-ajax.php',
-				'index.php',
-				'procesy5.php',
-				'session-expire.php',
-				'test-sync.php',
-				'wfs-data.php',
-				'wfs-qgis.php',
-				'wfs.php',
-                        ];
-			$freeEncodePhpFiles = [];
-
-			$cmd = "cd {$installPath}/SE && find . -name '*.php' ";
+		$domainEncodePhpFiles = [
+			'ant.php',
+			'api.php',
+			'budynki.php',
+			'index-ajax.php',
+			'index.php',
+			'procesy5.php',
+			'session-expire.php',
+			'test-sync.php',
+			'wfs-data.php',
+			'wfs-qgis.php',
+			'wfs.php',
+		];
+
+		$cmd = "find . -name '*.php' ";
+		$out = ''; $ret = '';
+		exec("cd {$installPath}/SE && {$cmd}", $out, $ret);
+		if ($dbg) DBG::nicePrint($out, "cmd: `{$cmd}` (return:{$ret})");
+		else DBG::log($out, 'array', "cmd: `{$cmd}` (return:{$ret})");
+		if (0 !== $ret) throw new Exception("Error at find php files");
+		if (empty($out)) throw new Exception("No php files found");
+		$allPhpFiles = array_map(function ($phpFilePath) {
+			return ('./' == substr($phpFilePath, 0, 2))? substr($phpFilePath, 2) : $phpFilePath;
+		}, $out);
+		$freeEncodePhpFiles = array_filter($allPhpFiles, function ($phpFilePath) use ($domainEncodePhpFiles) {
+			if ('se-lib/Vendor/' === substr($phpFilePath, 0, strlen('se-lib/Vendor/'))) return false; // SKIP se-lib/Vendor/*
+			if ('.ini.php' === substr($phpFilePath, -1 * strlen('.ini.php'))) return false; // SKIP *.ini.php
+			return (!in_array($phpFilePath, $domainEncodePhpFiles));
+		});
+		DBG::log($freeEncodePhpFiles, 'array', "\$freeEncodePhpFiles");
+		if (empty($freeEncodePhpFiles)) throw new Exception("No php files to encode");
+
+		$cmdTempl = "cd {$installPath}/SE && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder {$phpVersionsForSgencoder} -b- ";
+		$cmdDomainEncodeDomainsTempl = " --domain " . implode(" --domain ", array_merge($domains, ['localhost']));
+		$cmdDomainEncodeFilesTempl = " " . implode(" ", $domainEncodePhpFiles);
+		$cmdFreeEncodeFilesTempl = " " . implode(" ", $freeEncodePhpFiles);
+
+		$cmds = [
+			$cmdTempl . $cmdDomainEncodeDomainsTempl . $cmdDomainEncodeFilesTempl,
+			$cmdTempl . $cmdFreeEncodeFilesTempl,
+		];
+
+		$returnValues = [];
+		foreach ($cmds as $cmd) {
 			$out = ''; $ret = '';
 			exec($cmd, $out, $ret);
-			echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
-			if (0 !== $ret) throw new Exception("Error at find php files");
-			if (empty($out)) throw new Exception("No php files found");
-			foreach ($out as $phpFilePath) {
-				$phpFilePath = ('./' == substr($phpFilePath, 0, 2))? substr($phpFilePath, 2) : $phpFilePath;
-				if (in_array($phpFilePath, $domainEncodePhpFiles)) continue;
-				$freeEncodePhpFiles[] = $phpFilePath;
-			}
-
-			DBG::_(true, true, 'freeEncodePhpFiles', $freeEncodePhpFiles, __CLASS__, __FUNCTION__, __LINE__);
-			if (empty($freeEncodePhpFiles)) throw new Exception("No php files to encode");
-		}
-
-		{
-/*
-			$cmd = "cd {$installPath}/SE && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder {$phpVersionsForSgencoder} -b- ";
-			foreach ($appLicenceInfo->domains as $domain) {
-				$cmd .= " --domain {$domain} ";
+			$encoderSummaryLine = end($out);
+			$returnValues[] = $encoderSummaryLine;
+			if ($dbg) {
+				DBG::nicePrint([$cmd], "cmd");
+				DBG::nicePrint($out, "return: '{$ret}'");
+				// DBG::nicePrint([$encoderSummaryLine], "cmd last line");
 			}
-			$cmd .= " --domain localhost ";
-			$cmd .= " " . implode(" ", $phpFiles);
-			$out = ''; $ret = '';
-			exec($cmd, $out, $ret);
-			echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
-			if (0 !== $ret) throw new Exception("Error at encode files");
+			else DBG::log([$cmd, $out], 'array', "cmd return:'{$ret}'");
+			// if (0 !== $ret) throw new Exception("Error at encode files");
 			if (empty($out)) throw new Exception("No output for encode files command");
-*/
-
-			$cmdTempl = "cd {$installPath}/SE && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder {$phpVersionsForSgencoder} -b- ";
-			$cmdDomainEncodeDomainsTempl = " --domain " . implode(" --domain ", array_merge($appLicenceInfo->domains, ['localhost']));
-			$cmdDomainEncodeFilesTempl = " " . implode(" ", $domainEncodePhpFiles);
-			$cmdFreeEncodeFilesTempl = " " . implode(" ", $freeEncodePhpFiles);
-
-			$cmds = [
-				$cmdTempl . $cmdDomainEncodeDomainsTempl . $cmdDomainEncodeFilesTempl,
-				$cmdTempl . $cmdFreeEncodeFilesTempl,
+		}
+		$statusInfo = array_reduce($returnValues, function ($ret, $encoderSummaryLine) {
+			DBG::log([$ret, $encoderSummaryLine], 'array', "DBG reduce [\$ret, \$encoderSummaryLine]");
+			$matches = [];
+			preg_match_all('/(\d+) files, (\d+) processed, (\d+) errors/', $encoderSummaryLine, $matches, PREG_SET_ORDER, 0);
+			return [
+				'files' => $ret['files'] + (int)$matches[0][1],
+				'processed' => $ret['processed'] + (int)$matches[0][2],
+				'errors' => $ret['errors'] + (int)$matches[0][3],
 			];
-
-			foreach ($cmds as $cmd) {
-				$out = ''; $ret = '';
-				exec($cmd, $out, $ret);
-				echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
-				if (0 !== $ret) throw new Exception("Error at encode files");
-				if (empty($out)) throw new Exception("No output for encode files command");
-			}
+		}, [
+			'files' => 0,
+			'processed' => 0,
+			'errors' => 0,
+		]);
+		if ($statusInfo['errors'] === 0 && $statusInfo['files'] > 0) {
+			if ($dbg) UI::alert('success', "{$statusInfo['files']} files, {$statusInfo['processed']} processed, {$statusInfo['errors']} errors");
+		} else {
+			if ($dbg) UI::alert('danger', "{$statusInfo['files']} files, {$statusInfo['processed']} processed, {$statusInfo['errors']} errors");
+			else throw new Exception("{$statusInfo['files']} files, {$statusInfo['processed']} processed, {$statusInfo['errors']} errors");
 		}
 	}
 
@@ -450,8 +399,8 @@ jQuery(document).ready(function () {
 
 	public function fetchActiveLicences() {
 		$activeLic = array();
-		$db = DB::getDB();
-		$sql = "select l.`ID`
+		$sql = "
+			select l.`ID`
 				, l.`SERVER_ADDRESS`
 				, l.`SERVER_ADDRESS_SHORT` as domain -- domain for sgencoder
 				, l.`SERVER_ADDRESS_IP`
@@ -460,8 +409,9 @@ jQuery(document).ready(function () {
 			-- TODO: and l.`A_STATUS` in('NORMAL','WAITING')
 			order by l.`ID` DESC
 		";
-		$res = $db->query($sql);
-		while ($r = $db->fetch($res)) {
+		$rows = DB::getPDO()->fetchAll($sql);
+		foreach ($rows as $row) {
+			$r = (object)$row;
 			$activeLic[$r->ID] = $r;
 		}
 		return $activeLic;
@@ -469,8 +419,8 @@ jQuery(document).ready(function () {
 
 	public function fetchDomainsByLicenceId($licenceId) {
 		$domains = array();
-		$db = DB::getDB();
-		$sql = "select g.`SERVER_ADDRESS_SHORT`, g.`SERVER_ADDRESS`
+		$sql = "
+			select g.`SERVER_ADDRESS_SHORT`, g.`SERVER_ADDRESS`
 			from `SES_PROCESY5_A` g
 			where g.`SERVER_ADDRESS_IP`=(select l.`SERVER_ADDRESS_IP`
 				from `SES_PROCESY5_A` l
@@ -479,8 +429,9 @@ jQuery(document).ready(function () {
 			 )
 			-- TODO: and g.`A_STATUS` in('NORMAL','WAITING')
 		";
-		$res = $db->query($sql);
-		while ($r = $db->fetch($res)) {
+		$rows = DB::getPDO()->fetchAll($sql);
+		foreach ($rows as $row) {
+			$r = (object)$row;
 			$domains[] = $r->SERVER_ADDRESS_SHORT;
 			if ($r->SERVER_ADDRESS != $r->SERVER_ADDRESS_SHORT) {
 				$domains[] = $r->SERVER_ADDRESS;
@@ -490,23 +441,17 @@ jQuery(document).ready(function () {
 	}
 
 	public function fetchMainServerByLicenceId($licenceId) {
-		$mainServer = null;
-		$db = DB::getDB();
-		$sql = "select g.`SERVER_ADDRESS`
+		return DB::getPDO()->fetchValue("
+			select g.`SERVER_ADDRESS`
 			from `SES_PROCESY5_A` g
 			where g.`ID`='{$licenceId}'
 			-- TODO: and g.`A_STATUS` in('NORMAL','WAITING')
-		";
-		$res = $db->query($sql);
-		while ($r = $db->fetch($res)) {
-			$mainServer = $r->SERVER_ADDRESS;
-		}
-		return $mainServer;
+		");
 	}
 
 	public function fetchAppLicenceInfo($licenceId) {
 		$licenceInfo = null;
-		$sth = DB::getPDO()->prepare("
+		$sql = "
 			select g.ID
 				, g.SERVER_ADDRESS as mainServer
 				, g.ADMIN_USERNAME as rootLogin
@@ -515,10 +460,8 @@ jQuery(document).ready(function () {
 			from SES_PROCESY5_A g
 			where g.`ID`=:id_licence
 			-- TODO: and g.A_STATUS in('NORMAL','WAITING')
-		");
-		$sth->bindValue('id_licence', $licenceId, PDO::PARAM_INT);
-		$sth->execute();
-		$all = $sth->fetchAll();
+		";
+		$all = DB::getPDO()->fetchAll($sql, [ ':id_licence' => $licenceId ]);
 		if (empty($all)) throw new Exception("Brak licencji o nr '{$licenceId}'");
 		$licenceInfo = reset($all);
 		if (!$licenceInfo['sshPort']) $licenceInfo['sshPort'] = 22;

+ 3 - 2
SE/superedit-INSTALL_SES_PROCESY_A.php

@@ -1658,7 +1658,7 @@ function INSTALL_GETCOMMANDS_SE_LAST_UPDATE($ADMIN_USERNAME,$SERVER_ADDRESS_IP,$
 
  }
 $cmd[]['rsh']='php /Library/Server/Web/Data/Sites/Default/SE/bash_install_check.php '.$SERVER_ADDRESS;
-$cmd[]['rsh']='php /Library/Server/Web/Data/Sites/Default/SE/bash_install_check.php '.$SERVER_ADDRESS; //dla pewnosci - TODO 
+$cmd[]['rsh']='php /Library/Server/Web/Data/Sites/Default/SE/bash_install_check.php '.$SERVER_ADDRESS; //dla pewnosci - TODO
 
 //@2016-06 bindera: po aktualizacji sql czesto nie dziala event sheduler np zoompak
 $cmd[]['rsh']=' echo " SET GLOBAL event_scheduler =  \"ON\" " |mysql -h127.0.0.1 -uroot -p\''.$ADMIN_USERNAME_PASSWD.'\' -D'.$REMOTE_FOLDER_ROOT.' '; //zalozenie 1 usera TODO synchronizacja z ldap
@@ -2082,7 +2082,7 @@ database=\"SES_USERS2\"
 																												//addUser($usrLogin, $idGroup, $idTelboxes,  $ADM_ADMIN_LEVEL, $ADM_NAME,             $ADM_ADMIN_DESC,											 $EMPLOYEE_TYPE, $ADM_PASSWD, $A_ADM_COMPANY, $A_CLASSIFIED, $DEFAULT_ACL_GROUP )
 
 // $cmd[]['rsh']='php /Library/Server/Web/Data/Sites/Default/SE/bash_Route_Users_Task_AddUser.php '.$SERVER_ADDRESS.' '.$ADMIN_USERNAME_L1.' 3  0              0               '.$ADMIN_USERNAME_L1.' Podstawowe_konto_uzytkownika_z_licencji_instalatora_systemu  Pracownik  '.$ADMIN_USERNAME_L1_PASS.' 3  3 ' ;
- 
+
  $cmd[]['rsh']=' echo " delete from ADMIN_USERS  where ADM_ACCOUNT=\''.$ADMIN_USERNAME_DIRECTORY.'\'" |mysql -h127.0.0.1 -uroot -p\''.$ADMIN_USERNAME_PASSWD.'\' -D'.$REMOTE_FOLDER_ROOT.' '; //zalozenie 1 usera TODO synchronizacja z ldap
  $cmd[]['rsh']=' echo " insert into ADMIN_USERS (ADM_ACCOUNT,ADM_ADMIN_LEVEL,ADM_PASSWD,A_STATUS) values (\''.$ADMIN_USERNAME_DIRECTORY.'\',\'0\',\''.$ADMIN_USERNAME_PASSWD.'\',\'NORMAL\')" |mysql -h127.0.0.1 -uroot -p\''.$ADMIN_USERNAME_PASSWD.'\' -D'.$REMOTE_FOLDER_ROOT.' '; //zalozenie 1 usera TODO synchronizacja z ldap
 
@@ -2409,6 +2409,7 @@ function MAKE_PKG_COMPONENT($h,$installer_dir_tar,$installer_url,$installer_tmp,
 		}
 
 			if(($encrypt)) {
+				// TODO: Router::getRoute('Install')->encodeSourceFiles($installPath = '/path_to_git_repo', $domains = [ 'domain.com', 'localhost' ], $dbg = false);
 				//TODO detect several hosts at the same IP address/domain to make it allow to work.
 				// select SERVER_ADDRESS_IP
 	 			//$exec='cd '.$installer_dir.' && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder -b- --domain '.$h->SERVER_ADDRESS_SHORT.' -r *.php -x superedit-DB_PROCEDURES_CREATE.php';