Переглянути джерело

added send encoded files to remote test dir in Install

Piotr Labudda 10 роки тому
батько
коміт
38b9ff5f20
1 змінених файлів з 115 додано та 20 видалено
  1. 115 20
      SE/se-lib/Route/Install.php

+ 115 - 20
SE/se-lib/Route/Install.php

@@ -24,7 +24,8 @@ class Route_Install extends RouteBase {
 		?>
 		?>
 <div class="jumbotron">
 <div class="jumbotron">
   <div class="container">
   <div class="container">
-		<form class="form-inline" method="POST">
+		<form class="form-inline" method="GET">
+			<input type="hidden" name="_route" value="Install" />
 			<input type="hidden" name="_task" value="createApp" />
 			<input type="hidden" name="_task" value="createApp" />
 			<label>Przygotuj kod źródłowy do aktualizacji na serwerze klienta:</label>
 			<label>Przygotuj kod źródłowy do aktualizacji na serwerze klienta:</label>
 			<select class="form-control" name="licence_id">
 			<select class="form-control" name="licence_id">
@@ -58,7 +59,7 @@ jQuery(document).ready(function () {
 		//$this->menu($args['licence_id']);// TODO: GO BACK BTN
 		//$this->menu($args['licence_id']);// TODO: GO BACK BTN
 
 
 		try {
 		try {
-			$appLicenceInfo = $this->_fetchAppLicenceInfo($args['licence_id']);
+			$appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
 		} catch (Exception $e) {
 		} catch (Exception $e) {
 			$this->_endWithException($e);
 			$this->_endWithException($e);
 		}
 		}
@@ -88,17 +89,12 @@ jQuery(document).ready(function () {
 				<br> - <a href="index.php?_route=Install&_task=encodeSource&licence_id=<?php echo $appLicenceInfo->ID; ?>"
 				<br> - <a href="index.php?_route=Install&_task=encodeSource&licence_id=<?php echo $appLicenceInfo->ID; ?>"
 					 target="_blank"
 					 target="_blank"
 					 class="btn btn-xs btn-default">encode files</a>
 					 class="btn btn-xs btn-default">encode files</a>
-				<br> TODO: send files:
-				<pre>
-ssh server@<?php echo $appLicenceInfo->mainServer; ?> 'rm -rf ~/se.encrypted.upgrade/SE'
-cd '<?php echo $appLicenceInfo->installPath; ?>'
-# scp -r SE server@<?php echo $appLicenceInfo->mainServer; ?>:~/se.encrypted.upgrade/
-rsync --archive --verbose --update --times --compress --one-file-system --omit-dir-times --no-g --no-perms SE/ server@<?php echo $appLicenceInfo->mainServer; ?>:~/se.encrypted.upgrade/SE/
-ssh server@<?php echo $appLicenceInfo->mainServer; ?> 'ln -s /Library/Server/Web/Data/Sites/Default/SE/config ~/se.encrypted.upgrade/SE/config'
-ssh server@<?php echo $appLicenceInfo->mainServer; ?> 'ln -s ~/se.encrypted.upgrade/SE /Library/Server/Web/Data/Sites/Default/se.encrypted.upgrade'
-Test online: <a href="https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/">https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/</a>
-				</pre>
-				</ul>
+				<br> - <a href="index.php?_route=Install&_task=sendToRemoteTestDir&licence_id=<?php echo $appLicenceInfo->ID; ?>"
+					 target="_blank"
+					 class="btn btn-xs btn-default">send encoded files to remote server test folder (generates ssh key if not set)</a>
+				<p style="margin-top:30px">
+Test online: <a target="_blank" href="https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/">https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/</a>
+				</p>
 			</div>
 			</div>
 		<?php endif; ?>
 		<?php endif; ?>
 	</div>
 	</div>
@@ -141,7 +137,7 @@ jQuery(document).ready(function () {
 		//$this->menu($args['licence_id']);// TODO: GO BACK BTN
 		//$this->menu($args['licence_id']);// TODO: GO BACK BTN
 
 
 		try {
 		try {
-			$appLicenceInfo = $this->_fetchAppLicenceInfo($args['licence_id']);
+			$appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
 			$this->_gitResetHard($appLicenceInfo);
 			$this->_gitResetHard($appLicenceInfo);
 		} catch (Exception $e) {
 		} catch (Exception $e) {
 			$this->_endWithException($e);
 			$this->_endWithException($e);
@@ -175,7 +171,7 @@ jQuery(document).ready(function () {
 		//$this->menu($args['licence_id']);// TODO: GO BACK BTN
 		//$this->menu($args['licence_id']);// TODO: GO BACK BTN
 
 
 		try {
 		try {
-			$appLicenceInfo = $this->_fetchAppLicenceInfo($args['licence_id']);
+			$appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
 			$this->_encodeSource($appLicenceInfo);
 			$this->_encodeSource($appLicenceInfo);
 		} catch (Exception $e) {
 		} catch (Exception $e) {
 			$this->_endWithException($e);
 			$this->_endWithException($e);
@@ -254,14 +250,12 @@ jQuery(document).ready(function () {
 		}
 		}
 	}
 	}
 
 
-	public function _fetchAppLicenceInfo($idLicence) {
+	public function getAppLicenceInfo($idLicence) {
 		$idLicence = intval($idLicence);
 		$idLicence = intval($idLicence);
 		if (empty($idLicence)) throw new Exception("Nie wybrano serwera/licencji.");
 		if (empty($idLicence)) throw new Exception("Nie wybrano serwera/licencji.");
 
 
 		//DBG::_(true, true, 'idLicence', $idLicence, __CLASS__, __FUNCTION__, __LINE__);
 		//DBG::_(true, true, 'idLicence', $idLicence, __CLASS__, __FUNCTION__, __LINE__);
-		$appLicenceInfo = new stdClass();
-		$appLicenceInfo->ID = $idLicence;
-		$appLicenceInfo->mainServer = $this->fetchMainServerByLicenceId($idLicence);
+		$appLicenceInfo = (object)$this->fetchAppLicenceInfo($idLicence);
 		$appLicenceInfo->domains = $this->fetchDomainsByLicenceId($idLicence);
 		$appLicenceInfo->domains = $this->fetchDomainsByLicenceId($idLicence);
 		$installRootPath = '/Library/Server/Web/Data/Sites/Default/PLIKI/SES_PROCESY5_A';
 		$installRootPath = '/Library/Server/Web/Data/Sites/Default/PLIKI/SES_PROCESY5_A';
 		$appLicenceInfo->installFolderName = "{$idLicence}_upgrade_SE_source_encrypted";
 		$appLicenceInfo->installFolderName = "{$idLicence}_upgrade_SE_source_encrypted";
@@ -366,11 +360,31 @@ jQuery(document).ready(function () {
 		return $mainServer;
 		return $mainServer;
 	}
 	}
 
 
+	public function fetchAppLicenceInfo($licenceId) {
+		$licenceInfo = null;
+		$pdo = DB::getPDO();
+		$sth = $pdo->prepare("
+			select g.ID
+				, g.SERVER_ADDRESS as mainServer
+				, g.ADMIN_USERNAME as rootLogin
+				, g.ADMIN_USERNAME_PASSWD as rootPassword
+			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();
+		if (empty($all)) throw new Exception("Brak licencji o nr '{$licenceId}'");
+		$licenceInfo = reset($all);
+		return $licenceInfo;
+	}
+
 	public function _endWithException($e) {
 	public function _endWithException($e) {
 ?>
 ?>
 <div class="container">
 <div class="container">
 	<div class="alert alert-danger">
 	<div class="alert alert-danger">
-		<?php echo $e->getMessage(); ?>
+		#<?php echo $e->getLine(); ?>: <?php echo $e->getMessage(); ?>
 	</div>
 	</div>
 	<p>Wróć do <a href="index.php?_route=Install">menu</a></p>
 	<p>Wróć do <a href="index.php?_route=Install">menu</a></p>
 </div>
 </div>
@@ -379,4 +393,85 @@ jQuery(document).ready(function () {
 		exit;
 		exit;
 	}
 	}
 
 
+	public function sendToRemoteTestDirAction() {
+		$args = array();
+		$args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
+		SE_Layout::gora();
+		//SE_Layout::menu();
+		//$this->menu($args['licence_id']);// TODO: GO BACK BTN
+
+		try {
+			$appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
+			$this->_sendToRemoteTestDir($appLicenceInfo);
+?>
+			Test online: <a target="_blank" href="https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/">https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/</a>
+<?php
+		} catch (Exception $e) {
+			$this->_endWithException($e);
+		}
+
+		SE_Layout::dol();
+	}
+
+	public function _sendToRemoteTestDir($appLicenceInfo) {
+		$cmd = ''; $out = ''; $ret = '';
+
+		$cmd = "echo ~ && pwd";// /Library/WebServer
+		$cmd = "ls -1 ~/.ssh/";
+		//$cmd = "rm /tmp/id_rsa";
+		//$cmd = "rm /tmp/id_rsa.pub";
+		//$cmd = "ssh-keygen -t rsa -N '' -C '_www@biuro.biall-net.pl' -f /tmp/id_rsa";
+		//$cmd = "ssh-keygen -t rsa -N '' -C '_www@biuro.biall-net.pl' -f /tmp/id_rsa";
+		V::exec($cmd, $out, $ret);
+		DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
+		if (0 !== $ret) {// no ~/.ssh directory
+			$rsaKeyPath = "~/.ssh";
+			$cmds = array();
+			$cmds[] = "mkdir {$rsaKeyPath}";
+			$cmds[] = "ssh-keygen -t rsa -N '' -C '_www@biuro.biall-net.pl' -f {$rsaKeyPath}/id_rsa 2>&1";
+			$cmds[] = "ls -1 $rsaKeyPath";
+			foreach ($cmds as $cmd) {
+				V::exec($cmd, $out, $ret);
+				DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
+				if (0 !== $ret) throw new Exception("Error '{$ret}' cmd({$cmd}): " . implode("\n", $out));
+			}
+		}
+
+		$cmd = "cat ~/.ssh/id_rsa.pub";
+		V::exec($cmd, $out, $ret);
+		DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
+		if (0 !== $ret || empty($out) || empty($out[0])) throw new Exception("Cannot read rsa public key");
+		$rsaPubKey = $out[0];
+
+		$cmd = 'ls -1a';
+		$cmd = "
+			[ ! -d ~/.sshX ] && mkdir ~/.sshX || echo 'OK ~/.ssh exists';
+			cat ~/.sshX/authorized_keys| grep '{$rsaPubKey}' && echo 'OK' || echo '{$rsaPubKey}' >> ~/.sshX/authorized_keys;
+			chmod 600 ~/.sshX/authorized_keys;
+		";
+		V::execRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret);
+		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'";
+		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}");
+
+		$sshHostUsr = "{$appLicenceInfo->rootLogin}@{$appLicenceInfo->mainServer}";
+		$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 \
+				'{$appLicenceInfo->installPath}/SE/' {$sshHostUsr}:~/se.encrypted.upgrade/SE/;
+			ssh {$sshHostUsr} 'ln -s /Library/Server/Web/Data/Sites/Default/SE/config ~/se.encrypted.upgrade/SE/config';
+			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'
+		";
+		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}");
+	}
+
 }
 }