Install.php 25 KB

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