Install.php 22 KB

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