Install.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_Install extends RouteBase {
  4. public function handleAuth() {
  5. if (!User::logged()) {
  6. throw new HttpException('Unauthorized', 401);
  7. }
  8. if (!User::isAdmin()) {
  9. throw new HttpException('Unauthorized - only for administrators', 401);
  10. }
  11. }
  12. public function defaultAction() {
  13. SE_Layout::gora();
  14. SE_Layout::menu();
  15. $this->menu();
  16. SE_Layout::dol();
  17. }
  18. private function menu() {
  19. $serversList = $this->fetchActiveLicences();
  20. ?>
  21. <div class="jumbotron">
  22. <div class="container">
  23. <form class="form-inline" method="GET">
  24. <input type="hidden" name="_route" value="Install" />
  25. <input type="hidden" name="_task" value="createApp" />
  26. <label>Przygotuj kod źródłowy do aktualizacji na serwerze klienta:</label>
  27. <select class="form-control" name="licence_id">
  28. <?php foreach ($serversList as $srv) : ?>
  29. <option value="<?php echo $srv->ID; ?>">[<?php echo $srv->ID; ?>] <?php echo $srv->domain; ?></option>
  30. <?php endforeach; ?>
  31. </select>
  32. <button type="submit" id="fldSbmtBtn" class="btn btn-primary" autocomplete="off">
  33. Generuj
  34. </button>
  35. </form>
  36. </div>
  37. </div>
  38. <script type="text/javascript">
  39. jQuery(document).ready(function () {
  40. jQuery('#fldSbmtBtn').on('click', function () {
  41. jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
  42. jQuery(this).parent().submit();
  43. })
  44. });
  45. </script>
  46. <?php
  47. }
  48. public function createAppAction() {
  49. $args = array();
  50. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  51. SE_Layout::gora();
  52. SE_Layout::menu();
  53. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  54. try {
  55. $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
  56. } catch (Exception $e) {
  57. $this->_endWithException($e);
  58. }
  59. //$this->generateApp($args['licence_id']);
  60. //DBG::_(true, true, "appLicenceInfo", $appLicenceInfo, __CLASS__, __FUNCTION__, __LINE__);
  61. ?>
  62. <div class="jumbotron">
  63. <div class="container">
  64. <h3>Generowanie aplikacji dla licencji <?php echo $appLicenceInfo->ID; ?></h3>
  65. <p>Licencja dla domen: <?php echo implode(', ', $appLicenceInfo->domains); ?></p>
  66. <p>Katalog z zakodowanymi plikami: <?php echo $appLicenceInfo->installFolderName; ?></p>
  67. <form class="form-inline" method="POST">
  68. <input type="hidden" name="_task" value="createApp" />
  69. <input type="hidden" name="licence_id" value="<?php echo $appLicenceInfo->ID; ?>" />
  70. <input type="hidden" name="_generateEncryptedSource" value="1" />
  71. <button type="submit" id="fldSbmtBtn" class="btn btn-primary" autocomplete="off">
  72. Generuj
  73. </button>
  74. </form>
  75. <?php if ($appLicenceInfo->installFolderGitExists) : ?>
  76. <br>
  77. <div class="alert alert-info">
  78. Katalog istnieje i zawiera już repozytorium git:
  79. <br> - <a href="index.php?_route=Install&_task=gitResetHard&licence_id=<?php echo $appLicenceInfo->ID; ?>"
  80. target="_blank"
  81. class="btn btn-xs btn-default">aktualizuj werjsę</a> (git reset --hard, git pull, set SE/VERSION - tak samo co 'rm -rf; git clone', ale szybciej)
  82. <br> - <a href="index.php?_route=Install&_task=encodeSource&licence_id=<?php echo $appLicenceInfo->ID; ?>"
  83. target="_blank"
  84. class="btn btn-xs btn-default">encode files</a>
  85. <br> - <a href="index.php?_route=Install&_task=sendToRemoteTestDir&licence_id=<?php echo $appLicenceInfo->ID; ?>"
  86. target="_blank"
  87. class="btn btn-xs btn-default">send encoded files to remote server test folder (generates ssh key if not set)</a>
  88. <p style="margin-top:30px">
  89. Test online: <a target="_blank" href="https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/">https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/</a>
  90. </p>
  91. <br> - <a href="index.php?_route=Install&_task=upgradeRemoteFromTestDir&licence_id=<?php echo $appLicenceInfo->ID; ?>"
  92. target="_blank"
  93. class="btn btn-xs btn-warning">UPGRADE Production folder from uploaded dir: ~/se.encrypted.upgrade/</a>
  94. </div>
  95. <?php endif; ?>
  96. </div>
  97. </div>
  98. <script type="text/javascript">
  99. jQuery(document).ready(function () {
  100. jQuery('#fldSbmtBtn').on('click', function () {
  101. jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
  102. jQuery(this).parent().submit();
  103. })
  104. });
  105. </script>
  106. <?php
  107. $generateEncryptedSource = (1 == V::get('_generateEncryptedSource', 0, $_REQUEST, 'int'));
  108. if ($generateEncryptedSource) {
  109. echo '<div class="container">';
  110. echo '<h4>' . "Generowanie..." . '</h4>';
  111. echo '<div style="border:1px solid silver; max-height:400px; overflow-y:scroll">';
  112. try {
  113. $this->generateApp($appLicenceInfo);
  114. } catch (Exception $e) {
  115. echo '</div></div>';// .container/ scroll
  116. $this->_endWithException($e);
  117. }
  118. echo '</div>';// .container
  119. ?>
  120. <div class="alert alert-success">
  121. <strong>Gotowe</strong> Aplikacja znajduje się w katalogu <?php echo $appLicenceInfo->installFolderName; ?>
  122. </div>
  123. <?php
  124. }
  125. SE_Layout::dol();
  126. }
  127. public function gitResetHardAction() {
  128. $args = array();
  129. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  130. SE_Layout::gora();
  131. SE_Layout::menu();
  132. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  133. try {
  134. $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
  135. $this->_gitResetHard($appLicenceInfo);
  136. } catch (Exception $e) {
  137. $this->_endWithException($e);
  138. }
  139. SE_Layout::dol();
  140. }
  141. public function _gitResetHard($appLicenceInfo) {
  142. if (empty($appLicenceInfo->ID)) throw new Exception("Nie wybrano serwera/licencji.");
  143. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found");
  144. $installPath = $appLicenceInfo->installPath;
  145. if (empty($installPath)) throw new Exception("Install path not found");
  146. $cmds = array();
  147. $cmds[] = "cd {$installPath} && git reset --hard";
  148. $cmds[] = "cd {$installPath} && git pull";
  149. $cmds[] = "cd {$installPath} && echo `git show-ref --head|head -1|head -c 8` > SE/VERSION ";
  150. foreach ($cmds as $cmd) {
  151. $out = ''; $ret = '';
  152. exec($cmd, $out, $ret);
  153. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  154. }
  155. }
  156. public function encodeSourceAction() {
  157. $args = array();
  158. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  159. SE_Layout::gora();
  160. SE_Layout::menu();
  161. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  162. try {
  163. $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
  164. $this->_encodeSource($appLicenceInfo);
  165. } catch (Exception $e) {
  166. $this->_endWithException($e);
  167. }
  168. SE_Layout::dol();
  169. }
  170. public function _encodeSource($appLicenceInfo) {
  171. if (empty($appLicenceInfo->ID)) throw new Exception("Nie wybrano serwera/licencji.");
  172. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found");
  173. $installPath = $appLicenceInfo->installPath;
  174. if (empty($installPath)) throw new Exception("Install path not found");
  175. $phpVersion = '5.5';
  176. if ('1' == V::get('DBG_ENCODER_HELP', '', $_REQUEST)) {// encoder help
  177. $cmd = "cd {$installPath}/SE && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder --help ";
  178. $out = ''; $ret = '';
  179. exec($cmd, $out, $ret);
  180. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  181. exit;
  182. }
  183. {
  184. $phpFiles = array();
  185. $skipPhpFiles = array();
  186. $skipPhpFiles[] = 'bash_sync_perms.php';
  187. $skipPhpFiles[] = '.config.php';
  188. $skipPhpFiles[] = 'se-lib/V.php';
  189. $skipPhpFiles[] = 'se-lib/Lib.php';
  190. $skipPhpFiles[] = 'se-lib/DB.php';
  191. $skipPhpFiles[] = 'se-lib/User.php';
  192. $skipPhpFiles[] = 'se-lib/UserProfile.php';
  193. $skipPhpFiles[] = 'se-lib/Config.php';
  194. $skipPhpFiles[] = 'se-lib/Config/INI.php';
  195. $skipPhpFiles[] = 'se-lib/FoldersConfig.php';
  196. $skipPhpFiles[] = 'se-lib/FileUploader.php';
  197. $skipPhpFiles[] = 'se-lib/Route/Budget.php';
  198. $skipPhpFiles[] = 'se-lib/Route/FixCrmProcesInitIdx.php';
  199. $skipPhpFiles[] = 'se-lib/Route/FixProjectPath.php';
  200. $skipPhpFiles[] = 'se-lib/Route/FixZasobPath.php';
  201. //$skipPhpFiles[] = 'se-lib/Route/Install.php';
  202. $skipPhpFiles[] = 'se-lib/Route/Msgs.php';
  203. $skipPhpFiles[] = 'superedit-DB_PROCEDURES_CREATE.php';
  204. //$skipPhpFiles[] = '';
  205. $skipPhpFiles[] = 'se-lib/DataSourceException.php';
  206. $skipPhpFiles[] = 'se-lib/Core/Pdo.php';
  207. $skipPhpFiles[] = 'se-lib/Router.php';
  208. $skipPhpFiles[] = 'se-lib/HttpException.php';
  209. $skipPhpFiles[] = 'se-lib/DebugExecutionTime.php';
  210. $cmd = "cd {$installPath}/SE && find . -name '*.php' ";
  211. $out = ''; $ret = '';
  212. exec($cmd, $out, $ret);
  213. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  214. if (0 !== $ret) throw new Exception("Error at find php files");
  215. if (empty($out)) throw new Exception("No php files found");
  216. foreach ($out as $phpFilePath) {
  217. $phpFilePath = ('./' == substr($phpFilePath, 0, 2))? substr($phpFilePath, 2) : $phpFilePath;
  218. if (in_array($phpFilePath, $skipPhpFiles)) continue;
  219. if ('schema/' == substr($phpFilePath, 0, 7)) continue;
  220. $phpFiles[] = $phpFilePath;
  221. }
  222. DBG::_(true, true, 'phpFiles', $phpFiles);
  223. if (empty($phpFiles)) throw new Exception("No php files to encode");
  224. }
  225. {
  226. $cmd = "cd {$installPath}/SE && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder --phpversion {$phpVersion} -b- ";
  227. foreach ($appLicenceInfo->domains as $domain) {
  228. $cmd .= " --domain {$domain} ";
  229. }
  230. $cmd .= " " . implode(" ", $phpFiles);
  231. $out = ''; $ret = '';
  232. exec($cmd, $out, $ret);
  233. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  234. if (0 !== $ret) throw new Exception("Error at encode files");
  235. if (empty($out)) throw new Exception("No output for encode files command");
  236. }
  237. }
  238. public function getAppLicenceInfo($idLicence) {
  239. $idLicence = intval($idLicence);
  240. if (empty($idLicence)) throw new Exception("Nie wybrano serwera/licencji.");
  241. //DBG::_(true, true, 'idLicence', $idLicence, __CLASS__, __FUNCTION__, __LINE__);
  242. $appLicenceInfo = (object)$this->fetchAppLicenceInfo($idLicence);
  243. $appLicenceInfo->domains = $this->fetchDomainsByLicenceId($idLicence);
  244. $installRootPath = '/Library/Server/Web/Data/Sites/Default/PLIKI/SES_PROCESY5_A';
  245. $appLicenceInfo->installFolderName = "{$idLicence}_upgrade_SE_source_encrypted";
  246. $appLicenceInfo->installPath = "{$installRootPath}/{$appLicenceInfo->installFolderName}";
  247. //DBG::_(true, true, 'appLicenceInfo', $appLicenceInfo, __CLASS__, __FUNCTION__, __LINE__);
  248. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found.");
  249. $appLicenceInfo->installFolderExists = file_exists("{$appLicenceInfo->installPath}/SE");
  250. $appLicenceInfo->installFolderGitExists = file_exists("{$appLicenceInfo->installPath}/.git");
  251. return $appLicenceInfo;
  252. }
  253. public function generateApp($appLicenceInfo) {
  254. if (empty($appLicenceInfo->ID)) throw new Exception("Nie wybrano serwera/licencji.");
  255. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found");
  256. $installPath = $appLicenceInfo->installPath;
  257. if (empty($installPath)) throw new Exception("Install path not found");
  258. $cmds = array();
  259. $cmds[] = "if [ -d {$installPath} ] ; then rm -rf '{$installPath}'; fi";
  260. $cmds[] = "mkdir {$installPath}";
  261. $cmds[] = "cd {$installPath} && git clone git@biuro.biall-net.pl:plabudda/se.git .";
  262. $cmds[] = "cd {$installPath} && echo `git show-ref --head|head -1|head -c 8` > SE/VERSION ";
  263. //echo'<pre>cmds: ';print_r($cmds);echo'</pre>';
  264. foreach ($cmds as $cmd) {
  265. $out = ''; $ret = '';
  266. exec($cmd, $out, $ret);
  267. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  268. }
  269. $this->_encodeSource($appLicenceInfo);
  270. // 1763: $exec='cd '.$installer_dir.' && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder -b-
  271. // '.INSTALL_SES_PROCESY_A::get_same_domains_for_install($h->SERVER_ADDRESS_SHORT).'
  272. // -r *.php
  273. // -x superedit-DB_PROCEDURES_CREATE.php
  274. // -x INI.php
  275. // -x .config_base_structure.php
  276. // ';
  277. //
  278. // INSTALL_SES_PROCESY_A::get_same_domains_for_install($h->SERVER_ADDRESS_SHORT):
  279. // $res2=DB::query("select SERVER_ADDRESS_SHORT from SES_PROCESY5_A where SERVER_ADDRESS_IP='".$h->SERVER_ADDRESS_IP."'");
  280. // while($h2=DB::fetch($res2)) {
  281. // $domain[]=' --domain '.$h2->SERVER_ADDRESS_SHORT;
  282. // ssh server@biuro.galeriaprzymorze.eu: PHP 5.5.20
  283. // ssh server@biuro.biall-net.pl
  284. // cd /Users/plabudda/procesy5-install-galeriaprzymorze.eu/
  285. // sudo chown -R server:admin SE/
  286. // /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
  287. }
  288. public function fetchActiveLicences() {
  289. $activeLic = array();
  290. $db = DB::getDB();
  291. $sql = "select l.`ID`
  292. , l.`SERVER_ADDRESS`
  293. , l.`SERVER_ADDRESS_SHORT` as domain -- domain for sgencoder
  294. , l.`SERVER_ADDRESS_IP`
  295. from `SES_PROCESY5_A` l
  296. where 1=1
  297. -- TODO: and l.`A_STATUS` in('NORMAL','WAITING')
  298. order by l.`ID` DESC
  299. ";
  300. $res = $db->query($sql);
  301. while ($r = $db->fetch($res)) {
  302. $activeLic[$r->ID] = $r;
  303. }
  304. return $activeLic;
  305. }
  306. public function fetchDomainsByLicenceId($licenceId) {
  307. $domains = array();
  308. $db = DB::getDB();
  309. $sql = "select g.`SERVER_ADDRESS_SHORT`, g.`SERVER_ADDRESS`
  310. from `SES_PROCESY5_A` g
  311. where g.`SERVER_ADDRESS_IP`=(select l.`SERVER_ADDRESS_IP`
  312. from `SES_PROCESY5_A` l
  313. where l.`ID`='{$licenceId}'
  314. -- TODO: and l.`A_STATUS` in('NORMAL','WAITING')
  315. )
  316. -- TODO: and g.`A_STATUS` in('NORMAL','WAITING')
  317. ";
  318. $res = $db->query($sql);
  319. while ($r = $db->fetch($res)) {
  320. $domains[] = $r->SERVER_ADDRESS_SHORT;
  321. if ($r->SERVER_ADDRESS != $r->SERVER_ADDRESS_SHORT) {
  322. $domains[] = $r->SERVER_ADDRESS;
  323. }
  324. }
  325. return $domains;
  326. }
  327. public function fetchMainServerByLicenceId($licenceId) {
  328. $mainServer = null;
  329. $db = DB::getDB();
  330. $sql = "select g.`SERVER_ADDRESS`
  331. from `SES_PROCESY5_A` g
  332. where g.`ID`='{$licenceId}'
  333. -- TODO: and g.`A_STATUS` in('NORMAL','WAITING')
  334. ";
  335. $res = $db->query($sql);
  336. while ($r = $db->fetch($res)) {
  337. $mainServer = $r->SERVER_ADDRESS;
  338. }
  339. return $mainServer;
  340. }
  341. public function fetchAppLicenceInfo($licenceId) {
  342. $licenceInfo = null;
  343. $pdo = DB::getPDO();
  344. $sth = $pdo->prepare("
  345. select g.ID
  346. , g.SERVER_ADDRESS as mainServer
  347. , g.ADMIN_USERNAME as rootLogin
  348. , g.ADMIN_USERNAME_PASSWD as rootPassword
  349. from SES_PROCESY5_A g
  350. where g.`ID`=:id_licence
  351. -- TODO: and g.A_STATUS in('NORMAL','WAITING')
  352. ");
  353. $sth->bindValue('id_licence', $licenceId, PDO::PARAM_INT);
  354. $sth->execute();
  355. $all = $sth->fetchAll();
  356. if (empty($all)) throw new Exception("Brak licencji o nr '{$licenceId}'");
  357. $licenceInfo = reset($all);
  358. return $licenceInfo;
  359. }
  360. public function _endWithException($e) {
  361. ?>
  362. <div class="container">
  363. <div class="alert alert-danger">
  364. #<?php echo $e->getLine(); ?>: <?php echo $e->getMessage(); ?>
  365. </div>
  366. <p>Wróć do <a href="index.php?_route=Install">menu</a></p>
  367. </div>
  368. <?php
  369. SE_Layout::dol();
  370. exit;
  371. }
  372. public function sendToRemoteTestDirAction() {
  373. $args = array();
  374. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  375. SE_Layout::gora();
  376. //SE_Layout::menu();
  377. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  378. try {
  379. $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
  380. $this->_sendToRemoteTestDir($appLicenceInfo);
  381. ?>
  382. Test online: <a target="_blank" href="https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/">https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/</a>
  383. <?php
  384. } catch (Exception $e) {
  385. $this->_endWithException($e);
  386. }
  387. SE_Layout::dol();
  388. }
  389. public function _sendToRemoteTestDir($appLicenceInfo) {
  390. $cmd = ''; $out = ''; $ret = '';
  391. $this->_assertRsaKeyExists($appLicenceInfo);
  392. $sshHostUsr = "{$appLicenceInfo->rootLogin}@{$appLicenceInfo->mainServer}";
  393. $cmd = "
  394. ssh {$sshHostUsr} '[ ! -d ~/se.encrypted.upgrade ] && mkdir ~/se.encrypted.upgrade || echo 1';
  395. ssh {$sshHostUsr} 'rm -rf ~/se.encrypted.upgrade/SE';
  396. rsync --archive --verbose --update --times --compress --one-file-system --omit-dir-times --no-g --no-perms \
  397. '{$appLicenceInfo->installPath}/SE/' {$sshHostUsr}:~/se.encrypted.upgrade/SE/;
  398. ssh {$sshHostUsr} 'cp -r /Library/Server/Web/Data/Sites/Default/SE/config ~/se.encrypted.upgrade/SE/';
  399. ssh {$sshHostUsr} 'rm /Library/Server/Web/Data/Sites/Default/se.encrypted.upgrade'
  400. ssh {$sshHostUsr} 'ln -s ~/se.encrypted.upgrade/SE /Library/Server/Web/Data/Sites/Default/se.encrypted.upgrade'
  401. ";
  402. V::exec($cmd, $out, $ret);
  403. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  404. if (0 !== $ret) throw new Exception("Cannot run remote command using rsa key! #{$ret}");
  405. }
  406. public function upgradeRemoteFromTestDirAction() {
  407. $args = array();
  408. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  409. SE_Layout::gora();
  410. //SE_Layout::menu();
  411. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  412. try {
  413. $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
  414. $this->_upgradeRemoteFromTestDir($appLicenceInfo);
  415. ?>
  416. Test online: <a target="_blank" href="https://<?php echo $appLicenceInfo->mainServer; ?>/SE/">https://<?php echo $appLicenceInfo->mainServer; ?>/SE/</a>
  417. <?php
  418. } catch (Exception $e) {
  419. $this->_endWithException($e);
  420. }
  421. SE_Layout::dol();
  422. }
  423. public function _upgradeRemoteFromTestDir($appLicenceInfo) {
  424. $cmd = ''; $out = ''; $ret = '';
  425. $this->_assertRsaKeyExists($appLicenceInfo);
  426. $sshHostUsr = "{$appLicenceInfo->rootLogin}@{$appLicenceInfo->mainServer}";
  427. $dateStr = date("Y-m-d_H-i-s");
  428. $cmd = "
  429. ssh {$sshHostUsr} 'rm -rf /Library/Server/Web/Data/Sites/Default/SE.test';
  430. ssh {$sshHostUsr} 'cp -r ~/se.encrypted.upgrade/SE /Library/Server/Web/Data/Sites/Default/SE.test';
  431. ssh {$sshHostUsr} '[ ! -d ~/bup.se.upgrade ] && mkdir ~/bup.se.upgrade || echo 1';
  432. ssh {$sshHostUsr} '[ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/VERSION ] && echo \"Error: plik VERSION nie istnieje\" || echo \"check VERSION OK\"';
  433. 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\"';
  434. 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\"';
  435. ssh {$sshHostUsr} 'mv /Library/Server/Web/Data/Sites/Default/SE ~/bup.se.upgrade/bup.{$dateStr}';
  436. ssh {$sshHostUsr} 'mv /Library/Server/Web/Data/Sites/Default/SE.test /Library/Server/Web/Data/Sites/Default/SE';
  437. ";
  438. V::exec($cmd, $out, $ret);
  439. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  440. if (0 !== $ret) throw new Exception("Cannot run remote command using rsa key! #{$ret}");
  441. }
  442. public function _assertRsaKeyExists($appLicenceInfo) {
  443. $cmd = ''; $out = ''; $ret = '';
  444. $cmd = "echo ~ && pwd";// /Library/WebServer
  445. $cmd = "ls -1 ~/.ssh/";
  446. //$cmd = "rm /tmp/id_rsa";
  447. //$cmd = "rm /tmp/id_rsa.pub";
  448. //$cmd = "ssh-keygen -t rsa -N '' -C '_www@biuro.biall-net.pl' -f /tmp/id_rsa";
  449. //$cmd = "ssh-keygen -t rsa -N '' -C '_www@biuro.biall-net.pl' -f /tmp/id_rsa";
  450. V::exec($cmd, $out, $ret);
  451. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  452. if (0 !== $ret) {// no ~/.ssh directory
  453. $rsaKeyPath = "~/.ssh";
  454. $cmds = array();
  455. $cmds[] = "mkdir {$rsaKeyPath}";
  456. $cmds[] = "ssh-keygen -t rsa -N '' -C '_www@biuro.biall-net.pl' -f {$rsaKeyPath}/id_rsa 2>&1";
  457. $cmds[] = "ls -1 $rsaKeyPath";
  458. foreach ($cmds as $cmd) {
  459. V::exec($cmd, $out, $ret);
  460. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  461. if (0 !== $ret) throw new Exception("Error '{$ret}' cmd({$cmd}): " . implode("\n", $out));
  462. }
  463. }
  464. $cmd = "cat ~/.ssh/id_rsa.pub";
  465. V::exec($cmd, $out, $ret);
  466. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  467. if (0 !== $ret || empty($out) || empty($out[0])) throw new Exception("Cannot read rsa public key");
  468. $rsaPubKey = $out[0];
  469. $cmd = 'ls -1a';
  470. $cmd = "
  471. [ ! -d ~/.ssh ] && mkdir ~/.ssh || echo 'OK ~/.ssh exists';
  472. cat ~/.ssh/authorized_keys| grep '{$rsaPubKey}' && echo 'OK' || echo '{$rsaPubKey}' >> ~/.ssh/authorized_keys;
  473. chmod 600 ~/.ssh/authorized_keys;
  474. ";
  475. V::execRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret);
  476. DBG::_(true, true, "remote cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  477. //$cmd = "ssh server@{$appLicenceInfo->mainServer} 'ls -1 .ssh/'";
  478. //$cmd = "ssh -i ~/.ssh/id_rsa server@{$appLicenceInfo->mainServer} 'ls -1'";
  479. $cmd = "ssh server@{$appLicenceInfo->mainServer} 'ls -1'";
  480. V::exec($cmd, $out, $ret);
  481. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  482. if (0 !== $ret) throw new Exception("Cannot run remote command using rsa key! #{$ret}");
  483. }
  484. }