Install.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. session_write_close();
  50. $args = array();
  51. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  52. SE_Layout::gora();
  53. SE_Layout::menu();
  54. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  55. try {
  56. $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
  57. } catch (Exception $e) {
  58. $this->_endWithException($e);
  59. }
  60. //$this->generateApp($args['licence_id']);
  61. //DBG::_(true, true, "appLicenceInfo", $appLicenceInfo, __CLASS__, __FUNCTION__, __LINE__);
  62. ?>
  63. <div class="jumbotron">
  64. <div class="container">
  65. <h3>Generowanie aplikacji dla licencji <?php echo $appLicenceInfo->ID; ?></h3>
  66. <p>Licencja dla domen: <?php echo implode(', ', $appLicenceInfo->domains); ?></p>
  67. <p>Katalog z zakodowanymi plikami: <?php echo $appLicenceInfo->installFolderName; ?></p>
  68. <form class="form-inline" method="POST">
  69. <input type="hidden" name="_task" value="createApp" />
  70. <input type="hidden" name="licence_id" value="<?php echo $appLicenceInfo->ID; ?>" />
  71. <input type="hidden" name="_generateEncryptedSource" value="1" />
  72. <button type="submit" id="fldSbmtBtn" class="btn btn-primary" autocomplete="off">
  73. Generuj
  74. </button>
  75. </form>
  76. <?php if ($appLicenceInfo->installFolderGitExists) : ?>
  77. <br>
  78. <div class="alert alert-info">
  79. Katalog istnieje i zawiera już repozytorium git:
  80. <br> - <a href="index.php?_route=Install&_task=gitResetHard&licence_id=<?php echo $appLicenceInfo->ID; ?>"
  81. target="_blank"
  82. 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)
  83. <br> - <a href="index.php?_route=Install&_task=encodeSource&licence_id=<?php echo $appLicenceInfo->ID; ?>"
  84. target="_blank"
  85. class="btn btn-xs btn-default">encode files</a>
  86. <br> - <a href="index.php?_route=Install&_task=sendToRemoteTestDir&licence_id=<?php echo $appLicenceInfo->ID; ?>"
  87. target="_blank"
  88. class="btn btn-xs btn-default">send encoded files to remote server test folder (generates ssh key if not set)</a>
  89. <p style="margin-top:30px">
  90. Test online: <a target="_blank" href="https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/">https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/</a>
  91. </p>
  92. <br> - <a href="index.php?_route=Install&_task=upgradeRemoteFromTestDir&licence_id=<?php echo $appLicenceInfo->ID; ?>"
  93. onclick="return confirm('Uruchomić aktualizację SE na https://<?php echo $appLicenceInfo->mainServer; ?>/SE/?')"
  94. target="_blank"
  95. class="btn btn-xs btn-warning">UPGRADE Production folder from uploaded dir: ~/se.encrypted.upgrade/</a>
  96. </div>
  97. <?php endif; ?>
  98. </div>
  99. </div>
  100. <script type="text/javascript">
  101. jQuery(document).ready(function () {
  102. jQuery('#fldSbmtBtn').on('click', function () {
  103. jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
  104. jQuery(this).parent().submit();
  105. })
  106. });
  107. </script>
  108. <?php
  109. $generateEncryptedSource = (1 == V::get('_generateEncryptedSource', 0, $_REQUEST, 'int'));
  110. if ($generateEncryptedSource) {
  111. echo '<div class="container">';
  112. echo '<h4>' . "Generowanie..." . '</h4>';
  113. echo '<div style="border:1px solid silver; max-height:400px; overflow-y:scroll">';
  114. try {
  115. $this->generateApp($appLicenceInfo);
  116. } catch (Exception $e) {
  117. echo '</div></div>';// .container/ scroll
  118. $this->_endWithException($e);
  119. }
  120. echo '</div>';// .container
  121. ?>
  122. <div class="alert alert-success">
  123. <strong>Gotowe</strong> Aplikacja znajduje się w katalogu <?php echo $appLicenceInfo->installFolderName; ?>
  124. </div>
  125. <?php
  126. }
  127. SE_Layout::dol();
  128. }
  129. public function gitResetHardAction() {
  130. session_write_close();
  131. $args = array();
  132. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  133. SE_Layout::gora();
  134. SE_Layout::menu();
  135. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  136. try {
  137. $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
  138. $this->_gitResetHard($appLicenceInfo);
  139. } catch (Exception $e) {
  140. $this->_endWithException($e);
  141. }
  142. SE_Layout::dol();
  143. }
  144. public function _gitResetHard($appLicenceInfo) {
  145. if (empty($appLicenceInfo->ID)) throw new Exception("Nie wybrano serwera/licencji.");
  146. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found");
  147. $installPath = $appLicenceInfo->installPath;
  148. if (empty($installPath)) throw new Exception("Install path not found");
  149. $cmds = array();
  150. $cmds[] = "cd {$installPath} && git reset --hard";
  151. $cmds[] = "cd {$installPath} && git pull";
  152. $cmds[] = "cd {$installPath} && echo `git show-ref --head|head -1|head -c 8` > SE/VERSION ";
  153. foreach ($cmds as $cmd) {
  154. $out = ''; $ret = '';
  155. exec($cmd, $out, $ret);
  156. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  157. }
  158. }
  159. public function encodeSourceAction() {
  160. session_write_close();
  161. $args = array();
  162. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  163. SE_Layout::gora();
  164. SE_Layout::menu();
  165. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  166. try {
  167. $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
  168. $this->_encodeSource($appLicenceInfo);
  169. } catch (Exception $e) {
  170. $this->_endWithException($e);
  171. }
  172. SE_Layout::dol();
  173. }
  174. public function _encodeSource($appLicenceInfo) {
  175. if (empty($appLicenceInfo->ID)) throw new Exception("Nie wybrano serwera/licencji.");
  176. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found");
  177. $installPath = $appLicenceInfo->installPath;
  178. if (empty($installPath)) throw new Exception("Install path not found");
  179. $phpVersionsForSgencoder = '--phpversion 5.5 --phpversion 5.6';// encode for PHP 5.x (currently supported PHP 5.0-5.6)
  180. if ('1' == V::get('DBG_ENCODER_HELP', '', $_REQUEST)) {// encoder help
  181. $cmd = "cd {$installPath}/SE && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder --help ";
  182. $out = ''; $ret = '';
  183. exec($cmd, $out, $ret);
  184. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  185. exit;
  186. }
  187. if (0) {//'1' == V::get('DBG_ENCODER_TEST_IP', '', $_REQUEST)) {
  188. $phpFiles = array();
  189. $phpFiles[] = 't.php';
  190. $cmd = "cd {$installPath}/SE && echo '<?php' > t.php ; echo 'echo \"test\\n\";' >> t.php ";
  191. //$cmd = "cd {$installPath}/SE && rm t.php ";
  192. V::exec($cmd, $out, $ret);
  193. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  194. $cmd = "cd {$installPath}/SE && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder {$phpVersionsForSgencoder} -b- ";
  195. //$mac = '38:c9:86:31:80:77';
  196. //$cmd .= " --mac {$mac}";
  197. foreach ($appLicenceInfo->domains as $domain) {
  198. $cmd .= " --domain {$domain} ";
  199. }
  200. $cmd .= " " . implode(" ", $phpFiles);
  201. V::exec($cmd, $out, $ret);
  202. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  203. if (0 !== $ret) throw new Exception("Error at encode files");
  204. if (empty($out)) throw new Exception("No output for encode files command");
  205. exit;
  206. }
  207. {
  208. $phpFiles = array();
  209. $skipPhpFiles = array();
  210. $skipPhpFiles[] = 'bash_sync_perms.php';
  211. $skipPhpFiles[] = 'bash_install_check.php';
  212. $skipPhpFiles[] = '.config.php';
  213. $skipPhpFiles[] = '.config_base_structure.php';
  214. $skipPhpFiles[] = 'se-lib/V.php';
  215. $skipPhpFiles[] = 'se-lib/Lib.php';
  216. $skipPhpFiles[] = 'se-lib/DB.php';
  217. $skipPhpFiles[] = 'se-lib/DBG.php';
  218. $skipPhpFiles[] = 'se-lib/User.php';
  219. $skipPhpFiles[] = 'se-lib/UserProfile.php';
  220. $skipPhpFiles[] = 'se-lib/Config.php';
  221. $skipPhpFiles[] = 'se-lib/Config/INI.php';
  222. $skipPhpFiles[] = 'se-lib/FoldersConfig.php';
  223. $skipPhpFiles[] = 'se-lib/FileUploader.php';
  224. $skipPhpFiles[] = 'se-lib/Route/Budget.php';
  225. $skipPhpFiles[] = 'se-lib/Route/FixCrmProcesInitIdx.php';
  226. $skipPhpFiles[] = 'se-lib/Route/FixProjectPath.php';
  227. $skipPhpFiles[] = 'se-lib/Route/FixZasobPath.php';
  228. //$skipPhpFiles[] = 'se-lib/Route/Install.php';
  229. $skipPhpFiles[] = 'se-lib/Route/Msgs.php';
  230. $skipPhpFiles[] = 'se-lib/Route/UrlAction/WmsGenerate.php';
  231. $skipPhpFiles[] = 'superedit-DB_PROCEDURES_CREATE.php';
  232. //$skipPhpFiles[] = '';
  233. $skipPhpFiles[] = 'se-lib/DataSourceException.php';
  234. $skipPhpFiles[] = 'se-lib/Core/Pdo.php';
  235. $skipPhpFiles[] = 'se-lib/Cron.php';
  236. $skipPhpFiles[] = 'se-lib/Router.php';
  237. $skipPhpFiles[] = 'se-lib/RouteBase.php';
  238. $skipPhpFiles[] = 'se-lib/HttpException.php';
  239. $skipPhpFiles[] = 'se-lib/DebugExecutionTime.php';
  240. $cmd = "cd {$installPath}/SE && find . -name '*.php' ";
  241. $out = ''; $ret = '';
  242. exec($cmd, $out, $ret);
  243. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  244. if (0 !== $ret) throw new Exception("Error at find php files");
  245. if (empty($out)) throw new Exception("No php files found");
  246. foreach ($out as $phpFilePath) {
  247. $phpFilePath = ('./' == substr($phpFilePath, 0, 2))? substr($phpFilePath, 2) : $phpFilePath;
  248. if (in_array($phpFilePath, $skipPhpFiles)) continue;
  249. if ('schema/' == substr($phpFilePath, 0, 7)) continue;
  250. $phpFiles[] = $phpFilePath;
  251. }
  252. DBG::_(true, true, 'phpFiles', $phpFiles, __CLASS__, __FUNCTION__, __LINE__);
  253. if (empty($phpFiles)) throw new Exception("No php files to encode");
  254. }
  255. {
  256. $cmd = "cd {$installPath}/SE && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder {$phpVersionsForSgencoder} -b- ";
  257. foreach ($appLicenceInfo->domains as $domain) {
  258. $cmd .= " --domain {$domain} ";
  259. }
  260. $cmd .= " " . implode(" ", $phpFiles);
  261. $out = ''; $ret = '';
  262. exec($cmd, $out, $ret);
  263. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  264. if (0 !== $ret) throw new Exception("Error at encode files");
  265. if (empty($out)) throw new Exception("No output for encode files command");
  266. }
  267. }
  268. public function getAppLicenceInfo($idLicence) {
  269. $idLicence = intval($idLicence);
  270. if (empty($idLicence)) throw new Exception("Nie wybrano serwera/licencji.");
  271. //DBG::_(true, true, 'idLicence', $idLicence, __CLASS__, __FUNCTION__, __LINE__);
  272. $appLicenceInfo = (object)$this->fetchAppLicenceInfo($idLicence);
  273. $appLicenceInfo->domains = $this->fetchDomainsByLicenceId($idLicence);
  274. $installRootPath = '/Library/Server/Web/Data/Sites/Default/PLIKI/SES_PROCESY5_A';
  275. $appLicenceInfo->installFolderName = "{$idLicence}_upgrade_SE_source_encrypted";
  276. $appLicenceInfo->installPath = "{$installRootPath}/{$appLicenceInfo->installFolderName}";
  277. //DBG::_(true, true, 'appLicenceInfo', $appLicenceInfo, __CLASS__, __FUNCTION__, __LINE__);
  278. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found.");
  279. $appLicenceInfo->installFolderExists = file_exists("{$appLicenceInfo->installPath}/SE");
  280. $appLicenceInfo->installFolderGitExists = file_exists("{$appLicenceInfo->installPath}/.git");
  281. return $appLicenceInfo;
  282. }
  283. public function generateApp($appLicenceInfo) {
  284. if (empty($appLicenceInfo->ID)) throw new Exception("Nie wybrano serwera/licencji.");
  285. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found");
  286. $installPath = $appLicenceInfo->installPath;
  287. if (empty($installPath)) throw new Exception("Install path not found");
  288. $cmds = array();
  289. $cmds[] = "if [ -d {$installPath} ] ; then rm -rf '{$installPath}'; fi";
  290. $cmds[] = "mkdir {$installPath}";
  291. $cmds[] = "cd {$installPath} && git clone ssh://git@biuro.biall-net.pl:2222/plabudda/se.git .";
  292. $cmds[] = "cd {$installPath} && echo `git show-ref --head|head -1|head -c 8` > SE/VERSION ";
  293. //echo'<pre>cmds: ';print_r($cmds);echo'</pre>';
  294. foreach ($cmds as $cmd) {
  295. $out = ''; $ret = '';
  296. exec($cmd, $out, $ret);
  297. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  298. }
  299. $this->_encodeSource($appLicenceInfo);
  300. // 1763: $exec='cd '.$installer_dir.' && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder -b-
  301. // '.INSTALL_SES_PROCESY_A::get_same_domains_for_install($h->SERVER_ADDRESS_SHORT).'
  302. // -r *.php
  303. // -x superedit-DB_PROCEDURES_CREATE.php
  304. // -x INI.php
  305. // -x .config_base_structure.php
  306. // ';
  307. //
  308. // INSTALL_SES_PROCESY_A::get_same_domains_for_install($h->SERVER_ADDRESS_SHORT):
  309. // $res2=DB::query("select SERVER_ADDRESS_SHORT from SES_PROCESY5_A where SERVER_ADDRESS_IP='".$h->SERVER_ADDRESS_IP."'");
  310. // while($h2=DB::fetch($res2)) {
  311. // $domain[]=' --domain '.$h2->SERVER_ADDRESS_SHORT;
  312. // ssh server@biuro.galeriaprzymorze.eu: PHP 5.5.20
  313. // ssh server@biuro.biall-net.pl
  314. // cd /Users/plabudda/procesy5-install-galeriaprzymorze.eu/
  315. // sudo chown -R server:admin SE/
  316. // /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
  317. }
  318. public function fetchActiveLicences() {
  319. $activeLic = array();
  320. $db = DB::getDB();
  321. $sql = "select l.`ID`
  322. , l.`SERVER_ADDRESS`
  323. , l.`SERVER_ADDRESS_SHORT` as domain -- domain for sgencoder
  324. , l.`SERVER_ADDRESS_IP`
  325. from `SES_PROCESY5_A` l
  326. where 1=1
  327. -- TODO: and l.`A_STATUS` in('NORMAL','WAITING')
  328. order by l.`ID` DESC
  329. ";
  330. $res = $db->query($sql);
  331. while ($r = $db->fetch($res)) {
  332. $activeLic[$r->ID] = $r;
  333. }
  334. return $activeLic;
  335. }
  336. public function fetchDomainsByLicenceId($licenceId) {
  337. $domains = array();
  338. $db = DB::getDB();
  339. $sql = "select g.`SERVER_ADDRESS_SHORT`, g.`SERVER_ADDRESS`
  340. from `SES_PROCESY5_A` g
  341. where g.`SERVER_ADDRESS_IP`=(select l.`SERVER_ADDRESS_IP`
  342. from `SES_PROCESY5_A` l
  343. where l.`ID`='{$licenceId}'
  344. -- TODO: and l.`A_STATUS` in('NORMAL','WAITING')
  345. )
  346. -- TODO: and g.`A_STATUS` in('NORMAL','WAITING')
  347. ";
  348. $res = $db->query($sql);
  349. while ($r = $db->fetch($res)) {
  350. $domains[] = $r->SERVER_ADDRESS_SHORT;
  351. if ($r->SERVER_ADDRESS != $r->SERVER_ADDRESS_SHORT) {
  352. $domains[] = $r->SERVER_ADDRESS;
  353. }
  354. }
  355. return $domains;
  356. }
  357. public function fetchMainServerByLicenceId($licenceId) {
  358. $mainServer = null;
  359. $db = DB::getDB();
  360. $sql = "select g.`SERVER_ADDRESS`
  361. from `SES_PROCESY5_A` g
  362. where g.`ID`='{$licenceId}'
  363. -- TODO: and g.`A_STATUS` in('NORMAL','WAITING')
  364. ";
  365. $res = $db->query($sql);
  366. while ($r = $db->fetch($res)) {
  367. $mainServer = $r->SERVER_ADDRESS;
  368. }
  369. return $mainServer;
  370. }
  371. public function fetchAppLicenceInfo($licenceId) {
  372. $licenceInfo = null;
  373. $sth = DB::getPDO()->prepare("
  374. select g.ID
  375. , g.SERVER_ADDRESS as mainServer
  376. , g.ADMIN_USERNAME as rootLogin
  377. , g.ADMIN_USERNAME_PASSWD as rootPassword
  378. , g.SSH_PORT as sshPort
  379. from SES_PROCESY5_A g
  380. where g.`ID`=:id_licence
  381. -- TODO: and g.A_STATUS in('NORMAL','WAITING')
  382. ");
  383. $sth->bindValue('id_licence', $licenceId, PDO::PARAM_INT);
  384. $sth->execute();
  385. $all = $sth->fetchAll();
  386. if (empty($all)) throw new Exception("Brak licencji o nr '{$licenceId}'");
  387. $licenceInfo = reset($all);
  388. if (!$licenceInfo['sshPort']) $licenceInfo['sshPort'] = 22;
  389. return $licenceInfo;
  390. }
  391. public function _endWithException($e) {
  392. ?>
  393. <div class="container">
  394. <div class="alert alert-danger">
  395. #<?php echo $e->getLine(); ?>: <?php echo $e->getMessage(); ?>
  396. </div>
  397. <p>Wróć do <a href="index.php?_route=Install">menu</a></p>
  398. </div>
  399. <?php
  400. SE_Layout::dol();
  401. exit;
  402. }
  403. public function sendToRemoteTestDirAction() {
  404. session_write_close();
  405. $args = array();
  406. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  407. SE_Layout::gora();
  408. //SE_Layout::menu();
  409. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  410. try {
  411. $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
  412. $this->_sendToRemoteTestDir($appLicenceInfo);
  413. ?>
  414. Test online: <a target="_blank" href="https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/">https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/</a>
  415. <?php
  416. } catch (Exception $e) {
  417. $this->_endWithException($e);
  418. }
  419. SE_Layout::dol();
  420. }
  421. public function _fetchRemoteHomeDir($appLicenceInfo) {
  422. $this->_assertRsaKeyExists($appLicenceInfo);
  423. $sshHostUsr = "{$appLicenceInfo->rootLogin}@{$appLicenceInfo->mainServer}";
  424. $sshArgs = (22 != $appLicenceInfo->sshPort)? "-p {$appLicenceInfo->sshPort}" : '';
  425. $rsyncSshPort = (22 != $appLicenceInfo->sshPort)? "-e 'ssh -p {$appLicenceInfo->sshPort}'" : '';
  426. if (V::get('DBG_REMOTE', '', $_GET)) {// DBG
  427. $cmd = "echo ~";
  428. V::exec("ssh {$sshArgs} {$sshHostUsr} '{$cmd}'", $out, $ret);
  429. DBG::_(true, true, "exec(ssh ... 'cmd'): {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  430. V::execRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
  431. DBG::_(true, true, "execRemote(cmd): {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  432. V::execRootRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
  433. DBG::_(true, true, "execRootRemote(cmd): {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  434. }
  435. $cmd = "echo ~";
  436. V::exec("ssh {$sshArgs} {$sshHostUsr} '{$cmd}'", $out, $ret);
  437. DBG::_(true, true, "exec(ssh ... '{$cmd}') (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  438. if (empty($out) || empty($out[0])) throw new Exception("Cannot fetch remote home dir");
  439. return $out[0];
  440. }
  441. public function _sendToRemoteTestDir($appLicenceInfo) {
  442. $cmd = ''; $out = ''; $ret = '';
  443. $this->_assertRsaKeyExists($appLicenceInfo);
  444. $sshHostUsr = "{$appLicenceInfo->rootLogin}@{$appLicenceInfo->mainServer}";
  445. $sshArgs = (22 != $appLicenceInfo->sshPort)? "-p {$appLicenceInfo->sshPort}" : '';
  446. $rsyncSshPort = (22 != $appLicenceInfo->sshPort)? "-e 'ssh -p {$appLicenceInfo->sshPort}'" : '';
  447. // $appLicenceInfo->sshPort
  448. /* rsync options:
  449. -a, --archive archive mode; same as -rlptgoD (no -H)
  450. -u, --update skip files that are newer on the receiver
  451. -t, --times preserve times
  452. --delete delete extraneous files from dest dirs
  453. */
  454. DBG::_(true, true, "remote home dir:", $this->_fetchRemoteHomeDir($appLicenceInfo), __CLASS__, __FUNCTION__, __LINE__);
  455. $cmd = "
  456. ssh {$sshArgs} {$sshHostUsr} '[ ! -d ~/se.encrypted.upgrade ] && mkdir ~/se.encrypted.upgrade || echo 1';
  457. ssh {$sshArgs} {$sshHostUsr} 'rm -rf ~/se.encrypted.upgrade/SE';
  458. ssh {$sshArgs} {$sshHostUsr} 'cp -r /Library/Server/Web/Data/Sites/Default/SE ~/se.encrypted.upgrade/SE';
  459. ssh {$sshArgs} {$sshHostUsr} 'rm -rf ~/se.encrypted.upgrade/SE/config';
  460. rsync --archive --times --delete --compress --one-file-system --omit-dir-times --no-g --no-perms \
  461. --verbose {$rsyncSshPort} \
  462. --exclude='stuff/qgis__kyngchaos.com' \
  463. --exclude='stuff/qgis__kyngchaos.com/QGIS-2.14.3-1.dmg' \
  464. '{$appLicenceInfo->installPath}/SE/' {$sshHostUsr}:~/se.encrypted.upgrade/SE/;
  465. ssh {$sshArgs} {$sshHostUsr} 'cp -r /Library/Server/Web/Data/Sites/Default/SE/config ~/se.encrypted.upgrade/SE/';
  466. ssh {$sshArgs} {$sshHostUsr} 'rm /Library/Server/Web/Data/Sites/Default/se.encrypted.upgrade'
  467. ssh {$sshArgs} {$sshHostUsr} 'ln -s ~/se.encrypted.upgrade/SE /Library/Server/Web/Data/Sites/Default/se.encrypted.upgrade'
  468. ";
  469. // --dry-run \
  470. V::exec($cmd, $out, $ret);
  471. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  472. if (0 !== $ret) throw new Exception("Cannot run remote command using rsa key! #{$ret}");
  473. }
  474. public function upgradeRemoteFromTestDirAction() {
  475. session_write_close();
  476. $args = array();
  477. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  478. SE_Layout::gora();
  479. //SE_Layout::menu();
  480. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  481. try {
  482. $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
  483. $this->_upgradeRemoteFromTestDir($appLicenceInfo);
  484. ?>
  485. Test online: <a target="_blank" href="https://<?php echo $appLicenceInfo->mainServer; ?>/SE/">https://<?php echo $appLicenceInfo->mainServer; ?>/SE/</a>
  486. <?php
  487. } catch (Exception $e) {
  488. $this->_endWithException($e);
  489. }
  490. SE_Layout::dol();
  491. }
  492. public function _upgradeRemoteFromTestDir($appLicenceInfo) {
  493. $cmd = ''; $out = ''; $ret = '';
  494. $this->_assertRsaKeyExists($appLicenceInfo);
  495. $dateStr = date("Y-m-d_H-i-s");
  496. // $remoteHomeDir = "/Users/{$appLicenceInfo->rootLogin}";// BUG: alias like in ams - login = prezes is alias for arkadiuszbinder
  497. $remoteHomeDir = $this->_fetchRemoteHomeDir($appLicenceInfo);
  498. $cmd = "
  499. rm -rf /Library/Server/Web/Data/Sites/Default/SE.test 2>&1
  500. cp -r {$remoteHomeDir}/se.encrypted.upgrade/SE /Library/Server/Web/Data/Sites/Default/SE.test 2>&1
  501. [ ! -d {$remoteHomeDir}/bup.se.upgrade ] && mkdir {$remoteHomeDir}/bup.se.upgrade || echo 1
  502. [ ! -d {$remoteHomeDir}/bup.se.upgrade ] && exit 1
  503. [ ! -d {$remoteHomeDir}/bup.se.upgrade-testttttt ] && exit 1
  504. [ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/VERSION ] && echo \"Error: plik VERSION nie istnieje\" || echo \"check VERSION OK\"
  505. [ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/VERSION ] && exit 1
  506. [ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/index.php ] && echo \"Error: plik index.php nie istnieje\" || echo \"check index.php OK\"
  507. [ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/index.php ] && exit 1
  508. [ ! -d /Library/Server/Web/Data/Sites/Default/SE.test/se-lib ] && echo \"Error: plik se-lib nie istnieje\" || echo \"check se-lib OK\"
  509. [ ! -d /Library/Server/Web/Data/Sites/Default/SE.test/se-lib ] && exit 1
  510. exit 0
  511. ";
  512. V::execRootRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
  513. $cmd = "
  514. mv /Library/Server/Web/Data/Sites/Default/SE {$remoteHomeDir}/bup.se.upgrade/bup.{$dateStr} 2>&1
  515. mv /Library/Server/Web/Data/Sites/Default/SE.test /Library/Server/Web/Data/Sites/Default/SE 2>&1
  516. exit 0
  517. ";
  518. V::execRootRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
  519. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  520. if (0 !== $ret) throw new Exception("Cannot run remote command as root! #{$ret}");
  521. $cmd = "/usr/bin/php /Library/Server/Web/Data/Sites/Default/SE/bash_install_check.php";
  522. V::execRootRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
  523. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  524. if (0 !== $ret) throw new Exception("Cannot run remote command as root! #{$ret}");
  525. }
  526. public function _assertRsaKeyExists($appLicenceInfo) {
  527. $cmd = ''; $out = ''; $ret = '';
  528. $cmd = "echo ~ && pwd";// /Library/WebServer
  529. $cmd = "ls -1 ~/.ssh/";
  530. //$cmd = "rm /tmp/id_rsa";
  531. //$cmd = "rm /tmp/id_rsa.pub";
  532. //$cmd = "ssh-keygen -t rsa -N '' -C '_www@biuro.biall-net.pl' -f /tmp/id_rsa";
  533. //$cmd = "ssh-keygen -t rsa -N '' -C '_www@biuro.biall-net.pl' -f /tmp/id_rsa";
  534. V::exec($cmd, $out, $ret);
  535. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  536. if (0 !== $ret) {// no ~/.ssh directory
  537. $rsaKeyPath = "~/.ssh";
  538. $cmds = array();
  539. $cmds[] = "mkdir {$rsaKeyPath}";
  540. $cmds[] = "ssh-keygen -t rsa -N '' -C '_www@biuro.biall-net.pl' -f {$rsaKeyPath}/id_rsa 2>&1";
  541. $cmds[] = "ls -1 $rsaKeyPath";
  542. foreach ($cmds as $cmd) {
  543. V::exec($cmd, $out, $ret);
  544. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  545. if (0 !== $ret) throw new Exception("Error '{$ret}' cmd({$cmd}): " . implode("\n", $out));
  546. }
  547. }
  548. $cmd = "cat ~/.ssh/id_rsa.pub";
  549. V::exec($cmd, $out, $ret);
  550. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  551. if (0 !== $ret || empty($out) || empty($out[0])) throw new Exception("Cannot read rsa public key");
  552. $rsaPubKey = $out[0];
  553. $cmd = 'ls -1a';
  554. $cmd = "
  555. [ ! -d ~/.ssh ] && mkdir ~/.ssh;
  556. [ ! -d ~/.ssh ] && echo 'ERROR ~/.ssh not exists and cannot be created';
  557. [ ! -f ~/.ssh/authorized_keys ] && echo '{$rsaPubKey}' > ~/.ssh/authorized_keys;
  558. [ ! -f ~/.ssh/authorized_keys ] && echo 'ERROR ~/.ssh/authorized_keys not exists and cannot be created';
  559. cat ~/.ssh/authorized_keys| grep '{$rsaPubKey}' && echo 'OK' || echo '{$rsaPubKey}' >> ~/.ssh/authorized_keys;
  560. chmod 600 ~/.ssh/authorized_keys;
  561. ";
  562. V::execRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
  563. DBG::_(true, true, "remote cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  564. //$cmd = "ssh server@{$appLicenceInfo->mainServer} 'ls -1 .ssh/'";
  565. //$cmd = "ssh -i ~/.ssh/id_rsa server@{$appLicenceInfo->mainServer} 'ls -1'";
  566. $sshPort = (22 != $appLicenceInfo->sshPort)? "-p {$appLicenceInfo->sshPort}" : '';
  567. $cmd = "ssh {$sshPort} {$appLicenceInfo->rootLogin}@{$appLicenceInfo->mainServer} 'ls -1'";
  568. V::exec($cmd, $out, $ret);
  569. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  570. if (0 !== $ret) throw new Exception("Cannot run remote command using rsa key! #{$ret}");
  571. }
  572. }