Install.php 20 KB

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