menu(); UI::dol(); } public function menu() { $serversList = $this->fetchActiveLicences(); ?>
Licencja dla domen: domains); ?>
Katalog z zakodowanymi plikami: installFolderName; ?>
installFolderGitExists) : ?>Test online: https://mainServer; ?>/se.encrypted.upgrade/
cmds: ';print_r($cmds);echo''; foreach ($cmds as $cmd) { $out = ''; $ret = ''; exec($cmd, $out, $ret); echo'
cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo''; } $this->_encodeSource($appLicenceInfo); // 1763: $exec='cd '.$installer_dir.' && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder -b- // '.INSTALL_SES_PROCESY_A::get_same_domains_for_install($h->SERVER_ADDRESS_SHORT).' // -r *.php // -x superedit-DB_PROCEDURES_CREATE.php // -x INI.php // -x .config_base_structure.php // '; // // INSTALL_SES_PROCESY_A::get_same_domains_for_install($h->SERVER_ADDRESS_SHORT): // $res2=DB::query("select SERVER_ADDRESS_SHORT from SES_PROCESY5_A where SERVER_ADDRESS_IP='".$h->SERVER_ADDRESS_IP."'"); // while($h2=DB::fetch($res2)) { // $domain[]=' --domain '.$h2->SERVER_ADDRESS_SHORT; // ssh server@biuro.galeriaprzymorze.eu: PHP 5.5.20 // ssh server@biuro.biall-net.pl // cd /Users/plabudda/procesy5-install-galeriaprzymorze.eu/ // sudo chown -R server:admin SE/ // /Applications/SourceGuardian.app/Contents/MacOS/sgencoder --phpversion 5.5 -b- --domain galeriaprzymorze.eu -r SE/*.php SE/se-lib/*.php SE/se-lib/*/*.php SE/se-lib/*/*/*.php SE/procesy/*.php SE/odt2xhtml/*.php -x superedit-DB_PROCEDURES_CREATE.php -x INI.php -x .config_base_structure.php } public function fetchActiveLicences() { $activeLic = array(); $sql = " select l.`ID` , l.`SERVER_ADDRESS` , l.`SERVER_ADDRESS_SHORT` as domain -- domain for sgencoder , l.`SERVER_ADDRESS_IP` from `SES_PROCESY5_A` l where 1=1 -- TODO: and l.`A_STATUS` in('NORMAL','WAITING') order by l.`ID` DESC "; $rows = DB::getPDO()->fetchAll($sql); foreach ($rows as $row) { $r = (object)$row; $activeLic[$r->ID] = $r; } return $activeLic; } public function fetchDomainsByLicenceId($licenceId) { $domains = array(); $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 where l.`ID`='{$licenceId}' -- TODO: and l.`A_STATUS` in('NORMAL','WAITING') ) -- TODO: and g.`A_STATUS` in('NORMAL','WAITING') "; $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; } } return $domains; } public function fetchMainServerByLicenceId($licenceId) { 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') "); } public function fetchAppLicenceInfo($licenceId) { $licenceInfo = null; $sql = " 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') "; $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; return $licenceInfo; } public function _endWithException($e) { echo UI::h('div', ['class'=>"container"], [ UI::h('div', ['class' => "alert alert-danger"], "#" . $e->getLine() . ": " . $e->getMessage()), UI::h('p', [], [ "Wróć do ", UI::h('a', ['href'=>"index.php?_route=Install"], "menu") ]) ]); UI::dol(); exit; } public function sendToRemoteTestDirAction() { session_write_close(); $args = array(); $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int'); UI::gora(); //UI::menu(); //$this->menu($args['licence_id']);// TODO: GO BACK BTN try { $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']); // TODO: if (59 ) => baratosz.sledz - na lokalnym kompie // bn:~/$ scp -r SE server@192.168.61.153:~/se.encrypted.upgrade // remote SE.git = '/Users/bartoszsledz/Desktop/production-se' // remote:~/$ mv SE ~/SE.bup.2017-07-03 // remote:~/$ mv ~/se.encrypted.upgrade/SE SE // remote:~/$ cp -r ~/SE.bup.2017-07-03/config SE/ // remote:~/$ sudo chown -R bartoszsledz:staff SE/ $this->_sendToRemoteTestDir($appLicenceInfo); echo "Test online: "; echo UI::h('a', ['target'=>"_blank", 'href'=>"https://{$appLicenceInfo->mainServer}/se.encrypted.upgrade/"], "https://{$appLicenceInfo->mainServer}/se.encrypted.upgrade/"); } catch (Exception $e) { $this->_endWithException($e); } UI::dol(); } public function _fetchRemoteHomeDir($appLicenceInfo) { $this->_assertRsaKeyExists($appLicenceInfo); $sshHostUsr = "{$appLicenceInfo->rootLogin}@{$appLicenceInfo->mainServer}"; $sshArgs = (22 != $appLicenceInfo->sshPort)? "-p {$appLicenceInfo->sshPort}" : ''; $rsyncSshPort = (22 != $appLicenceInfo->sshPort)? "-e 'ssh -p {$appLicenceInfo->sshPort}'" : ''; if (V::get('DBG_REMOTE', '', $_GET)) {// DBG $cmd = "echo ~"; V::exec("ssh {$sshArgs} {$sshHostUsr} '{$cmd}'", $out, $ret); DBG::_(true, true, "exec(ssh ... 'cmd'): {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__); V::execRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort); DBG::_(true, true, "execRemote(cmd): {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__); V::execRootRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort); DBG::_(true, true, "execRootRemote(cmd): {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__); } $cmd = "echo ~"; V::exec("ssh {$sshArgs} {$sshHostUsr} '{$cmd}'", $out, $ret); DBG::_(true, true, "exec(ssh ... '{$cmd}') (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__); if (empty($out) || empty($out[0])) throw new Exception("Cannot fetch remote home dir"); return $out[0]; } public function _sendToRemoteTestDir($appLicenceInfo) { $cmd = ''; $out = ''; $ret = ''; $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 */ DBG::_(true, true, "remote home dir:", $this->_fetchRemoteHomeDir($appLicenceInfo), __CLASS__, __FUNCTION__, __LINE__); $cmd = " 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} \ --exclude='stuff/qgis__kyngchaos.com' \ --exclude='stuff/qgis__kyngchaos.com/QGIS-2.14.3-1.dmg' \ --exclude='stuff/postgis' \ --exclude='stuff/postgis2' \ --exclude='stuff/KindleGen_Mac_i386_v2_9' \ --exclude='stuff/jdk-8u121-macosx-x64.dmg' \ --exclude='stuff/jre-8u112-macosx-x64.tar.gz' \ --exclude='stuff/jre-8u121-macosx-x64.dmg' \ --exclude='schema/default_db.instance.xml' \ --exclude='schema/default_db_xml_cache.public' \ --exclude='schema/default_db.instance.xml/**' \ --exclude='schema/default_db_xml_cache.public/**' \ '{$appLicenceInfo->installPath}/SE/' {$sshHostUsr}:~/se.encrypted.upgrade/SE/; 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' "; // --dry-run \ 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}"); } public function upgradeRemoteFromTestDirAction() { session_write_close(); $args = array(); $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int'); UI::gora(); //UI::menu(); //$this->menu($args['licence_id']);// TODO: GO BACK BTN try { $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']); $this->_upgradeRemoteFromTestDir($appLicenceInfo); echo "Test online: "; echo UI::h('a', ['target'=>"_blank", 'href'=>"https://{$appLicenceInfo->mainServer}/SE/"], "https://{$appLicenceInfo->mainServer}/SE/"); } catch (Exception $e) { $this->_endWithException($e); } UI::dol(); } public function _upgradeRemoteFromTestDir($appLicenceInfo) { $cmd = ''; $out = ''; $ret = ''; $this->_assertRsaKeyExists($appLicenceInfo); $dateStr = date("Y-m-d_H-i-s"); // $remoteHomeDir = "/Users/{$appLicenceInfo->rootLogin}";// BUG: alias like in ams - login = prezes is alias for arkadiuszbinder $remoteHomeDir = $this->_fetchRemoteHomeDir($appLicenceInfo); $cmd = " 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 "; 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 as root! #{$ret}"); // TODO: SE/bash_install_check.php requires $domain! // $cmd = "/usr/bin/php /Library/Server/Web/Data/Sites/Default/SE/bash_install_check.php {$domain}"; // 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 as root! #{$ret}"); } public function _assertRsaKeyExists($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 ~/.ssh ] && mkdir ~/.ssh; [ ! -d ~/.ssh ] && echo 'ERROR ~/.ssh not exists and cannot be created'; [ ! -f ~/.ssh/authorized_keys ] && echo '{$rsaPubKey}' > ~/.ssh/authorized_keys; [ ! -f ~/.ssh/authorized_keys ] && echo 'ERROR ~/.ssh/authorized_keys not exists and cannot be created'; 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, $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'"; $sshPort = (22 != $appLicenceInfo->sshPort)? "-p {$appLicenceInfo->sshPort}" : ''; $cmd = "ssh {$sshPort} {$appLicenceInfo->rootLogin}@{$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}"); } }