PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/hypervm-install/hypervm-linux/lxins.php

https://github.com/lxcenter/hypervm
PHP | 248 lines | 155 code | 60 blank | 33 comment | 39 complexity | 6260bc62ecc3799ee67487f1648357cd 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. // PHp4, without the lxlabs infrastructure... The code is very bad, primarily because it falls outside the lxlabs framework, and has to work on php4 too, which is something we have no experience in. We need to move as much of the activities to code after the installation of the lxlabs framework.
  23. include_once "../install_common.php";
  24. lxins_main();
  25. function lxins_main()
  26. {
  27. global $argv;
  28. $opt = parse_opt($argv);
  29. $installtype = $opt['install-type'];
  30. $highmem = false;
  31. if (isset($opt['has-highmem'])) {
  32. $highmem = true;
  33. }
  34. if (!isset($opt['virtualization-type'])) {
  35. print("Need virtualization type --virtualization-type=xen/openvz/NONE\n");
  36. exit;
  37. } else {
  38. $virtualization = $opt['virtualization-type'];
  39. }
  40. $skipostemplate = false;
  41. if (isset($opt['skip-ostemplate'])) { $skipostemplate = true; }
  42. if (array_search($virtualization, array("xen", "openvz", "NONE")) === false) {
  43. print("Only xen/openvz/NONE are curently supported\n");
  44. exit;
  45. }
  46. $dbroot = isset($opt['db-rootuser'])? $opt['db-rootuser']: "root";
  47. $dbpass = isset($opt['db-rootpassword'])? $opt['db-rootpassword']: "";
  48. if (!$dbpass) {
  49. //$dbpass = slave_get_db_pass("hypervm");
  50. }
  51. $osversion = find_os_version();
  52. if(file_exists("/usr/local/lxlabs/hypervm")) {
  53. print("HyperVM is installed do you wish to continue?(No/Yes):\n");
  54. flush();
  55. $stdin = fopen('php://stdin','r');
  56. $argq = fread($stdin, 5);
  57. $arg=trim($argq);
  58. if(!($arg=='y' ||$arg=='yes'||$arg=='Yes'||$arg=='Y'||$arg=='YES')) {
  59. print("Exiting.....\n");
  60. exit;
  61. }
  62. }
  63. if ($virtualization === 'xen') {
  64. if (!char_search_beg($osversion, "fedora-9") && !char_search_beg($osversion, "centos-5") && !char_search_beg($osversion, "rhel-5")) {
  65. print("Xen is only supported on Fedora 9 or CentOS 5\n");
  66. exit;
  67. }
  68. }
  69. if ($virtualization === 'openvz') {
  70. if (!char_search_beg($osversion, "centos") && !char_search_beg($osversion, "rhel")) {
  71. print("OpenVZ is only supported on CentOS 4/5, RHEL 4/5 distributions\n");
  72. exit;
  73. }
  74. }
  75. /*
  76. $file = "http://download.lxlabs.com/download/update/$osversion/headers/header.info";
  77. $cont = @file_get_contents($file);
  78. if (!$cont) {
  79. print("This OS is not suported at this moment.... Please contact our Support personnel\n");
  80. exit;
  81. }
  82. */
  83. //install_rhn_sources($osversion);
  84. install_yum_repo($osversion);
  85. exec("groupadd lxlabs");
  86. exec("useradd lxlabs -g lxlabs -s '/sbin/nologin'");
  87. $list = array("which", "lxlighttpd", "zip","unzip", "lxphp", "lxzend", "curl");
  88. if ($installtype !== 'slave') {
  89. $mysql = array("mysql", "mysql-server", "mysqlclient*");
  90. $list = array_merge($list, $mysql);
  91. }
  92. while (true) {
  93. run_package_installer($list);
  94. if (file_exists("/usr/local/lxlabs/ext/php/php")) {
  95. break;
  96. } else {
  97. print("Yum Gave Error... Trying Again...\n");
  98. }
  99. }
  100. if ($installtype !== 'slave') {
  101. check_default_mysql($dbroot, $dbpass);
  102. }
  103. $xenfailed = false;
  104. exec("killall wget");
  105. system("mkdir -p /usr/local/lxlabs/hypervm");
  106. chdir("/usr/local/lxlabs/hypervm");
  107. system("mkdir -p /usr/local/lxlabs/hypervm/log");
  108. @ unlink("hypervm-current.zip");
  109. if(file_exists('.git')) {
  110. echo 'Development GIT version found. Skipping download sources.';
  111. }
  112. else {
  113. system("wget http://download.lxcenter.org/download/hypervm/production/hypervm/hypervm-current.zip");
  114. }
  115. system("unzip -oq hypervm-current.zip", $return);
  116. if ($return) {
  117. print("Unzipping the core Failed.. Most likely it is corrupted. Please contact the support personnel\n");
  118. exit;
  119. }
  120. unlink("hypervm-current.zip");
  121. system("chown -R lxlabs:lxlabs /usr/local/lxlabs/");
  122. $dir_name=dirname(__FILE__);
  123. fix_network_forwarding();
  124. system("mkdir -p /usr/local/lxlabs/hypervm/etc/");
  125. @ unlink("/usr/local/lxlabs/hypervm/etc/install_xen");
  126. @ unlink("/usr/local/lxlabs/hypervm/etc/install_openvz");
  127. touch("/usr/local/lxlabs/hypervm/etc/install_$virtualization");
  128. chdir("/usr/local/lxlabs/hypervm/httpdocs/");
  129. system("/bin/cp /usr/local/lxlabs/hypervm/httpdocs/htmllib/filecore/php.ini /usr/local/lxlabs/ext/php/etc/php.ini");
  130. system("/usr/local/lxlabs/ext/php/php ../bin/install/create.php --install-type=$installtype --db-rootuser=$dbroot --db-rootpassword=$dbpass");
  131. //@ unlink("/usr/local/lxlabs/lxadmin/bin/install/create.php");
  132. system("chmod 755 /etc/init.d/hypervm");
  133. system("/sbin/chkconfig hypervm on");
  134. system("/sbin/chkconfig iptables off");
  135. $skiparg = null;
  136. if ($skipostemplate) { $skiparg = "--skipostemplate=true"; }
  137. if ($virtualization === "NONE") {
  138. print("No Virtualization has been chosen. It is assumed that it is an existing installation\n");
  139. } else {
  140. print("Virtualization is $virtualization. Installing $virtualization Components\n");
  141. }
  142. //
  143. // call script to install base OS templates and OpenVZ repo
  144. //
  145. passthru("/usr/local/lxlabs/ext/php/php ../bin/install/virt-install.php --install-type=$installtype --virtualization-type=$virtualization $skiparg");
  146. print("Congratuations. HyperVM has been installed succesfully on your server as $installtype \n");
  147. if ($installtype === 'master') {
  148. print("You can connect to the server at https://<ip-address>:8887 or http://<ip-address>:8888\n");
  149. print("Please note that first is secure ssl connection, while the second is normal one.\n");
  150. print("The login and password are 'admin' 'admin'. After Logging in, you will have to change your password to something more secure.\n");
  151. print("Thanks for choosing HyperVM to manage your Server, and allowing us to be of service.\n");
  152. } else {
  153. print("You should open the port 8889 on this server, since this is used for the communication between master and slave.\n");
  154. print("To access this slave, go admin->slaves->add slave, give the ip/machine name of this server. The password is 'admin'. The slave will appear in the list of slaves, and you can access it just like you access localhost.\n");
  155. }
  156. if ($virtualization === 'openvz') {
  157. print("\n***There is one more step you have to do to make this complete. Open /etc/grub.conf, and change the 'default=1' line to 'default=0', and reboot this machine. You will be rebooted into the OpenVZ kernel and will able to manage VPSes from the HyperVM interface.\n");
  158. } else if ($virtualization === 'xen'){
  159. print("\n**** You will have to reboot for the XEN kernel to take effect. Once rebooted, you will able to manage XEN virtual machines using the HyperVM interface.\n");
  160. }
  161. print("\n\nExtra note:\n");
  162. print("To install extra XEN and/or OpenVZ OS templates please run:\n\n");
  163. print("sh /script/install-extra-ostemplates\n");
  164. print("\nThese template are left out the install process to speed up the HyperVM installation. By default only CentOS 5 and HostInBox(Kloxo) OS templates are installed.");
  165. }
  166. function fix_network_forwarding()
  167. {
  168. $list = file("/etc/sysctl.conf");
  169. foreach($list as $__l) {
  170. if (strstr($__l, "net.ipv4.ip_forward") !== false) {
  171. $newlist[] = "net.ipv4.ip_forward = 1\n";
  172. } else {
  173. $newlist[] = $__l;
  174. }
  175. }
  176. our_file_put_contents("/etc/sysctl.conf", implode("", $newlist));
  177. shell_exec("sysctl -p");
  178. }
  179. function run_package_installer($list)
  180. {
  181. $package = implode(" ", $list);
  182. print("Installing packages $package...\n");
  183. if (file_exists("/usr/bin/yum")) {
  184. system("yum -y install $package", $return_value);
  185. } else {
  186. system("PATH=\$PATH:/usr/sbin up2date --nosig $package", $return_value);
  187. }
  188. }