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

/trunk/src/plugins/vmware-esx/web/vmware-esx-create.php

https://github.com/qyjohn/openqrm
PHP | 379 lines | 292 code | 43 blank | 44 comment | 39 complexity | 83fe7bd0e5d747811d13c2dc5659b220 MD5 | raw file
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>VMware ESX Create VM</title>
  5. <link rel="stylesheet" type="text/css" href="../../css/htmlobject.css" />
  6. <link rel="stylesheet" type="text/css" href="vmware-esx.css" />
  7. <link type="text/css" href="/openqrm/base/js/jquery/development-bundle/themes/smoothness/ui.all.css" rel="stylesheet" />
  8. <script type="text/javascript" src="/openqrm/base/js/jquery/js/jquery-1.3.2.min.js"></script>
  9. <script type="text/javascript" src="/openqrm/base/js/jquery/js/jquery-ui-1.7.1.custom.min.js"></script>
  10. <style type="text/css">
  11. .ui-progressbar-value {
  12. background-image: url(/openqrm/base/img/progress.gif);
  13. }
  14. #progressbar {
  15. position: absolute;
  16. left: 150px;
  17. top: 250px;
  18. width: 400px;
  19. height: 20px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="progressbar">
  25. </div>
  26. <?php
  27. /*
  28. This file is part of openQRM.
  29. openQRM is free software: you can redistribute it and/or modify
  30. it under the terms of the GNU General Public License version 2
  31. as published by the Free Software Foundation.
  32. openQRM is distributed in the hope that it will be useful,
  33. but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. GNU General Public License for more details.
  36. You should have received a copy of the GNU General Public License
  37. along with openQRM. If not, see <http://www.gnu.org/licenses/>.
  38. Copyright 2009, Matthias Rechenburg <matt@openqrm.com>
  39. */
  40. // error_reporting(E_ALL);
  41. $thisfile = basename($_SERVER['PHP_SELF']);
  42. $RootDir = $_SERVER["DOCUMENT_ROOT"].'/openqrm/base/';
  43. $BaseDir = $_SERVER["DOCUMENT_ROOT"].'/openqrm/';
  44. require_once "$RootDir/include/user.inc.php";
  45. require_once "$RootDir/class/image.class.php";
  46. require_once "$RootDir/class/resource.class.php";
  47. require_once "$RootDir/class/appliance.class.php";
  48. require_once "$RootDir/class/deployment.class.php";
  49. require_once "$RootDir/include/htmlobject.inc.php";
  50. require_once "$RootDir/class/event.class.php";
  51. require_once "$RootDir/class/openqrm_server.class.php";
  52. global $RESOURCE_INFO_TABLE;
  53. $vmware_esx_id = htmlobject_request('vmware_esx_id');
  54. $vmware_esx_name = htmlobject_request('vmware_esx_name');
  55. $vmware_esx_mac = htmlobject_request('vmware_esx_mac');
  56. $vmware_esx_ram = htmlobject_request('vmware_esx_ram');
  57. $vmware_esx_disk = htmlobject_request('vmware_esx_disk');
  58. $vmware_esx_swap = htmlobject_request('vmware_esx_swap');
  59. $vmware_esx_cpus = htmlobject_request('vmware_esx_cpus');
  60. $vmware_vm_vnc_auth = htmlobject_request('vmware_vm_vnc_auth');
  61. $vmware_vm_vnc_port = htmlobject_request('vmware_vm_vnc_port');
  62. global $vmware_esx_id;
  63. global $vmware_esx_name;
  64. global $vmware_esx_mac;
  65. global $vmware_esx_ram;
  66. global $vmware_esx_disk;
  67. global $vmware_esx_swap;
  68. global $vmware_esx_cpus;
  69. global $vmware_vm_vnc_auth;
  70. global $vmware_vm_vnc_port;
  71. $action=htmlobject_request('action');
  72. $refresh_delay=1;
  73. $refresh_loop_max=600;
  74. $vmware_mac_address_space = "00:50:56:20";
  75. $event = new event();
  76. global $event;
  77. $openqrm_server = new openqrm_server();
  78. $OPENQRM_SERVER_IP_ADDRESS=$openqrm_server->get_ip_address();
  79. global $OPENQRM_SERVER_IP_ADDRESS;
  80. global $OPENQRM_SERVER_BASE_DIR;
  81. function redirect_mgmt($strMsg, $currenttab = 'tab0', $vmware_esx_id) {
  82. $url = 'vmware-esx-manager.php?strMsg='.urlencode($strMsg).'&currenttab='.$currenttab.'&vmware_esx_id='.$vmware_esx_id;
  83. echo "<meta http-equiv=\"refresh\" content=\"0; URL=$url\">";
  84. exit;
  85. }
  86. function redirect($strMsg, $currenttab = 'tab0', $vmware_esx_id) {
  87. global $thisfile;
  88. $url = $thisfile.'?strMsg='.urlencode($strMsg).'&currenttab='.$currenttab.'&vmware_esx_id='.$vmware_esx_id;
  89. echo "<meta http-equiv=\"refresh\" content=\"0; URL=$url\">";
  90. exit;
  91. }
  92. function wait_for_statfile($sfile) {
  93. global $refresh_delay;
  94. global $refresh_loop_max;
  95. $refresh_loop=0;
  96. while (!file_exists($sfile)) {
  97. sleep($refresh_delay);
  98. $refresh_loop++;
  99. flush();
  100. if ($refresh_loop > $refresh_loop_max) {
  101. return false;
  102. }
  103. }
  104. return true;
  105. }
  106. function show_progressbar() {
  107. ?>
  108. <script type="text/javascript">
  109. $("#progressbar").progressbar({
  110. value: 100
  111. });
  112. var options = {};
  113. $("#progressbar").effect("shake",options,2000,null);
  114. </script>
  115. <?php
  116. flush();
  117. }
  118. function validate_input($var, $type) {
  119. switch ($type) {
  120. case 'string':
  121. // remove allowed chars
  122. $var = str_replace(".", "", $var);
  123. $var = str_replace("-", "", $var);
  124. $var = str_replace("_", "", $var);
  125. for ($i = 0; $i<strlen($var); $i++) {
  126. if (!ctype_alpha($var[$i])) {
  127. if (!ctype_digit($var[$i])) {
  128. return false;
  129. }
  130. }
  131. }
  132. return true;
  133. break;
  134. case 'number';
  135. for ($i = 0; $i<strlen($var); $i++) {
  136. if (!ctype_digit($var[$i])) {
  137. return false;
  138. }
  139. }
  140. return true;
  141. break;
  142. }
  143. }
  144. // running the actions
  145. $strMsg = '';
  146. if(htmlobject_request('action') != '') {
  147. if ($OPENQRM_USER->role == "administrator") {
  148. switch (htmlobject_request('action')) {
  149. // vmware-server-actions
  150. case 'new':
  151. if (strlen($vmware_esx_id)) {
  152. // name check
  153. if (!strlen($vmware_esx_name)) {
  154. $strMsg .= "Empty vm name. Not creating the vm on VMware ESX Host $vmware_esx_id<br>";
  155. redirect($strMsg, "tab0", $vmware_esx_id);
  156. } else if (!validate_input($vmware_esx_name, 'string')) {
  157. $strMsg .= "Invalid vm name. Not creating the vm on VMware ESX Host $vmware_esx_id<br>(allowed characters are [a-z][A-z][0-9].-_)";
  158. redirect($strMsg, "tab0", $vmware_esx_id);
  159. }
  160. // mach check
  161. if (!strlen($vmware_esx_mac)) {
  162. $strMsg .= "Empty vm mac-address. Not creating the vm on VMware ESX Host $vmware_esx_id<br>";
  163. redirect($strMsg, "tab0", $vmware_esx_id);
  164. }
  165. // check for wrong vmware mac address space
  166. $posted_mac_address_space = substr($vmware_esx_mac, 0, 11);
  167. if (strcmp($vmware_mac_address_space, $posted_mac_address_space)) {
  168. $strMsg .= "Please notice that VMware is using the special mac-address space $vmware_mac_address_space:xx:yy !<br>Other mac-addresses are not supported.<br>";
  169. redirect($strMsg, "tab0", $vmware_esx_id);
  170. }
  171. // ram check
  172. if (!strlen($vmware_esx_ram)) {
  173. $strMsg .= "Empty vm memory. Not creating the vm on VMware ESX Host $vmware_esx_id<br>";
  174. redirect($strMsg, "tab0", $vmware_esx_id);
  175. } else if (!validate_input($vmware_esx_ram, 'number')) {
  176. $strMsg .= "Invalid vm memory $vmware_esx_ram. Not creating the vm on VMware ESX Host $vmware_esx_id<br>";
  177. redirect($strMsg, "tab0", $vmware_esx_id);
  178. }
  179. // check for disk size is int
  180. if (strlen($vmware_esx_disk)) {
  181. if (!validate_input($vmware_esx_disk, 'number')) {
  182. $strMsg .= "Invalid vm disk size. Not creating the vm on VMware ESX Host $vmware_esx_id<br>";
  183. redirect($strMsg, "tab0", $vmware_esx_id);
  184. }
  185. $vmware_esx_disk_parameter = "-d ".$vmware_esx_disk;
  186. } else {
  187. $vmware_esx_disk_parameter = "";
  188. }
  189. // check for swap size is int
  190. if (strlen($vmware_esx_swap)) {
  191. if (!validate_input($vmware_esx_swap, 'number')) {
  192. $strMsg .= "Invalid vm swap size. Not creating the vm on VMware ESX Host $vmware_esx_id<br>";
  193. redirect($strMsg, "tab0", $vmware_esx_id);
  194. }
  195. $vmware_esx_swap_parameter = "-s ".$vmware_esx_swap;
  196. } else {
  197. $vmware_esx_swap_parameter = "";
  198. }
  199. // check for cpu count is int
  200. if (!strlen($vmware_esx_cpus)) {
  201. $strMsg .= "Empty vm cpu number. Not creating new vm on VMware ESX Host $vmware_esx_id";
  202. redirect($strMsg, "tab0", $vmware_esx_id);
  203. }
  204. if (!validate_input($vmware_esx_cpus, 'number')) {
  205. $strMsg .= "Invalid vm cpu number. Not creating new vm on VMware ESX Host $vmware_esx_id";
  206. redirect($strMsg, "tab0", $vmware_esx_id);
  207. }
  208. // vnc ?
  209. if (strlen($vmware_vm_vnc_port)) {
  210. if (!validate_input($vmware_vm_vnc_port, 'number')) {
  211. $strMsg .= "Invalid vm VNC port number. Not creating new vm on VMware ESX Host $vmware_esx_id";
  212. redirect($strMsg, "tab0", $vmware_esx_id);
  213. }
  214. $vnc_pass_len = strlen($vmware_vm_vnc_auth);
  215. if ($vnc_pass_len < 8) {
  216. $strMsg .= "VNC password too short. Must be min 8 chars. Not creating new vm on VMware ESX Host $vmware_esx_id";
  217. redirect($strMsg, "tab0", $vmware_esx_id);
  218. }
  219. $create_vm_vnc_parameter = "-vp ".$vmware_vm_vnc_port." -va ".$vmware_vm_vnc_auth;
  220. }
  221. // send command to vmware_esx-host to create the new vm
  222. show_progressbar();
  223. $vmware_appliance = new appliance();
  224. $vmware_appliance->get_instance_by_id($vmware_esx_id);
  225. $vmware_esx = new resource();
  226. $vmware_esx->get_instance_by_id($vmware_appliance->resources);
  227. $resource_command="$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/vmware-esx/bin/openqrm-vmware-esx create -n $vmware_esx_name -m $vmware_esx_mac -r $vmware_esx_ram -i $vmware_esx->ip -c $vmware_esx_cpus $vmware_esx_disk_parameter $vmware_esx_swap_parameter $create_vm_vnc_parameter";
  228. // remove current stat file
  229. $vmware_esx_resource_id = $vmware_esx->id;
  230. $vmware_esx_resource_ip = $vmware_esx->ip;
  231. $statfile="vmware-esx-stat/".$vmware_esx_resource_ip.".vm_list";
  232. if (file_exists($statfile)) {
  233. unlink($statfile);
  234. }
  235. // add resource + type + vhostid
  236. $resource = new resource();
  237. $resource_id=openqrm_db_get_free_id('resource_id', $RESOURCE_INFO_TABLE);
  238. $resource_ip="0.0.0.0";
  239. // send command to the openQRM-server
  240. $openqrm_server->send_command("openqrm_server_add_resource $resource_id $vmware_esx_mac $resource_ip");
  241. // set resource type
  242. $virtualization = new virtualization();
  243. $virtualization->get_instance_by_type("vmware-esx-vm");
  244. // add to openQRM database
  245. $resource_fields["resource_id"]=$resource_id;
  246. $resource_fields["resource_ip"]=$resource_ip;
  247. $resource_fields["resource_mac"]=$vmware_esx_mac;
  248. $resource_fields["resource_localboot"]=0;
  249. $resource_fields["resource_vtype"]=$virtualization->id;
  250. $resource_fields["resource_vhostid"]=$vmware_esx->id;
  251. $resource->add($resource_fields);
  252. // send command
  253. $openqrm_server->send_command($resource_command);
  254. // and wait for the resulting statfile
  255. if (!wait_for_statfile($statfile)) {
  256. $strMsg .= "Error during creating the vm on VMware ESX Host $vmware_esx_id ! Please check the Event-Log<br>";
  257. redirect($strMsg, "tab0", $vmware_esx_id);
  258. } else {
  259. $strMsg .="Created vm $vmware_esx_name on VMware ESX Host $vmware_esx_id<br>";
  260. redirect_mgmt($strMsg, "tab0", $vmware_esx_id);
  261. }
  262. }
  263. break;
  264. default:
  265. $event->log("$vmware_esx_command", $_SERVER['REQUEST_TIME'], 3, "vmware-esx-action", "No such vmware-esx command ($vmware_esx_command)", "", "", 0, 0, 0);
  266. break;
  267. }
  268. }
  269. }
  270. function vmware_esx_create() {
  271. global $vmware_esx_id;
  272. global $vmware_mac_address_space;
  273. global $thisfile;
  274. $vmware_esx_appliance = new appliance();
  275. $vmware_esx_appliance->get_instance_by_id($vmware_esx_id);
  276. $vmware_esx = new resource();
  277. $vmware_esx->get_instance_by_id($vmware_esx_appliance->resources);
  278. // suggest a mac in the "manual configured mac address" space of vmware
  279. // please notice that "other" mac address won't work !
  280. $resource_mac_gen = new resource();
  281. $resource_mac_gen->generate_mac();
  282. $suggested_mac = $resource_mac_gen->mac;
  283. $suggested_last_two_bytes = substr($suggested_mac, 12);
  284. $suggested_vmware_mac = $vmware_mac_address_space.":".$suggested_last_two_bytes;
  285. // cpus array for the select
  286. $cpu_identifier_array = array();
  287. $cpu_identifier_array[] = array("value" => "1", "label" => "1 CPU");
  288. $cpu_identifier_array[] = array("value" => "2", "label" => "2 CPUs");
  289. $cpu_identifier_array[] = array("value" => "3", "label" => "3 CPUs");
  290. $cpu_identifier_array[] = array("value" => "4", "label" => "4 CPUs");
  291. // vnc port array
  292. $vnc_port_identifier_array[] = array("value" => "", "label" => "No VNC");
  293. $vnc_start_port = 5901;
  294. $vnc_end_port = 6000;
  295. $vnc_port = $vnc_start_port;
  296. while ($vnc_port < $vnc_end_port) {
  297. $vnc_port_identifier_array[] = array("value" => $vnc_port, "label" => $vnc_port);
  298. $vnc_port++;
  299. }
  300. // set template
  301. $t = new Template_PHPLIB();
  302. $t->debug = false;
  303. $t->setFile('tplfile', './tpl/' . 'vmware-esx-create.tpl.php');
  304. $t->setVar(array(
  305. 'formaction' => $thisfile,
  306. 'vmware_esx_id' => $vmware_esx_id,
  307. 'vmware_vm_name' => htmlobject_input('vmware_esx_name', array("value" => '', "label" => 'VM name'), 'text', 20),
  308. 'vmware_vm_cpus' => htmlobject_select('vmware_esx_cpus', $cpu_identifier_array, 'CPUs'),
  309. 'vmware_vm_mac' => htmlobject_input('vmware_esx_mac', array("value" => $suggested_vmware_mac, "label" => 'Mac address'), 'text', 20),
  310. 'vmware_vm_ram' => htmlobject_input('vmware_esx_ram', array("value" => '512', "label" => 'Memory (MB)'), 'text', 10),
  311. 'vmware_vm_disk' => htmlobject_input('vmware_esx_disk', array("value" => '2000', "label" => 'Disk (MB)'), 'text', 10),
  312. 'vmware_vm_swap' => htmlobject_input('vmware_esx_swap', array("value" => '1024', "label" => 'Swap (MB)'), 'text', 10),
  313. 'vmware_vm_vnc_auth' => htmlobject_input('vmware_vm_vnc_auth', array("value" => '[min. 8 chars]', "label" => 'VNC Password'), 'text', 10),
  314. 'vmware_vm_vnc_port' => htmlobject_select('vmware_vm_vnc_port', $vnc_port_identifier_array, 'VNC Port'),
  315. 'hidden_vmware_esx_id' => "<input type=hidden name=vmware_esx_id value=$vmware_esx_id>",
  316. 'submit' => htmlobject_input('action', array("value" => 'new', "label" => 'Create'), 'submit'),
  317. ));
  318. $disp = $t->parse('out', 'tplfile');
  319. return $disp;
  320. }
  321. $output = array();
  322. // if admin
  323. if ($OPENQRM_USER->role == "administrator") {
  324. $output[] = array('label' => 'VMware ESX Create VM', 'value' => vmware_esx_create());
  325. }
  326. ?>
  327. <script type="text/javascript">
  328. $("#progressbar").remove();
  329. </script>
  330. <?php
  331. echo htmlobject_tabmenu($output);
  332. ?>