Преглед изворни кода

fixed install on remote server

Piotr Labudda пре 10 година
родитељ
комит
eed90d120c
2 измењених фајлова са 63 додато и 30 уклоњено
  1. 53 24
      SE/se-lib/Route/Install.php
  2. 10 6
      SE/se-lib/V.php

+ 53 - 24
SE/se-lib/Route/Install.php

@@ -52,6 +52,7 @@ jQuery(document).ready(function () {
 	}
 
 	public function createAppAction() {
+		session_write_close();
 		$args = array();
 		$args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
 
@@ -97,8 +98,9 @@ jQuery(document).ready(function () {
 Test online: <a target="_blank" href="https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/">https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/</a>
 				</p>
 				<br> - <a href="index.php?_route=Install&_task=upgradeRemoteFromTestDir&licence_id=<?php echo $appLicenceInfo->ID; ?>"
-					 target="_blank"
-					 class="btn btn-xs btn-warning">UPGRADE Production folder from uploaded dir: ~/se.encrypted.upgrade/</a>
+									onclick="return confirm('Uruchomić aktualizację SE na https://<?php echo $appLicenceInfo->mainServer; ?>/SE/?')"
+									target="_blank"
+									class="btn btn-xs btn-warning">UPGRADE Production folder from uploaded dir: ~/se.encrypted.upgrade/</a>
 			</div>
 		<?php endif; ?>
 	</div>
@@ -134,6 +136,7 @@ jQuery(document).ready(function () {
 	}
 
 	public function gitResetHardAction() {
+		session_write_close();
 		$args = array();
 		$args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
 		SE_Layout::gora();
@@ -168,6 +171,7 @@ jQuery(document).ready(function () {
 	}
 
 	public function encodeSourceAction() {
+		session_write_close();
 		$args = array();
 		$args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
 		SE_Layout::gora();
@@ -395,12 +399,12 @@ jQuery(document).ready(function () {
 
 	public function fetchAppLicenceInfo($licenceId) {
 		$licenceInfo = null;
-		$pdo = DB::getPDO();
-		$sth = $pdo->prepare("
+		$sth = DB::getPDO()->prepare("
 			select g.ID
 				, g.SERVER_ADDRESS as mainServer
 				, g.ADMIN_USERNAME as rootLogin
 				, g.ADMIN_USERNAME_PASSWD as rootPassword
+				, g.SSH_PORT as sshPort
 			from SES_PROCESY5_A g
 			where g.`ID`=:id_licence
 			-- TODO: and g.A_STATUS in('NORMAL','WAITING')
@@ -410,6 +414,7 @@ jQuery(document).ready(function () {
 		$all = $sth->fetchAll();
 		if (empty($all)) throw new Exception("Brak licencji o nr '{$licenceId}'");
 		$licenceInfo = reset($all);
+		if (!$licenceInfo['sshPort']) $licenceInfo['sshPort'] = 22;
 		return $licenceInfo;
 	}
 
@@ -427,6 +432,7 @@ jQuery(document).ready(function () {
 	}
 
 	public function sendToRemoteTestDirAction() {
+		session_write_close();
 		$args = array();
 		$args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
 		SE_Layout::gora();
@@ -452,14 +458,26 @@ jQuery(document).ready(function () {
 		$this->_assertRsaKeyExists($appLicenceInfo);
 
 		$sshHostUsr = "{$appLicenceInfo->rootLogin}@{$appLicenceInfo->mainServer}";
+		$sshArgs = (22 != $appLicenceInfo->sshPort)? "-p {$appLicenceInfo->sshPort}" : '';
+		$rsyncSshPort = (22 != $appLicenceInfo->sshPort)? "-e 'ssh -p {$appLicenceInfo->sshPort}'" : '';
+		// $appLicenceInfo->sshPort
+		/* rsync options:
+			-a, --archive               archive mode; same as -rlptgoD (no -H)
+		  -u, --update                skip files that are newer on the receiver
+			-t, --times                 preserve times
+			    --delete                delete extraneous files from dest dirs
+		*/
 		$cmd = "
-ssh {$sshHostUsr} '[ ! -d ~/se.encrypted.upgrade ] && mkdir ~/se.encrypted.upgrade || echo 1';
-ssh {$sshHostUsr} 'rm -rf ~/se.encrypted.upgrade/SE';
-rsync --archive --verbose --update --times --compress --one-file-system --omit-dir-times --no-g --no-perms \
+ssh {$sshArgs} {$sshHostUsr} '[ ! -d ~/se.encrypted.upgrade ] && mkdir ~/se.encrypted.upgrade || echo 1';
+ssh {$sshArgs} {$sshHostUsr} 'rm -rf ~/se.encrypted.upgrade/SE';
+ssh {$sshArgs} {$sshHostUsr} 'cp -r /Library/Server/Web/Data/Sites/Default/SE ~/se.encrypted.upgrade/SE';
+ssh {$sshArgs} {$sshHostUsr} 'rm -rf ~/se.encrypted.upgrade/SE/config';
+rsync --archive --times --delete --compress --one-file-system --omit-dir-times --no-g --no-perms \
+	--verbose {$rsyncSshPort} \
 	'{$appLicenceInfo->installPath}/SE/' {$sshHostUsr}:~/se.encrypted.upgrade/SE/;
-ssh {$sshHostUsr} 'cp -r /Library/Server/Web/Data/Sites/Default/SE/config ~/se.encrypted.upgrade/SE/';
-ssh {$sshHostUsr} 'rm /Library/Server/Web/Data/Sites/Default/se.encrypted.upgrade'
-ssh {$sshHostUsr} 'ln -s ~/se.encrypted.upgrade/SE /Library/Server/Web/Data/Sites/Default/se.encrypted.upgrade'
+ssh {$sshArgs} {$sshHostUsr} 'cp -r /Library/Server/Web/Data/Sites/Default/SE/config ~/se.encrypted.upgrade/SE/';
+ssh {$sshArgs} {$sshHostUsr} 'rm /Library/Server/Web/Data/Sites/Default/se.encrypted.upgrade'
+ssh {$sshArgs} {$sshHostUsr} 'ln -s ~/se.encrypted.upgrade/SE /Library/Server/Web/Data/Sites/Default/se.encrypted.upgrade'
 		";
 		V::exec($cmd, $out, $ret);
 		DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
@@ -467,6 +485,7 @@ ssh {$sshHostUsr} 'ln -s ~/se.encrypted.upgrade/SE /Library/Server/Web/Data/Site
 	}
 
 	public function upgradeRemoteFromTestDirAction() {
+		session_write_close();
 		$args = array();
 		$args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
 		SE_Layout::gora();
@@ -491,22 +510,31 @@ ssh {$sshHostUsr} 'ln -s ~/se.encrypted.upgrade/SE /Library/Server/Web/Data/Site
 
 		$this->_assertRsaKeyExists($appLicenceInfo);
 
-		$sshHostUsr = "{$appLicenceInfo->rootLogin}@{$appLicenceInfo->mainServer}";
 		$dateStr = date("Y-m-d_H-i-s");
+		$remoteHomeDir = "/Users/{$appLicenceInfo->rootLogin}";
 		$cmd = "
-ssh {$sshHostUsr} 'rm -rf /Library/Server/Web/Data/Sites/Default/SE.test';
-ssh {$sshHostUsr} 'cp -r ~/se.encrypted.upgrade/SE /Library/Server/Web/Data/Sites/Default/SE.test';
-ssh {$sshHostUsr} '[ ! -d ~/bup.se.upgrade ] && mkdir ~/bup.se.upgrade || echo 1';
-ssh {$sshHostUsr} '[ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/VERSION ] && echo \"Error: plik VERSION nie istnieje\" || echo \"check VERSION OK\"';
-ssh {$sshHostUsr} '[ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/index.php ] && echo \"Error: plik index.php nie istnieje\" || echo \"check index.php OK\"';
-ssh {$sshHostUsr} '[ ! -d /Library/Server/Web/Data/Sites/Default/SE.test/se-lib ] && echo \"Error: plik se-lib nie istnieje\" || echo \"check se-lib OK\"';
-ssh {$sshHostUsr} 'mv /Library/Server/Web/Data/Sites/Default/SE ~/bup.se.upgrade/bup.{$dateStr}';
-ssh {$sshHostUsr} 'mv /Library/Server/Web/Data/Sites/Default/SE.test /Library/Server/Web/Data/Sites/Default/SE';
+rm -rf /Library/Server/Web/Data/Sites/Default/SE.test 2>&1
+cp -r {$remoteHomeDir}/se.encrypted.upgrade/SE /Library/Server/Web/Data/Sites/Default/SE.test 2>&1
+[ ! -d {$remoteHomeDir}/bup.se.upgrade ] && mkdir {$remoteHomeDir}/bup.se.upgrade || echo 1
+[ ! -d {$remoteHomeDir}/bup.se.upgrade ] && exit 1
+[ ! -d {$remoteHomeDir}/bup.se.upgrade-testttttt ] && exit 1
+[ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/VERSION ] && echo \"Error: plik VERSION nie istnieje\" || echo \"check VERSION OK\"
+[ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/VERSION ] && exit 1
+[ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/index.php ] && echo \"Error: plik index.php nie istnieje\" || echo \"check index.php OK\"
+[ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/index.php ] && exit 1
+[ ! -d /Library/Server/Web/Data/Sites/Default/SE.test/se-lib ] && echo \"Error: plik se-lib nie istnieje\" || echo \"check se-lib OK\"
+[ ! -d /Library/Server/Web/Data/Sites/Default/SE.test/se-lib ] && exit 1
+exit 0
 		";
-		// sudo for mv?
-		V::exec($cmd, $out, $ret);
+		V::execRootRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
+		$cmd = "
+mv /Library/Server/Web/Data/Sites/Default/SE {$remoteHomeDir}/bup.se.upgrade/bup.{$dateStr} 2>&1
+mv /Library/Server/Web/Data/Sites/Default/SE.test /Library/Server/Web/Data/Sites/Default/SE 2>&1
+exit 0
+		";
+		V::execRootRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
 		DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
-		if (0 !== $ret) throw new Exception("Cannot run remote command using rsa key! #{$ret}");
+		if (0 !== $ret) throw new Exception("Cannot run remote command as root! #{$ret}");
 	}
 
 	public function _assertRsaKeyExists($appLicenceInfo) {
@@ -547,12 +575,13 @@ ssh {$sshHostUsr} 'mv /Library/Server/Web/Data/Sites/Default/SE.test /Library/Se
 cat ~/.ssh/authorized_keys| grep '{$rsaPubKey}' && echo 'OK' || echo '{$rsaPubKey}' >> ~/.ssh/authorized_keys;
 chmod 600 ~/.ssh/authorized_keys;
 		";
-		V::execRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret);
+		V::execRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
 		DBG::_(true, true, "remote cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
 
 		//$cmd = "ssh server@{$appLicenceInfo->mainServer} 'ls -1 .ssh/'";
 		//$cmd = "ssh -i ~/.ssh/id_rsa server@{$appLicenceInfo->mainServer} 'ls -1'";
-		$cmd = "ssh server@{$appLicenceInfo->mainServer} 'ls -1'";
+		$sshPort = (22 != $appLicenceInfo->sshPort)? "-p {$appLicenceInfo->sshPort}" : '';
+		$cmd = "ssh {$sshPort} server@{$appLicenceInfo->mainServer} 'ls -1'";
 		V::exec($cmd, $out, $ret);
 		DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
 		if (0 !== $ret) throw new Exception("Cannot run remote command using rsa key! #{$ret}");

+ 10 - 6
SE/se-lib/V.php

@@ -2,7 +2,7 @@
 
 /**
  * @see http://pl2.php.net/manual/en/book.var.php
- * 
+ *
  * Define Your own convert function: var func_type_convert_{$type}($var);
  */
 class V {
@@ -141,7 +141,7 @@ class V {
 
 	/**
 	 * Merge the contents of two objects/array.
-	 * 
+	 *
 	 * array V::extend(mixed $defaults, mixed $params);
 	 * @see http://api.jquery.com/jQuery.extend/
 	 * is_scalar($from) - return TRUE if int,float,string,bool, FALSE if array,object,resource, ...
@@ -367,12 +367,13 @@ class V {
 		return $ret;
 	}
 
-	public static function execRemote($host, $login, $password, $command, &$out, &$ret) {
+	public static function execRemote($host, $login, $password, $command, &$out, &$ret, $port = 22) {
 		$out = null;
 		$ret = null;
 		$pass = $password;
 		$pass = str_replace('!', '\!', $pass);
-		$cmd = '/opt/local/bin/sshpass -p ' . $pass . ' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=99999 ' . $login . '@' . $host . ' -t <<EOF
+		$sshPort = (22 != $port)? "-p {$port}" : '';
+		$cmd = '/opt/local/bin/sshpass -p ' . $pass . ' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=99999 ' . $sshPort . ' ' . $login . '@' . $host . ' -t <<EOF
 declare PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/local/bin:/opt/local/lib/mysql55/bin/:/Applications/Server.app/Contents/ServerRoot/usr/sbin/
 
 '.$command.'
@@ -382,10 +383,13 @@ EOF';
 		return $ret;
 	}
 
-	public static function execRootRemote($host, $login, $password, $command, &$out, &$ret) {
+	public static function execRootRemote($host, $login, $password, $command, &$out, &$ret, $port = 22) {
 		$out = null;
 		$ret = null;
-		$cmd = '/opt/local/bin/sshpass -p '.str_replace('!','\!',$password).' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=99999 '.$login.'@'.$host.' -t <<EOF
+		$pass = $password;
+		$pass = str_replace('!', '\!', $pass);
+		$sshPort = (22 != $port)? "-p {$port}" : '';
+		$cmd = '/opt/local/bin/sshpass -p ' . $pass . ' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=99999 ' . $sshPort . ' ' . $login . '@' . $host . ' -t <<EOF
 sudo -n su -
 declare PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/local/bin:/opt/local/lib/mysql55/bin/:/Applications/Server.app/Contents/ServerRoot/usr/sbin/