Install.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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[] = 'se-lib/V.php';
  214. $skipPhpFiles[] = 'se-lib/Lib.php';
  215. $skipPhpFiles[] = 'se-lib/DB.php';
  216. $skipPhpFiles[] = 'se-lib/DBG.php';
  217. $skipPhpFiles[] = 'se-lib/User.php';
  218. $skipPhpFiles[] = 'se-lib/UserProfile.php';
  219. $skipPhpFiles[] = 'se-lib/Config.php';
  220. $skipPhpFiles[] = 'se-lib/Config/INI.php';
  221. $skipPhpFiles[] = 'se-lib/FoldersConfig.php';
  222. $skipPhpFiles[] = 'se-lib/FileUploader.php';
  223. $skipPhpFiles[] = 'se-lib/Route/Budget.php';
  224. $skipPhpFiles[] = 'se-lib/Route/FixCrmProcesInitIdx.php';
  225. $skipPhpFiles[] = 'se-lib/Route/FixProjectPath.php';
  226. $skipPhpFiles[] = 'se-lib/Route/FixZasobPath.php';
  227. //$skipPhpFiles[] = 'se-lib/Route/Install.php';
  228. $skipPhpFiles[] = 'se-lib/Route/Msgs.php';
  229. $skipPhpFiles[] = 'se-lib/Route/UrlAction/WmsGenerate.php';
  230. $skipPhpFiles[] = 'superedit-DB_PROCEDURES_CREATE.php';
  231. //$skipPhpFiles[] = '';
  232. $skipPhpFiles[] = 'se-lib/DataSourceException.php';
  233. $skipPhpFiles[] = 'se-lib/Core/Pdo.php';
  234. $skipPhpFiles[] = 'se-lib/Cron.php';
  235. $skipPhpFiles[] = 'se-lib/Router.php';
  236. $skipPhpFiles[] = 'se-lib/RouteBase.php';
  237. $skipPhpFiles[] = 'se-lib/HttpException.php';
  238. $skipPhpFiles[] = 'se-lib/DebugExecutionTime.php';
  239. $cmd = "cd {$installPath}/SE && find . -name '*.php' ";
  240. $out = ''; $ret = '';
  241. exec($cmd, $out, $ret);
  242. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  243. if (0 !== $ret) throw new Exception("Error at find php files");
  244. if (empty($out)) throw new Exception("No php files found");
  245. foreach ($out as $phpFilePath) {
  246. $phpFilePath = ('./' == substr($phpFilePath, 0, 2))? substr($phpFilePath, 2) : $phpFilePath;
  247. if (in_array($phpFilePath, $skipPhpFiles)) continue;
  248. if ('schema/' == substr($phpFilePath, 0, 7)) continue;
  249. $phpFiles[] = $phpFilePath;
  250. }
  251. DBG::_(true, true, 'phpFiles', $phpFiles, __CLASS__, __FUNCTION__, __LINE__);
  252. if (empty($phpFiles)) throw new Exception("No php files to encode");
  253. }
  254. {
  255. $cmd = "cd {$installPath}/SE && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder {$phpVersionsForSgencoder} -b- ";
  256. foreach ($appLicenceInfo->domains as $domain) {
  257. $cmd .= " --domain {$domain} ";
  258. }
  259. $cmd .= " " . implode(" ", $phpFiles);
  260. $out = ''; $ret = '';
  261. exec($cmd, $out, $ret);
  262. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  263. if (0 !== $ret) throw new Exception("Error at encode files");
  264. if (empty($out)) throw new Exception("No output for encode files command");
  265. }
  266. }
  267. public function getAppLicenceInfo($idLicence) {
  268. $idLicence = intval($idLicence);
  269. if (empty($idLicence)) throw new Exception("Nie wybrano serwera/licencji.");
  270. //DBG::_(true, true, 'idLicence', $idLicence, __CLASS__, __FUNCTION__, __LINE__);
  271. $appLicenceInfo = (object)$this->fetchAppLicenceInfo($idLicence);
  272. $appLicenceInfo->domains = $this->fetchDomainsByLicenceId($idLicence);
  273. $installRootPath = '/Library/Server/Web/Data/Sites/Default/PLIKI/SES_PROCESY5_A';
  274. $appLicenceInfo->installFolderName = "{$idLicence}_upgrade_SE_source_encrypted";
  275. $appLicenceInfo->installPath = "{$installRootPath}/{$appLicenceInfo->installFolderName}";
  276. //DBG::_(true, true, 'appLicenceInfo', $appLicenceInfo, __CLASS__, __FUNCTION__, __LINE__);
  277. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found.");
  278. $appLicenceInfo->installFolderExists = file_exists("{$appLicenceInfo->installPath}/SE");
  279. $appLicenceInfo->installFolderGitExists = file_exists("{$appLicenceInfo->installPath}/.git");
  280. return $appLicenceInfo;
  281. }
  282. public function generateApp($appLicenceInfo) {
  283. if (empty($appLicenceInfo->ID)) throw new Exception("Nie wybrano serwera/licencji.");
  284. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found");
  285. $installPath = $appLicenceInfo->installPath;
  286. if (empty($installPath)) throw new Exception("Install path not found");
  287. $cmds = array();
  288. $cmds[] = "if [ -d {$installPath} ] ; then rm -rf '{$installPath}'; fi";
  289. $cmds[] = "mkdir {$installPath}";
  290. $cmds[] = "cd {$installPath} && git clone ssh://git@biuro.biall-net.pl:2222/plabudda/se.git .";
  291. $cmds[] = "cd {$installPath} && echo `git show-ref --head|head -1|head -c 8` > SE/VERSION ";
  292. //echo'<pre>cmds: ';print_r($cmds);echo'</pre>';
  293. foreach ($cmds as $cmd) {
  294. $out = ''; $ret = '';
  295. exec($cmd, $out, $ret);
  296. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  297. }
  298. $this->_encodeSource($appLicenceInfo);
  299. // 1763: $exec='cd '.$installer_dir.' && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder -b-
  300. // '.INSTALL_SES_PROCESY_A::get_same_domains_for_install($h->SERVER_ADDRESS_SHORT).'
  301. // -r *.php
  302. // -x superedit-DB_PROCEDURES_CREATE.php
  303. // -x INI.php
  304. // -x .config_base_structure.php
  305. // ';
  306. //
  307. // INSTALL_SES_PROCESY_A::get_same_domains_for_install($h->SERVER_ADDRESS_SHORT):
  308. // $res2=DB::query("select SERVER_ADDRESS_SHORT from SES_PROCESY5_A where SERVER_ADDRESS_IP='".$h->SERVER_ADDRESS_IP."'");
  309. // while($h2=DB::fetch($res2)) {
  310. // $domain[]=' --domain '.$h2->SERVER_ADDRESS_SHORT;
  311. // ssh server@biuro.galeriaprzymorze.eu: PHP 5.5.20
  312. // ssh server@biuro.biall-net.pl
  313. // cd /Users/plabudda/procesy5-install-galeriaprzymorze.eu/
  314. // sudo chown -R server:admin SE/
  315. // /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
  316. }
  317. public function fetchActiveLicences() {
  318. $activeLic = array();
  319. $db = DB::getDB();
  320. $sql = "select l.`ID`
  321. , l.`SERVER_ADDRESS`
  322. , l.`SERVER_ADDRESS_SHORT` as domain -- domain for sgencoder
  323. , l.`SERVER_ADDRESS_IP`
  324. from `SES_PROCESY5_A` l
  325. where 1=1
  326. -- TODO: and l.`A_STATUS` in('NORMAL','WAITING')
  327. order by l.`ID` DESC
  328. ";
  329. $res = $db->query($sql);
  330. while ($r = $db->fetch($res)) {
  331. $activeLic[$r->ID] = $r;
  332. }
  333. return $activeLic;
  334. }
  335. public function fetchDomainsByLicenceId($licenceId) {
  336. $domains = array();
  337. $db = DB::getDB();
  338. $sql = "select g.`SERVER_ADDRESS_SHORT`, g.`SERVER_ADDRESS`
  339. from `SES_PROCESY5_A` g
  340. where g.`SERVER_ADDRESS_IP`=(select l.`SERVER_ADDRESS_IP`
  341. from `SES_PROCESY5_A` l
  342. where l.`ID`='{$licenceId}'
  343. -- TODO: and l.`A_STATUS` in('NORMAL','WAITING')
  344. )
  345. -- TODO: and g.`A_STATUS` in('NORMAL','WAITING')
  346. ";
  347. $res = $db->query($sql);
  348. while ($r = $db->fetch($res)) {
  349. $domains[] = $r->SERVER_ADDRESS_SHORT;
  350. if ($r->SERVER_ADDRESS != $r->SERVER_ADDRESS_SHORT) {
  351. $domains[] = $r->SERVER_ADDRESS;
  352. }
  353. }
  354. return $domains;
  355. }
  356. public function fetchMainServerByLicenceId($licenceId) {
  357. $mainServer = null;
  358. $db = DB::getDB();
  359. $sql = "select g.`SERVER_ADDRESS`
  360. from `SES_PROCESY5_A` g
  361. where g.`ID`='{$licenceId}'
  362. -- TODO: and g.`A_STATUS` in('NORMAL','WAITING')
  363. ";
  364. $res = $db->query($sql);
  365. while ($r = $db->fetch($res)) {
  366. $mainServer = $r->SERVER_ADDRESS;
  367. }
  368. return $mainServer;
  369. }
  370. public function fetchAppLicenceInfo($licenceId) {
  371. $licenceInfo = null;
  372. $sth = DB::getPDO()->prepare("
  373. select g.ID
  374. , g.SERVER_ADDRESS as mainServer
  375. , g.ADMIN_USERNAME as rootLogin
  376. , g.ADMIN_USERNAME_PASSWD as rootPassword
  377. , g.SSH_PORT as sshPort
  378. from SES_PROCESY5_A g
  379. where g.`ID`=:id_licence
  380. -- TODO: and g.A_STATUS in('NORMAL','WAITING')
  381. ");
  382. $sth->bindValue('id_licence', $licenceId, PDO::PARAM_INT);
  383. $sth->execute();
  384. $all = $sth->fetchAll();
  385. if (empty($all)) throw new Exception("Brak licencji o nr '{$licenceId}'");
  386. $licenceInfo = reset($all);
  387. if (!$licenceInfo['sshPort']) $licenceInfo['sshPort'] = 22;
  388. return $licenceInfo;
  389. }
  390. public function _endWithException($e) {
  391. ?>
  392. <div class="container">
  393. <div class="alert alert-danger">
  394. #<?php echo $e->getLine(); ?>: <?php echo $e->getMessage(); ?>
  395. </div>
  396. <p>Wróć do <a href="index.php?_route=Install">menu</a></p>
  397. </div>
  398. <?php
  399. SE_Layout::dol();
  400. exit;
  401. }
  402. public function sendToRemoteTestDirAction() {
  403. session_write_close();
  404. $args = array();
  405. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  406. SE_Layout::gora();
  407. //SE_Layout::menu();
  408. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  409. try {
  410. $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
  411. $this->_sendToRemoteTestDir($appLicenceInfo);
  412. ?>
  413. Test online: <a target="_blank" href="https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/">https://<?php echo $appLicenceInfo->mainServer; ?>/se.encrypted.upgrade/</a>
  414. <?php
  415. } catch (Exception $e) {
  416. $this->_endWithException($e);
  417. }
  418. SE_Layout::dol();
  419. }
  420. public function _fetchRemoteHomeDir($appLicenceInfo) {
  421. $this->_assertRsaKeyExists($appLicenceInfo);
  422. $sshHostUsr = "{$appLicenceInfo->rootLogin}@{$appLicenceInfo->mainServer}";
  423. $sshArgs = (22 != $appLicenceInfo->sshPort)? "-p {$appLicenceInfo->sshPort}" : '';
  424. $rsyncSshPort = (22 != $appLicenceInfo->sshPort)? "-e 'ssh -p {$appLicenceInfo->sshPort}'" : '';
  425. if (V::get('DBG_REMOTE', '', $_GET)) {// DBG
  426. $cmd = "echo ~";
  427. V::exec("ssh {$sshArgs} {$sshHostUsr} '{$cmd}'", $out, $ret);
  428. DBG::_(true, true, "exec(ssh ... 'cmd'): {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  429. V::execRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
  430. DBG::_(true, true, "execRemote(cmd): {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  431. V::execRootRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
  432. DBG::_(true, true, "execRootRemote(cmd): {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  433. }
  434. $cmd = "echo ~";
  435. V::exec("ssh {$sshArgs} {$sshHostUsr} '{$cmd}'", $out, $ret);
  436. DBG::_(true, true, "exec(ssh ... '{$cmd}') (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  437. if (empty($out) || empty($out[0])) throw new Exception("Cannot fetch remote home dir");
  438. return $out[0];
  439. }
  440. public function _sendToRemoteTestDir($appLicenceInfo) {
  441. $cmd = ''; $out = ''; $ret = '';
  442. $this->_assertRsaKeyExists($appLicenceInfo);
  443. $sshHostUsr = "{$appLicenceInfo->rootLogin}@{$appLicenceInfo->mainServer}";
  444. $sshArgs = (22 != $appLicenceInfo->sshPort)? "-p {$appLicenceInfo->sshPort}" : '';
  445. $rsyncSshPort = (22 != $appLicenceInfo->sshPort)? "-e 'ssh -p {$appLicenceInfo->sshPort}'" : '';
  446. // $appLicenceInfo->sshPort
  447. /* rsync options:
  448. -a, --archive archive mode; same as -rlptgoD (no -H)
  449. -u, --update skip files that are newer on the receiver
  450. -t, --times preserve times
  451. --delete delete extraneous files from dest dirs
  452. */
  453. DBG::_(true, true, "remote home dir:", $this->_fetchRemoteHomeDir($appLicenceInfo), __CLASS__, __FUNCTION__, __LINE__);
  454. $cmd = "
  455. ssh {$sshArgs} {$sshHostUsr} '[ ! -d ~/se.encrypted.upgrade ] && mkdir ~/se.encrypted.upgrade || echo 1';
  456. ssh {$sshArgs} {$sshHostUsr} 'rm -rf ~/se.encrypted.upgrade/SE';
  457. ssh {$sshArgs} {$sshHostUsr} 'cp -r /Library/Server/Web/Data/Sites/Default/SE ~/se.encrypted.upgrade/SE';
  458. ssh {$sshArgs} {$sshHostUsr} 'rm -rf ~/se.encrypted.upgrade/SE/config';
  459. rsync --archive --times --delete --compress --one-file-system --omit-dir-times --no-g --no-perms \
  460. --verbose {$rsyncSshPort} \
  461. --exclude='stuff/qgis__kyngchaos.com' \
  462. --exclude='stuff/qgis__kyngchaos.com/QGIS-2.14.3-1.dmg' \
  463. '{$appLicenceInfo->installPath}/SE/' {$sshHostUsr}:~/se.encrypted.upgrade/SE/;
  464. ssh {$sshArgs} {$sshHostUsr} 'cp -r /Library/Server/Web/Data/Sites/Default/SE/config ~/se.encrypted.upgrade/SE/';
  465. ssh {$sshArgs} {$sshHostUsr} 'rm /Library/Server/Web/Data/Sites/Default/se.encrypted.upgrade'
  466. ssh {$sshArgs} {$sshHostUsr} 'ln -s ~/se.encrypted.upgrade/SE /Library/Server/Web/Data/Sites/Default/se.encrypted.upgrade'
  467. ";
  468. // --dry-run \
  469. V::exec($cmd, $out, $ret);
  470. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  471. if (0 !== $ret) throw new Exception("Cannot run remote command using rsa key! #{$ret}");
  472. }
  473. public function upgradeRemoteFromTestDirAction() {
  474. session_write_close();
  475. $args = array();
  476. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  477. SE_Layout::gora();
  478. //SE_Layout::menu();
  479. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  480. try {
  481. $appLicenceInfo = $this->getAppLicenceInfo($args['licence_id']);
  482. $this->_upgradeRemoteFromTestDir($appLicenceInfo);
  483. ?>
  484. Test online: <a target="_blank" href="https://<?php echo $appLicenceInfo->mainServer; ?>/SE/">https://<?php echo $appLicenceInfo->mainServer; ?>/SE/</a>
  485. <?php
  486. } catch (Exception $e) {
  487. $this->_endWithException($e);
  488. }
  489. SE_Layout::dol();
  490. }
  491. public function _upgradeRemoteFromTestDir($appLicenceInfo) {
  492. $cmd = ''; $out = ''; $ret = '';
  493. $this->_assertRsaKeyExists($appLicenceInfo);
  494. $dateStr = date("Y-m-d_H-i-s");
  495. // $remoteHomeDir = "/Users/{$appLicenceInfo->rootLogin}";// BUG: alias like in ams - login = prezes is alias for arkadiuszbinder
  496. $remoteHomeDir = $this->_fetchRemoteHomeDir($appLicenceInfo);
  497. $cmd = "
  498. rm -rf /Library/Server/Web/Data/Sites/Default/SE.test 2>&1
  499. cp -r {$remoteHomeDir}/se.encrypted.upgrade/SE /Library/Server/Web/Data/Sites/Default/SE.test 2>&1
  500. [ ! -d {$remoteHomeDir}/bup.se.upgrade ] && mkdir {$remoteHomeDir}/bup.se.upgrade || echo 1
  501. [ ! -d {$remoteHomeDir}/bup.se.upgrade ] && exit 1
  502. [ ! -d {$remoteHomeDir}/bup.se.upgrade-testttttt ] && exit 1
  503. [ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/VERSION ] && echo \"Error: plik VERSION nie istnieje\" || echo \"check VERSION OK\"
  504. [ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/VERSION ] && exit 1
  505. [ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/index.php ] && echo \"Error: plik index.php nie istnieje\" || echo \"check index.php OK\"
  506. [ ! -f /Library/Server/Web/Data/Sites/Default/SE.test/index.php ] && exit 1
  507. [ ! -d /Library/Server/Web/Data/Sites/Default/SE.test/se-lib ] && echo \"Error: plik se-lib nie istnieje\" || echo \"check se-lib OK\"
  508. [ ! -d /Library/Server/Web/Data/Sites/Default/SE.test/se-lib ] && exit 1
  509. exit 0
  510. ";
  511. V::execRootRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
  512. $cmd = "
  513. mv /Library/Server/Web/Data/Sites/Default/SE {$remoteHomeDir}/bup.se.upgrade/bup.{$dateStr} 2>&1
  514. mv /Library/Server/Web/Data/Sites/Default/SE.test /Library/Server/Web/Data/Sites/Default/SE 2>&1
  515. exit 0
  516. ";
  517. V::execRootRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
  518. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  519. if (0 !== $ret) throw new Exception("Cannot run remote command as root! #{$ret}");
  520. $cmd = "/usr/bin/php /Library/Server/Web/Data/Sites/Default/SE/bash_install_check.php";
  521. V::execRootRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
  522. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  523. if (0 !== $ret) throw new Exception("Cannot run remote command as root! #{$ret}");
  524. }
  525. public function _assertRsaKeyExists($appLicenceInfo) {
  526. $cmd = ''; $out = ''; $ret = '';
  527. $cmd = "echo ~ && pwd";// /Library/WebServer
  528. $cmd = "ls -1 ~/.ssh/";
  529. //$cmd = "rm /tmp/id_rsa";
  530. //$cmd = "rm /tmp/id_rsa.pub";
  531. //$cmd = "ssh-keygen -t rsa -N '' -C '_www@biuro.biall-net.pl' -f /tmp/id_rsa";
  532. //$cmd = "ssh-keygen -t rsa -N '' -C '_www@biuro.biall-net.pl' -f /tmp/id_rsa";
  533. V::exec($cmd, $out, $ret);
  534. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  535. if (0 !== $ret) {// no ~/.ssh directory
  536. $rsaKeyPath = "~/.ssh";
  537. $cmds = array();
  538. $cmds[] = "mkdir {$rsaKeyPath}";
  539. $cmds[] = "ssh-keygen -t rsa -N '' -C '_www@biuro.biall-net.pl' -f {$rsaKeyPath}/id_rsa 2>&1";
  540. $cmds[] = "ls -1 $rsaKeyPath";
  541. foreach ($cmds as $cmd) {
  542. V::exec($cmd, $out, $ret);
  543. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  544. if (0 !== $ret) throw new Exception("Error '{$ret}' cmd({$cmd}): " . implode("\n", $out));
  545. }
  546. }
  547. $cmd = "cat ~/.ssh/id_rsa.pub";
  548. V::exec($cmd, $out, $ret);
  549. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  550. if (0 !== $ret || empty($out) || empty($out[0])) throw new Exception("Cannot read rsa public key");
  551. $rsaPubKey = $out[0];
  552. $cmd = 'ls -1a';
  553. $cmd = "
  554. [ ! -d ~/.ssh ] && mkdir ~/.ssh;
  555. [ ! -d ~/.ssh ] && echo 'ERROR ~/.ssh not exists and cannot be created';
  556. [ ! -f ~/.ssh/authorized_keys ] && echo '{$rsaPubKey}' > ~/.ssh/authorized_keys;
  557. [ ! -f ~/.ssh/authorized_keys ] && echo 'ERROR ~/.ssh/authorized_keys not exists and cannot be created';
  558. cat ~/.ssh/authorized_keys| grep '{$rsaPubKey}' && echo 'OK' || echo '{$rsaPubKey}' >> ~/.ssh/authorized_keys;
  559. chmod 600 ~/.ssh/authorized_keys;
  560. ";
  561. V::execRemote($appLicenceInfo->mainServer, $appLicenceInfo->rootLogin, $appLicenceInfo->rootPassword, $cmd, $out, $ret, $appLicenceInfo->sshPort);
  562. DBG::_(true, true, "remote cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  563. //$cmd = "ssh server@{$appLicenceInfo->mainServer} 'ls -1 .ssh/'";
  564. //$cmd = "ssh -i ~/.ssh/id_rsa server@{$appLicenceInfo->mainServer} 'ls -1'";
  565. $sshPort = (22 != $appLicenceInfo->sshPort)? "-p {$appLicenceInfo->sshPort}" : '';
  566. $cmd = "ssh {$sshPort} {$appLicenceInfo->rootLogin}@{$appLicenceInfo->mainServer} 'ls -1'";
  567. V::exec($cmd, $out, $ret);
  568. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  569. if (0 !== $ret) throw new Exception("Cannot run remote command using rsa key! #{$ret}");
  570. }
  571. }