PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/hypervm-install/install_common.php

https://github.com/lxcenter/hypervm
PHP | 237 lines | 166 code | 49 blank | 22 comment | 27 complexity | d79ac260a283a84017ffcc5462edaf00 MD5 | raw file
  1. <?PHP
  2. //
  3. // HyperVM, Server Virtualization GUI for OpenVZ and Xen
  4. //
  5. // Copyright (C) 2000-2009 LxLabs
  6. // Copyright (C) 2009 LxCenter
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU Affero General Public License as
  10. // published by the Free Software Foundation, either version 3 of the
  11. // License, or (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. ?>
  21. <?php
  22. class remote { }
  23. // PHp4, without the lxlabs infrastructure...
  24. //There is one inside the installall.ph too. The problem is that installall seems to be using the entire lxadmin library. We must detach installall from the mains sytem and make it a standalone one.
  25. function slave_get_db_pass($program = "lxadmin")
  26. {
  27. $file = "/usr/local/lxlabs/$program/etc/slavedb/dbadmin";
  28. if (!file_exists($file)) { return null; }
  29. $var = file_get_contents($file);
  30. $rmt = unserialize($var);
  31. return $rmt->data['mysql']['dbpassword'];
  32. }
  33. function addLineIfNotExistTemp($filename, $pattern, $comment)
  34. {
  35. $cont = our_file_get_contents($filename);
  36. if(!preg_match("+$pattern+i", $cont)) {
  37. our_file_put_contents($filename, "\n$comment \n\n", true);
  38. our_file_put_contents($filename, $pattern, true);
  39. our_file_put_contents($filename, "\n\n\n", true);
  40. } else {
  41. print("Pattern '$pattern' Already present in $filename\n");
  42. }
  43. }
  44. function check_default_mysql($dbroot, $dbpass)
  45. {
  46. system("service mysqld restart");
  47. if ($dbpass) {
  48. exec("echo \"show tables\" | mysql -u $dbroot -p\"$dbpass\" mysql", $out, $return);
  49. } else {
  50. exec("echo \"show tables\" | mysql -u $dbroot mysql", $out, $return);
  51. }
  52. if ($return) {
  53. print("Fatal Error: Could not connect to Mysql Localhost using user $dbroot and password \"$dbpass\"\n");
  54. print("If this is a brand new install, you can completely remove mysql by running the commands below\n");
  55. print(" rm -rf /var/lib/mysql\n");
  56. print(" rpm -e mysql-server\n");
  57. print("And then run the installer again\n");
  58. exit;
  59. }
  60. }
  61. function parse_opt($argv)
  62. {
  63. unset($argv[0]);
  64. if (!$argv) {
  65. return null;
  66. }
  67. foreach($argv as $v) {
  68. if (strstr($v, "=") === false || strstr($v, "--") === false) {
  69. continue;
  70. }
  71. $opt = explode("=", $v);
  72. $opt[0] = substr($opt[0], 2);
  73. $ret[$opt[0]] = $opt[1];
  74. }
  75. return $ret;
  76. }
  77. function our_file_get_contents($file)
  78. {
  79. $string = null;
  80. $fp = fopen($file, "r");
  81. if (!$fp) {
  82. return null;
  83. }
  84. while(!feof($fp)) {
  85. $string .= fread($fp, 8192);
  86. }
  87. fclose($fp);
  88. return $string;
  89. }
  90. function our_file_put_contents($file, $contents, $appendflag = false)
  91. {
  92. if ($appendflag) {
  93. $flag = "a";
  94. } else {
  95. $flag = "w";
  96. }
  97. $fp = fopen($file, $flag);
  98. if (!$fp) {
  99. return null;
  100. }
  101. fwrite($fp, $contents);
  102. fclose($fp);
  103. }
  104. function password_gen()
  105. {
  106. $data=mt_rand(2,30);
  107. $pass="lx".$data;
  108. return $pass;
  109. }
  110. function strtil($string, $needle)
  111. {
  112. if (strrpos($string, $needle)) {
  113. return substr($string, 0, strrpos($string, $needle));
  114. } else {
  115. return $string;
  116. }
  117. }
  118. function strtilfirst($string, $needle)
  119. {
  120. if (strpos($string, $needle)) {
  121. return substr($string, 0, strpos($string, $needle));
  122. } else {
  123. return $string;
  124. }
  125. }
  126. function strfrom($string, $needle)
  127. {
  128. return substr($string, strpos($string, $needle) + strlen($needle));
  129. }
  130. function char_search_beg($haystack, $needle)
  131. {
  132. if (strpos($haystack, $needle) === 0) {
  133. return true;
  134. }
  135. return false;
  136. }
  137. function install_rhn_sources($osversion)
  138. {
  139. if (!file_exists("/etc/sysconfig/rhn/sources")) {
  140. return;
  141. }
  142. $data = our_file_get_contents("/etc/sysconfig/rhn/sources");
  143. if(!preg_match('/lxcenter/i', $data)) {
  144. $ndata = "yum lxcenter-base http://download.lxcenter.org/download/update/$osversion/\$ARCH/\nyum lxcenter-extra http://download.lxcenter.org/download/update/lxgeneral";
  145. //append it to the file...
  146. our_file_put_contents("/etc/sysconfig/rhn/sources","\n\n", true);
  147. our_file_put_contents("/etc/sysconfig/rhn/sources", $ndata, true);
  148. our_file_put_contents("/etc/sysconfig/rhn/sources","\n\n", true);
  149. }
  150. }
  151. function install_yum_repo($osversion)
  152. {
  153. if (!file_exists("/etc/yum.repos.d")) {
  154. return;
  155. }
  156. $cont = our_file_get_contents("../lxcenter.repo.template");
  157. $cont = str_replace("%distro%", $osversion, $cont);
  158. our_file_put_contents("/etc/yum.repos.d/lxcenter.repo", $cont);
  159. if ($osversion === 'centos-4') {
  160. //system("cp ../CentOS-Base.repo /etc/yum.repos.d/");
  161. }
  162. }
  163. function find_os_version()
  164. {
  165. if (file_exists("/etc/fedora-release")) {
  166. $release = trim(file_get_contents("/etc/fedora-release"));
  167. $osv = explode(" ", $release);
  168. if (strtolower($osv[1]) === 'core') {
  169. $osversion = "fedora-" . $osv[3];
  170. } else {
  171. $osversion = "fedora-" . $osv[2];
  172. }
  173. return $osversion;
  174. }
  175. if (file_exists("/etc/redhat-release")) {
  176. $release = trim(file_get_contents("/etc/redhat-release"));
  177. $osv = explode(" ", $release);
  178. if(isset($osv[6])) {
  179. $osversion = "rhel-" . $osv[6];
  180. } else{
  181. $oss = explode(".", $osv[2]);
  182. $osversion = "centos-" . $oss[0];
  183. }
  184. return $osversion;
  185. }
  186. print("This Operating System is Currently Not supported.\n");
  187. exit;
  188. }